From 5b11947f775550f4bc981c71f17c7ff476789c15 Mon Sep 17 00:00:00 2001 From: rogerman Date: Sun, 4 Mar 2012 08:00:29 +0000 Subject: [PATCH] Cocoa OpenEmu Plug-in: - Integrate OpenEmu's audio system with the OpenEmu Plug-in. (Special thanks to jweinberg, clobber, and Psy from the OpenEmu team!) - Make input handling asynchronous through the Distributed Object system. - Rename OpenEmuSoundInterface.mm/.h to OESoundInterface.mm/.h. - Some minor cleanup to global defines. --- .../project.pbxproj | 16 +++--- desmume/src/cocoa/cocoa_globals.h | 4 +- desmume/src/cocoa/openemu/NDSGameCore.mm | 39 +++++---------- .../openemu/OENDSSystemResponderClient.h | 4 +- ...EmuSoundInterface.h => OESoundInterface.h} | 16 +++--- ...SoundInterface.cpp => OESoundInterface.mm} | 49 +++++-------------- 6 files changed, 44 insertions(+), 84 deletions(-) rename desmume/src/cocoa/openemu/{OpenEmuSoundInterface.h => OESoundInterface.h} (86%) rename desmume/src/cocoa/openemu/{OpenEmuSoundInterface.cpp => OESoundInterface.mm} (59%) diff --git a/desmume/src/cocoa/DeSmuME (XCode 4).xcodeproj/project.pbxproj b/desmume/src/cocoa/DeSmuME (XCode 4).xcodeproj/project.pbxproj index 6e7ef1f5d..769a3363a 100644 --- a/desmume/src/cocoa/DeSmuME (XCode 4).xcodeproj/project.pbxproj +++ b/desmume/src/cocoa/DeSmuME (XCode 4).xcodeproj/project.pbxproj @@ -104,6 +104,7 @@ AB0F29C914BE7213009ABC6F /* Icon_Speaker_420x420.png in Resources */ = {isa = PBXBuildFile; fileRef = AB0F29A514BE7213009ABC6F /* Icon_Speaker_420x420.png */; }; AB15CA7214A29EE500B8A6A4 /* cpu_detect_x86_gcc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ABAD3E6613AF1D6D00502E1E /* cpu_detect_x86_gcc.cpp */; }; AB181D0013B66889006CA82D /* datetime.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ABD1FF1F1345ACBF00AF11D1 /* datetime.cpp */; }; + AB1949DB15034F900098793E /* OESoundInterface.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABB3C6401501BB8300E0C22E /* OESoundInterface.mm */; }; AB1B9E631501A78000464647 /* coreaudiosound.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB1B9E5F1501A78000464647 /* coreaudiosound.cpp */; }; AB1B9E641501A78000464647 /* coreaudiosound.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB1B9E5F1501A78000464647 /* coreaudiosound.cpp */; }; AB1B9E651501A78000464647 /* coreaudiosound.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB1B9E5F1501A78000464647 /* coreaudiosound.cpp */; }; @@ -291,9 +292,6 @@ ABB3C6691501BF8A00E0C22E /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ABC570D4134431DA00E7B0B1 /* OpenGL.framework */; }; ABB3C66A1501BF8A00E0C22E /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = AB0A0D1914AACA9600E83E91 /* libz.dylib */; }; ABB3C66C1501C04F00E0C22E /* NDSGameCore.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABB3C63E1501BB8300E0C22E /* NDSGameCore.mm */; }; - ABB3C66D1501C04F00E0C22E /* coreaudiosound.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB1B9E5F1501A78000464647 /* coreaudiosound.cpp */; }; - ABB3C66E1501C04F00E0C22E /* ringbuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB1B9E601501A78000464647 /* ringbuffer.cpp */; }; - ABB3C66F1501C04F00E0C22E /* sndOSX.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ABD104141346652500AF11D1 /* sndOSX.cpp */; }; ABB3C6701501C04F00E0C22E /* videofilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB817A35143EE2DB00A7DFE9 /* videofilter.cpp */; }; ABB3C6711501C04F00E0C22E /* cocoa_core.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABD104121346652500AF11D1 /* cocoa_core.mm */; }; ABB3C6721501C04F00E0C22E /* cocoa_file.mm in Sources */ = {isa = PBXBuildFile; fileRef = AB58F32C1364F44B0074C376 /* cocoa_file.mm */; }; @@ -844,8 +842,8 @@ ABB3C63D1501BB8300E0C22E /* NDSGameCore.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NDSGameCore.h; sourceTree = ""; }; ABB3C63E1501BB8300E0C22E /* NDSGameCore.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NDSGameCore.mm; sourceTree = ""; }; ABB3C63F1501BB8300E0C22E /* OENDSSystemResponderClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OENDSSystemResponderClient.h; sourceTree = ""; }; - ABB3C6401501BB8300E0C22E /* OpenEmuSoundInterface.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = OpenEmuSoundInterface.cpp; sourceTree = ""; }; - ABB3C6411501BB8300E0C22E /* OpenEmuSoundInterface.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OpenEmuSoundInterface.h; sourceTree = ""; }; + ABB3C6401501BB8300E0C22E /* OESoundInterface.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = OESoundInterface.mm; sourceTree = ""; }; + ABB3C6411501BB8300E0C22E /* OESoundInterface.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OESoundInterface.h; sourceTree = ""; }; ABB3C6471501BC6D00E0C22E /* DeSmuME.oecoreplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DeSmuME.oecoreplugin; sourceTree = BUILT_PRODUCTS_DIR; }; ABB7160914E386740027FE88 /* DeSmuME (Debug).app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "DeSmuME (Debug).app"; sourceTree = BUILT_PRODUCTS_DIR; }; ABB97873144E89CC00793FA3 /* Icon_ActionReplay_32x32.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = Icon_ActionReplay_32x32.png; path = Images/Icon_ActionReplay_32x32.png; sourceTree = ""; }; @@ -1294,10 +1292,10 @@ ABB3C63A1501BB8300E0C22E /* openemu */ = { isa = PBXGroup; children = ( - ABB3C6401501BB8300E0C22E /* OpenEmuSoundInterface.cpp */, + ABB3C6401501BB8300E0C22E /* OESoundInterface.mm */, ABB3C63D1501BB8300E0C22E /* NDSGameCore.h */, ABB3C63F1501BB8300E0C22E /* OENDSSystemResponderClient.h */, - ABB3C6411501BB8300E0C22E /* OpenEmuSoundInterface.h */, + ABB3C6411501BB8300E0C22E /* OESoundInterface.h */, ABB3C63E1501BB8300E0C22E /* NDSGameCore.mm */, ABB3C63B1501BB8300E0C22E /* DeSmuME_Prefix_OpenEmu.pch */, ABB3C63C1501BB8300E0C22E /* Info (OpenEmu Plug-in).plist */, @@ -2324,9 +2322,6 @@ files = ( ABE9EEEB1501C78700D3FB19 /* fs-linux.cpp in Sources */, ABB3C66C1501C04F00E0C22E /* NDSGameCore.mm in Sources */, - ABB3C66D1501C04F00E0C22E /* coreaudiosound.cpp in Sources */, - ABB3C66E1501C04F00E0C22E /* ringbuffer.cpp in Sources */, - ABB3C66F1501C04F00E0C22E /* sndOSX.cpp in Sources */, ABB3C6701501C04F00E0C22E /* videofilter.cpp in Sources */, ABB3C6711501C04F00E0C22E /* cocoa_core.mm in Sources */, ABB3C6721501C04F00E0C22E /* cocoa_file.mm in Sources */, @@ -2430,6 +2425,7 @@ ABB3C6D61501C04F00E0C22E /* version.cpp in Sources */, ABB3C6D71501C04F00E0C22E /* wifi.cpp in Sources */, ABE9EEEA1501C6EB00D3FB19 /* cocoa_firmware.mm in Sources */, + AB1949DB15034F900098793E /* OESoundInterface.mm in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/desmume/src/cocoa/cocoa_globals.h b/desmume/src/cocoa/cocoa_globals.h index 2bdf79892..c003b6d59 100644 --- a/desmume/src/cocoa/cocoa_globals.h +++ b/desmume/src/cocoa/cocoa_globals.h @@ -161,11 +161,11 @@ #define FRAME_SKIP_BIAS 0.5 // May be any real number. This value acts as a vector addition to the frame skip. #define MAX_FRAME_SKIP (DS_FRAMES_PER_SECOND / 3.0) -#define SPU_SAMPLE_RATE 44100 // Samples per second +#define SPU_SAMPLE_RATE 44100.0 // Samples per second #define SPU_SAMPLE_RESOLUTION 16 // Bits per sample; must be a multiple of 8 #define SPU_NUMBER_CHANNELS 2 // Number of channels #define SPU_SAMPLE_SIZE ((SPU_SAMPLE_RESOLUTION / 8) * SPU_NUMBER_CHANNELS) // Bytes per sample, multiplied by the number of channels -#define SPU_BUFFER_BYTES ((SPU_SAMPLE_RATE / DS_FRAMES_PER_SECOND) * SPU_SAMPLE_SIZE) +#define SPU_BUFFER_BYTES ((SPU_SAMPLE_RATE / DS_FRAMES_PER_SECOND) * SPU_SAMPLE_SIZE) // Note that this value may be returned as floating point #define CLOCKWISE_DEGREES(x) (360.0 - x) // Converts an angle in degrees from normal-direction to clockwise-direction. diff --git a/desmume/src/cocoa/openemu/NDSGameCore.mm b/desmume/src/cocoa/openemu/NDSGameCore.mm index d603126a8..e684e2044 100644 --- a/desmume/src/cocoa/openemu/NDSGameCore.mm +++ b/desmume/src/cocoa/openemu/NDSGameCore.mm @@ -22,8 +22,7 @@ #import "cocoa_core.h" #import "cocoa_mic.h" #import "cocoa_output.h" -#include "../sndOSX.h" -//#include "OpenEmuSoundInterface.h" +#import "OESoundInterface.h" #include #include "../../path.h" @@ -76,8 +75,9 @@ CommonSettings.spuInterpolationMode = SPUInterpolation_Cosine; // Set up the sound core - NSInteger result = SPU_ChangeSoundCore(SNDCORE_OSX, (int)SPU_BUFFER_BYTES); - //NSInteger result = SPU_ChangeSoundCore(SNDCORE_OPENEMU, (int)SPU_BUFFER_BYTES); + openEmuSoundInterfaceBuffer = [self ringBufferAtIndex:0]; + + NSInteger result = SPU_ChangeSoundCore(SNDCORE_OPENEMU, (int)SPU_BUFFER_BYTES); if(result == -1) { SPU_ChangeSoundCore(SNDCORE_DUMMY, 0); @@ -187,40 +187,24 @@ - (NSTimeInterval)frameInterval { - return DS_FRAMES_PER_SECOND; + return DS_FRAMES_PER_SECOND; } #pragma mark Audio - (NSUInteger)audioBufferCount { - //return 1; - return 0; -} - -- (void)getAudioBuffer:(void *)buffer frameCount:(NSUInteger)frameCount bufferIndex:(NSUInteger)index -{ - // TODO: Better integrate the emulator audio with OpenEmu's audio system. - // - // Right now, we're using the Cocoa port's audio handling code, which works great. - // But it's a good idea to not use it for the purposes of proving good code modularity. - - //if (openEmuSoundInterfaceBuffer != NULL) - //{ - // openEmuSoundInterfaceBuffer->read(buffer, frameCount * SPU_SAMPLE_SIZE); - //} + return 1; } - (NSUInteger)channelCount { - //return SPU_NUMBER_CHANNELS; - return 0; + return SPU_NUMBER_CHANNELS; } - (double)audioSampleRate { - //return SPU_SAMPLE_RATE; - return 0.0; + return SPU_SAMPLE_RATE; } - (NSUInteger)channelCountForBuffer:(NSUInteger)buffer @@ -230,8 +214,7 @@ - (NSUInteger)audioBufferSizeForBuffer:(NSUInteger)buffer { - //return (SPU_BUFFER_BYTES * 2); - return 0; + return (NSUInteger)SPU_BUFFER_BYTES; } - (double)audioSampleRateForBuffer:(NSUInteger)buffer @@ -242,12 +225,12 @@ #pragma mark Input -- (void)didPushNDSButton:(OENDSButton)button forPlayer:(NSUInteger)player +- (oneway void)didPushNDSButton:(OENDSButton)button forPlayer:(NSUInteger)player { input[button] = true; } -- (void)didReleaseNDSButton:(OENDSButton)button forPlayer:(NSUInteger)player +- (oneway void)didReleaseNDSButton:(OENDSButton)button forPlayer:(NSUInteger)player { input[button] = false; } diff --git a/desmume/src/cocoa/openemu/OENDSSystemResponderClient.h b/desmume/src/cocoa/openemu/OENDSSystemResponderClient.h index 4443bdc5b..b64933317 100644 --- a/desmume/src/cocoa/openemu/OENDSSystemResponderClient.h +++ b/desmume/src/cocoa/openemu/OENDSSystemResponderClient.h @@ -41,7 +41,7 @@ typedef enum _OENDSButton @protocol OENDSSystemResponderClient -- (void)didPushNDSButton:(OENDSButton)button forPlayer:(NSUInteger)player; -- (void)didReleaseNDSButton:(OENDSButton)button forPlayer:(NSUInteger)player; +- (oneway void)didPushNDSButton:(OENDSButton)button forPlayer:(NSUInteger)player; +- (oneway void)didReleaseNDSButton:(OENDSButton)button forPlayer:(NSUInteger)player; @end diff --git a/desmume/src/cocoa/openemu/OpenEmuSoundInterface.h b/desmume/src/cocoa/openemu/OESoundInterface.h similarity index 86% rename from desmume/src/cocoa/openemu/OpenEmuSoundInterface.h rename to desmume/src/cocoa/openemu/OESoundInterface.h index 590ac0350..d2ca0fff5 100644 --- a/desmume/src/cocoa/openemu/OpenEmuSoundInterface.h +++ b/desmume/src/cocoa/openemu/OESoundInterface.h @@ -15,10 +15,7 @@ along with the this software. If not, see . */ -#ifndef _OPENEMUSOUNDINTERFACE_ -#define _OPENEMUSOUNDINTERFACE_ - -#include "../ringbuffer.h" +#import #include "../../SPU.h" #define SNDCORE_OPENEMU 58326 @@ -27,7 +24,12 @@ extern SoundInterface_struct SNDOpenEmu; // Ring buffer -extern RingBuffer *openEmuSoundInterfaceBuffer; +extern OERingBuffer *openEmuSoundInterfaceBuffer; + +#ifdef __cplusplus +extern "C" +{ +#endif // OpenEmu functions for the sound interface int SNDOpenEmuInit(int buffer_size); @@ -40,4 +42,6 @@ void SNDOpenEmuUnMuteAudio(); void SNDOpenEmuSetVolume(int volume); void SNDOpenEmuClearBuffer(); -#endif // _OPENEMUSOUNDINTERFACE_ +#ifdef __cplusplus +} +#endif diff --git a/desmume/src/cocoa/openemu/OpenEmuSoundInterface.cpp b/desmume/src/cocoa/openemu/OESoundInterface.mm similarity index 59% rename from desmume/src/cocoa/openemu/OpenEmuSoundInterface.cpp rename to desmume/src/cocoa/openemu/OESoundInterface.mm index d7620ea19..d810a0b04 100644 --- a/desmume/src/cocoa/openemu/OpenEmuSoundInterface.cpp +++ b/desmume/src/cocoa/openemu/OESoundInterface.mm @@ -15,11 +15,11 @@ along with the this software. If not, see . */ -#include "OpenEmuSoundInterface.h" +#include "OESoundInterface.h" #include "cocoa_globals.h" -RingBuffer *openEmuSoundInterfaceBuffer = NULL; +OERingBuffer *openEmuSoundInterfaceBuffer = nil; // Sound interface to the SPU SoundInterface_struct SNDOpenEmu = { @@ -43,72 +43,49 @@ SoundInterface_struct *SNDCoreList[] = { int SNDOpenEmuInit(int buffer_size) { - if (openEmuSoundInterfaceBuffer != NULL) - { - RingBuffer *oldBuffer = openEmuSoundInterfaceBuffer; - openEmuSoundInterfaceBuffer = new RingBuffer(buffer_size / SPU_SAMPLE_SIZE, SPU_SAMPLE_SIZE); - delete oldBuffer; - } - else - { - openEmuSoundInterfaceBuffer = new RingBuffer(buffer_size / SPU_SAMPLE_SIZE, SPU_SAMPLE_SIZE); - } + // Do nothing. The OpenEmu frontend will take care of this. + [openEmuSoundInterfaceBuffer setLength:buffer_size]; return 0; } void SNDOpenEmuDeInit() { - delete openEmuSoundInterfaceBuffer; - openEmuSoundInterfaceBuffer = NULL; + // Do nothing. The OpenEmu frontend will take care of this. } int SNDOpenEmuReset() { - SNDOpenEmuClearBuffer(); - + // Do nothing. The OpenEmu frontend will take care of this. return 0; } void SNDOpenEmuUpdateAudio(s16 *buffer, u32 num_samples) { - if (openEmuSoundInterfaceBuffer != NULL) - { - openEmuSoundInterfaceBuffer->write(buffer, openEmuSoundInterfaceBuffer->getElementSize() * (size_t)num_samples); - } + [openEmuSoundInterfaceBuffer write:buffer maxLength:num_samples * SPU_SAMPLE_SIZE]; } u32 SNDOpenEmuGetAudioSpace() -{ - u32 availableSamples = 0; - - if (openEmuSoundInterfaceBuffer != NULL) - { - availableSamples = (u32)openEmuSoundInterfaceBuffer->getAvailableElements(); - } - - return availableSamples; +{ + return (u32)[openEmuSoundInterfaceBuffer usedBytes] / SPU_SAMPLE_SIZE; } void SNDOpenEmuMuteAudio() { - + // Do nothing. The OpenEmu frontend will take care of this. } void SNDOpenEmuUnMuteAudio() { - + // Do nothing. The OpenEmu frontend will take care of this. } void SNDOpenEmuSetVolume(int volume) { - + // Do nothing. The OpenEmu frontend will take care of this. } void SNDOpenEmuClearBuffer() { - if (openEmuSoundInterfaceBuffer != NULL) - { - openEmuSoundInterfaceBuffer->clear(); - } + // Do nothing. The OpenEmu frontend will take care of this. }