In [1]:
midifile = 'data/chopin-fantaisie.mid'
In [2]:
import time
import copy
import subprocess
from abc import abstractmethod
import numpy as np
import midi # Midi file parser
from midipattern import MidiPattern
from distorter import *
from align import align_frame_to_frame, read_align, write_align
In [3]:
MidiPattern.MIDI_DEVICE = 2
In [4]:
pattern = MidiPattern(midi.read_midifile(midifile))
simple = pattern.simplified(bpm=160)
simple.stamp_time('t0')
midi.write_midifile("generated/simple.mid", simple)
In [5]:
print simple.attributes[0][-40:]
In [6]:
pattern[0]
pattern.play(180)
In [7]:
simple.play()
In [8]:
distorter = VelocityNoiseDistorter(sigma=20.)
distorter.randomize()
print distorter
dist_pattern = distorter.distort(simple)
midi.write_midifile('generated/velocity-noise.mid', dist_pattern)
dist_pattern.play(bpm=180)
In [9]:
print dist_pattern.attributes[0][-4:]
In [10]:
distorter = VelocityWalkDistorter(sigma=0.1)
distorter.randomize()
print distorter
dist_pattern = distorter.distort(simple)
midi.write_midifile('generated/velocity-walk.mid', dist_pattern)
dist_pattern.play(bpm=180)
In [11]:
distorter = ProgramDistorter()
distorter.randomize()
# for some reason GM 1- 3 makes no sound in pygame?
print distorter
dist_pattern = distorter.distort(simple)
midi.write_midifile('generated/program.mid', dist_pattern)
dist_pattern.play(bpm=180)
In [12]:
distorter = TempoDistorter(sigma=0, min=0.5, max=2.)
distorter.randomize()
print distorter
dist_pattern = distorter.distort(simple)
print 'time warp', dist_pattern.attributes[0][-4:]
midi.write_midifile('generated/tempo.mid', dist_pattern)
dist_pattern.play(bpm=180)
In [13]:
distorter = TimeNoiseDistorter()
distorter.randomize()
print distorter
dist_pattern = distorter.distort(simple)
print 'time warp', dist_pattern.attributes[0][-4:]
midi.write_midifile('generated/time.mid', dist_pattern)
dist_pattern.play(bpm=180)
In [14]:
stride = 1.
align = align_frame_to_frame(dist_pattern, stride)
align
Out[14]:
In [15]:
write_align('generated/align.txt', align, stride)
align2, stride2 = read_align('generated/align.txt')
print align2 == align
print int(stride2) == int(stride), stride2, stride
In [20]:
dist_pattern = random_distort(simple)
align = align_frame_to_frame(dist_pattern, stride=1.)
print align
dist_pattern.play()
In [21]:
num_samples = 10
stride = 0.1
for i in xrange(num_samples):
base_name = 'generated/sample-{}'.format(i)
align_name = '{}.txt'.format(base_name)
midi_name = '{}.mid'.format(base_name)
wav_name = '{}.wav'.format(base_name)
distorted = random_distort(simple)
align = align_frame_to_frame(distorted, stride)
write_align(align_name, align, stride)
midi.write_midifile(midi_name, distorted)
# Convert to wav using timidity
print wav_name
subprocess.check_call(['timidity', '-Ow', midi_name, '-o', wav_name])
print 'Done generating {}'.format(base_name)