DEV Community

Discussion on: Creating audio from raw bits in Scala

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

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 volume
def wave(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 semitone
def f(n)
  return 440.0 * (2 ** (1.0/12)) ** n
end

# Play nth semitone
def note(n, duration = 1.0, sampleRate = 48000, vol = 0.2)
  return wave( 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-pack
def save(fname, sound)
  File.write(fname, sound.pack("F*"), mode: "wb")
end

# Uses ffplay (installed as part of ffmpeg)
def play(fname, sampleRate = 48000)
  cmd = "ffplay -autoexit -showmode 1 -f f32le -ar #{sampleRate} #{fname}"
  puts cmd
  puts `#{cmd}`
end

x = 'sound.bin'
d = 0.3
w = 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)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
awwsmm profile image
Andrew (he/him)

I haven't tried playing it in ffplay... I'll give that a shot. Thanks, Raunak!