src/cdrom/macosx/AudioFilePlayer.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Apr 2003 16:33:56 +0000
changeset 613 9c6717a1c66f
child 653 1bd056de5d1b
permissions -rw-r--r--
Added MacOS X CD-ROM audio support (thanks Max and Darrell)
slouken@613
     1
/*
slouken@613
     2
    SDL - Simple DirectMedia Layer
slouken@613
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@613
     4
slouken@613
     5
    This library is free software; you can redistribute it and/or
slouken@613
     6
    modify it under the terms of the GNU Library General Public
slouken@613
     7
    License as published by the Free Software Foundation; either
slouken@613
     8
    version 2 of the License, or (at your option) any later version.
slouken@613
     9
slouken@613
    10
    This library is distributed in the hope that it will be useful,
slouken@613
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@613
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@613
    13
    Library General Public License for more details.
slouken@613
    14
slouken@613
    15
    You should have received a copy of the GNU Library General Public
slouken@613
    16
    License along with this library; if not, write to the Free
slouken@613
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@613
    18
slouken@613
    19
    Sam Lantinga
slouken@613
    20
    slouken@libsdl.org
slouken@613
    21
slouken@613
    22
    This file based on Apple sample code. We haven't changed the file name, 
slouken@613
    23
    so if you want to see the original search for it on apple.com/developer
slouken@613
    24
*/
slouken@613
    25
slouken@613
    26
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
slouken@613
    27
//  AudioFilePlayer.h
slouken@613
    28
//
slouken@613
    29
#ifndef __AudioFilePlayer_H__
slouken@613
    30
#define __AudioFilePlayer_H__
slouken@613
    31
slouken@613
    32
#include <CoreServices/CoreServices.h>
slouken@613
    33
slouken@613
    34
#include <AudioToolbox/AudioToolbox.h>
slouken@613
    35
#include <AudioUnit/AudioUnit.h>
slouken@613
    36
slouken@613
    37
#include "SDL_Error.h"
slouken@613
    38
slouken@613
    39
const char* AudioFilePlayerErrorStr (OSStatus error);
slouken@613
    40
slouken@613
    41
void ThrowResult (OSStatus result, const char *str);
slouken@613
    42
slouken@613
    43
#define THROW_RESULT(str)                                       \
slouken@613
    44
    if (result) {                                               \
slouken@613
    45
        ThrowResult (result, str);                              \
slouken@613
    46
    }
slouken@613
    47
slouken@613
    48
typedef void (*AudioFilePlayNotifier)(void          *inRefCon,
slouken@613
    49
                                    OSStatus        inStatus);
slouken@613
    50
slouken@613
    51
enum {
slouken@613
    52
    kAudioFilePlayErr_FilePlayUnderrun = -10000,
slouken@613
    53
    kAudioFilePlay_FileIsFinished = -10001,
slouken@613
    54
    kAudioFilePlay_PlayerIsUninitialized = -10002
slouken@613
    55
};
slouken@613
    56
slouken@613
    57
slouken@613
    58
class AudioFileManager;
slouken@613
    59
slouken@613
    60
#pragma mark __________ AudioFilePlayer
slouken@613
    61
