def ks_synth(freq): """ Synthesize the given frequency into a Stream by using a model based on Karplus-Strong. """ ks_mem = (sum(lz.sinusoid(x * freq) for x in [1, 3, 9]) + lz.white_noise() + lz.Stream(-1, 1)) / 5 return lz.karplus_strong(freq, memory=ks_mem)
inoisy = chain.from_iterable(repeat(inoisy_thub).limit(starting_beats)) smix.add(.1 * s, inoisy) smix.add(starting_beats * beat_duration - dur, []) # Event timing # Wavetable lookup initialization square_table = TableLookup([1] * 256 + [-1] * 256) harmonics = dict(enumerate([1, 3, 2, 1, .3, .1, .7, .9, 1, 1, .5, .4, .2], 1)) table = sin_table.harmonize(harmonics).normalize() mem_table = (3 * saw_table + (sin_table - saw_table)**3).normalize() # Notes synth midi_tuning = str2midi([gs.tune for gs in guitar]) midi_pitches = [midi_tuning[string_idx] + fret for string_idx, fret in notes] for freq in midi2freq(midi_pitches): ks_memory = .1 * gauss_noise() + .9 * mem_table(freq * Hz) ks_snd = distortion(karplus_strong(freq * Hz, tau=.2 * s, memory=ks_memory)) tl_snd = .5 * table(freq * Hz) * env smix.add(dur, .5 * ks_snd + .5 * tl_snd) # Shows synthesis wavetables pylab.subplot(2, 1, 1) pylab.plot(table.table) pylab.title("Table lookup waveform") pylab.axis(xmax=len(table) - 1) pylab.subplot(2, 1, 2) pylab.plot(mem_table.table) pylab.title("Karplus-strong memory (besides noise)") pylab.axis(xmax=len(mem_table) - 1) pylab.tight_layout() pylab.show()
smix.add(.1 * s, inoisy) smix.add(starting_beats * beat_duration - dur, []) # Event timing # Wavetable lookup initialization square_table = TableLookup([1] * 256 + [-1] * 256) harmonics = dict(enumerate([1, 3, 2, 1, .3, .1, .7, .9, 1, 1, .5, .4, .2], 1)) table = sin_table.harmonize(harmonics).normalize() mem_table = (3 * saw_table + (sin_table - saw_table) ** 3).normalize() # Notes synth midi_tuning = str2midi([gs.tune for gs in guitar]) midi_pitches = [midi_tuning[string_idx] + fret for string_idx, fret in notes] for freq in midi2freq(midi_pitches): ks_memory = .1 * gauss_noise() + .9 * mem_table(freq * Hz) ks_snd = distortion(karplus_strong(freq * Hz, tau=.2 * s, memory=ks_memory)) tl_snd = .5 * table(freq * Hz) * env smix.add(dur, .5 * ks_snd + .5 * tl_snd) # Shows synthesis wavetables pylab.subplot(2, 1, 1) pylab.plot(table.table) pylab.title("Table lookup waveform") pylab.axis(xmax=len(table) - 1) pylab.subplot(2, 1, 2) pylab.plot(mem_table.table) pylab.title("Karplus-strong memory (besides noise)") pylab.axis(xmax=len(mem_table) - 1) pylab.tight_layout() pylab.show()
step = 60. / beat * s # Open the choral file choral_file = corpus.getBachChorales()[random.randint(0, 399)] choral = corpus.parse(choral_file) print u"Playing", choral.metadata.title # Creates the score from the music21 data score = reduce(operator.concat, [[(pitch.frequency * Hz, # Note note.offset * step, # Starting time note.quarterLength * step, # Duration Fermata in note.expressions) for pitch in note.pitches] for note in choral.flat.notes] ) # Mix all notes into song song = lz.Streamix() last_start = 0 for freq, start, dur, has_fermata in score: delta = start - last_start if has_fermata: delta *= 2 song.add(delta, lz.karplus_strong(freq, memory=ks_mem(freq)) * lz.ones(dur)) last_start = start # Play the song! with lz.AudioIO(True) as player: song = song.append(lz.zeros(.5 * s)) # To avoid a click at the end player.play(song, rate=rate)