Fix temporary audio distortions affecting some Macs and iOS devices

This commit is contained in:
Lior Halphon
2024-05-27 23:02:54 +03:00
parent 5408fe8dd2
commit e5e9cf145a
2 changed files with 13 additions and 11 deletions

View File

@@ -297,12 +297,12 @@ static void debuggerReloadCallback(GB_gameboy_t *gb)
} }
} }
- (void) updatePalette - (void)updatePalette
{ {
GB_set_palette(&_gb, [GBPaletteEditorController userPalette]); GB_set_palette(&_gb, [GBPaletteEditorController userPalette]);
} }
- (void) initCommon - (void)initCommon
{ {
GB_init(&_gb, [self internalModel]); GB_init(&_gb, [self internalModel]);
GB_set_user_data(&_gb, (__bridge void *)(self)); GB_set_user_data(&_gb, (__bridge void *)(self));
@@ -359,7 +359,7 @@ static void debuggerReloadCallback(GB_gameboy_t *gb)
}]; }];
} }
- (void) updateMinSize - (void)updateMinSize
{ {
self.mainWindow.contentMinSize = NSMakeSize(GB_get_screen_width(&_gb), GB_get_screen_height(&_gb)); self.mainWindow.contentMinSize = NSMakeSize(GB_get_screen_width(&_gb), GB_get_screen_height(&_gb));
if (self.mainWindow.contentView.bounds.size.width < GB_get_screen_width(&_gb) || if (self.mainWindow.contentView.bounds.size.width < GB_get_screen_width(&_gb) ||
@@ -369,7 +369,7 @@ static void debuggerReloadCallback(GB_gameboy_t *gb)
self.osdView.usesSGBScale = GB_get_screen_width(&_gb) == 256; self.osdView.usesSGBScale = GB_get_screen_width(&_gb) == 256;
} }
- (void) vblankWithType:(GB_vblank_type_t)type - (void)vblankWithType:(GB_vblank_type_t)type
{ {
if (_gbsVisualizer) { if (_gbsVisualizer) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
@@ -444,7 +444,7 @@ static void debuggerReloadCallback(GB_gameboy_t *gb)
[_view setRumble:amp]; [_view setRumble:amp];
} }
- (void) preRun - (void)preRun
{ {
GB_set_pixels_output(&_gb, self.view.pixels); GB_set_pixels_output(&_gb, self.view.pixels);
GB_set_sample_rate(&_gb, 96000); GB_set_sample_rate(&_gb, 96000);
@@ -453,7 +453,8 @@ static void debuggerReloadCallback(GB_gameboy_t *gb)
if (_audioBufferPosition < nFrames) { if (_audioBufferPosition < nFrames) {
_audioBufferNeeded = nFrames; _audioBufferNeeded = nFrames;
[_audioLock waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.125]]; [_audioLock waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:(double)(_audioBufferNeeded - _audioBufferPosition) / sampleRate]];
_audioBufferNeeded = 0;
} }
if (_stopping || GB_debugger_is_stopped(&_gb)) { if (_stopping || GB_debugger_is_stopped(&_gb)) {
@@ -466,7 +467,7 @@ static void debuggerReloadCallback(GB_gameboy_t *gb)
// Not enough audio // Not enough audio
memset(buffer, 0, (nFrames - _audioBufferPosition) * sizeof(*buffer)); memset(buffer, 0, (nFrames - _audioBufferPosition) * sizeof(*buffer));
memcpy(buffer, _audioBuffer, _audioBufferPosition * sizeof(*buffer)); memcpy(buffer, _audioBuffer, _audioBufferPosition * sizeof(*buffer));
_audioBufferPosition = 0; // Do not reset the audio position to avoid more underflows
} }
else if (_audioBufferPosition < nFrames + 4800) { else if (_audioBufferPosition < nFrames + 4800) {
memcpy(buffer, _audioBuffer, nFrames * sizeof(*buffer)); memcpy(buffer, _audioBuffer, nFrames * sizeof(*buffer));

View File

@@ -492,8 +492,8 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp)
uint8_t *rom = GB_get_direct_access(&_gb, GB_DIRECT_ACCESS_ROM, NULL, NULL); uint8_t *rom = GB_get_direct_access(&_gb, GB_DIRECT_ACCESS_ROM, NULL, NULL);
if ((rom[0x143] & 0x80) && !GB_is_cgb(&_gb)) { if ((rom[0x143] & 0x80) && !GB_is_cgb(&_gb)) {
GB_switch_model_and_reset(&_gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBCGBModel"]); GB_switch_model_and_reset(&_gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBCGBModel"]);
} }
else if ((rom[0x146] == 3) && !GB_is_sgb(&_gb)) { else if ((rom[0x146] == 3) && !GB_is_sgb(&_gb)) {
GB_switch_model_and_reset(&_gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBSGBModel"]); GB_switch_model_and_reset(&_gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBSGBModel"]);
} }
@@ -736,7 +736,8 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp)
if (_audioBufferPosition < nFrames) { if (_audioBufferPosition < nFrames) {
_audioBufferNeeded = nFrames; _audioBufferNeeded = nFrames;
[_audioLock waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.125]]; [_audioLock waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:(double)(_audioBufferNeeded - _audioBufferPosition) / sampleRate]];
_audioBufferNeeded = 0;
} }
if (_stopping) { if (_stopping) {
@@ -749,7 +750,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp)
// Not enough audio // Not enough audio
memset(buffer, 0, (nFrames - _audioBufferPosition) * sizeof(*buffer)); memset(buffer, 0, (nFrames - _audioBufferPosition) * sizeof(*buffer));
memcpy(buffer, _audioBuffer, _audioBufferPosition * sizeof(*buffer)); memcpy(buffer, _audioBuffer, _audioBufferPosition * sizeof(*buffer));
_audioBufferPosition = 0; // Do not reset the audio position to avoid more underflows
} }
else if (_audioBufferPosition < nFrames + 4800) { else if (_audioBufferPosition < nFrames + 4800) {
memcpy(buffer, _audioBuffer, nFrames * sizeof(*buffer)); memcpy(buffer, _audioBuffer, nFrames * sizeof(*buffer));