From 876ca841f9f7373c8af22204415bdf61ced1d2e3 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Thu, 12 Jul 2018 21:14:33 +0300 Subject: Added simple Python 3 example of library usage --- examples/python-alsa/playmidi.py | 106 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100755 examples/python-alsa/playmidi.py (limited to 'examples/python-alsa/playmidi.py') diff --git a/examples/python-alsa/playmidi.py b/examples/python-alsa/playmidi.py new file mode 100755 index 0000000..294ab16 --- /dev/null +++ b/examples/python-alsa/playmidi.py @@ -0,0 +1,106 @@ +#!/usr/bin/python3 + +import sys +import signal +import wave +import getopt +import alsaaudio + +from ctypes import * + +# adl = CDLL("./libADLMIDI.so") +adl = CDLL("/usr/local/lib/libADLMIDI.so") + +#class ADLMIDI_AudioFormat(Structure): +# _fields_ = [("type", c_int), +# ("containerSize", c_uint), +# ("outasampleOffset", c_uint)] + +playing = True + +# Allows to quit by Ctrl+C +def catch_signal(signum, frame): + global playing + playing = False + print("Caught Ctrl+C!") + +def play(device, MIDIPlay, sampleRate): + global playing + + # Opening ALSA output device + device = alsaaudio.PCM(device=device) + + # Set attributes + device.setchannels(2) + device.setrate(sampleRate) + device.setperiodsize(512) + + # format = ADLMIDI_AudioFormat(0, 2, 4); + device.setformat(alsaaudio.PCM_FORMAT_S16_LE) + + # Preparing the audio output buffer + data = create_string_buffer(4096 * 2); + dataLen = 4096 + + print("Streaming...") + + # Streaming the audio data into ALSA output + while playing: + got = adl.adl_play(MIDIPlay, dataLen, cast(data, POINTER(c_short))); + if got <= 0: + break; + device.write(bytearray(data.raw)) + + # Closing the audio output device + device.close() + + print("Quiting...") + + +def usage(): + print('usage: playmidi.py ', file=sys.stderr) + sys.exit(2) + +if __name__ == '__main__': + device = 'default' + sampleRate = 44100 + + opts, args = getopt.getopt(sys.argv[1:], 'd:') + for o, a in opts: + if o == '-d': + device = a + + if not args: + usage() + + midiFile = args[0] + + # Initialize the library + MIDIPlay = adl.adl_init(sampleRate) + + # ============= Optional setup ==================== + # Turn on looping (optionally) + # adl.adl_setLoopEnabled(MIDIPlay, 1) + + # Changing the bank number (optionally) + # if adl.adl_setBank(MIDIPlay, 68) < 0: + # print(c_char_p(adl.adl_errorInfo(MIDIPlay)).value) + # sys.exit(1) + # ============= Optional setup =End================ + + # Open music file... + if adl.adl_openFile(MIDIPlay, midiFile.encode()) < 0: + print(c_char_p(adl.adl_errorInfo(MIDIPlay)).value) + sys.exit(1) + + print("Playing MIDI file %s..." % (midiFile)) + + # Allows to quit by Ctrl+C + signal.signal(signal.SIGINT, catch_signal) + + # Start the audio streaming + play(device, MIDIPlay, sampleRate) + + # Closing the library + adl.adl_close(MIDIPlay) + -- cgit v1.2.3