如何将MIDI-CSV转换后的文件转换为两个指定格式的列表?
Solution to Convert MIDI-CSV Data to Target Lists
Step 1: Break Down the CSV Line Structure
First, let's clarify the parts we care about in your MIDI-CSV lines. Each line like this:
2, 960, Note_on_c, 0, 76, 71
The key value here is the 5th element (zero-indexed position 4) — that's the raw MIDI note number we need to adjust for your keyboard input.
Step 2: Define the Conversion Logic
Your requirements are straightforward:
- For every
Note_on_cline, take the MIDI note number, subtract 20, and add the result toNoteList. - For each corresponding
Note_on_centry, add'EthNote'toNoteTypeList.
We can ignore Note_off_c lines entirely since they don't contribute to your target lists.
Step 3: Practical Implementation (Python)
Here's a simple, readable script that processes your CSV data and builds the two lists. I'll use sample lines from your example, but you can easily adapt this to read from your actual CSV file:
# Replace this with code to read from your CSV file (e.g., using open() or csv module) csv_lines = [ "2, 960, Note_on_c, 0, 76, 71", "2, 1200, Note_off_c, 0, 76, 0", "2, 1200, Note_on_c, 0, 75, 38", "2, 1440, Note_off_c, 0, 75, 0" ] NoteList = [] NoteTypeList = [] for line in csv_lines: # Split the line into components and clean up whitespace components = [item.strip() for item in line.split(",")] # Only process Note_on_c events if components[2] == "Note_on_c": # Convert the MIDI note to an integer, adjust for keyboard input adjusted_note = int(components[4]) - 20 NoteList.append(adjusted_note) # Add the fixed note type NoteTypeList.append('EthNote') # Print the final result print("NoteList =", NoteList) print("NoteTypeList =", NoteTypeList)
Step 4: Expected Output
Running this script will produce exactly the output you're looking for:
NoteList = [56, 55] NoteTypeList = ['EthNote', 'EthNote']
Quick Tips for Production
- If you're working with a large CSV file, use Python's built-in
csvmodule instead of splitting lines manually — it handles edge cases (like unexpected whitespace) more reliably. - Double-check for
Note_on_clines with a velocity of 0: some MIDI files use these as implicit note-off events, so you might want to skip those to avoid adding unwanted entries to your lists.
内容的提问来源于stack exchange,提问作者Tyler Bloom
相关产品推荐
相关产品推荐

