19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This file based on Apple sample code. We haven't changed the file name,
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    so if you want to see the original search for it on apple.com/developer
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    AudioFilePlayer.h
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef __AudioFilePlayer_H__
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __AudioFilePlayer_H__
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <CoreServices/CoreServices.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <AudioUnit/AudioUnit.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <AudioUnit/AUNTComponent.h>
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if (MAC_OS_X_VERSION_MAX_ALLOWED < 1050)
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef SInt16 FSIORefNum;
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_error.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char* AudioFilePlayerErrorStr (OSStatus error);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid ThrowResult (OSStatus result, const char *str);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define THROW_RESULT(str)                                       \
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (result) {                                               \
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ThrowResult (result, str);                              \
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef void (*AudioFilePlayNotifier)(void          *inRefCon,
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    OSStatus        inStatus);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallenum {
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    kAudioFilePlayErr_FilePlayUnderrun = -10000,
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    kAudioFilePlay_FileIsFinished = -10001,
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    kAudioFilePlay_PlayerIsUninitialized = -10002
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct S_AudioFileManager;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma mark __________ AudioFilePlayer
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct S_AudioFilePlayer
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*public:*/
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int             (*SetDestination)(struct S_AudioFilePlayer *afp, AudioUnit *inDestUnit);
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void            (*SetNotifier)(struct S_AudioFilePlayer *afp, AudioFilePlayNotifier inNotifier, void *inRefCon);
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void            (*SetStartFrame)(struct S_AudioFilePlayer *afp, int frame); /* seek in the file */
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int             (*GetCurrentFrame)(struct S_AudioFilePlayer *afp); /* get the current frame position */
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void            (*SetStopFrame)(struct S_AudioFilePlayer *afp, int frame);   /* set limit in the file */
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int             (*Connect)(struct S_AudioFilePlayer *afp);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void            (*Disconnect)(struct S_AudioFilePlayer *afp);
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void            (*DoNotification)(struct S_AudioFilePlayer *afp, OSStatus inError);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int             (*IsConnected)(struct S_AudioFilePlayer *afp);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    AudioUnit       (*GetDestUnit)(struct S_AudioFilePlayer *afp);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void            (*Print)(struct S_AudioFilePlayer *afp);
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*private:*/
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    AudioUnit                       mPlayUnit;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FSIORefNum                      mForkRefNum;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    AURenderCallbackStruct          mInputCallback;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    AudioStreamBasicDescription     mFileDescription;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                             mConnected;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct S_AudioFileManager*      mAudioFileManager;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    AudioFilePlayNotifier           mNotifier;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void*                           mRefCon;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                             mStartFrame;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma mark __________ Private_Methods
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int          (*OpenFile)(struct S_AudioFilePlayer *afp, const FSRef *inRef, SInt64 *outFileSize);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} AudioFilePlayer;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioFilePlayer *new_AudioFilePlayer(const FSRef    *inFileRef);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid delete_AudioFilePlayer(AudioFilePlayer *afp);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma mark __________ AudioFileManager
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct S_AudioFileManager
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*public:*/
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* this method should NOT be called by an object of this class
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           as it is called by the parent's Disconnect() method */
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void                (*Disconnect)(struct S_AudioFileManager *afm);
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                 (*DoConnect)(struct S_AudioFileManager *afm);
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    OSStatus            (*Read)(struct S_AudioFileManager *afm, char *buffer, ByteCount *len);
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char*         (*GetFileBuffer)(struct S_AudioFileManager *afm);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const AudioFilePlayer *(*GetParent)(struct S_AudioFileManager *afm);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void                (*SetPosition)(struct S_AudioFileManager *afm, SInt64 pos);  /* seek/rewind in the file */
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                 (*GetByteCounter)(struct S_AudioFileManager *afm);  /* return actual bytes streamed to audio hardware */
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void                (*SetEndOfFile)(struct S_AudioFileManager *afm, SInt64 pos);  /* set the "EOF" (will behave just like it reached eof) */
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*protected:*/
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    AudioFilePlayer*    mParent;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SInt16              mForkRefNum;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SInt64              mAudioDataOffset;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char*               mFileBuffer;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                 mByteCounter;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                mReadFromFirstBuffer;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                mLockUnsuccessful;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                mIsEngaged;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                 mNumTimesAskedSinceFinished;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void*               mTmpBuffer;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UInt32              mBufferSize;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UInt32              mBufferOffset;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*public:*/
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    UInt32              mChunkSize;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SInt64              mFileLength;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SInt64              mReadFilePosition;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                 mWriteToFirstBuffer;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int                 mFinishedReadingData;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*protected:*/
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    OSStatus            (*Render)(struct S_AudioFileManager *afm, AudioBufferList *ioData);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    OSStatus            (*GetFileData)(struct S_AudioFileManager *afm, void** inOutData, UInt32 *inOutDataSize);
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void                (*AfterRender)(struct S_AudioFileManager *afm);
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*public:*/
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*static*/
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    OSStatus            (*FileInputProc)(void                            *inRefCon,
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                         AudioUnitRenderActionFlags      *ioActionFlags,
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                         const AudioTimeStamp            *inTimeStamp,
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                         UInt32                          inBusNumber,
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                         UInt32                          inNumberFrames,
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                         AudioBufferList                 *ioData);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} AudioFileManager;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioFileManager *new_AudioFileManager (AudioFilePlayer *inParent,
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      SInt16          inForkRefNum,
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      SInt64          inFileLength,
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      UInt32          inChunkSize);
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid delete_AudioFileManager(AudioFileManager *afm);
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
179