Play the sound of Pi
Python 2, 182 bytes
x=p=6637
while~-p:x=p/2*x/p+2*10**999;p-=2
s="MThd\0\0\0\6\0\1\0\1\342\4MTrk\0\0\13\301\0\220"
for i in`x`:s+="JHGECA@><L\260"[~ord(i)%29]+'{<'
open('p.mid','w').write(s+"\0\377/\0")
`x`
will produce 31415926...20198L
. The trailing L
is used to produce the final channel message byte, via the mapping ~ord(i)%29
.
Outputs a single track Type 1 Midi file, named p.mid
to the current working directory.
0000: 4d 54 68 64 00 00 00 06 MThd.... # Midi header, 6 bytes to follow
0008: 00 01 00 01 .... # Type 1, 1 track
000c: e2 04 â. # (-)30 ticks per beat, 4 beats per second
000e: 4d 54 72 6b 00 00 0b c1 MTrk...Á # Track header, 3009 bytes to follow
0016: 00 90 40 7b ..@{ # Wait 0 ticks, play E4 (3), 97% volume
001a: 3c 3c 7b <<{ # Wait 60 ticks, play C4 (1), 97% volume
001d: 3c 41 7b <A{ # Wait 60 ticks, play F4 (4), 97% volume
0020: 3c 3c 7b <<{ # Wait 60 ticks, play C4 (1), 97% volume
0023: 3c 43 7b <C{ # Wait 60 ticks, play G4 (5), 97% volume
...
0bcf: 3c b0 7b 3c <°{< # Wait 60 ticks, all notes off
0bd3: 00 ff 2f 00 .ÿ/. # End of track marker
Mathematica, 107 87 bytes
Thanks to Martin Ender for saving 20 bytes!
"t.au"~Export~Sound[SoundNote[⌊12Mod[#,10,1]/7⌋-1,.5]&/@#&@@RealDigits[Pi,10,1000]]
#&@@RealDigits[Pi,10,1000]
gives the list of the first 1000 digits of π. SoundNote[⌊12Mod[#,10,1]/7⌋-1
produces the correct pitch number (where 0 is middle C by default) from a digit. Then SoundNote[...,.5]&/@
turns that pitch name into a sound object of duration 1/2 second, which Sound
gathers into an actual audio snippet. Finally "t.au"~Export~
exports to a Unix Audio Format file, mostly because the extension is the shortest supported one, but also because we get to make the filename a slap in the face to π!
Previous submission:
"t.au"~Export~Sound[StringSplit["E5 C D E F G A B C5 D5"][[#+1]]~SoundNote~.5&/@#&@@RealDigits[Pi,10,1000]]
Scratch, 530 bytes
Inspired by BookOwl's answer.
Online Demonstration. Playback will begin immediately, press space to stop and reset. Click the cat to start again.
Edit: golfed down slightly. I found some golfing tips on the official wiki.
when gf clicked
set[c v]to[4e3
repeat(c
add[2e3]to[f v
end
repeat(250
set[b v]to(c
set[h v]to((d)mod(1e4
change[c v]by(-16
repeat(b
set[d v]to(((d)*(b))+((1e4)*(item(b)of[f v
set[g v]to(((2)*(b))-(1
replace item(b)of[f v]with((d)mod(g
set[d v]to(((d)-((d)mod(g)))/(g
change[b v]by(-1
end
change[h v]by(((d)-((d)mod(1e4)))/(1e4
repeat(4
add((h)mod(10))to[a v
set[h v]to(((h)-((h)mod(10)))/(10
end
repeat(4
say(item(last v)of[a v
play note((round((((item(last v)of[a v])-(1))mod(10))*(1.78)))+(60))for(0.5)beats
delete(last v)of[a v
Graphical:
Uses the Rabinowitz Wagon spigot to produce 4 digits at a time.