Awesome stuff! I made a similar thing in Ruby while following this video in Haskell which derives a lot of musical stuff from first principles. Re the crackling, have you tried saving it as array of floats and playing it in ffplay?
# Create an array of floats of required frequency, duration, sample rate and volumedefwave(freq=440.0,duration=2.0,sampleRate=48000,vol=0.2)(0..sampleRate*duration).step(1).map{|w|Math.sin(w*freq*2*Math::PI/sampleRate)*vol}end# Get nth semitonedeff(n)return440.0*(2**(1.0/12))**nend# Play nth semitonedefnote(n,duration=1.0,sampleRate=48000,vol=0.2)returnwave(f(n),duration,sampleRate,vol)end# Pack as 32-bit floats to file https://ruby-doc.org/core-2.6.4/Array.html#method-i-packdefsave(fname,sound)File.write(fname,sound.pack("F*"),mode: "wb")end# Uses ffplay (installed as part of ffmpeg)defplay(fname,sampleRate=48000)cmd="ffplay -autoexit -showmode 1 -f f32le -ar #{sampleRate}#{fname}"putscmdputs`#{cmd}`endx='sound.bin'd=0.3w=note(0,d)+note(2,d)+note(4,d)+note(5,d)+note(7,d)+note(9,d)+note(11,d)+note(12,d)+note(0,0.1,48000,0.01)save(x,w)play(x)
Awesome stuff! I made a similar thing in Ruby while following this video in Haskell which derives a lot of musical stuff from first principles. Re the crackling, have you tried saving it as array of floats and playing it in
ffplay
?I haven't tried playing it in
ffplay
... I'll give that a shot. Thanks, Raunak!