Friday, January 23, 2015

Cacophony for the whole family

Taking a break from my series of excerpts from Think DSP, I want to share an example I wrote to demonstrate some of the features in the thinkdsp library.  Last night I had the pleasure of listening to a grade school band, and during the breaks between sets, my mind wandered to the problem of synthesizing their unique sound.

With careful listening and consideration, I identified three important factors:

  1. Some of the instruments are out of tune by as much as a half step.
  2. At any point in the song, some fraction of the kids are not playing the written note, but choosing an alternative interpretation, plus or minus a major third.
  3. At any time, a kid might pop an overtone, playing a note at 2, 3, 4, or even 5 times the intended frequency (look out, David Sanborn!).
I implemented these effects with this function, which takes a MIDI number representing the written note and returns the chosen frequency:


def random_freq(midi_num):

    # simulate poor tuning by adding 
    # gaussian noise to the MIDI number
    midi_num += random.gauss(0, 0.5)
    
    # one kid out of 10 plays the wrong note
    if random.random() < 0.1:
        midi_num += random.randint(-5, 5)
        
    freq = midi_to_freq(midi_num)
    
    # and one kid in 10 pops an overtone
    if random.random() < 0.1:
        freq *= random.randint(2, 5)

    return freq

To hear what it all sounds like, check out this IPython notebook.  I hope you enjoy it as much as I enjoyed last night's performance!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.