Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processing folder? Why not? :) #13

Closed
Midnight93 opened this issue Jun 20, 2021 · 1 comment
Closed

Processing folder? Why not? :) #13

Midnight93 opened this issue Jun 20, 2021 · 1 comment

Comments

@Midnight93
Copy link

Midnight93 commented Jun 20, 2021

Tree project

├── csv
│   └── AllBlues.csv
├── mid
│   └── AllBlues.mid
├── midi
│   └── AllBlues.mid

Implementation

import os
import py_midicsv as pm

#MIDI folder
file_list=os.listdir(r"midi")

#Custom Path
path = ''

#Create folders where to save files
try:
    os.mkdir(path +"csv")
    os.mkdir(path +"mid") 

except OSError:
    print ("Creation of the directory %s failed" % path)
else:
    print ("Successfully created the directory %s" % path)
    
for file_name_song in file_list:
    
    name_file_without_ext = file_name_song.rsplit('.', 1)[0]
    
    # Load the MIDI file and parse it into CSV format
    csv_string = pm.midi_to_csv("midi/"+ name_file_without_ext + ".mid")

    with open("csv/" + name_file_without_ext + ".csv", "w") as f:
        f.writelines(csv_string)

    # Parse the CSV output of the previous command back into a MIDI file
    midi_object = pm.csv_to_midi(csv_string)

    # Save the parsed MIDI file to disk
    with open("mid/" + name_file_without_ext + ".mid", "wb") as output_file:
        midi_writer = pm.FileWriter(output_file)
        midi_writer.write(midi_object)
    print("processed file name:",file_name_song)
@timwedde
Copy link
Owner

You are free to implement whatever process you like using this library, so this is as valid as any other purpose one might use this code for.

When it comes to batch-processing stuff (and potentially splitting MIDI files into their channels and tracks), I have a related project which may be interesting here.

As far as this library goes: It's a library. It's not intended to do any specific task like processing a folder full of files in a specific way. It simply provides some primitives so you can build whatever you like.

Thank you for sharing this either way, maybe somebody else will come across this in the future and find it useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants