I want to use python infmidi to merge two midi files (one gets played, then the other one)
- User
I want to use python infmidi to merge two midi files (one gets played, then the other one)
- Assistant
- User
Traceback (most recent call last): File "c:\Users\alous\Desktop\ProgProjekte\MidiEditor\main.py", line 1, in import infmidi File "C:\Python312\Lib\site-packages\infmidi_init_.py", line 2, in from .devices import * File "C:\Python312\Lib\site-packages\infmidi\devices_init_.py", line 3, in from .viewer import * File "C:\Python312\Lib\site-packages\infmidi\devices\viewer.py", line 7, in import websockets ModuleNotFoundError: No module named 'websockets'
[Done] exited with code=1 in 0.377 seconds
- Assistant
It looks like
infmidirelies on thewebsocketslibrary, which isn't installed in your environment. To resolve this issue, you'll need to install thewebsocketslibrary.You can install
websocketsusing pip:pip install websocketsAfter installing
websockets, try running your script again. Here's a reminder of what your script should look like:import infmidi def merge_midi_files(file1, file2, output_file): # Load the MIDI files midi1 = infmidi.read(file1) midi2 = infmidi.read(file2) # Create a new MIDI file for the output merged_midi = infmidi.MidiFile() # Append tracks from the first MIDI file for track in midi1.tracks: merged_track = infmidi.MidiTrack() merged_track.extend(track) merged_midi.tracks.append(merged_track) # Find the end time of the first MIDI file end_time = sum(evt['delta'] for track in midi1.tracks for evt in track) # Append tracks from the second MIDI file with adjusted timing for track in midi2.tracks: merged_track = infmidi.MidiTrack() for event in track: adjusted_event = event.copy() # Adjust delta time by adding the end time of the first MIDI adjusted_event['delta'] = event['delta'] + end_time merged_track.append(adjusted_event) merged_midi.tracks.append(merged_track) # Write the merged MIDI file to the output with open(output_file, 'wb') as f: infmidi.write(merged_midi, f) # Specify files file1 = 'path/to/your/first_midi_file.mid' file2 = 'path/to/your/second_midi_file.mid' output_file = 'path/to/output/merged_midi_file.mid' # Merge the files merge_midi_files(file1, file2, output_file) - User
Traceback (most recent call last): File "c:\Users\alous\Desktop\ProgProjekte\MidiEditor\main.py", line 40, in merge_midi_files(file1, file2, output_file) File "c:\Users\alous\Desktop\ProgProjekte\MidiEditor\main.py", line 5, in merge_midi_files midi1 = infmidi.read(file1) ^^^^^^^^^^^^ AttributeError: module 'infmidi' has no attribute 'read'
- Assistant
It appears there might be some confusion or misalignment with the API. Instead of , another widely used library for working with MIDI files in Python is . Let's switch to for handling the merging of MIDI files.