Added volume control to the Cocoa port

This commit is contained in:
Lior Halphon 2021-05-21 18:12:29 +03:00
parent 75d3470d55
commit 033f025851
5 changed files with 51 additions and 5 deletions

View File

@ -68,6 +68,8 @@ static uint32_t color_to_int(NSColor *color)
@"GBCGBModel": @(GB_MODEL_CGB_E), @"GBCGBModel": @(GB_MODEL_CGB_E),
@"GBSGBModel": @(GB_MODEL_SGB2), @"GBSGBModel": @(GB_MODEL_SGB2),
@"GBRumbleMode": @(GB_RUMBLE_CARTRIDGE_ONLY), @"GBRumbleMode": @(GB_RUMBLE_CARTRIDGE_ONLY),
@"GBVolume": @(1.0),
}]; }];
[JOYController startOnRunLoop:[NSRunLoop currentRunLoop] withOptions:@{ [JOYController startOnRunLoop:[NSRunLoop currentRunLoop] withOptions:@{

View File

@ -369,6 +369,11 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
} }
audioBuffer = realloc(audioBuffer, sizeof(*sample) * audioBufferSize); audioBuffer = realloc(audioBuffer, sizeof(*sample) * audioBufferSize);
} }
double volume = [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBVolume"];
if (volume != 1) {
sample->left *= volume;
sample->right *= volume;
}
audioBuffer[audioBufferPosition++] = *sample; audioBuffer[audioBufferPosition++] = *sample;
} }
if (audioBufferPosition == audioBufferNeeded) { if (audioBufferPosition == audioBufferNeeded) {
@ -994,6 +999,9 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
} }
else { else {
[self.audioClient start]; [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"]; [[NSUserDefaults standardUserDefaults] setBool:!self.audioClient.isPlaying forKey:@"Mute"];
} }

View File

@ -26,5 +26,6 @@
@property (nonatomic, weak) IBOutlet NSPopUpButton *preferredJoypadButton; @property (nonatomic, weak) IBOutlet NSPopUpButton *preferredJoypadButton;
@property (nonatomic, weak) IBOutlet NSPopUpButton *playerListButton; @property (nonatomic, weak) IBOutlet NSPopUpButton *playerListButton;
@property (nonatomic, weak) IBOutlet NSButton *autoUpdatesCheckbox; @property (nonatomic, weak) IBOutlet NSButton *autoUpdatesCheckbox;
@property (weak) IBOutlet NSSlider *volumeSlider;
@end @end

View File

@ -29,6 +29,7 @@
NSPopUpButton *_rumbleModePopupButton; NSPopUpButton *_rumbleModePopupButton;
NSSlider *_temperatureSlider; NSSlider *_temperatureSlider;
NSSlider *_interferenceSlider; NSSlider *_interferenceSlider;
NSSlider *_volumeSlider;
NSButton *_autoUpdatesCheckbox; NSButton *_autoUpdatesCheckbox;
} }
@ -124,6 +125,17 @@
return _interferenceSlider; return _interferenceSlider;
} }
- (void)setVolumeSlider:(NSSlider *)volumeSlider
{
_volumeSlider = volumeSlider;
[volumeSlider setDoubleValue:[[NSUserDefaults standardUserDefaults] doubleForKey:@"GBVolume"] * 256];
}
- (NSSlider *)volumeSlider
{
return _volumeSlider;
}
- (void)setFrameBlendingModePopupButton:(NSPopUpButton *)frameBlendingModePopupButton - (void)setFrameBlendingModePopupButton:(NSPopUpButton *)frameBlendingModePopupButton
{ {
_frameBlendingModePopupButton = frameBlendingModePopupButton; _frameBlendingModePopupButton = frameBlendingModePopupButton;
@ -331,12 +343,17 @@
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBLightTemperatureChanged" object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"GBLightTemperatureChanged" object:nil];
} }
- (IBAction)volumeTemperatureChanged:(id)sender - (IBAction)interferenceVolumeChanged:(id)sender
{ {
[[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0) [[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0)
forKey:@"GBInterferenceVolume"]; forKey:@"GBInterferenceVolume"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBInterferenceVolumeChanged" object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"GBInterferenceVolumeChanged" object:nil];
}
- (IBAction)volumeChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0)
forKey:@"GBVolume"];
} }
- (IBAction)franeBlendingModeChanged:(id)sender - (IBAction)franeBlendingModeChanged:(id)sender

