def test_repr(self): self.assertEqual(repr(pm.Note("E")), '<Note E>') self.assertEqual(repr(pm.Note( "A#", 4, )), '<Note A#4>') self.assertEqual(repr(pm.Note("Gbb", 3, "4")), '<Note Gbb3:4>') self.assertEqual(repr(pm.Note("F#", 0, "3.t")), '<Note F#0:3.t>') self.assertEqual(repr(pm.Note("B#", 3, "5.")), '<Note B#3:5.>')
def test_octave(self): self.assertEqual(pm.Note("C").octave, None) self.assertEqual(pm.Note("C", 4).octave, 4) _note = pm.Note("F") _note.octave = 0 self.assertEqual(_note.octave, 0) with self.assertRaises(ValueError): pm.Note("C", 1.1) with self.assertRaises(ValueError): pm.Note("C", "C")
def test_note_method(self): A5 = pm.Note("A", 5) B2 = pm.Note("B", 2) Bb3 = pm.Note("Bb", 3) As4 = pm.Note("A#", 4) G5 = pm.Note("G", 5) Gb2 = pm.Note("Gb", 2) E6 = pm.Note("E", 6) Dbb2 = pm.Note("Dbb", 2) Dss3 = pm.Note("D##", 3) intvl1 = pm.Interval("p1") intvl2 = pm.Interval("a1") intvl3 = pm.Interval("m2") intvl4 = pm.Interval("M2") intvl5 = pm.Interval("M3") intvl6 = pm.Interval("P4") intvl7 = pm.Interval("D5") intvl8 = pm.Interval("D5", 1) intvl9 = pm.Interval("p1", 2) intvl10 = pm.Interval("A5") intvl11 = pm.Interval("M7") intvl12 = pm.Interval("d7") intvl13 = pm.Interval("a3") intvl14 = pm.Interval("a..4") intvl15 = pm.Interval("d...5", 2) intvl16 = pm.Interval("d.2") intvl17 = pm.Interval("m3", 1) intvl18 = pm.Interval("m3", 2) intvl19 = pm.Interval("M7", 1) intvl20 = pm.Interval("M7", 3) intvl21 = pm.Interval("p1", 1) self.assertEqual((A5 + intvl1).name, "A") self.assertEqual((A5 + intvl2).name, "A#") self.assertEqual((B2 + intvl19).name, "A#") self.assertEqual((B2 + intvl19).octave, 4) self.assertEqual((E6 + intvl15).name, "Bbbbb") self.assertEqual((Dss3 + intvl16).name, "Eb") self.assertEqual((G5 + intvl20).name, "F#") self.assertEqual((G5 + intvl20).octave, 9) self.assertEqual((A5 - intvl1).name, "A") self.assertEqual((A5 - intvl2).name, "Ab") self.assertEqual((B2 - intvl19).name, "C") self.assertEqual((B2 - intvl19).octave, 1) self.assertEqual((E6 - intvl15).name, "A####") self.assertEqual((Dss3 - intvl16).name, "C#####") self.assertEqual((G5 - intvl20).name, "Ab") self.assertEqual((G5 - intvl20).octave, 1)
def test_rhythm_set(self): pm.Note("C", 4, "0t") dot_quarter = pm.Note("D", 5, "3.") sextuplet = pm.Note("E", 2, "5t") crazy = pm.Note("Fbbb", -10, "7..t") crazy.rhythm = 0 crazy.rhythm = "3.t" with self.assertRaises(ValueError): pm.Note("C", 1, "4tt") with self.assertRaises(ValueError): pm.Note("C", 1, "11") with self.assertRaises(ValueError): pm.Note("C", 1, "t") with self.assertRaises(ValueError): pm.Note("C", 1, "0t.") with self.assertRaises(ValueError): pm.Note("C", 1, ".")
def test_set_A4(self): pm.A4.setA4(442) self.assertEqual(pm.A4.getA4(), 442) self.assertAlmostEqual(pm.Note("A", 5).frequency, 884) with self.assertRaises(ValueError): pm.A4.setA4(-1) with self.assertRaises(ValueError): pm.A4.setA4(0) with self.assertRaises(ValueError): pm.A4.setA4("440")
def test_hard_pitch(self): C0 = pm.Note("C", 0) C1 = pm.Note("C", 1) Cn1 = pm.Note("C", -1) C4 = pm.Note("C", 4) B3 = pm.Note("B", 3) B4 = pm.Note("B", 4) Db4 = pm.Note("Db", 4) A4 = pm.Note("A", 4) self.assertEqual(C0.hard_pitch, 0) self.assertEqual(C1.hard_pitch, 12) self.assertEqual(Cn1.hard_pitch, -12) self.assertEqual(C4.hard_pitch, 48) self.assertEqual(B3.hard_pitch, 47) self.assertEqual(B4.hard_pitch, 59) self.assertEqual(Db4.hard_pitch, 49) self.assertEqual(A4.hard_pitch, 57)
def test_frequency(self): A4 = pm.Note("A", 4) A3 = pm.Note("A", 3) A5 = pm.Note("A", 5) C4 = pm.Note("C", 4) B3 = pm.Note("B", 3) B4 = pm.Note("B", 4) Db4 = pm.Note("Db", 4) self.assertEqual(A4.frequency, 440) self.assertEqual(A3.frequency, 220) self.assertEqual(A5.frequency, 880) self.assertAlmostEqual(C4.frequency, 261.6, 1) self.assertAlmostEqual(B3.frequency, 246.9, 1) self.assertAlmostEqual(B4.frequency, 493.9, 1) self.assertAlmostEqual(Db4.frequency, 277.2, 1)
def test_pitch_offset(self): note_As = pm.Note("A#") note_C = pm.Note("C") note_Gb = pm.Note("Gb") note_G = pm.Note("G") note_Ess = pm.Note("E##") note_Bbb = pm.Note("Bbb") note_Dss = pm.Note("D##") self.assertEqual(note_As.pitch_offset, 1) self.assertEqual(note_C.pitch_offset, 0) self.assertEqual(note_Gb.pitch_offset, -1) self.assertEqual(note_G.pitch_offset, 0) self.assertEqual(note_Ess.pitch_offset, 2) self.assertEqual(note_Bbb.pitch_offset, -2) self.assertEqual(note_Dss.pitch_offset, 2)
def test_enharmonic(self): C = pm.Note("C") Bs = pm.Note("B#") E = pm.Note("E") Fb = pm.Note("Fb") Dss = pm.Note("D##") As = pm.Note("A#") Bb = pm.Note("Bb") Abbb = pm.Note("Abbb") self.assertEqual(Bs.enharmonic().name, C.name) self.assertEqual(E.enharmonic(None, True).name, Fb.name) self.assertEqual(Fb.enharmonic().name, E.name) self.assertEqual(Dss.enharmonic().name, E.name) self.assertEqual(As.enharmonic().name, Bb.name) self.assertEqual(Bb.enharmonic().name, As.name) self.assertEqual(As.enharmonic("#").name, "A#") self.assertEqual(Bb.enharmonic("b").name, "Bb") self.assertEqual(Abbb.enharmonic("#").name, "F#") self.assertEqual(Abbb.enharmonic("b").name, "Gb")
def test_intvl_from_notes(self): A = pm.Note("A") B = pm.Note("B") Bb = pm.Note("Bb") As = pm.Note("A#") G = pm.Note("G") Gb = pm.Note("Gb") E = pm.Note("E") Dbb = pm.Note("Dbb") Dss = pm.Note("D##") self.assertEqual(pm.Interval.from_notes(A, B).name, "Major 2nd") self.assertEqual(pm.Interval.from_notes(A, Bb).name, "Minor 2nd") self.assertEqual( pm.Interval.from_notes(A, As).name, "Augmented unison") self.assertEqual(pm.Interval.from_notes(G, A).name, "Major 2nd") self.assertEqual(pm.Interval.from_notes(Gb, B).name, "Augmented 3rd") self.assertEqual( pm.Interval.from_notes(Dss, Dbb).name, "Diminished(x4) unison") self.assertEqual(pm.Interval.from_notes(E, B).name, "Perfect 5th") self.assertEqual(pm.Interval.from_notes(B, E).name, "Perfect 4th") A4 = A A4.octave = 4 A5 = pm.Note("A", 5) A6 = pm.Note("A", 6) B3 = B B3.octave = 3 G2 = G G2.octave = 2 Bb4 = Bb Bb4.octave = 4 E5 = E E5.octave = 5 Gb2 = Gb Gb2.octave = 2 self.assertEqual(pm.Interval.from_notes(A4, Bb4).name, "Minor 2nd") self.assertEqual(pm.Interval.from_notes(A4, E5).name, "Perfect 5th") self.assertEqual(pm.Interval.from_notes(G2, B3).name, "Major 10th") self.assertEqual(pm.Interval.from_notes(E5, Bb4).name, "Augmented 4th") self.assertEqual(pm.Interval.from_notes(Bb4, E5).name, "Augmented 4th") self.assertEqual(pm.Interval.from_notes(Bb4, E5).name, "Augmented 4th") self.assertEqual(pm.Interval.from_notes(Bb4, E5).name, "Augmented 4th") self.assertEqual(pm.Interval.from_notes(A4, A5).name, "Perfect octave") self.assertEqual(pm.Interval.from_notes(A4, A6).name, "2 octaves")
def test_letter_value(self): self.assertEqual(pm.Note("C").letter, 0) self.assertEqual(pm.Note("C#").letter, 0) self.assertEqual(pm.Note("Cb").letter, 0) self.assertEqual(pm.Note("Cbb").letter, 0) self.assertEqual(pm.Note("C##").letter, 0) self.assertEqual(pm.Note("D").letter, 1) self.assertEqual(pm.Note("D#").letter, 1) self.assertEqual(pm.Note("Db").letter, 1) self.assertEqual(pm.Note("Dbb").letter, 1) self.assertEqual(pm.Note("D##").letter, 1) self.assertEqual(pm.Note("E").letter, 2) self.assertEqual(pm.Note("E#").letter, 2) self.assertEqual(pm.Note("Eb").letter, 2) self.assertEqual(pm.Note("Ebb").letter, 2) self.assertEqual(pm.Note("E##").letter, 2) self.assertEqual(pm.Note("F").letter, 3) self.assertEqual(pm.Note("F#").letter, 3) self.assertEqual(pm.Note("Fb").letter, 3) self.assertEqual(pm.Note("Fbb").letter, 3) self.assertEqual(pm.Note("F##").letter, 3) self.assertEqual(pm.Note("G").letter, 4) self.assertEqual(pm.Note("G#").letter, 4) self.assertEqual(pm.Note("Gb").letter, 4) self.assertEqual(pm.Note("Gbb").letter, 4) self.assertEqual(pm.Note("G##").letter, 4) self.assertEqual(pm.Note("A").letter, 5) self.assertEqual(pm.Note("A#").letter, 5) self.assertEqual(pm.Note("Ab").letter, 5) self.assertEqual(pm.Note("Abb").letter, 5) self.assertEqual(pm.Note("A##").letter, 5) self.assertEqual(pm.Note("B").letter, 6) self.assertEqual(pm.Note("B#").letter, 6) self.assertEqual(pm.Note("Bb").letter, 6) self.assertEqual(pm.Note("Bbb").letter, 6) self.assertEqual(pm.Note("B##").letter, 6)
def test_accidentals(self): pm.Note("A#") pm.Note("A##") pm.Note("Ab") pm.Note("Abb") pm.Note("B#") pm.Note("B##") pm.Note("Bb") pm.Note("Bbb") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("A#") pm.Note("Bbb") pm.Note("G##") pm.Note("Fb") with self.assertRaises(ValueError): pm.Note('Ab#') with self.assertRaises(ValueError): pm.Note('Eb#')
def test_natural_note(self): self.assertEqual(pm.Note("A").name, "A") self.assertEqual(pm.Note("b").name, "B") self.assertEqual(pm.Note("B ").name, "B") self.assertEqual(pm.Note("B").name, "B") self.assertEqual(pm.Note(" c").name, "C") self.assertEqual(pm.Note("C").name, "C") self.assertEqual(pm.Note("D").name, "D") self.assertEqual(pm.Note("E").name, "E") self.assertEqual(pm.Note("F").name, "F") self.assertEqual(pm.Note("G").name, "G") with self.assertRaises(ValueError): pm.Note('AA') with self.assertRaises(ValueError): pm.Note('H')
def test_rhythm_val(self): pm.Note("C", 4, "0t").rhythm double_whole = pm.Note("C", 2, "0") self.assertEqual(double_whole.rhythm.value, 1024) five_twelth = pm.Note("C", 3, "10") self.assertEqual(five_twelth.rhythm.value, 1) dot_quarter = pm.Note("D", 5, "3.") self.assertEqual(dot_quarter.rhythm.value, 192) trip_quarter = pm.Note("E", 2, "3t") self.assertAlmostEqual(trip_quarter.rhythm.value, 85.3333333333333) self.assertEqual(pm.Note("A", None, "0").rhythm.value, 1024) self.assertEqual(pm.Note("A", None, "1").rhythm.value, 512) self.assertEqual(pm.Note("A", None, "2").rhythm.value, 256) self.assertEqual(pm.Note("A", None, "3").rhythm.value, 128) self.assertEqual(pm.Note("A", None, "4").rhythm.value, 64) self.assertEqual(pm.Note("A", None, "5").rhythm.value, 32) self.assertEqual(pm.Note("A", None, "6").rhythm.value, 16) self.assertEqual(pm.Note("A", None, "7").rhythm.value, 8) self.assertEqual(pm.Note("A", None, "8").rhythm.value, 4) self.assertEqual(pm.Note("A", None, "9").rhythm.value, 2) self.assertEqual(pm.Note("A", None, "10").rhythm.value, 1) self.assertEqual(pm.Note("A", None, "0.").rhythm.value, 1536) self.assertEqual(pm.Note("A", None, "10.").rhythm.value, 1.5) self.assertAlmostEqual( pm.Note("A", None, "10t").rhythm.value, 0.6666666666666)
def test_pitch_value(self): self.assertEqual(pm.Note("C").pitch, 0) self.assertEqual(pm.Note("C#").pitch, 1) self.assertEqual(pm.Note("Cb").pitch, 11) self.assertEqual(pm.Note("Cbb").pitch, 10) self.assertEqual(pm.Note("C##").pitch, 2) self.assertEqual(pm.Note("D").pitch, 2) self.assertEqual(pm.Note("D#").pitch, 3) self.assertEqual(pm.Note("Db").pitch, 1) self.assertEqual(pm.Note("Dbb").pitch, 0) self.assertEqual(pm.Note("D##").pitch, 4) self.assertEqual(pm.Note("E").pitch, 4) self.assertEqual(pm.Note("E#").pitch, 5) self.assertEqual(pm.Note("Eb").pitch, 3) self.assertEqual(pm.Note("Ebb").pitch, 2) self.assertEqual(pm.Note("E##").pitch, 6) self.assertEqual(pm.Note("F").pitch, 5) self.assertEqual(pm.Note("F#").pitch, 6) self.assertEqual(pm.Note("Fb").pitch, 4) self.assertEqual(pm.Note("Fbb").pitch, 3) self.assertEqual(pm.Note("F##").pitch, 7) self.assertEqual(pm.Note("G").pitch, 7) self.assertEqual(pm.Note("G#").pitch, 8) self.assertEqual(pm.Note("Gb").pitch, 6) self.assertEqual(pm.Note("Gbb").pitch, 5) self.assertEqual(pm.Note("G##").pitch, 9) self.assertEqual(pm.Note("A").pitch, 9) self.assertEqual(pm.Note("A#").pitch, 10) self.assertEqual(pm.Note("Ab").pitch, 8) self.assertEqual(pm.Note("Abb").pitch, 7) self.assertEqual(pm.Note("A##").pitch, 11) self.assertEqual(pm.Note("B").pitch, 11) self.assertEqual(pm.Note("B#").pitch, 0) self.assertEqual(pm.Note("Bb").pitch, 10) self.assertEqual(pm.Note("Bbb").pitch, 9) self.assertEqual(pm.Note("B##").pitch, 1)
if previous: if previous < rhythm_val: rhythm_len = (60 / TEMPO) * (note.rhythm.value / 128) proportion_to_prev = (rhythm_len - ((60 / TEMPO) * (previous / 128))) / rhythm_len TurnOffLater(midi_val,(60 / TEMPO) * (note.rhythm.value / 128) * 0.9).start() else: fl.noteoff(0,midi_val) else: fl.noteoff(0,midi_val) previous = rhythm_val samples = np.append(samples,fl.get_samples(round(SAMPLE_RATE * (0.02 if not last else 1)))) strm.write(fluidsynth.raw_audio_string(samples)) if __name__ == "__main__": import pymusician as pm import bach_prelude_bb_minor as song note_groups = [[pm.Note(*note_deets) for note_deets in note_data] for note_data in song.notes] ng_len = len(note_groups) _last = False for i in range(ng_len): if i == ng_len - 1: _last = True play_notes(*note_groups[i],velocity=70,last=_last) fl.delete() strm.close()