1+ import mido
2+ import os
3+ from ctypes import *
4+ import signal
5+ import sys
6+
7+ MonikaDLL = cdll .LoadLibrary (os .getcwd () + '\\ MonikaDLL.dll' )
8+ MonikaDLL .get_my_driver_handle .restype = c_uint8
9+ status = MonikaDLL .get_my_driver_handle ()
10+ if status != 0 :
11+ print ("Error in loading driver" )
12+ exit ()
13+
14+ # regist ctrl-c handler
15+ def signal_handler (sig , frame ):
16+ print ('You pressed Ctrl+C!' )
17+ MonikaDLL .MonikaBeepStop ()
18+ sys .exit (0 )
19+ signal .signal (signal .SIGINT , signal_handler )
20+
21+ # Function to convert MIDI note number to frequency (Hz)
22+ def midi_note_to_freq (note ):
23+ A4 = 440 # Frequency of A4
24+ return A4 * (2 ** ((note - 69 ) / 12 ))
25+
26+ # Function to play a beep sound for a given MIDI note
27+ def play_beep_for_midi (note , duration_ms ):
28+ frequency = int (midi_note_to_freq (note )) # Convert MIDI note to frequency
29+ print (f'Playing note { note } at { frequency } Hz for { duration_ms } ms' )
30+ MonikaDLL .MonikaBeepStart (frequency ) # Start beep
31+
32+ # Read MIDI file
33+ def play_midi_beep (file_path ):
34+ mid = mido .MidiFile (file_path )
35+ tempo = 500000 # Default tempo (microseconds per beat)
36+
37+ for msg in mid .play ():
38+ if msg .type == 'note_on' and msg .velocity > 0 : # Play note
39+ duration_ms = int (tempo / 1000 ) # Convert tempo to milliseconds
40+ play_beep_for_midi (msg .note , duration_ms )
41+ elif msg .type == 'set_tempo' : # Update tempo
42+ tempo = msg .tempo
43+
44+ MonikaDLL .MonikaBeepStop () # Stop beep
45+
46+ midi_file = 'ddlc_main.mid'
47+ play_midi_beep (midi_file )
0 commit comments