View File

@ -92,6 +92,7 @@
<outlet property="sgbPopupButton" destination="dza-T7-RkX" id="B0o-Nb-pIH"/> <outlet property="sgbPopupButton" destination="dza-T7-RkX" id="B0o-Nb-pIH"/>
<outlet property="skipButton" destination="d2I-jU-sLb" id="udX-8K-0sK"/> <outlet property="skipButton" destination="d2I-jU-sLb" id="udX-8K-0sK"/>
<outlet property="temperatureSlider" destination="NuA-mL-AJZ" id="w11-n7-Bmj"/> <outlet property="temperatureSlider" destination="NuA-mL-AJZ" id="w11-n7-Bmj"/>
<outlet property="volumeSlider" destination="LNs-v1-Eki" id="DU4-An-OrC"/>
</connections> </connections>
<point key="canvasLocation" x="183" y="354"/> <point key="canvasLocation" x="183" y="354"/>
</window> </window>
@ -486,9 +487,26 @@
<point key="canvasLocation" x="-501" y="667.5"/> <point key="canvasLocation" x="-501" y="667.5"/>
</customView> </customView>
<customView id="Zn1-Y5-RbR"> <customView id="Zn1-Y5-RbR">
<rect key="frame" x="0.0" y="0.0" width="292" height="135"/> <rect key="frame" x="0.0" y="0.0" width="292" height="183"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews> <subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Dzd-uB-B6l">
<rect key="frame" x="18" y="146" width="252" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Volume:" id="IbP-7k-xsZ">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<slider verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="LNs-v1-Eki">
<rect key="frame" x="40" y="121" width="230" height="19"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<sliderCell key="cell" continuous="YES" state="on" alignment="left" maxValue="256" doubleValue="256" tickMarkPosition="below" sliderType="linear" id="Xvk-Vj-NHx"/>
<connections>
<action selector="volumeChanged:" target="QvC-M9-y7g" id="4g3-NK-ay4"/>
</connections>
</slider>
<popUpButton verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="T69-6N-dhT"> <popUpButton verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="T69-6N-dhT">
<rect key="frame" x="30" y="65" width="234" height="26"/> <rect key="frame" x="30" y="65" width="234" height="26"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
@ -532,11 +550,11 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<sliderCell key="cell" continuous="YES" state="on" alignment="left" maxValue="256" tickMarkPosition="below" sliderType="linear" id="Rbx-DU-xYf"/> <sliderCell key="cell" continuous="YES" state="on" alignment="left" maxValue="256" tickMarkPosition="below" sliderType="linear" id="Rbx-DU-xYf"/>
<connections> <connections>
<action selector="volumeTemperatureChanged:" target="QvC-M9-y7g" id="HFU-0q-hj1"/> <action selector="interferenceVolumeChanged:" target="QvC-M9-y7g" id="HFU-0q-hj1"/>
</connections> </connections>
</slider> </slider>
</subviews> </subviews>
<point key="canvasLocation" x="-825" y="547.5"/> <point key="canvasLocation" x="-825" y="446.5"/>
</customView> </customView>
<customView id="8TU-6J-NCg"> <customView id="8TU-6J-NCg">
<rect key="frame" x="0.0" y="0.0" width="292" height="467"/> <rect key="frame" x="0.0" y="0.0" width="292" height="467"/>
@ -565,7 +583,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<clipView key="contentView" focusRingType="none" ambiguous="YES" drawsBackground="NO" id="AMs-PO-nid"> <clipView key="contentView" focusRingType="none" ambiguous="YES" drawsBackground="NO" id="AMs-PO-nid">
<rect key="frame" x="1" y="1" width="227" height="209"/> <rect key="frame" x="1" y="1" width="227" height="209"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask"/>
<subviews> <subviews>
<tableView focusRingType="none" verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnReordering="NO" columnResizing="NO" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" typeSelect="NO" id="UDd-IJ-fxX"> <tableView focusRingType="none" verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnReordering="NO" columnResizing="NO" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" typeSelect="NO" id="UDd-IJ-fxX">
<rect key="frame" x="0.0" y="0.0" width="227" height="209"/> <rect key="frame" x="0.0" y="0.0" width="227" height="209"/>