@@ -10,12 +10,10 @@ def __init__(self, pynds) -> None:
1010 self .running = False
1111
1212 def init (self , width : int , height : int ) -> None :
13- pygame .mixer .pre_init (frequency = 44100 , size = - 16 , channels = 2 , buffer = 512 )
1413 pygame .init ()
1514 self .screen = pygame .display .set_mode ((width , height ), pygame .RESIZABLE )
1615 pygame .display .set_caption ("PyNDS" )
1716 self .running = True
18- self ._audio_buffer = np .zeros ((0 , 2 ), dtype = np .int16 ) # continuous buffer
1917
2018 def close (self ) -> None :
2119 pygame .quit ()
@@ -30,9 +28,6 @@ def render(self) -> None:
3028 elif (self .running ):
3129 self .process_frame_nds ()
3230
33- if (self .running ):
34- self .process_audio ()
35-
3631 def handle_events (self ) -> None :
3732 for event in pygame .event .get ():
3833 if event .type == pygame .QUIT :
@@ -142,33 +137,3 @@ def process_frame_nds(self):
142137 surface = pygame .transform .scale (surface , (width , height // 2 ))
143138 self .screen .blit (surface , surface .get_rect (topleft = (0 , height // 2 )))
144139 pygame .display .flip ()
145-
146- def process_audio (self ):
147- samples = self ._pynds .get_audio (699 )
148- if samples is not None and len (samples ) > 0 :
149- # Resample 32768 -> 44100
150- ratio = 44100 / 32768
151- new_len = int (len (samples ) * ratio )
152- left = np .interp (
153- np .linspace (0 , len (samples ), new_len ),
154- np .arange (len (samples )),
155- samples [:, 0 ].astype (np .float32 )
156- ).astype (np .int16 )
157- right = np .interp (
158- np .linspace (0 , len (samples ), new_len ),
159- np .arange (len (samples )),
160- samples [:, 1 ].astype (np .float32 )
161- ).astype (np .int16 )
162- resampled = np .column_stack ((left , right ))
163- self ._audio_buffer = np .concatenate ((self ._audio_buffer , resampled ))
164-
165- # Only play when we have enough buffered samples
166- CHUNK = 2048
167- while len (self ._audio_buffer ) >= CHUNK :
168- chunk = np .ascontiguousarray (self ._audio_buffer [:CHUNK ])
169- self ._audio_buffer = self ._audio_buffer [CHUNK :]
170- # Find a free channel or wait
171- ch = pygame .mixer .find_channel (False )
172- if ch is not None :
173- sound = pygame .sndarray .make_sound (chunk )
174- ch .play (sound )
0 commit comments