Objects for realtime playback of Music21 Streams as MIDI.

From an idea of Joe “Codeswell”: how-to-produce-python-controlled-audio-output-from-music-made-with-music21 how-can-i-produce-real-time-audio-output-from-music-made-with-music21

Requires pygame:


class music21.midi.realtime.StreamPlayer(streamIn, **keywords)

Create a player for a stream that plays its midi version in realtime using pygame.

Set up a detuned piano (where each key has a random but consistent detuning from 30 cents flat to sharp) and play a Bach Chorale on it in real time.

>>> import random
>>> keyDetune = []
>>> for i in range(0, 127):
...    keyDetune.append(random.randint(-30, 30))
>>> b = corpus.parse('bwv66.6')
>>> for n in b.flat.notes:
...    n.microtone = keyDetune[n.midi]
>>> sp = midi.realtime.StreamPlayer(b)

The stream is stored (unaltered) in StreamPlayer.streamIn, and can be changed any time the midi file is not playing.

A number of mixer controls can be passed in with keywords:

  • mixerFreq (default 44100 – CD quality)

  • mixerBitSize (default -16 (=unsigned 16bit) –

    really, are you going to do 24bit audio with Python?? :-) )

  • mixerChannels (default 2 = stereo)

  • mixerBuffer (default 1024 = number of samples)

StreamPlayer methods

StreamPlayer.getStringOrBytesIOFile(), busyArgs=None, endFunction=None, endArgs=None, busyWaitMilliseconds=50)
StreamPlayer.playStringIOFile(stringIOFile, busyFunction=None, busyArgs=None, endFunction=None, endArgs=None, busyWaitMilliseconds=50)