99try : import numpy
1010except ImportError : pass
1111
12- drivers = ["alsa" , "oss" , "jack" , "portaudio" , "sndmgr" , "coreaudio" , "Direct Sound" , "pulseaudio" ]
12+ drivers = ["alsa" , "oss" , "jack" , "pulseaudio" , "portaudio" , "sndmgr" , "coreaudio" , "dsound" , "waveout" ]
13+ platform_drivers = {"linux" : "alsa" , "windows" : "dsound" , "macos" : "coreaudio" }
1314
15+ from .funutils import isNonnegative , isInbounds
1416from .FluidSynth import *
1517
16- def fluid_synth_write_s16_stereo (synth , n : int , n_channel = 2 ): # -> numpy.ndarray
18+ def fluid_synth_write_s16_stereo (synth , n : int , n_channel = 2 ) -> List [ int ]:
1719 """Return generated samples in stereo 16-bit format"""
1820 buf = create_string_buffer (n * n_channel * sizeof (c_int16 ))
1921 fluid_synth_write_s16 (synth , n , buf , 0 , n_channel , buf , 1 , n_channel )
@@ -44,29 +46,29 @@ def setting(self, key, value):
4446 fluid_settings_setstr (sets , bk , value .encode ())
4547
4648 def sfload (self , filename , update_midi_preset = 0 ) -> int :
47- return fluid_synth_sfload (self .synth , filename .encode (), update_midi_preset )
49+ return fluid_synth_sfload (self .synth , filename .encode (), update_midi_preset )
4850 def sfunload (self , sfid , update_midi_preset = 0 ):
49- return fluid_synth_sfunload (self .synth , sfid , update_midi_preset )
51+ fluid_synth_sfunload (self .synth , sfid , update_midi_preset )
5052 def program_select (self , chan , sfid , bank , preset ):
51- return fluid_synth_program_select (self .synth , chan , sfid , bank , preset )
53+ fluid_synth_program_select (self .synth , chan , sfid , bank , preset )
5254
5355 def noteon (self , chan , key , vel = 127 ):
5456 require (chan , isNonnegative , "bad channel" )
5557 require (key , isInbounds (0 , 128 ), "bad key" )
5658 require (vel , isInbounds (0 , 128 ), "bad velocity" )
57- return fluid_synth_noteon (self .synth , chan , key , vel )
59+ fluid_synth_noteon (self .synth , chan , key , vel )
5860 def noteoff (self , chan , key ):
5961 require (chan , isNonnegative , "bad channel" )
6062 require (key , isInbounds (0 , 128 ), "bad key" )
61- return fluid_synth_noteoff (self .synth , chan , key )
63+ fluid_synth_noteoff (self .synth , chan , key )
6264
63- def start (self , driver = platform ({ "linux" : "alsa" , "windows" : "sndmgr" , "macos" : "coreaudio" }) , device = None ):
65+ def start (self , driver = platform_drivers [ platform ()] , device = None ):
6466 """driver could be any str in drivers"""
6567 require (driver , drivers .__contains__ , "unsupported driver" )
6668 self .setting (b"audio.driver" , driver )
6769 if device is not None : self .setting (f"audio.{ driver } .device" , device )
6870 self .audio_driver = new_fluid_audio_driver (self .settings , self .synth )
69- def get_samples (self , n = 1024 ):
71+ def get_samples (self , n = 1024 ) -> List [ int ] :
7072 """Generate audio samples
7173 Returns ndarray containing n audio samples.
7274 If synth is set to stereo output(default) the array will be size 2*n.
@@ -81,24 +83,25 @@ def __init__(self, sample_rate):
8183 @staticmethod
8284 def getFontPresets (path_sfont ) -> List [Tuple [int , int , str ]]:
8385 with open (path_sfont , "rb" ) as fbuf :
84- sf = Sf2File (fbuf )
86+ sf = Sf2File (fbuf ) #< parse sf2
8587 return [(p .bank , p .preset , p .name ) for p in sf .build_presets () if len (p .bags ) != 0 ]
8688
87- def setFont (self , path_sfont , idx_preset = 0 ):
89+ def setFont (self , path_sfont , idx_preset = 0 ):
8890 presets = NoteSynth .getFontPresets (path_sfont )
8991 require (presets , hasIndex (idx_preset ), "preset outbounds" )
9092 preset = presets [idx_preset ]
9193 (bank , patch , _ ) = preset
9294 self .program_select (0 , self .sfload (path_sfont ), bank , patch )
9395
9496 def noteon (self , pitch ): return super ().noteon (0 , pitch )
95- def noteoff (self , pitch = None ):
96- return super ().noteoff (0 , pitch ) if pitch != None else super ().noteoff (0 , self .last_pitch )
97+ def noteoff (self , pitch = None ):
98+ if pitch != None : super ().noteoff (0 , pitch )
99+ else : super ().noteoff (0 , self .last_pitch )
97100 def noteSwitch (self , pitch ):
98101 if self .last_pitch != (- 1 ):
99102 self .noteoff ()
100103 self .noteon (pitch )
101104 self .last_pitch = pitch
102105
103- def sampleNote (self , n_sec ):
106+ def sampleNote (self , n_sec ) -> List [ int ] :
104107 return self .get_samples (self .sample_rate * n_sec )
0 commit comments