1717
1818class AudioOut :
1919 output_rate : float = 48000.0 # Hz
20- audio_delay : float = 0.100 # seconds
2120 audio_volume : float = 0.25
2221
22+ # Delay configuration | In frames | At 0.5 speed | At 1x speed | At 2x speed |
23+ # ----------------------|------------|--------------|-------------|-------------|
24+ # Emulator push | 1 frame | 33 ms | 17 ms | 8 ms |
25+ # Miniaudio poll | 1.5 frames | 50 ms | 25 ms | 12 ms |
26+ # Expected audio delay | 3 frames | 100 ms | 50 ms | 25 ms |
27+ # Ring buffer size | 6 frames | 200 ms | 100 ms | 50 ms |
28+ audio_delay_in_frames : int = 3
29+
2330 # Controller configuration
2431 kp : float = 0.1
2532 ki : float = 0.001
@@ -36,6 +43,7 @@ def __init__(
3643 self .resampler = resampler
3744 input_rate = console .FPS * console .TICKS_IN_FRAME
3845 self .nominal_sampling_ratio = self .output_rate / input_rate / speed
46+ self .audio_delay = self .audio_delay_in_frames / console .FPS / speed
3947
4048 # Ring buffer state
4149 self .ring_size = int (self .output_rate * self .audio_delay * 2 )
@@ -78,11 +86,6 @@ def start(self) -> miniaudio.PlaybackDevice:
7886 device .start (stream )
7987 return device
8088
81- def update_speed (self , console : Console , speed : float ) -> None :
82- input_rate = console .FPS * console .TICKS_IN_FRAME
83- self .nominal_sampling_ratio = self .output_rate / input_rate / speed
84- self .sampling_ratio = self .nominal_sampling_ratio
85-
8689 def adapt_sample_rate (self ) -> None :
8790 # First perform a short moving average of the last 5 measurements
8891 ring_fill = self .write_counter - self .read_counter
@@ -216,7 +219,7 @@ def __init__(self, disable_audio: bool = False):
216219
217220 def stop (self ) -> None :
218221 if self .device is not None :
219- self .device .stop ()
222+ self .device .close ()
220223 self .device = None
221224 self .audio_out = None
222225
@@ -225,14 +228,11 @@ def update_speed(self, console: Console, speed: float) -> None:
225228 if self .disable_audio :
226229 return
227230
231+ # Stop the current device if any
232+ self .stop ()
233+
228234 # Speed not supported, disable audio
229235 if not (0.499 < speed < 2.001 ):
230- self .stop ()
231- return
232-
233- # Adjust speed if audio is enabled
234- if self .audio_out is not None :
235- self .audio_out .update_speed (console , speed )
236236 return
237237
238238 # Late import
0 commit comments