Chuỗi bài: Music
python
25 dòng
· Cập nhật 2026-02-03
extract_midi_notes.py
Music/extract_midi_notes.py
import pretty_midi
from collections import defaultdict
# Load the MIDI file
midi_data = pretty_midi.PrettyMIDI('./cs3-1pre.mid')
# Dictionary to group notes by MIDI channel
channel_notes = defaultdict(list)
# Iterate through all instruments (each corresponds to a MIDI track/channel)
for instrument in midi_data.instruments:
channel = instrument.program if not instrument.is_drum else 9 # channel 9 is usually drums
for note in instrument.notes:
channel_notes[channel].append({
'pitch': note.pitch, # MIDI note number (e.g., 60 = Middle C)
'start': note.start, # Note-on time in seconds
'end': note.end, # Note-off time in seconds
'velocity': note.velocity
})
# Display notes by channel
for channel, notes in channel_notes.items():
print(f"\nChannel {channel} ({'Drums' if channel == 9 else 'Program ' + str(channel)}):")
for note in notes:
print(f" Pitch: {note['pitch']}, Start: {note['start']:.3f}s, End: {note['end']:.3f}s, Velocity: {note['velocity']}")
Bài viết liên quan
Music
python
Cập nhật 2026-02-03
essentia.py
essentia.py — python source code from the Music learning materials (Music/essentia.py).
Đọc bài viết →
Music
python
Cập nhật 2026-02-03
ex01.py
ex01.py — python source code from the Music learning materials (Music/ex01.py).
Đọc bài viết →
Music
python
Cập nhật 2026-02-03
getMidiNotes.py
getMidiNotes.py — python source code from the Music learning materials (Music/getMidiNotes.py).
Đọc bài viết →
Music
python
Cập nhật 2026-02-03
markovchain_v2.py
markovchain_v2.py — python source code from the Music learning materials (Music/markovchain_v2.py).
Đọc bài viết →
Music
python
Cập nhật 2026-02-03
musc5211.py
musc5211.py — python source code from the Music learning materials (Music/musc5211.py).
Đọc bài viết →
Music
python
Cập nhật 2026-02-03
music_ai_util.py
music_ai_util.py — python source code from the Music learning materials (Music/music_ai_util.py).
Đọc bài viết →