From 033f025851622bd15c5ebff92df95963fc942ca2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 May 2021 18:12:29 +0300 Subject: [PATCH] Added volume control to the Cocoa port --- Cocoa/AppDelegate.m | 2 ++ Cocoa/Document.m | 8 ++++++++ Cocoa/GBPreferencesWindow.h | 1 + Cocoa/GBPreferencesWindow.m | 19 ++++++++++++++++++- Cocoa/Preferences.xib | 26 ++++++++++++++++++++++---- 5 files changed, 51 insertions(+), 5 deletions(-) diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index 01b1527..627a7b6 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -68,6 +68,8 @@ static uint32_t color_to_int(NSColor *color) @"GBCGBModel": @(GB_MODEL_CGB_E), @"GBSGBModel": @(GB_MODEL_SGB2), @"GBRumbleMode": @(GB_RUMBLE_CARTRIDGE_ONLY), + + @"GBVolume": @(1.0), }]; [JOYController startOnRunLoop:[NSRunLoop currentRunLoop] withOptions:@{ diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 297547c..010cff7 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -369,6 +369,11 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on) } audioBuffer = realloc(audioBuffer, sizeof(*sample) * audioBufferSize); } + double volume = [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBVolume"]; + if (volume != 1) { + sample->left *= volume; + sample->right *= volume; + } audioBuffer[audioBufferPosition++] = *sample; } if (audioBufferPosition == audioBufferNeeded) { @@ -994,6 +999,9 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency) } else { [self.audioClient start]; + if ([[NSUserDefaults standardUserDefaults] doubleForKey:@"GBVolume"] == 0) { + [GBWarningPopover popoverWithContents:@"Warning: Volume is set to to zero in the preferences panel" onWindow:self.mainWindow]; + } } [[NSUserDefaults standardUserDefaults] setBool:!self.audioClient.isPlaying forKey:@"Mute"]; } diff --git a/Cocoa/GBPreferencesWindow.h b/Cocoa/GBPreferencesWindow.h index 0c7b422..16e36e2 100644 --- a/Cocoa/GBPreferencesWindow.h +++ b/Cocoa/GBPreferencesWindow.h @@ -26,5 +26,6 @@ @property (nonatomic, weak) IBOutlet NSPopUpButton *preferredJoypadButton; @property (nonatomic, weak) IBOutlet NSPopUpButton *playerListButton; @property (nonatomic, weak) IBOutlet NSButton *autoUpdatesCheckbox; +@property (weak) IBOutlet NSSlider *volumeSlider; @end diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index 217c7e1..4398ba3 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -29,6 +29,7 @@ NSPopUpButton *_rumbleModePopupButton; NSSlider *_temperatureSlider; NSSlider *_interferenceSlider; + NSSlider *_volumeSlider; NSButton *_autoUpdatesCheckbox; } @@ -124,6 +125,17 @@ return _interferenceSlider; } +- (void)setVolumeSlider:(NSSlider *)volumeSlider +{ + _volumeSlider = volumeSlider; + [volumeSlider setDoubleValue:[[NSUserDefaults standardUserDefaults] doubleForKey:@"GBVolume"] * 256]; +} + +- (NSSlider *)volumeSlider +{ + return _volumeSlider; +} + - (void)setFrameBlendingModePopupButton:(NSPopUpButton *)frameBlendingModePopupButton { _frameBlendingModePopupButton = frameBlendingModePopupButton; @@ -331,12 +343,17 @@ [[NSNotificationCenter defaultCenter] postNotificationName:@"GBLightTemperatureChanged" object:nil]; } -- (IBAction)volumeTemperatureChanged:(id)sender +- (IBAction)interferenceVolumeChanged:(id)sender { [[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0) forKey:@"GBInterferenceVolume"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"GBInterferenceVolumeChanged" object:nil]; +} +- (IBAction)volumeChanged:(id)sender +{ + [[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0) + forKey:@"GBVolume"]; } - (IBAction)franeBlendingModeChanged:(id)sender diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index b1d67f7..ad510c8 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -92,6 +92,7 @@ + @@ -486,9 +487,26 @@ - + + + + + + + + + + + + + + + + + + @@ -532,11 +550,11 @@ - + - + @@ -565,7 +583,7 @@ - +