Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How Music Works, using Ruby

How Music Works, using Ruby

That strange phenomenon where air molecules bounce against each other in a way that somehow comforts you, makes you cry, or makes you dance all night: music. Since the advent of recorded audio, a musician doesn't even need to be present anymore for this to happen (which makes putting "I will always love you" on repeat a little less awkward).

Sound engineers have found many ways of making music sound good when played from a record. Some of their methods have become industry staples used on every recording released today.

Let's look at what they do and reproduce some of their methods in Ruby!

See https://github.com/thijsc/how_music_works for all code examples.

Thijs Cadier

October 18, 2022
Tweet

More Decks by Thijs Cadier

Other Decks in Programming

Transcript

  1. •Get interested, don’t understand it •Write code •Understand it a

    whole lot better •Show the outcome of this to you all! My process 00
  2. What we are covering today: 01 What is music made

    of? 02 A very brief history of recorded music 03 Digital audio 04 Ampli fi cation 05 Mixing 06 Compression 07 Making sounds
  3. A waveform that we perceive as having pitch, timbre and

    a tempo. What is music made of? 01
  4. 3 def read_wave(path) 4 # Open the input wave 5

    reader = WaveFile::Reader.new(path) 6 # Prepare a buffer for all samples 7 samples = [] 8 # Loop through all buffers in the file 9 loop do 10 begin 11 # Read part of the file 12 buffer = reader.read(8198) 13 # Add it to the samples buffer 14 samples.concat(buffer.samples) 15 rescue EOFError 16 # We're at the end, return the samples buffer 17 return samples 18 end 19 end 20 end
  5. 17 samples.each_with_index do |sample, i| 20 position = if sample

    > 0 21 HALFWAY - offset_from_halfway(sample) 22 else 23 HALFWAY + offset_from_halfway(sample) 24 end 25 26 image.rect(i, position, i + 8, position + 8, YELLOW, YELLOW) 27 end
  6. 41 slices = samples.each_slice(samples_per_pixel).map do |samples| 42 positive_average = samples.select

    do |sample| 43 sample.positive? 44 end.sum / samples_per_pixel 45 46 negative_average = samples.select do |sample| 47 sample.negative? 48 end.map do |sample| 49 sample.abs 50 end.sum / samples_per_pixel 51 52 # Return both 53 [positive_average, negative_average] 54 end
  7. 1 drum = read_wave("input/drum.wav") 2 3 def amplify(track, ratio) 4

    track.map do |sample| 5 sample * ratio 6 end 7 end 8 9 louder = amplify(drum, 2.0) 10 write_wave("output/louder.wav", louder)
  8. 1 drum = read_wave("input/drum.wav") 2 3 def amplify(track, ratio) 4

    track.map do |sample| 5 sample * ratio 6 end 7 end 8 9 clipping = amplify(drum, 4.0) 10 write_wave("output/clipping.wav", clipping)
  9. 1 require_relative "../helpers" 2 3 bass = read_wave("input/bass.wav") 4 drum

    = read_wave("input/drum.wav") 5 piano = read_wave("input/piano.wav")
  10. 9 def sum_tracks(*tracks) 10 tracks.first.map.with_index do |_, i| 11 summed

    = 0 12 # Loop through all the tracks and increment 13 # the summed sample with its value 14 tracks.each do |track| 15 # Increment the summed sample, but first 16 # make it a bit less loud so we don't clip 17 summed += track[i] / 1.5 18 end 19 # Return the summed value 20 summed 21 end 22 end
  11. 0 1 2 3 4 5 6 7 8 9

    10 4 8 12 16 0 1 2 3 4 5 6 7 8 9 10 4 8 12 16
  12. Apply make up gain 0 1 2 3 4 5

    6 7 8 9 10 4 8 12 16
  13. 5 def reduce_peaks(track, treshold, ratio) 6 track.map do |sample| 7

    if sample > treshold or sample < -treshold 8 sample / ratio 9 else 10 sample 11 end 12 end 13 end
  14. 1 class Square 2 include Enumerable 3 4 def next_sample

    5 sample = if @position < @length / 2 6 20_000 7 else 8 -20_000 9 end 10 @position += 1 11 if @position > @length 12 @position = 0 13 end 14 sample 15 end 16 17 def each 18 loop { yield next_sample } 19 end 20 end
  15. 3 # Create a new oscillator 4 oscillator = Square.new(440,

    SAMPLE_RATE) 5 6 # Create output array 7 output = [] 8 9 oscillator.each_with_index do |sample, i| 10 break if i > SAMPLE_RATE 11 output << sample 12 end 13 14 write_wave("output/square.wav", output)
  16. 1 def next_sample 2 sample = Math.sin(@position * Math::PI /

    2) * 25_000 3 4 # Increment position 5 @position += @increment_by 6 7 # Reset position if over max 8 if @position >= frequency 9 @position = 0.0 10 end 11 12 # Return the sample 13 sample 14 end
  17. 3 SAMPLE_RATE = 44_100 4 5 # Create multiple oscillators

    6 a_note = Sine.new(440, SAMPLE_RATE) 7 c_sharp_note = Sine.new(554.37, SAMPLE_RATE) 8 e_note = Sine.new(659.25, SAMPLE_RATE)
  18. 13 0..SAMPLE_RATE.times do |i| 14 # Get the samples for

    all three notes 15 sample_one = a_note.next_sample / 3 16 sample_two = c_sharp_note.next_sample / 3 17 sample_three = e_note.next_sample / 3 18 19 output << sample_one + sample_two + sample_three 20 end
  19. 10 0..SAMPLE_RATE.times do |i| 11 # Get the samples for

    all three notes 12 sample_one = one.next_sample / 3 13 sample_two = two.next_sample / 3 14 15 output << sample_one + sample_two 16 end