Python librosa.note_to_midi() Examples
The following are 21
code examples of librosa.note_to_midi().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
librosa
, or try the search function
.
Example #1
Source File: key.py From pumpp with ISC License | 6 votes |
def enharmonic(self, key_str): '''Force the tonic spelling to fit our tonic list by spelling out of vocab keys enharmonically. Parameters ---------- key_str : str The key_mode string in jams style. Returns ------- key_str : str The key_mode string spelled enharmonically to fit our vocab. ''' key_list = key_str.split(':') # spell the tonic enharmonically if necessary if key_list[0] != 'N': key_list[0] = midi_to_note(note_to_midi(key_list[0]), octave=False) if len(key_list) == 1: key_list.append('major') return ':'.join(key_list)
Example #2
Source File: omr_endtoend_test.py From moonlight with Apache License 2.0 | 6 votes |
def testBeams_sixteenthNotes(self): filename = os.path.join(resource_loader.get_data_files_path(), 'testdata/IMSLP00747-000.png') notes = self.engine.run([filename], output_notesequence=True) def _sixteenth_note(pitch, start_time): return music_pb2.NoteSequence.Note( pitch=librosa.note_to_midi(pitch), start_time=start_time, end_time=start_time + 0.25) # TODO(ringw): Fix the phantom quarter note detected before the treble # clef, and the eighth rest before the first note (should be sixteenth). self.assertIn(_sixteenth_note('C4', 1.5), notes.notes) self.assertIn(_sixteenth_note('D4', 1.75), notes.notes) self.assertIn(_sixteenth_note('E4', 2), notes.notes) self.assertIn(_sixteenth_note('F4', 2.25), notes.notes) # TODO(ringw): The second D and E are detected with only one beam, even # though they are connected to the same beams as the F before them and the # C after them. Fix.
Example #3
Source File: key_signature.py From moonlight with Apache License 2.0 | 6 votes |
def _key_sig_pitch_classes(note_name, ascending_fifths): first_pitch_class = ( librosa.note_to_midi(note_name + '0') % constants.NUM_SEMITONES_PER_OCTAVE) # Go through the circle of fifths in ascending or descending order. step = 1 if ascending_fifths else -1 order = constants.CIRCLE_OF_FIFTHS[::step] # Get the start index for the key signature. first_pitch_class_ind = order.index(first_pitch_class) return list( itertools.islice( # Create a cycle of the order. We may loop around, e.g. from F back to # C. itertools.cycle(order), # Take the 7 pitch classes from the cycle. first_pitch_class_ind, first_pitch_class_ind + constants.NUM_NOTES_IN_DIATONIC_SCALE))
Example #4
Source File: reader_test.py From moonlight with Apache License 2.0 | 6 votes |
def testTreble_simple(self): staff = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph( type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=0), ]) notes = conversions.page_to_notesequence(reader.ScoreReader().read_page( musicscore_pb2.Page(system=[musicscore_pb2.StaffSystem( staff=[staff])]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ Note(pitch=librosa.note_to_midi('B4'), start_time=0, end_time=1) ]))
Example #5
Source File: reader_test.py From moonlight with Apache License 2.0 | 6 votes |
def testBass_simple(self): staff = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph( type=Glyph.CLEF_BASS, x=1, y_position=reader.BASS_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=0), ]) notes = conversions.page_to_notesequence(reader.ScoreReader().read_page( musicscore_pb2.Page(system=[musicscore_pb2.StaffSystem( staff=[staff])]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ Note(pitch=librosa.note_to_midi('D3'), start_time=0, end_time=1) ]))
Example #6
Source File: postprocess.py From Music-Transcription-with-Semantic-Segmentation with GNU General Public License v3.0 | 6 votes |
def to_midi(notes, t_unit=0.02): midi = pretty_midi.PrettyMIDI() piano = pretty_midi.Instrument(program=0) l, u = find_min_max_stren(notes) s_low = 60 s_up = 127 v_map = lambda stren: int(s_low+((s_up-s_low)*((stren-l)/(u-l+0.0001)))) low_b = note_to_midi("A0") coll = set() for nn in notes: pitch = nn["pitch"] + low_b start = nn["start"] * t_unit end = nn["end"] * t_unit volume = v_map(nn["stren"]) coll.add(pitch) m_note = pretty_midi.Note(velocity=volume, pitch=pitch, start=start, end=end) piano.notes.append(m_note) midi.instruments.append(piano) return midi
Example #7
Source File: reader_test.py From moonlight with Apache License 2.0 | 6 votes |
def testAllNoteheadTypes(self): staff = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph( type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-6), Glyph(type=Glyph.NOTEHEAD_EMPTY, x=10, y_position=-6), Glyph(type=Glyph.NOTEHEAD_WHOLE, x=10, y_position=-6), ]) notes = conversions.page_to_notesequence(reader.ScoreReader().read_page( musicscore_pb2.Page(system=[musicscore_pb2.StaffSystem( staff=[staff])]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ Note(pitch=librosa.note_to_midi('C4'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('C4'), start_time=1, end_time=3), Note(pitch=librosa.note_to_midi('C4'), start_time=3, end_time=7), ]))
Example #8
Source File: export_kmeans_centroids_test.py From moonlight with Apache License 2.0 | 6 votes |
def testEndToEnd(self): with tempfile.TemporaryDirectory() as tmpdir: with engine.get_included_labels_file() as centroids: export_dir = os.path.join(tmpdir, 'export') export_kmeans_centroids.run(centroids.name, export_dir) # Now load the saved model. omr = engine.OMREngine( glyph_classifier_fn=saved_classifier.SavedConvolutional1DClassifier .glyph_classifier_fn(export_dir)) filename = os.path.join(tf.resource_loader.get_data_files_path(), '../testdata/IMSLP00747-000.png') notes = omr.run(filename, output_notesequence=True) # TODO(ringw): Fix the extra note that is detected before the actual # first eighth note. self.assertEqual(librosa.note_to_midi('C4'), notes.notes[1].pitch) self.assertEqual(librosa.note_to_midi('D4'), notes.notes[2].pitch) self.assertEqual(librosa.note_to_midi('E4'), notes.notes[3].pitch)
Example #9
Source File: eval_utils.py From Music-Transcription-with-Semantic-Segmentation with GNU General Public License v3.0 | 6 votes |
def gen_onsets_info_from_label_v1(label, inst_num=1, t_unit=0.02): intervals = [] pitches = [] onsets = {} lowest_pitch = librosa.note_to_midi("A0") for t, ll in enumerate(label): for pitch, insts in ll.items(): if inst_num not in insts: continue if (pitch not in onsets) or (insts[inst_num][0] > onsets[pitch]): intervals.append([t*t_unit, (t+2)*t_unit]) pitches.append(librosa.midi_to_hz(lowest_pitch+pitch)) onsets[pitch] = insts[inst_num][0] return np.array(intervals), np.array(pitches)
Example #10
Source File: omr_endtoend_test.py From moonlight with Apache License 2.0 | 5 votes |
def testNoteSequence(self): filename = os.path.join(resource_loader.get_data_files_path(), 'testdata/IMSLP00747-000.png') notes = self.engine.run(filename, output_notesequence=True) # TODO(ringw): Fix the extra note that is detected before the actual # first eighth note. self.assertEqual(librosa.note_to_midi('C4'), notes.notes[1].pitch) self.assertEqual(librosa.note_to_midi('D4'), notes.notes[2].pitch) self.assertEqual(librosa.note_to_midi('E4'), notes.notes[3].pitch) self.assertEqual(librosa.note_to_midi('F4'), notes.notes[4].pitch) self.assertEqual(librosa.note_to_midi('D4'), notes.notes[5].pitch) self.assertEqual(librosa.note_to_midi('E4'), notes.notes[6].pitch) self.assertEqual(librosa.note_to_midi('C4'), notes.notes[7].pitch)
Example #11
Source File: test_deformers.py From muda with ISC License | 5 votes |
def pstrip(x): root = re.match(six.text_type('([A-G][b#]*).*'), six.text_type(x)).groups()[0] return librosa.note_to_midi(root)
Example #12
Source File: pitch.py From muda with ISC License | 5 votes |
def transpose(label, n_semitones): """Transpose a chord label by some number of semitones Parameters ---------- label : str A chord string n_semitones : float The number of semitones to move `label` Returns ------- label_transpose : str The transposed chord label """ # Otherwise, split off the note from the modifier match = re.match( six.text_type("(?P<note>[A-G][b#]*)(?P<mod>.*)"), six.text_type(label) ) if not match: return label note = match.group("note") new_note = librosa.midi_to_note( librosa.note_to_midi(note) + n_semitones, octave=False ) return new_note + match.group("mod")
Example #13
Source File: utils.py From Music-Transcription-with-Semantic-Segmentation with GNU General Public License v3.0 | 5 votes |
def to_midi(pred, out_path, velocity=100, threshold=0.4, t_unit=0.02): midi = pretty_midi.PrettyMIDI() piano = pretty_midi.Instrument(program=0) notes = [] pred = np.where(pred>threshold, 1, 0) pred = merge_channels(pred) pitch_offset = librosa.note_to_midi("A0") #print("Transformed shape: ", pred.shape) plt.imshow(pred.transpose(), origin="lower", aspect=20) plt.savefig("{}.png".format(out_path), dpi=250) for i in range(pred.shape[1]): pp = pred[:, i] candy = np.where(pp > 0.5)[0] if len(candy) == 0: # No pitch present continue shift = np.insert(candy, 0, 0)[:-1] diff = candy - shift on_idx = np.where(diff>1)[0] onsets = candy[on_idx] offsets = shift[on_idx[1:]] offsets = np.append(offsets, candy[-1]) for ii in range(len(onsets)): on_t = onsets[ii] * t_unit off_t = offsets[ii] * t_unit note = pretty_midi.Note(velocity=velocity, pitch=i+pitch_offset, start=on_t, end=off_t) notes.append(note) piano.notes = notes midi.instruments.append(piano) if not out_path.endswith(".mid"): out_path += ".mid" midi.write(out_path) return midi
Example #14
Source File: eval_utils.py From Music-Transcription-with-Semantic-Segmentation with GNU General Public License v3.0 | 5 votes |
def gen_frame_info_from_notes(midi_notes, t_unit=0.02): tmp_midi = pretty_midi.PrettyMIDI() inst = pretty_midi.Instrument(program=0) inst.notes += midi_notes tmp_midi.instruments.append(inst) piano_roll = tmp_midi.get_piano_roll(fs=round(1/t_unit)).transpose() low = librosa.note_to_midi("A0") hi = librosa.note_to_midi("C8")+1 piano_roll = piano_roll[:, low:hi] return gen_frame_info(piano_roll, t_unit=t_unit)
Example #15
Source File: eval_utils.py From Music-Transcription-with-Semantic-Segmentation with GNU General Public License v3.0 | 5 votes |
def gen_onsets_info(data, t_unit=0.02): #logging.debug("Data shape: %s", data.shape) pitches = [] intervals = [] lowest_pitch = librosa.note_to_midi("A0") for i in range(data.shape[1]): notes = find_occur(data[:, i], t_unit=t_unit) it = [] for nn in notes: it.append([nn["onset"]*t_unit, nn["offset"]*t_unit]) if len(intervals)==0 and len(it) > 0: intervals = np.array(it) elif len(it) > 0: intervals = np.concatenate((intervals, np.array(it)), axis=0) # hz = CentralFrequency[i] hz = librosa.midi_to_hz(lowest_pitch+i) for i in range(len(it)): pitches.append(hz) if type(intervals) == list: intervals = np.array([]).reshape((0, 2)) pitches = np.array(pitches) return intervals, pitches
Example #16
Source File: clef.py From moonlight with Apache License 2.0 | 5 votes |
def __init__(self): self.center_line_pitch = _ScalePitch(constants.MAJOR_SCALE, librosa.note_to_midi('D3')) self.glyph = musicscore_pb2.Glyph.CLEF_BASS
Example #17
Source File: clef_test.py From moonlight with Apache License 2.0 | 5 votes |
def testBassClef(self): self.assertEqual(clef.BassClef().y_position_to_midi(-10), librosa.note_to_midi('A1')) self.assertEqual(clef.BassClef().y_position_to_midi(-7), librosa.note_to_midi('D2')) self.assertEqual(clef.BassClef().y_position_to_midi(-5), librosa.note_to_midi('F2')) self.assertEqual(clef.BassClef().y_position_to_midi(-1), librosa.note_to_midi('C3')) self.assertEqual(clef.BassClef().y_position_to_midi(0), librosa.note_to_midi('D3')) self.assertEqual(clef.BassClef().y_position_to_midi(6), librosa.note_to_midi('C4')) self.assertEqual(clef.BassClef().y_position_to_midi(8), librosa.note_to_midi('E4'))
Example #18
Source File: clef_test.py From moonlight with Apache License 2.0 | 5 votes |
def testTrebleClef(self): self.assertEqual(clef.TrebleClef().y_position_to_midi(-8), librosa.note_to_midi('A3')) self.assertEqual(clef.TrebleClef().y_position_to_midi(-6), librosa.note_to_midi('C4')) self.assertEqual(clef.TrebleClef().y_position_to_midi(0), librosa.note_to_midi('B4')) self.assertEqual(clef.TrebleClef().y_position_to_midi(1), librosa.note_to_midi('C5')) self.assertEqual(clef.TrebleClef().y_position_to_midi(3), librosa.note_to_midi('E5')) self.assertEqual(clef.TrebleClef().y_position_to_midi(4), librosa.note_to_midi('F5')) self.assertEqual(clef.TrebleClef().y_position_to_midi(14), librosa.note_to_midi('B6'))
Example #19
Source File: reader_test.py From moonlight with Apache License 2.0 | 4 votes |
def testKeySignatures(self): # One staff per system, two systems. staff_1 = musicscore_pb2.Staff(glyph=[ Glyph( type=Glyph.CLEF_TREBLE, x=5, y_position=reader.TREBLE_CLEF_EXPECTED_Y), # D major key signature. Glyph(type=Glyph.SHARP, x=15, y_position=+4), Glyph(type=Glyph.SHARP, x=25, y_position=+1), # Accidental which cannot be interpreted as part of the key # signature. Glyph(type=Glyph.SHARP, x=35, y_position=+2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=45, y_position=+2), # D#5 Glyph(type=Glyph.NOTEHEAD_EMPTY, x=55, y_position=+1), # C#5 Glyph(type=Glyph.NOTEHEAD_FILLED, x=65, y_position=-3), # F#4 # New measure. The key signature should be retained. Glyph(type=Glyph.NOTEHEAD_EMPTY, x=105, y_position=-3), # F#4 Glyph(type=Glyph.NOTEHEAD_FILLED, x=125, y_position=+1), # C#5 # Accidental is not retained. Glyph(type=Glyph.NOTEHEAD_FILLED, x=145, y_position=+2), # D5 ]) staff_2 = musicscore_pb2.Staff(glyph=[ Glyph( type=Glyph.CLEF_TREBLE, x=5, y_position=reader.TREBLE_CLEF_EXPECTED_Y), # No key signature on this line. No accidentals. Glyph(type=Glyph.NOTEHEAD_EMPTY, x=25, y_position=-3), # F4 Glyph(type=Glyph.NOTEHEAD_EMPTY, x=45, y_position=+1), # C5 ]) notes = conversions.page_to_notesequence(reader.ScoreReader().read_page( musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem( staff=[staff_1], bar=[_bar(0), _bar(100), _bar(200)]), musicscore_pb2.StaffSystem(staff=[staff_2]), ]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ # First measure. Note(pitch=librosa.note_to_midi('D#5'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('C#5'), start_time=1, end_time=3), Note(pitch=librosa.note_to_midi('F#4'), start_time=3, end_time=4), # Second measure. Note(pitch=librosa.note_to_midi('F#4'), start_time=4, end_time=6), Note(pitch=librosa.note_to_midi('C#5'), start_time=6, end_time=7), Note(pitch=librosa.note_to_midi('D5'), start_time=7, end_time=8), # Third measure on a new line, with no key signature. Note(pitch=librosa.note_to_midi('F4'), start_time=8, end_time=10), Note(pitch=librosa.note_to_midi('C5'), start_time=10, end_time=12), ]))
Example #20
Source File: reader_test.py From moonlight with Apache License 2.0 | 4 votes |
def testChords(self): stem_1 = musicscore_pb2.LineSegment( start=Point(x=20, y=10), end=Point(x=20, y=70)) stem_2 = musicscore_pb2.LineSegment( start=Point(x=50, y=10), end=Point(x=50, y=70)) staff = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph( type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), # Chord of 2 notes. Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-4, stem=stem_1), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-1, stem=stem_1), # Note not attached to a stem. Glyph(type=Glyph.NOTEHEAD_FILLED, x=30, y_position=3), # Chord of 3 notes. Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=0, stem=stem_2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=60, y_position=2, stem=stem_2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=60, y_position=4, stem=stem_2), ]) notes = conversions.page_to_notesequence(reader.ScoreReader().read_page( musicscore_pb2.Page(system=[musicscore_pb2.StaffSystem( staff=[staff])]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ # First chord. Note(pitch=librosa.note_to_midi('E4'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('A4'), start_time=0, end_time=1), # Note without a stem. Note(pitch=librosa.note_to_midi('E5'), start_time=1, end_time=2), # Second chord. Note(pitch=librosa.note_to_midi('B4'), start_time=2, end_time=3), Note(pitch=librosa.note_to_midi('D5'), start_time=2, end_time=3), Note(pitch=librosa.note_to_midi('F5'), start_time=2, end_time=3), ]))
Example #21
Source File: reader_test.py From moonlight with Apache License 2.0 | 4 votes |
def testTreble_accidentals(self): staff_1 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph( type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-6), Glyph(type=Glyph.FLAT, x=16, y_position=-4), Glyph(type=Glyph.NOTEHEAD_FILLED, x=20, y_position=-4), Glyph(type=Glyph.NOTEHEAD_FILLED, x=30, y_position=-2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=-4), ]) staff_2 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=150), Point(x=100, y=150)], glyph=[ Glyph( type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-6), Glyph(type=Glyph.NOTEHEAD_FILLED, x=20, y_position=-4), Glyph(type=Glyph.NOTEHEAD_FILLED, x=30, y_position=-2), Glyph(type=Glyph.SHARP, x=35, y_position=-2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=-2), Glyph(type=Glyph.NATURAL, x=45, y_position=-2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=50, y_position=-2), ]) notes = conversions.page_to_notesequence(reader.ScoreReader().read_page( musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem(staff=[staff_1]), musicscore_pb2.StaffSystem(staff=[staff_2]) ]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ # First staff. Note(pitch=librosa.note_to_midi('C4'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('Eb4'), start_time=1, end_time=2), Note(pitch=librosa.note_to_midi('G4'), start_time=2, end_time=3), Note(pitch=librosa.note_to_midi('Eb4'), start_time=3, end_time=4), # Second staff. Note(pitch=librosa.note_to_midi('C4'), start_time=4, end_time=5), Note(pitch=librosa.note_to_midi('E4'), start_time=5, end_time=6), Note(pitch=librosa.note_to_midi('G4'), start_time=6, end_time=7), Note(pitch=librosa.note_to_midi('G#4'), start_time=7, end_time=8), Note(pitch=librosa.note_to_midi('G4'), start_time=8, end_time=9), ]))