def test_diminish(self): b = Bar() c = Bar() b + 'A' c + 'Ab' b.diminish() self.assertEqual(b, c)
def add_notes(self, note, duration=None): """Add a Note, note as string or NoteContainer to the last Bar. If the Bar is full, a new one will automatically be created. If the Bar is not full but the note can't fit in, this method will return False. True otherwise. An InstrumentRangeError exception will be raised if an Instrument is attached to the Track, but the note turns out not to be within the range of the Instrument. """ if self.instrument != None: if not self.instrument.can_play_notes(note): raise InstrumentRangeError( "Note '%s' is not in range of the instrument (%s)" % (note, self.instrument)) if duration == None: duration = 4 # Check whether the last bar is full, if so create a new bar and add the # note there if len(self.bars) == 0: self.bars.append(Bar()) last_bar = self.bars[-1] if last_bar.is_full(): self.bars.append(Bar(last_bar.key, last_bar.meter)) # warning should hold note if it doesn't fit return self.bars[-1].place_notes(note, duration)
def test_diminish(self): b = Bar() c = Bar() b + "A" c + "Ab" b.diminish() self.assertEqual(b, c)
def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4, 4)) self.fbar = Bar('F', (6, 8)) self.tbar = Bar('C', (4, 4)) self.mbar = Bar('C', (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ['C', 'E', 'G', 'B']) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), [ 'C', 'E', 'G', 'B', 'C', 'E', ]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ['C', 'E']) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ['G', 'B', 'C' ]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2)
def test_augment_rest(self): b = Bar() b.place_notes("C-4", 4) b.place_rest(4) c = Bar() c.place_notes("C#-4", 4) c.place_rest(4) b.augment() self.assertEqual(b, c)
def test_diminish_rest(self): b = Bar() b.place_notes("C#-4", 4) b.place_rest(4) c = Bar() c.place_notes("C-4", 4) c.place_rest(4) b.diminish() self.assertEqual(b, c)
def test_transpose_rest(self): b = Bar() b.place_notes("C-4", 4) b.place_rest(4) c = Bar() c.place_notes("E-4", 4) c.place_rest(4) b.transpose("3", True) self.assertEqual(b, c)
def test_transpose(self): b = Bar() c = Bar() b + ['C', 'E', 'G'] c + ['E', 'G#', 'B'] b + ['F', 'A', 'C'] c + ['A', 'C#', 'E'] b.transpose('3', True) self.assertEqual(b, c) b.transpose('3', False) b.transpose('3') self.assertEqual(b, c)
def test_transpose(self): b = Bar() c = Bar() b + ["C", "E", "G"] c + ["E", "G#", "B"] b + ["F", "A", "C"] c + ["A", "C#", "E"] b.transpose("3", True) self.assertEqual(b, c) b.transpose("3", False) b.transpose("3") self.assertEqual(b, c)
def test_augment(self): b = Bar() c = Bar() d = Bar() b + "A" c + "A#" d + "A##" b.augment() self.assertEqual(b, c) b.augment() self.assertEqual(b, d) c.augment() self.assertEqual(c, d)
def test_augment(self): b = Bar() c = Bar() d = Bar() b + 'A' c + 'A#' d + 'A##' b.augment() self.assertEqual(b, c) b.augment() self.assertEqual(b, d) c.augment() self.assertEqual(c, d)
def test_equality(self): self.assertEqual(self.b, self.c) self.assertEqual(self.b, self.meterless) self.assertEqual(self.c, self.meterless) b1 = Bar("C", (4, 4)) b1 + ["A", "C"] b1 + ["D"] b2 = Bar("C", (4, 4)) b2 + ["A", "C"] b2 + ["D"] self.assertEqual(b1, b2) self.assertNotEqual(b1, self.b)
def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4, 4)) self.fbar = Bar('F', (6, 8)) self.tbar = Bar('C', (4, 4)) self.mbar = Bar('C', (4, 4)) self.a_minor_bar = Bar('a', (4, 4)) self.b_flat_minor_bar = Bar('bb', (4, 4)) self.f_sharp_minor_bar = Bar('f#', (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: list(map(lambda x: y + x, ['C', 'E', 'G', 'B'])) list( map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), ['C', 'E', 'G', 'B', 'C', 'E'])) list( map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ['C', 'E'])) list( map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ['G', 'B', 'C'])) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2)
def setUp(self): # LilyPond output files are created in current working directory self.tempdir = tempfile.mkdtemp() self.oldcwd = os.getcwd() os.chdir(self.tempdir) self.commonbar = Bar() self.ebar = Bar("E", (4, 4)) self.fbar = Bar("F", (6, 8)) self.tbar = Bar("C", (4, 4)) self.mbar = Bar("C", (4, 4)) self.a_minor_bar = Bar("a", (4, 4)) self.b_flat_minor_bar = Bar("bb", (4, 4)) self.f_sharp_minor_bar = Bar("f#", (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: list(map(lambda x: y + x, ["C", "E", "G", "B"])) for x in ["C", "E", "G", "B", "C", "E"]: self.tbar.place_notes(NoteContainer(x), 6) for x in ["C", "E"]: self.mbar.place_notes(NoteContainer(x), 4) for x in ["G", "B", "C"]: self.mbar.place_notes(NoteContainer(x), 6) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2)
def test_set_item(self): b = Bar() b + ["A", "C", "E"] c = Bar() c + ["A", "C", "E"] self.assertEqual(b, c) c[0] = NoteContainer(["A", "C", "E"]) self.assertEqual(b, c) c[0] = ["A", "C", "E"] self.assertEqual(b, c) c[0] = Note("A") c[0] = c[0][2] + NoteContainer(["C", "E"]) self.assertEqual(b, c) c[0] = Note("A") c[0] = c[0][2] + "C" c[0] = c[0][2] + "E" self.assertEqual(b, c)
def test_set_item(self): b = Bar() b + ['A', 'C', 'E'] c = Bar() c + ['A', 'C', 'E'] self.assertEqual(b, c) c[0] = NoteContainer(['A', 'C', 'E']) self.assertEqual(b, c) c[0] = ['A', 'C', 'E'] self.assertEqual(b, c) c[0] = Note('A') c[0] = c[0][2] + NoteContainer(['C', 'E']) self.assertEqual(b, c) c[0] = Note('A') c[0] = c[0][2] + 'C' c[0] = c[0][2] + 'E' self.assertEqual(b, c)
def playNextNoteLong(self): """Plays the next note in the song and advances the index""" tempBar = Bar() tempBar.set_meter((1, 1)) tempBar.key = self._key tempBar.place_notes(self._notes[self._index], 1) fs.play_Bar(tempBar, 120)
def playManyNotes(self, notes): """Plays the next note in the song and advances the index""" tempBar = Bar() tempBar.set_meter((len(notes) * 4, 4)) for n in notes: tempBar.place_notes(n, 1) fs.play_Bar(tempBar, 1, 200)
def setup_composition(nbars=64, ntracks=2, quant=16): comp = Composition() tks =[Track() for i in xrange(ntracks)] for i in xrange(ntracks): bars = [Bar() for j in xrange(nbars)] for j in xrange(nbars): init_bar(bars[j], quant) tks[i] + bars[j] comp + tks[i] return comp
def playNextNote(self): """Plays the next note in the song and advances the index""" tempBar = Bar() beats = 1.0 / (1.0 / self._beat) / (1.0 / self._durs[self._index]) tempBar.set_meter((beats, self._beat)) tempBar.key = self._key tempBar.place_notes(self._notes[self._index], self._durs[self._index]) print tempBar fs.play_Bar(tempBar, self._bpm) return self._index
def gen(self, midifilename): dummy = Note('C-0') dummy.velocity = 0 dummy.channel = 9 dummy.duration = self.dur for nb in xrange( self.n_bars ): b = Bar('C', (4,4)) # note "dummy" posizionate su una "griglia di quantizzazione" for i in xrange(self.nsteps): b.place_notes(dummy, self.dur) # print repr(b) # debug # note "vere" for x in self.STRMAP.keys(): ys = self.STRMAP[x] y = self.DRMAP[x] if len(ys) > 0: if len(self.VARMAP[x]) > 0: var = self.VARMAP[x][nb] else: var = 0 if self.vartype == self._OR_VAR_: # or = variazione aggiunge !? xor = toggle tmps = or_str( ys, gen_bin_str(self.nsteps, var) ) elif self.vartype == self._XOR_VAR_: tmps = xor_str( ys, gen_bin_str(self.nsteps, var) ) else: tmps = ys # no var. bin_to_bar(b, y, tmps, self.dur) # cleanup - tolgo le note "dummy" for x in b.bar : x[2].remove_note('C', 0) # print repr(b) # debug self.tk + b # "dummy bar" db = Bar('C', (4,4)) db.place_notes(dummy,1) self.tk + db comp = Composition() comp + self.tk write_Composition(midifilename, comp, self.tempo)
def __init__(self): self._notes = [] self._bar = None self._durs = [] self._index = 0 self._key = "" self._bpm = 120 self._dynamics = {} self.paused = False self._bar = Bar() fs.init("sound.sf2", "oss") mixer.init()
def loadSong(self, fn, isTwoHand): """Loads in a song from the specified file""" header = dict() length = 0.0 self._notes = [] self._bar = None self._durs = [] with open(fn) as file: i = 0 for line in file: if line[0] == "#" or line[0].strip( ) == "": # Skips blank lines and comment lines continue if i < 3: # The first three lines are loaded into the header dictionary vals = line.split('=') header[vals[0]] = vals[1].strip() i += 1 else: vals = line.split(',') if line[0] == 'r': # Indicates a rest value self._notes.append(None) dur = vals[1].strip() else: if isTwoHand: # If isTwoHand is true, then a two note chord is looked for self._notes.append( [vals[0].strip(), vals[1].strip()]) dur = vals[2].strip() else: self._notes.append(vals[0].strip()) dur = vals[1].strip() if dur[len(dur) - 1] == '.': # Looks for dotted notes dur = value.dots(int(dur[:-1])) else: dur = int(dur) self._durs.append(dur) length += 1.0 / dur self._key = header["key"] self._bpm = int(header["bpm"]) self._beat = int(header["beat"]) length *= self._bpm self._bar = Bar() self._bar.set_meter((length, int(header["beat"])))
def rebuild_composition(old, xylo, cutoff): lowest = Note.__int__(Note('C', 8)) highest = Note.__int__(Note('C', 0)) new = Composition() for i in old.selected_tracks: t = Track() t.instrument = xylo for bar in old[i]: b = Bar() b.key.name = bar.key.name b.set_meter(bar.meter) # note value per beat == the denominator in time signature dem = b.meter[1] for lst in bar: value = lst[1] if (is_garbage(value, dem, cutoff[old.selected_tracks.index(i)])): continue # do not include lists without notes if (not lst[2]): continue nc = NoteContainer() for note in lst[2]: if (Note.__int__(note) < lowest): lowest = Note.__int__(note) if (Note.__int__(note) > highest): highest = Note.__int__(note) nc + note b.place_notes(nc, value) t.add_bar(b) new.add_track(t) # can't do the transposing until all notes in all tracks have been # compared against lowest and highest, which is why it is done here n1 = Note() n2 = Note() low = n1.from_int(lowest) high = n2.from_int(highest) # print("lowest and highest notes:", low, high) if (not xylo.notes_in_range([low, high])): new = transposer(new, lowest, highest, xylo) return new
def MIDI_to_Composition(self, file): (header, track_data) = self.parse_midi_file(file) c = Composition() bpm = 120 if header[2]['fps']: print "Don't know how to parse this yet" return c ticks_per_beat = header[2]['ticks_per_beat'] for track in track_data: t = Track() b = Bar() metronome = 1 # Tick once every quarter note thirtyseconds = 8 # 8 thirtyseconds in a quarter note meter = (4, 4) key = 'C' for e in track: (deltatime, event) = e duration = float(deltatime) / (ticks_per_beat * 4.0) if duration != 0.0: duration = 1.0 / duration if len(b.bar) > 0: current_length = b.bar[-1][1] b.bar[-1][1] = duration if current_length - duration != 0: b.current_beat -= 1.0 / current_length b.current_beat += 1.0 / duration if not b.place_notes(NoteContainer(), duration): t + b b = Bar(key, meter) b.place_notes(NoteContainer(), duration) if event['event'] == 8: if deltatime == 0: pass elif event['event'] == 9: # note on n = Note(notes.int_to_note(event['param1'] % 12), event['param1'] / 12 - 1) n.channel = event['channel'] n.velocity = event['param2'] if len(b.bar) > 0: b.bar[-1][2] + n else: b + n elif event['event'] == 10: # note aftertouch pass elif event['event'] == 11: # controller select pass elif event['event'] == 12: # program change i = MidiInstrument() i.instrument_nr = event['param1'] t.instrument = i elif event['event'] == 0x0f: # meta event Text if event['meta_event'] == 1: pass elif event['meta_event'] == 3: # Track name t.name = event['data'] elif event['meta_event'] == 6: # Marker pass elif event['meta_event'] == 7: # Cue Point pass elif event['meta_event'] == 47: # End of Track pass elif event['meta_event'] == 81: # Set tempo warning Only the last change in bpm will get # saved currently mpqn = self.bytes_to_int(event['data']) bpm = 60000000 / mpqn elif event['meta_event'] == 88: # Time Signature d = event['data'] thirtyseconds = self.bytes_to_int(d[3]) metronome = self.bytes_to_int(d[2]) / 24.0 denom = 2**self.bytes_to_int(d[1]) numer = self.bytes_to_int(d[0]) meter = (numer, denom) b.set_meter(meter) elif event['meta_event'] == 89: # Key Signature d = event['data'] sharps = self.bytes_to_int(d[0]) minor = self.bytes_to_int(d[0]) if minor: key = 'A' else: key = 'C' for i in xrange(abs(sharps)): if sharps < 0: key = intervals.major_fourth(key) else: key = intervals.major_fifth(key) b.key = Note(key) else: print 'Unsupported META event', event['meta_event'] else: print 'Unsupported MIDI event', event t + b c.tracks.append(t) return (c, bpm)
t += [MidiTrack(bpm)] m.tracks = t while repeat >= 0: for i in range(len(composition.tracks)): m.tracks[i].play_Track(composition.tracks[i]) repeat -= 1 return m.write_file(file, verbose) if __name__ == "__main__": from mingus.containers.note_container import NoteContainer from mingus.containers.bar import Bar from mingus.containers.track import Track from mingus.containers.instrument import MidiInstrument b = Bar() b2 = Bar("Ab", (3, 4)) n = NoteContainer(["A", "C", "E"]) t = Track() b + n b + [] b + n b + n b2 + n b2 + n b2 + [] t + b t + b m = MidiInstrument() m.instrument_nr = 13 t.instrument = m
def setUp(self): self.b = Bar("C", (4, 4)) self.c = Bar("E", (2, 2)) self.meterless = Bar("C", (0, 0))
def test_get_note_names(self): b = Bar() b + "C" b + "A" self.assertEqual(["C", "A"], b.get_note_names())
def test_determine_chords(self): b = Bar() b + ["C", "E", "G"] b + ["F", "A", "C"] self.assertEqual([[0.0, ["C major triad"]], [0.25, ["F major triad"]]], b.determine_chords())
def test_determine_progression(self): b = Bar() b + ["C", "E", "G"] b + ["F", "A", "C"] self.assertEqual([[0.0, ["I"]], [0.25, ["IV"]]], b.determine_progression(True))