]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_coreaudio.c
add SDLMain stuff for OS X; make the Makefiles ready for OS X cross compilation ...
[xonotic/darkplaces.git] / snd_coreaudio.c
index a7f711e7f3915f63b7933375f8882e0dff0c4223..246cff1d5f1a51e18c1569bc3088024954c8442e 100644 (file)
@@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
 #include <limits.h>
+#include <pthread.h>
 
 #include <CoreAudio/AudioHardware.h>
 
@@ -56,7 +57,7 @@ static OSStatus audioDeviceIOProc(AudioDeviceID inDevice,
        outBuffer = (float*)outOutputData->mBuffers[0].mData;
        factor = snd_renderbuffer->format.channels * snd_renderbuffer->format.width;
        frameCount = 0;
-       
+
        // Lock the snd_renderbuffer
        if (SndSys_LockRenderBuffer())
        {
@@ -81,7 +82,7 @@ static OSStatus audioDeviceIOProc(AudioDeviceID inDevice,
                        samples = (const short*)(&snd_renderbuffer->ring[startOffset * factor]);
                        for (sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
                                outBuffer[sampleIndex] = samples[sampleIndex] * scale;
-                       
+
                        outBuffer = &outBuffer[sampleCount];
                        sampleCount = frameCount * snd_renderbuffer->format.channels - sampleCount;
                        samples = (const short*)(&snd_renderbuffer->ring[0]);
@@ -106,9 +107,10 @@ static OSStatus audioDeviceIOProc(AudioDeviceID inDevice,
        if (frameCount < submissionChunk)
        {
                unsigned int missingFrames;
-               
+
                missingFrames = submissionChunk - frameCount;
-               Con_DPrintf("audioDeviceIOProc: %u sample frames missing\n", missingFrames);
+               if (developer.integer >= 1000 && vid_activewindow)
+                       Con_Printf("audioDeviceIOProc: %u sample frames missing\n", missingFrames);
                memset(&outBuffer[frameCount * snd_renderbuffer->format.channels], 0, missingFrames * sizeof(outBuffer[0]));
        }
 
@@ -135,20 +137,18 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
                return true;
 
        Con_Printf("Initializing CoreAudio...\n");
-       
-       // We only accept 16-bit samples for the moment
-       if (requested->width != 2)
+
+       if(requested->width != 2)
        {
-               // Suggest a 16-bit format instead
-               if (suggested != NULL)
+               // we can only do 16bit per sample for now
+               if(suggested != NULL)
                {
-                       memcpy (suggested, requested, sizeof (suggested));
+                       memcpy (suggested, requested, sizeof (*suggested));
                        suggested->width = 2;
                }
-
                return false;
        }
-       
+
        // Get the output device
        propertySize = sizeof(outputDeviceID);
        status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &propertySize, &outputDeviceID);
@@ -198,6 +198,7 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
                Con_Printf("CoreAudio: AudioDeviceGetProperty() returned %d when getting kAudioDevicePropertyStreamFormat\n", status);
                return false;
        }
+
        Con_DPrint ("   Hardware format:\n");
        Con_DPrintf("    %5d mSampleRate\n", (unsigned int)streamDesc.mSampleRate);
        Con_DPrintf("     %c%c%c%c mFormatID\n",
@@ -211,6 +212,18 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
        Con_DPrintf("    %5d mChannelsPerFrame\n", streamDesc.mChannelsPerFrame);
        Con_DPrintf("    %5d mBitsPerChannel\n", streamDesc.mBitsPerChannel);
 
+       // Suggest proper settings if they differ
+       if (requested->channels != streamDesc.mChannelsPerFrame || requested->speed != streamDesc.mSampleRate)
+       {
+               if (suggested != NULL)
+               {
+                       memcpy (suggested, requested, sizeof (*suggested));
+                       suggested->channels = streamDesc.mChannelsPerFrame;
+                       suggested->speed = streamDesc.mSampleRate;
+               }
+               return false;
+       }
+
        if(streamDesc.mFormatID != kAudioFormatLinearPCM)
        {
                Con_Print("CoreAudio: Default audio device doesn't support linear PCM!\n");
@@ -274,7 +287,7 @@ void SndSys_Shutdown(void)
                return;
        }
        s_isRunning = false;
-       
+
        pthread_mutex_destroy(&coreaudio_mutex);
 
        status = AudioDeviceRemoveIOProc(outputDeviceID, audioDeviceIOProc);