0

I am trying to extract all notes by instruments from a mid file. The question has been asked : "Music21: Get all Notes per Instrument from a MIDI File", but the answer doesn't work out. Some notes are missing.

In my mid file, the Japanese "Shakuhachi" is used but nothing is printed when the following code is executed:

PATH_FILE = 'my_file.mid'
piece = converter.parse(PATH_FILE)

s2 = instrument.partitionByInstrument(piece)
if s2 is not None:
   for i in s2.recurse().parts:
       if i.partName == "Shakuhachi":
           iNotes = i.notesAndRests.stream()
           for note in iNotes:
               print(note)

6 instruments are used in the midi file but I can only extract notes from the Piano instrument. Is there is something wrong with the code or are there other techniques to extract notes given an instrument ?

![Here is a view from the software Logic Pro X, showing that the Shakuhashi is used : ]1

Julien
  • 91
  • 8
  • Or maybe something is wrong with the MIDI file. Can you use some other program to look at the MIDI file structure and content? – Former contributor Nov 19 '19 at 17:41
  • If your MIDI file does not contain program change messages, it might be that music21 will classify everything as using the default program (0, which is Accoustic Grand Piano according to the General MIDI standard). – G. Sliepen Nov 19 '19 at 17:42
  • @Pedro thank you for your answer, I did use a midi sequencer software (Logic Pro X) where each note of each instrument is distinguishable. – Julien Nov 19 '19 at 17:53
  • @G.Sliepen Thank your for your answer, but the MIDI file contains the program change messages. – Julien Nov 19 '19 at 17:54
  • 1
    Are you sure that's the part name? Midi files not necessarily support names for parts, and sometimes those names are not really used as one would think. If you know the part number, you'll better use that by cycling through all parts. – musicamante Nov 19 '19 at 18:06
  • @musicamante Thank your for your answer, I did try cycling through all parts, but the results are same : there are only notes for the piano track. I used the function defined in this post (https://stackoverflow.com/questions/47556847/music21-manipulating-specific-instrument) deleting the condition on the instrument name. – Julien Nov 19 '19 at 18:16
  • 1
    Then there's something wrong in your file or in how you exported it from Logic. If you can't share at least some part of that file in some way, we cannot help you. – musicamante Nov 19 '19 at 18:52
  • @musicamante I just edited my post adding a picture. The music21.stream.Part of Shakuhachi doesn't contain any notes. – Julien Nov 19 '19 at 23:38
  • I finally found the solution. I thought all MIDI files had a same shape but they are not: I imported and then exported the MIDI file on Logic Pro X. So I need to implement the function that will behave in the same way as the Logic Pro export, do you have any recommendations ? – Julien Nov 20 '19 at 00:22
  • You'll need to be a bit more clear. If I understand correctly, you imported again the file you originally exported from Logic, then you exported it again and now it works? If you could share the file(s?) somewhere, I could try and understand what's the problem (and if the problem is actually on topic here on SO, as if it's an export issue, it certainly isn't). Unfortunately I only used Logic for some weeks more than 6/7 years ago and I don't know/remember how it behaves with MIDI export/import. – musicamante Nov 20 '19 at 21:11
  • I first downloaded a midi file 'songA.mid'. I imported it on Logic Pro to see which instruments were used. One of them was the "Shakuhachi" instrument. The image at https://i.stack.imgur.com/hNMez.png shows that a few notes are played by the Shakuhachi. I wanted to extract these notes using Python. So I executed the code written in the post loading 'songA.mid', but there were no notes assigned to the Shakuhachi. After that, I imported 'songA.mid' file on Logic Pro and then exported it as 'songB.mid'. Then, I executed the code on the 'songB.mid' file and the notes were well displayed. – Julien Nov 21 '19 at 23:11
  • So now I am wondering what is the equivalent of the "export" functionality on Logic Pro, in Python. – Julien Nov 21 '19 at 23:12

0 Answers0