Doesn't play midi files

According to this: http://www.psychopy.org/builder/components/sound.html [’ a filename, which can be a relative or absolute path (mid, wav, and ogg are supported)’] I should in theory be able to use midi files (.mid) in PsychoPy. However when I try to assign a midi file as follows:

Midi_file = sound.Sound('/Users/sam/Documents/file.mid' , secs=-1)

I receive this response:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/sound/backend_pyo.py", line 356, in __init__
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/sound/_base.py", line 192, in setSound
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/sound/backend_pyo.py", line 436, in _setSndFromFile
TypeError: Could not open sound file `/Users/sam/Documents/file.mid` using pyo; not found or format not supported.

Any thoughts?

Looking at previous forum posts about midi it is unclear as to whether PsychoPy can handle midi (.mid files), but then http://www.psychopy.org/builder/components/sound.html [’ a filename, which can be a relative or absolute path (mid, wav, and ogg are supported)’] suggests that it can…

No worries if it can’t, but soon I’ll be sending an experiment out to people online that includes a lot of .wav files, and therefore takes up a lot of space/ is slow to download. Obviously it would be less space-draining to send them midi files instead. As such it would be useful to know?

I don’t know the answer to your question: the reference to midi files occurs only in the Builder documentation files and not in the source code. However, PsychoPy can use multiple backends for sound. The error you are seeing comes back from the pyo library, not from somewhere in the PsychoPy library itself. So you could try switching to each of the other sound backends to see if they can handle a .midi file.

The exception is ambiguous however, so you should be absolutely sure that the path to the sound file is actually correct before you go much further.

Thanks Michael. The path to the sound file is definitely correct, so with that in mind I should experiment with the other sound backends… which leads me to the question, how would I do this and what other backends are available? (Sorry if that seems ignorant…)

Thanks - Sam

In the PsychoPy preferences, you’ll see a list of possible audio libraries that looks like this:

['pyo', 'pygame', 'sounddevice']

Change the order to promote another library ahead of pyo.

One added difficulty with midi is that they are not actual sound files. Midi is more of a communication protocol (and interface). The files may be small, but if you send your experiment out online, keep in mind that playback/sound may vary between users, depending on which output device they use. Therefore, it’s worth considering sending the actual sound files instead.

Having said that, Pygame should play midi. See here and here, plus example. There are various other scripts on GitHub (such as this one), but I can’t vouch for them.

2 Likes

Pyo doesn’t play midi files, only soundfiles. Pygame is probably a better backend for this.