TFer
TFer

Reputation: 21

Music21 Python Library will only output Piano sounds

I'm using the Music21 library and want to hear each track in the instrument is should be played in from within my jupyter notebook (IPython).

I can successfully output music on different tracks to a midi file and play through timidity, but am unable to hear any tracks other than the Piano (midiProgram = 0) from the IPython player.

I'm currently on Ubuntu 18.04 using music21 v5.7.0. I've also been able to replicate this on my Mac system.

I've tried writing out a stream to a midi file vs. showing it in the notebook and I can only hear the adjusted instruments in the generated midi file.

from music21 import *

core_corp = corpus.corpora.CoreCorpus()
bach_piece = core_corp.search('bwv120.8-a')[0].parse()

bach_piece.show("midi")

for el in bach_piece.recurse():
    if 'Instrument' in el.classes:
        el.activeSite.replace(el, instrument.Trumpet())

bach_piece.show("midi")

I expect to hear all trumpet sounds in the second show() call, but still only hear piano. However, when I open up the generated midi file using timidity, I can hear all of them as trumpets.

Digging through some of the source code, I expect it has something to do with the midiPlayer that is generated by the javascript or in the decoding of the base64 encoded midi file, but I don't have any expertise here.

Any help would be greatly appreciated, my current workaround is just to use

!timidity <path to file>

In my notebook in case anyone else is running this problem.

Upvotes: 2

Views: 1123

Answers (1)

Ege
Ege

Reputation: 178

There is a problem about Jupyter Notebook's MIDI player, not about the Music21. You can hear all the instruments' sound when you write&play the MIDI file with correct MIDI player. For writing MIDI files:

stream1 = converter.parse('d:/musicxml_folder/Rondo_Alla_Turca.xml')
stream1.write('midi', 'd:/musicxml_folder/Rondo_Alla_Turca.mid')

For playing the MIDI file, i use Windows Media Player. When i do this in Pycharm IDE

stream1.show('midi')

Windows Media Player opens up. If you want to hear the sound of MIDI file on another environment you can use a library that can play MIDI files.

Upvotes: 2

Related Questions