src/cdrom/macosx/AudioFilePlayer.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Mar 2006 06:33:21 +0000
changeset 1487 dc6b59e925a2
parent 1402 d910939febfa
child 1659 14717b52abc0
permissions -rw-r--r--
Cleaning up warnings on MacOS X
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 
    22     This file based on Apple sample code. We haven't changed the file name, 
    23     so if you want to see the original search for it on apple.com/developer
    24 */
    25 #include "SDL_config.h"
    26 
    27 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    28     AudioFilePlayer.cpp
    29 */
    30 #include "AudioFilePlayer.h"
    31 
    32 /*
    33 void ThrowResult (OSStatus result, const char* str)
    34 {
    35     SDL_SetError ("Error: %s %d", str, result);
    36     throw result;
    37 }
    38 */
    39 
    40 #if DEBUG
    41 static void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
    42 {
    43     if (!inDesc) {
    44         printf ("Can't print a NULL desc!\n");
    45         return;
    46     }
    47     
    48     printf ("- - - - - - - - - - - - - - - - - - - -\n");
    49     printf ("  Sample Rate:%f\n", inDesc->mSampleRate);
    50     printf ("  Format ID:%s\n", (char*)&inDesc->mFormatID);
    51     printf ("  Format Flags:%lX\n", inDesc->mFormatFlags);
    52     printf ("  Bytes per Packet:%ld\n", inDesc->mBytesPerPacket);
    53     printf ("  Frames per Packet:%ld\n", inDesc->mFramesPerPacket);
    54     printf ("  Bytes per Frame:%ld\n", inDesc->mBytesPerFrame);
    55     printf ("  Channels per Frame:%ld\n", inDesc->mChannelsPerFrame);
    56     printf ("  Bits per Channel:%ld\n", inDesc->mBitsPerChannel);
    57     printf ("- - - - - - - - - - - - - - - - - - - -\n");
    58 }
    59 #endif
    60 
    61 
    62 static int AudioFilePlayer_SetDestination (AudioFilePlayer *afp, AudioUnit  *inDestUnit)
    63 {
    64     /*if (afp->mConnected) throw static_cast<OSStatus>(-1);*/ /* can't set dest if already engaged */
    65     if (afp->mConnected)
    66         return 0 ;
    67 
    68     SDL_memcpy(&afp->mPlayUnit, inDestUnit, sizeof (afp->mPlayUnit));
    69 
    70     OSStatus result = noErr;
    71     
    72 
    73         /* we can "down" cast a component instance to a component */
    74     ComponentDescription desc;
    75     result = GetComponentInfo ((Component)*inDestUnit, &desc, 0, 0, 0);
    76     if (result) return 0; /*THROW_RESULT("GetComponentInfo")*/
    77         
    78         /* we're going to use this to know which convert routine to call
    79            a v1 audio unit will have a type of 'aunt'
    80            a v2 audio unit will have one of several different types. */
    81     if (desc.componentType != kAudioUnitComponentType) {
    82         result = badComponentInstance;
    83         /*THROW_RESULT("BAD COMPONENT")*/
    84         if (result) return 0;
    85     }
    86 
    87     /* Set the input format of the audio unit. */
    88     result = AudioUnitSetProperty (*inDestUnit,
    89                                kAudioUnitProperty_StreamFormat,
    90                                kAudioUnitScope_Input,
    91                                0,
    92                                &afp->mFileDescription,
    93                                sizeof (afp->mFileDescription));
    94         /*THROW_RESULT("AudioUnitSetProperty")*/
    95     if (result) return 0;
    96     return 1;
    97 }
    98 
    99 static void AudioFilePlayer_SetNotifier(AudioFilePlayer *afp, AudioFilePlayNotifier inNotifier, void *inRefCon)
   100 {
   101     afp->mNotifier = inNotifier;
   102     afp->mRefCon = inRefCon;
   103 }
   104 
   105 static int AudioFilePlayer_IsConnected(AudioFilePlayer *afp)
   106 {
   107     return afp->mConnected;
   108 }
   109 
   110 static AudioUnit AudioFilePlayer_GetDestUnit(AudioFilePlayer *afp)
   111 {
   112    return afp->mPlayUnit;
   113 }
   114 
   115 static void AudioFilePlayer_Print(AudioFilePlayer *afp)
   116 {
   117 #if DEBUG    
   118     printf ("Is Connected:%s\n", (IsConnected() ? "true" : "false"));
   119     printf ("- - - - - - - - - - - - - - \n");
   120 #endif
   121 }
   122 
   123 static void    AudioFilePlayer_SetStartFrame (AudioFilePlayer *afp, int frame)
   124 {
   125     SInt64 position = frame * 2352;
   126 
   127     afp->mStartFrame = frame;
   128     afp->mAudioFileManager->SetPosition (afp->mAudioFileManager, position);
   129 }
   130 
   131     
   132 static int    AudioFilePlayer_GetCurrentFrame (AudioFilePlayer *afp)
   133 {
   134     return afp->mStartFrame + (afp->mAudioFileManager->GetByteCounter(afp->mAudioFileManager) / 2352);
   135 }
   136     
   137 static void    AudioFilePlayer_SetStopFrame (AudioFilePlayer *afp, int frame)
   138 {
   139     SInt64 position  = frame * 2352;
   140     
   141     afp->mAudioFileManager->SetEndOfFile (afp->mAudioFileManager, position);
   142 }
   143     
   144 void delete_AudioFilePlayer(AudioFilePlayer *afp)
   145 {
   146     if (afp != NULL)
   147     {
   148         afp->Disconnect(afp);
   149         
   150         if (afp->mAudioFileManager) {
   151             delete_AudioFileManager(afp->mAudioFileManager);
   152             afp->mAudioFileManager = 0;
   153         }
   154     
   155         if (afp->mForkRefNum) {
   156             FSClose (afp->mForkRefNum);
   157             afp->mForkRefNum = 0;
   158         }
   159         SDL_free(afp);
   160     }
   161 }
   162 
   163 static int    AudioFilePlayer_Connect(AudioFilePlayer *afp)
   164 {
   165 #if DEBUG
   166     printf ("Connect:%x, engaged=%d\n", (int)afp->mPlayUnit, (afp->mConnected ? 1 : 0));
   167 #endif
   168     if (!afp->mConnected)
   169     {           
   170         if (!afp->mAudioFileManager->DoConnect(afp->mAudioFileManager))
   171             return 0;
   172 
   173         /* set the render callback for the file data to be supplied to the sound converter AU */
   174         afp->mInputCallback.inputProc = afp->mAudioFileManager->FileInputProc;
   175         afp->mInputCallback.inputProcRefCon = afp->mAudioFileManager;
   176 
   177         OSStatus result = AudioUnitSetProperty (afp->mPlayUnit, 
   178                             kAudioUnitProperty_SetInputCallback, 
   179                             kAudioUnitScope_Input, 
   180                             0,
   181                             &afp->mInputCallback, 
   182                             sizeof(afp->mInputCallback));
   183         if (result) return 0;  /*THROW_RESULT("AudioUnitSetProperty")*/
   184         afp->mConnected = 1;
   185     }
   186 
   187     return 1;
   188 }
   189 
   190 /* warning noted, now please go away ;-) */
   191 /* #warning This should redirect the calling of notification code to some other thread */
   192 static void    AudioFilePlayer_DoNotification (AudioFilePlayer *afp, OSStatus inStatus)
   193 {
   194     if (afp->mNotifier) {
   195         (*afp->mNotifier) (afp->mRefCon, inStatus);
   196     } else {
   197         SDL_SetError ("Notification posted with no notifier in place");
   198         
   199         if (inStatus == kAudioFilePlay_FileIsFinished)
   200             afp->Disconnect(afp);
   201         else if (inStatus != kAudioFilePlayErr_FilePlayUnderrun)
   202             afp->Disconnect(afp);
   203     }
   204 }
   205 
   206 static void    AudioFilePlayer_Disconnect (AudioFilePlayer *afp)
   207 {
   208 #if DEBUG
   209     printf ("Disconnect:%x,%ld, engaged=%d\n", (int)afp->mPlayUnit, 0, (afp->mConnected ? 1 : 0));
   210 #endif
   211     if (afp->mConnected)
   212     {
   213         afp->mConnected = 0;
   214             
   215         afp->mInputCallback.inputProc = 0;
   216         afp->mInputCallback.inputProcRefCon = 0;
   217         OSStatus result = AudioUnitSetProperty (afp->mPlayUnit, 
   218                                         kAudioUnitProperty_SetInputCallback, 
   219                                         kAudioUnitScope_Input, 
   220                                         0,
   221                                         &afp->mInputCallback, 
   222                                         sizeof(afp->mInputCallback));
   223         if (result) 
   224             SDL_SetError ("AudioUnitSetProperty:RemoveInputCallback:%ld", result);
   225 
   226         afp->mAudioFileManager->Disconnect(afp->mAudioFileManager);
   227     }
   228 }
   229 
   230 typedef struct {
   231     UInt32 offset;
   232     UInt32 blockSize;
   233 } SSNDData;
   234 
   235 static int    AudioFilePlayer_OpenFile (AudioFilePlayer *afp, const FSRef *inRef, SInt64 *outFileDataSize)
   236 {
   237     ContainerChunk chunkHeader;
   238     ChunkHeader chunk;
   239     SSNDData ssndData;
   240 
   241     OSErr result;
   242     HFSUniStr255 dfName;
   243     ByteCount actual;
   244     SInt64 offset;
   245 
   246     /* Open the data fork of the input file */
   247     result = FSGetDataForkName(&dfName);
   248        if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSGetDataForkName")*/
   249 
   250     result = FSOpenFork(inRef, dfName.length, dfName.unicode, fsRdPerm, &afp->mForkRefNum);
   251        if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSOpenFork")*/
   252  
   253     /* Read the file header, and check if it's indeed an AIFC file */
   254     result = FSReadFork(afp->mForkRefNum, fsAtMark, 0, sizeof(chunkHeader), &chunkHeader, &actual);
   255        if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/
   256 
   257     if (chunkHeader.ckID != 'FORM') {
   258         result = -1;
   259         if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): chunk id is not 'FORM'");*/
   260     }
   261 
   262     if (chunkHeader.formType != 'AIFC') {
   263         result = -1;
   264         if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): file format is not 'AIFC'");*/
   265     }
   266 
   267     /* Search for the SSND chunk. We ignore all compression etc. information
   268        in other chunks. Of course that is kind of evil, but for now we are lazy
   269        and rely on the cdfs to always give us the same fixed format.
   270        TODO: Parse the COMM chunk we currently skip to fill in mFileDescription.
   271     */
   272     offset = 0;
   273     do {
   274         result = FSReadFork(afp->mForkRefNum, fsFromMark, offset, sizeof(chunk), &chunk, &actual);
   275            if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/
   276             
   277         /* Skip the chunk data */
   278         offset = chunk.ckSize;
   279     } while (chunk.ckID != 'SSND');
   280 
   281     /* Read the header of the SSND chunk. After this, we are positioned right
   282        at the start of the audio data. */
   283     result = FSReadFork(afp->mForkRefNum, fsAtMark, 0, sizeof(ssndData), &ssndData, &actual);
   284        if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/
   285 
   286     result = FSSetForkPosition(afp->mForkRefNum, fsFromMark, ssndData.offset);
   287        if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSSetForkPosition")*/
   288 
   289     /* Data size */
   290     *outFileDataSize = chunk.ckSize - ssndData.offset - 8;
   291 
   292     /* File format */
   293     afp->mFileDescription.mSampleRate = 44100;
   294     afp->mFileDescription.mFormatID = kAudioFormatLinearPCM;
   295     afp->mFileDescription.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;
   296     afp->mFileDescription.mBytesPerPacket = 4;
   297     afp->mFileDescription.mFramesPerPacket = 1;
   298     afp->mFileDescription.mBytesPerFrame = 4;
   299     afp->mFileDescription.mChannelsPerFrame = 2;
   300     afp->mFileDescription.mBitsPerChannel = 16;
   301 
   302     return 1;
   303 }
   304 
   305 AudioFilePlayer *new_AudioFilePlayer (const FSRef *inFileRef)
   306 {
   307     SInt64 fileDataSize  = 0;
   308 
   309     AudioFilePlayer *afp = (AudioFilePlayer *) SDL_malloc(sizeof (AudioFilePlayer));
   310     if (afp == NULL)
   311         return NULL;
   312     SDL_memset(afp, '\0', sizeof (*afp));
   313 
   314     #define SET_AUDIOFILEPLAYER_METHOD(m) afp->m = AudioFilePlayer_##m
   315     SET_AUDIOFILEPLAYER_METHOD(SetDestination);
   316     SET_AUDIOFILEPLAYER_METHOD(SetNotifier);
   317     SET_AUDIOFILEPLAYER_METHOD(SetStartFrame);
   318     SET_AUDIOFILEPLAYER_METHOD(GetCurrentFrame);
   319     SET_AUDIOFILEPLAYER_METHOD(SetStopFrame);
   320     SET_AUDIOFILEPLAYER_METHOD(Connect);
   321     SET_AUDIOFILEPLAYER_METHOD(Disconnect);
   322     SET_AUDIOFILEPLAYER_METHOD(DoNotification);
   323     SET_AUDIOFILEPLAYER_METHOD(IsConnected);
   324     SET_AUDIOFILEPLAYER_METHOD(GetDestUnit);
   325     SET_AUDIOFILEPLAYER_METHOD(Print);
   326     SET_AUDIOFILEPLAYER_METHOD(OpenFile);
   327     #undef SET_AUDIOFILEPLAYER_METHOD
   328 
   329     if (!afp->OpenFile (afp, inFileRef, &fileDataSize))
   330     {
   331         SDL_free(afp);
   332         return NULL;
   333     }
   334         
   335     /* we want about 4 seconds worth of data for the buffer */
   336     int bytesPerSecond = (UInt32) (4 * afp->mFileDescription.mSampleRate * afp->mFileDescription.mBytesPerFrame);
   337     
   338 #if DEBUG
   339     printf("File format:\n");
   340     PrintStreamDesc (&afp->mFileDescription);
   341 #endif
   342     
   343     afp->mAudioFileManager = new_AudioFileManager(afp, afp->mForkRefNum,
   344                                                   fileDataSize,
   345                                                   bytesPerSecond);
   346     if (afp->mAudioFileManager == NULL)
   347     {
   348         delete_AudioFilePlayer(afp);
   349         return NULL;
   350     }
   351 
   352     return afp;
   353 }
   354