Re: [R] How to change the names in tone pitch column

From: Atte Tenkanen <attenka_at_utu.fi>
Date: Sun 14 Aug 2005 - 07:16:31 EST

Thanks Phil!

This is much more beautiful and elegant. And I learned 2 new R-functions, nchar and substr.

-Atte

> Atte -
> Here's one way - there certainly are others:
>
> notes = 0:11
> names(notes) = c("C","C#","D","D#","E","F","F#","G","G#","A","A#","B")
> pc = notes[substr(V4,1,nchar(V4) - 1)]
> names(pc) = NULL
> pcAb = pc + 12 * as.numeric(substr(V4,nchar(V4),nchar(V4)))
> names(pcAb) = NULL
>
> - Phil Spector
> Statistical Computing Facility
> Department of Statistics
> UC Berkeley
> spector@stat.berkeley.edu
>
>

> On Sat, 13 Aug 2005, Atte Tenkanen wrote:
>
>> Hi,
>>
>> I have a column (V4) in a midi event list which includes tone pitch
>> names,
>> i.e. "A4, E4, C#4, A3...":
>>
>>> compo[1:10,]
>> V1 V2 V3 V4 V5 V6 V7
>> 1 1 1 0 A4 96 2 0
>> 2 1 1 0 E4 96 2 0
>> 3 1 1 0 C#4 96 2 0
>> 4 1 1 0 A3 96 2 0
>> 5 1 3 0 B4 96 1 0
>> 6 1 3 0 E4 96 1 0
>> 7 1 3 0 B3 96 1 0
>> 8 1 3 0 G#3 96 1 0
>> 9 1 4 0 C#5 96 1 0
>> 10 1 4 0 D4 96 0 512
>>
>> Now I'd like to change them to "pitch classes" (column "pc" here, which
>> has the values between 0-11, using modulo 12) and absolute midi numbers
>> (column pcAb, values 0->107), so as results A4 gives 9 and 57 (and for
>> example C0 gives 0 and O, C2-> 0 and 24 etc):
>>
>>> compo[1:10,]
>> V1 V2 V3 V4 V5 V6 V7 pc pcAb
>> 1 1 1 0 A4 96 2 0 9 57
>> 2 1 1 0 E4 96 2 0 4 52
>> 3 1 1 0 C#4 96 2 0 1 49
>> 4 1 1 0 A3 96 2 0 9 45
>> 5 1 3 0 B4 96 1 0 11 59
>> 6 1 3 0 E4 96 1 0 4 52
>> 7 1 3 0 B3 96 1 0 11 47
>> 8 1 3 0 G#3 96 1 0 8 44
>> 9 1 4 0 C#5 96 1 0 1 61
>> 10 1 4 0 D4 96 0 512 2 50
>>
>> I have done it this way (see next under this comment), but there must be
>> some shorter way, using for-loop and paste-command with sep="" or some
>> other way?:
>>
>> ##*************************************************************************#
>> ## Create pitch class vector (add column pc to compo file) from column
>> V4:
>> ##*************************************************************************#
>>
>> pc=c();
>> pc[V4=="C0"|V4=="C1"|V4=="C2"|V4=="C3"|V4=="C4"|V4=="C5"|V4=="C6"|V4=="C7"|V4=="C8"]=0;
>> pc[V4=="C#0"|V4=="C#1"|V4=="C#2"|V4=="C#3"|V4=="C#4"|V4=="C#5"|V4=="C#6"|V4=="C#7"|V4=="C#8"]=1;
>> pc[V4=="D0"|V4=="D1"|V4=="D2"|V4=="D3"|V4=="D4"|V4=="D5"|V4=="D6"|V4=="D7"|V4=="D8"]=2;
>> pc[V4=="D#0"|V4=="D#1"|V4=="D#2"|V4=="D#3"|V4=="D#4"|V4=="D#5"|V4=="D#6"|V4=="D#7"|V4=="D#8"]=3;
>> pc[V4=="E0"|V4=="E1"|V4=="E2"|V4=="E3"|V4=="E4"|V4=="E5"|V4=="E6"|V4=="E7"|V4=="E8"]=4;
>> pc[V4=="F0"|V4=="F1"|V4=="F2"|V4=="F3"|V4=="F4"|V4=="F5"|V4=="F6"|V4=="F7"|V4=="F8"]=5;
>> pc[V4=="F#0"|V4=="F#1"|V4=="F#2"|V4=="F#3"|V4=="F#4"|V4=="F#5"|V4=="F#6"|V4=="F#7"|V4=="F#8"]=6;
>> pc[V4=="G0"|V4=="G1"|V4=="G2"|V4=="G3"|V4=="G4"|V4=="G5"|V4=="G6"|V4=="G7"|V4=="G8"]=7;
>> pc[V4=="G#0"|V4=="G#1"|V4=="G#2"|V4=="G#3"|V4=="G#4"|V4=="G#5"|V4=="G#6"|V4=="G#7"|V4=="G#8"]=8;
>> pc[V4=="A0"|V4=="A1"|V4=="A2"|V4=="A3"|V4=="A4"|V4=="A5"|V4=="A6"|V4=="A7"|V4=="A8"]=9;
>> pc[V4=="A#0"|V4=="A#1"|V4=="A#2"|V4=="A#3"|V4=="A#4"|V4=="A#5"|V4=="A#6"|V4=="A#7"|V4=="A#8"]=10;
>> pc[V4=="B0"|V4=="B1"|V4=="B2"|V4=="B3"|V4=="B4"|V4=="B5"|V4=="B6"|V4=="B7"|V4=="B8"]=11;
>>
>> ## ... and absolute pitches (0-107):
>>
>> pcAb=c();
>> pcAb[V4=="C0"]=0;pcAb[V4=="C#0"]=1;pcAb[V4=="D0"]=2;pcAb[V4=="D#0"]=3;pcAb[V4=="E0"]=4;pcAb[V4=="F0"]=5;pcAb[V4=="F#0"]=6;pcAb[V4=="G0"]=7;pcAb[V4=="G#0"]=8;pcAb[V4=="A0"]=9;pcAb[V4=="A#0"]=10;pcAb[V4=="B0"]=11;
>> pcAb[V4=="C1"]=12;pcAb[V4=="C#1"]=13;pcAb[V4=="D1"]=14;pcAb[V4=="D#1"]=15;pcAb[V4=="E1"]=16;pcAb[V4=="F1"]=17;pcAb[V4=="F#1"]=18;pcAb[V4=="G1"]=19;pcAb[V4=="G#1"]=20;pcAb[V4=="A1"]=21;pcAb[V4=="A#1"]=22;pcAb[V4=="B1"]=23;
>> pcAb[V4=="C2"]=24;pcAb[V4=="C#2"]=25;pcAb[V4=="D2"]=26;pcAb[V4=="D#2"]=27;pcAb[V4=="E2"]=28;pcAb[V4=="F2"]=29;pcAb[V4=="F#2"]=30;pcAb[V4=="G2"]=31;pcAb[V4=="G#2"]=32;pcAb[V4=="A2"]=33;pcAb[V4=="A#2"]=34;pcAb[V4=="B2"]=35;
>> pcAb[V4=="C3"]=36;pcAb[V4=="C#3"]=37;pcAb[V4=="D3"]=38;pcAb[V4=="D#3"]=39;pcAb[V4=="E3"]=40;pcAb[V4=="F3"]=41;pcAb[V4=="F#3"]=42;pcAb[V4=="G3"]=43;pcAb[V4=="G#3"]=44;pcAb[V4=="A3"]=45;pcAb[V4=="A#3"]=46;pcAb[V4=="B3"]=47;
>> pcAb[V4=="C4"]=48;pcAb[V4=="C#4"]=49;pcAb[V4=="D4"]=50;pcAb[V4=="D#4"]=51;pcAb[V4=="E4"]=52;pcAb[V4=="F4"]=53;pcAb[V4=="F#4"]=54;pcAb[V4=="G4"]=55;pcAb[V4=="G#4"]=56;pcAb[V4=="A4"]=57;pcAb[V4=="A#4"]=58;pcAb[V4=="B4"]=59;
>> pcAb[V4=="C5"]=60;pcAb[V4=="C#5"]=61;pcAb[V4=="D5"]=62;pcAb[V4=="D#5"]=63;pcAb[V4=="E5"]=64;pcAb[V4=="F5"]=65;pcAb[V4=="F#5"]=66;pcAb[V4=="G5"]=67;pcAb[V4=="G#5"]=68;pcAb[V4=="A5"]=69;pcAb[V4=="A#5"]=70;pcAb[V4=="B5"]=71;
>> pcAb[V4=="C6"]=72;pcAb[V4=="C#6"]=73;pcAb[V4=="D6"]=74;pcAb[V4=="D#6"]=75;pcAb[V4=="E6"]=76;pcAb[V4=="F6"]=77;pcAb[V4=="F#6"]=78;pcAb[V4=="G6"]=79;pcAb[V4=="G#6"]=80;pcAb[V4=="A6"]=81;pcAb[V4=="A#6"]=82;pcAb[V4=="B6"]=83;
>> pcAb[V4=="C7"]=84;pcAb[V4=="C#7"]=85;pcAb[V4=="D7"]=86;pcAb[V4=="D#7"]=87;pcAb[V4=="E7"]=88;pcAb[V4=="F7"]=89;pcAb[V4=="F#7"]=90;pcAb[V4=="G7"]=91;pcAb[V4=="G#7"]=92;pcAb[V4=="A7"]=93;pcAb[V4=="A#7"]=94;pcAb[V4=="B7"]=95;
>> pcAb[V4=="C8"]=96;pcAb[V4=="C#8"]=97;pcAb[V4=="D8"]=98;pcAb[V4=="D#8"]=99;pcAb[V4=="E8"]=100;pcAb[V4=="F8"]=101;pcAb[V4=="F#8"]=102;pcAb[V4=="G8"]=103;pcAb[V4=="G#8"]=104;pcAb[V4=="A8"]=105;pcAb[V4=="A#8"]=106;pcAb[V4=="B8"]=107;
>>
>> #*****************************************************************************
>>
>> ## Bind vectors pc and pcAb to original composition array:
>> compo=cbind(compo,pc,pcAb);
>>
>> -Atte
>>
>> ______________________________________________
>> R-help@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sun Aug 14 07:27:32 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:18:56 EST