class AudioFilePlayer
slouken@613
    62
{
slouken@613
    63
public:
slouken@613
    64
    AudioFilePlayer (const FSRef    *inFileRef);
slouken@613
    65
    
slouken@613
    66
    ~AudioFilePlayer();
slouken@613
    67
slouken@613
    68
    void            SetDestination (AudioUnit                   &inDestUnit, 
slouken@613
    69
                                int                             inBusNumber);
slouken@613
    70
    
slouken@613
    71
    void            SetNotifier (AudioFilePlayNotifier inNotifier, void *inRefCon)
slouken@613
    72
    {
slouken@613
    73
        mNotifier = inNotifier;
slouken@613
    74
        mRefCon = inRefCon;
slouken@613
    75
    }
slouken@613
    76
    
slouken@613
    77
    void            SetStartFrame (int frame); // seek in the file
slouken@613
    78
    
slouken@613
    79
    int             GetCurrentFrame (); // get the current frame position
slouken@613
    80
    
slouken@613
    81
    void            SetStopFrame (int frame);   // set limit in the file
slouken@613
    82
    
slouken@613
    83
    void            Connect();
slouken@613
    84
    
slouken@613
    85
    void            Disconnect();
slouken@613
    86
slouken@613
    87
    void            DoNotification (OSStatus inError) const;
slouken@613
    88
    
slouken@613
    89
    bool            IsConnected () const { return mConnected; }
slouken@613
    90
slouken@613
    91
    UInt32          GetBusNumber () const { return mBusNumber; }
slouken@613
    92
    
slouken@613
    93
    AudioUnit       GetDestUnit () const { return mPlayUnit; }
slouken@613
    94
    
slouken@613
    95
    AudioConverterRef   GetAudioConverter() const { return mConverter; }
slouken@613
    96
slouken@613
    97
#if DEBUG    
slouken@613
    98
    void            Print() const 
slouken@613
    99
    {
slouken@613
   100
        CAShow (mAudioFileID);
slouken@613
   101
        printf ("Destination Bus:%ld\n", GetBusNumber());
slouken@613
   102
        printf ("Is 'aunt' unit:%s\n", (mIsAUNTUnit ? "true" : "false"));
slouken@613
   103
        printf ("Is Connected:%s\n", (IsConnected() ? "true" : "false"));
slouken@613
   104
        if (mConverter) CAShow (mConverter);
slouken@613
   105
          printf ("- - - - - - - - - - - - - - \n");
slouken@613
   106
    }
slouken@613
   107
#endif
slouken@613
   108
slouken@613
   109
    const AudioStreamBasicDescription&      GetFileFormat() const { return mFileDescription; }
slouken@613
   110
    
slouken@613
   111
private:
slouken@613
   112
    AudioUnit                       mPlayUnit;
slouken@613
   113
    UInt32                          mBusNumber;
slouken@613
   114
    AudioFileID                     mAudioFileID;
slouken@613
   115
    
slouken@613
   116
    AudioUnitInputCallback          mInputCallback;
slouken@613
   117
slouken@613
   118
    AudioStreamBasicDescription     mFileDescription;
slouken@613
   119
    
slouken@613
   120
    bool                            mConnected;
slouken@613
   121
    bool                            mIsAUNTUnit;
slouken@613
   122
    
slouken@613
   123
    AudioFileManager*               mAudioFileManager;
slouken@613
   124
    AudioConverterRef               mConverter;
slouken@613
   125
    
slouken@613
   126
    AudioFilePlayNotifier           mNotifier;
slouken@613
   127
    void*                           mRefCon;
slouken@613
   128
    
slouken@613
   129
    int                             mStartFrame;
slouken@613
   130
    
slouken@613
   131
#pragma mark __________ Private_Methods
slouken@613
   132
    
slouken@613
   133
    void        OpenFile (const FSRef *inRef, SInt64& outFileSize);
slouken@613
   134
};
slouken@613
   135
slouken@613
   136
#pragma mark __________ AudioFileManager
slouken@613
   137
