Bill Allombert on Wed, 09 Jul 2025 23:14:45 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
playing music with GP |
Dear PARI lovers, I have written a GP script to play music using GP (really play music, not just sending MIDI data). One need the "aplay" alsa utility. the instrument is the formula in the playnote function. If you use the latest GIT version, you can use the first "start" function instead of the second one. Can you recognize the music (without reading the score) ? (Sorry in advance for any error in the score) Cheers, Bill.
\\start() = fileextern("aplay -r8000 -q -traw","w"); start(fifo="/tmp/parififo")= { system(Str("test -p ",fifo" || mkfifo ",fifo,";aplay -r8000 -q -traw < ",fifo," &")); fileopen(fifo,"w"); } freq(n)= 2^((n-69)/12)*440; playnote(f,freq,t)= { for(i=1,8000*t, my(r=round(128+80*sin(2*Pi*freq*i/8000)+40*sin(2*Pi*freq*i/16000))); filewrite1(f,strchr(r))); fileflush(f); } global(T=4); my(note=Map(["a",0;"b",2;"c",3;"d",5;"e",7;"f",8;"g",10])); play(f,s)= { my(p,m=1,V=Vec(s)); p=69+mapget(note,V[1]); foreach(V[^1],c,if(c=="#",p++, c=="b",p--, c=="'",p+=12, c==",",p-=12, c==".",m=3/2, T=eval(c))); playnote(f,freq(p),m/T); } mystery_score=strsplit("d4 d eb f f eb d c bb bb c d d4. c8 c2 d4 d eb f f eb d c bb bb c d c4. bb8 bb2"," "); f = start(); foreach(mystery_score,s,play(f,s)); fileclose(f);