What does your commit sound like?

348cb82a — fix typo in controller before_action

How to play a synth with Ruby

The basic setup is:

  1. Set up GarageBand to play an instrument when notes are sent to the channel
  2. Write and run the Ruby code to send notes to the channel
require "midi" # gem install micromidioutput = UniMIDI::Output.open(:first) MIDI.using(output) do  
  play "F4", 0.5
end
require "midi"o = UniMIDI::Output.open(:first)MIDI.using(o) do
  4.times do |oct|
     octave oct + 2
     play "C", 0.25
     play "D", 0.25
     play "E", 0.25
     play "G", 0.25
  endplay "C6", 0.254.times do |oct|
     octave 5 - oct
     play "G", 0.25
     play "E", 0.25
     play "D", 0.25
     play "C", 0.25 unless oct == 3
  end
end

How to turn any commit SHA into a melody

A standard short form git commit message takes the form of 8 hexadecimal characters, 79759e3b. An even number means we could easily transform this into a series of notes to be played in sequence, similar to an arpeggio.

  1. Take the second character, and use its position in the list of 16 hexadecimal characters to pick from a set of scales, in this case just major and minor scales. This combined with the root note gives us a key, such as F# Major.
  2. These scales are 8 notes long and defined as steps away from the root note along a chromatic scale, so the Major scale would be [0,2,4,5,7,9,11,12].
  3. The next step is to map the position of each note in the commit SHA to an equivalent position in the list of 16 hexadecimal characters and then half it, giving us a highest possible position as 8. We then reverse the list to try and ensure we don’t start on the root note most of the time. For example the commit above gives us the following list: [5, 1, 7, 4, 2, 3, 4, 3].
  4. The next step is to take this list of 8 numbers and treat each item as an index in the scale, mapping over the list to pull out the number at that index in the scale. In this commit’s case, this gave us: [9, 2, 12, 7, 4, 5, 7, 5].
  5. Finally, we map over that list of steps and transpose the root note (F#) by that number of steps along the chromatic scale to give you a new note in each position, finally giving a list of notes such as [“Eb5”, “G#4”, “F#5”, “C#5”, “Bb4”, “B4”, “C#5”, “B4”]

The evidence

Running this code logs a line with the outcome of the algorithm and then uses micromodi to send the notes to GarageBand.

$ ruby musical_commit.rb ~/path/to/repo_a
bb5f3455: key of A4 major : ["C#5", "C#5", "C#5", "B4", "A5", "C#5", "F#5", "F#5"]$ ruby musical_commit ~/path/to/repo_b
79759e3b: key of F#4 major : ["Eb5", "G#4", "F#5", "C#5", "Bb4", "B4", "C#5", "B4"]

Elsewhere

Of course Ruby is not the only language that can help you create music, so here are some other useful libraries I am aware of in other languages — feel free to post any others in the comments.

carwow Product, Design & Engineering

What happens under the hood at carwow.co.uk

184

184 claps
Luke Williams

Written by

Polyglot developer based in London. Musician, rugby player, enemy of sushi buffets

carwow Product, Design & Engineering

What happens under the hood at carwow.co.uk