music21.midi.realtime¶
Objects for realtime playback of Music21 Streams as MIDI.
From an idea of Joe “Codeswell”:
Requires pygame: http://www.pygame.org/download.shtml
StreamPlayer¶
- class music21.midi.realtime.StreamPlayer(streamIn: Stream, reinitMixer: bool = False, mixerFreq: int = 44100, mixerBitSize: int = -16, mixerChannels: int = 2, mixerBuffer: int = 1024)¶
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(127): ... keyDetune.append(random.randint(-30, 30))
>>> b = corpus.parse('bwv66.6') >>> for n in b.flatten().notes: ... n.pitch.microtone = keyDetune[n.pitch.midi] >>> sp = midi.realtime.StreamPlayer(b) >>> sp.play()
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()¶
- StreamPlayer.play(busyFunction=None, busyArgs=None, endFunction=None, endArgs=None, busyWaitMilliseconds=50, *, playForMilliseconds=inf, blocked=True)¶
busyFunction is a function that is called with busyArgs when the music is busy every busyWaitMilliseconds.
endFunction is a function that is called with endArgs when the music finishes playing.
playForMilliseconds is the amount of time in milliseconds after which the playback will be automatically stopped.
If blocked is False, the method will finish before ending the stream, allowing you to completely control whether to stop it. Ignore every other arguments
- StreamPlayer.playStringIOFile(stringIOFile, busyFunction=None, busyArgs=None, endFunction=None, endArgs=None, busyWaitMilliseconds=50, *, playForMilliseconds=inf, blocked=True)¶
busyFunction is a function that is called with busyArgs when the music is busy every busyWaitMilliseconds.
endFunction is a function that is called with endArgs when the music finishes playing.
playForMilliseconds is the amount of time in milliseconds after which the playback will be automatically stopped.
If blocked is False, the method will finish before ending the stream, allowing you to completely control whether to stop it. Ignore every other arguments but for stringIOFile
- StreamPlayer.stop()¶