class AudioFileManager
slouken@613
   138
{
slouken@613
   139
public:
slouken@613
   140
    AudioFileManager (AudioFilePlayer& inParent, AudioFileID inFile)
slouken@613
   141
        : mParent (inParent),
slouken@613
   142
          mAudioFileID (inFile),
slouken@613
   143
          mFileBuffer (0),
slouken@613
   144
          mByteCounter (0)
slouken@613
   145
        {}
slouken@613
   146
    
slouken@613
   147
    virtual ~AudioFileManager();
slouken@613
   148
    
slouken@613
   149
    
slouken@613
   150
    void                Connect (AudioConverterRef inConverter) 
slouken@613
   151
    {
slouken@613
   152
        mParentConverter = inConverter;
slouken@613
   153
        DoConnect();
slouken@613
   154
    }
slouken@613
   155
slouken@613
   156
        // this method should NOT be called by an object of this class
slouken@613
   157
        // as it is called by the parent's Disconnect() method
slouken@613
   158
    virtual void        Disconnect () {}
slouken@613
   159
slouken@613
   160
    const AudioFileID&  GetFileID() const { return mAudioFileID; }
slouken@613
   161
slouken@613
   162
    const char*         GetFileBuffer () { return mFileBuffer; }
slouken@613
   163
slouken@613
   164
    const AudioFilePlayer&  GetParent () const { return mParent; }
slouken@613
   165
    
slouken@613
   166
    virtual void        SetPosition (SInt64 pos) = 0;  // seek/rewind in the file
slouken@613
   167
    
slouken@613
   168
    virtual int         GetByteCounter () { return mByteCounter; } // return actual bytes streamed to audio hardware
slouken@613
   169
    
slouken@613
   170
    virtual void        SetEndOfFile (SInt64 pos) = 0;  // set the "EOF" (will behave just like it reached eof)
slouken@613
   171
   
slouken@613
   172
protected:
slouken@613
   173
    AudioFilePlayer&            mParent;
slouken@613
   174
    AudioConverterRef           mParentConverter;
slouken@613
   175
    const AudioFileID           mAudioFileID;
slouken@613
   176
    
slouken@613
   177
    char*                       mFileBuffer;
slouken@613
   178
slouken@613
   179
    OSStatus            Render (AudioBuffer &ioData);
slouken@613
   180
slouken@613
   181
    int                         mByteCounter;
slouken@613
   182
    
slouken@613
   183
    virtual OSStatus    GetFileData (void** inOutData, UInt32 *inOutDataSize) = 0;
slouken@613
   184
    
slouken@613
   185
    virtual void        DoConnect () = 0;
slouken@613
   186
        
slouken@613
   187
    virtual void        AfterRender () = 0;
slouken@613
   188
slouken@613
   189
public:
slouken@613
   190
    static OSStatus     FileInputProc (void                             *inRefCon, 
slouken@613
   191
                                        AudioUnitRenderActionFlags      inActionFlags,
slouken@613
   192
                                        const AudioTimeStamp            *inTimeStamp, 
slouken@613
   193
                                        UInt32                          inBusNumber, 
slouken@613
   194
                                        AudioBuffer                     *ioData);
slouken@613
   195
    static OSStatus     ACInputProc (AudioConverterRef          inAudioConverter,
slouken@613
   196
                                            UInt32*                     outDataSize,
slouken@613
   197
                                            void**                      outData,
slouken@613
   198
                                            void*                       inUserData);
slouken@613
   199
};
slouken@613
   200
slouken@613
   201
slouken@613
   202
#pragma mark __________ AudioFileReaderThread
slouken@613
   203
class AudioFileReaderThread 
slouken@613
   204
    : public AudioFileManager
slouken@613
   205
{
slouken@613
   206
public:
slouken@613
   207
    const UInt32    mChunkSize;
slouken@613
   208
    SInt64          mFileLength;
slouken@613
   209
    SInt64          mReadFilePosition;
slouken@613
   210
    bool            mWriteToFirstBuffer;
slouken@613
   211
    bool            mFinishedReadingData;
slouken@613
   212
    
slouken@613
   213
    AudioFileReaderThread (AudioFilePlayer  &inParent, 
slouken@613
   214
                            AudioFileID     &inFile, 
slouken@613
   215
                            SInt64          inFileLength,
slouken@613
   216
                            UInt32          inChunkSize);
slouken@613
   217
    
slouken@613
   218
    virtual void        Disconnect ();
slouken@613
   219
slouken@613
   220
    virtual void        SetPosition (SInt64 pos);  // seek/rewind in the file
slouken@613
   221
    
slouken@613
   222
    virtual void        SetEndOfFile (SInt64 pos);  // set the "EOF" (will behave just like it reached eof)
slouken@613
   223
    
slouken@613
   224
protected:
slouken@613
   225
    virtual void        DoConnect ();
slouken@613
   226
slouken@613
   227
    virtual OSStatus    GetFileData (void** inOutData, UInt32 *inOutDataSize);
slouken@613
   228
slouken@613
   229
    virtual void        AfterRender ();
slouken@613
   230
slouken@613
   231
private:
slouken@613
   232
    bool                        mReadFromFirstBuffer;
slouken@613
   233
    bool                        mLockUnsuccessful;
slouken@613
   234
    bool                        mIsEngaged;
slouken@613
   235
    
slouken@613
   236
    int                         mNumTimesAskedSinceFinished;
slouken@613
   237
};
slouken@613
   238
slouken@613
   239
slouken@613
   240
#endif