0

I've tried to merge 2 midi files into one, such that the second midi file will not be heard when playing the output midi file. I've managed to get a result that plays only the first song, but I wonder if there is a way to hide (encrypt) the second file with the option to recover it out of the output file.

In my way, there is no option for recovery, because all of the second song's notes are in velocity 0, so unless I write the original velocity to an external file or something like that, it's impossible to recover them.

def merge(first, second):
  mid1 = MidiFile(first)
  mid2 = MidiFile(second)
  output = MidiFile(ticks_per_beat=mid1.ticks_per_beat, clip=mid1.clip, charset=mid1.charset, type=mid1.type)

  for i, track in enumerate(mid2.tracks):
      for j, msg in enumerate(mid2.tracks[i]):
          try:
              msg.velocity = 0
          except Exception as e:
              pass
      output.tracks.append(track)

  for i, track in enumerate(mid1.tracks):
      output.tracks.append(track)

  print(output.length)
  output.save(filename="merged.mid")

Thank you!

adsl
  • 123
  • 7

1 Answers1

0

I've managed to hide the velocities with Meta-Messages.

For every note's message I took it's velocity and created a new Meta-Message with a json object "{index_of_note_in_track: old_velocity}", and put those Meta-Messages in the end of the track.

def merge(first, second):
mid1 = MidiFile(first)
mid2 = MidiFile(second)
output = MidiFile(ticks_per_beat=mid1.ticks_per_beat, clip=mid1.clip, charset=mid1.charset, type=mid1.type)

for i, track in enumerate(mid2.tracks):
    new_msgs = []
    for j, msg in enumerate(mid2.tracks[i]):
        if "velocity" in msg.dict().keys():
            new_msgs.append(MetaMessage('text', **{'text': f'{{"{j}":{str(msg.velocity)}}}'}))
            msg.velocity = 0
    for msg in new_msgs:
        track.insert(len(track), msg)
    output.tracks.append(track)

for i, track in enumerate(mid1.tracks):
    output.tracks.append(track)

print(output.length)
output.save(filename="merged.mid")

Can you think of any other way to hide one MIDI file in another?

adsl
  • 123
  • 7