src/cdrom/macosx/AudioFilePlayer.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 16:49:27 +0000
changeset 769 b8d311d90021
parent 768 de1b2c3063b9
child 935 f8d5ddc7aef1
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
slouken@613
     1
/*
slouken@613
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 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@768
    34
#include <AudioToolbox/AudioConverter.h>
slouken@613
    35
#include <AudioUnit/AudioUnit.h>
slouken@613
    36
slouken@653
    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
        printf ("Destination Bus:%ld\n", GetBusNumber());
slouken@613
   101
        printf ("Is Connected:%s\n", (IsConnected() ? "true" : "false"));
slouken@768
   102
        printf ("- - - - - - - - - - - - - - \n");
slouken@613
   103
    }
slouken@613
   104
#endif
slouken@613
   105
slouken@613
   106
    const AudioStreamBasicDescription&      GetFileFormat() const { return mFileDescription; }
slouken@613
   107
    
slouken@613
   108
private:
slouken@613
   109
    AudioUnit                       mPlayUnit;
slouken@613
   110
    UInt32                          mBusNumber;
slouken@768
   111
    SInt16                          mForkRefNum;
slouken@613
   112
    
slouken@613
   113
    AudioUnitInputCallback          mInputCallback;
slouken@613
   114
slouken@613
   115
    AudioStreamBasicDescription     mFileDescription;
slouken@613
   116
    
slouken@613
   117
    bool                            mConnected;
slouken@613
   118
    
slouken@613
   119
    AudioFileManager*               mAudioFileManager;
slouken@613
   120
    AudioConverterRef               mConverter;
slouken@613
   121
    
slouken@613
   122
    AudioFilePlayNotifier           mNotifier;
slouken@613
   123
    void*                           mRefCon;
slouken@613
   124
    
slouken@613
   125
    int                             mStartFrame;
slouken@613
   126
    
slouken@613
   127
#pragma mark __________ Private_Methods
slouken@613
   128
    
slouken@613
   129
    void        OpenFile (const FSRef *inRef, SInt64& outFileSize);
slouken@613
   130
};
slouken@613
   131
slouken@613
   132
#pragma mark __________ AudioFileManager
slouken@613
   133
class AudioFileManager
slouken@613
   134
{
slouken@613
   135
public:
slouken@768
   136
    AudioFileManager (AudioFilePlayer &inParent, 
slouken@768
   137
                      SInt16          inForkRefNum, 
slouken@768
   138
                      SInt64          inFileLength,
slouken@768
   139
                      UInt32          inChunkSize);
slouken@613
   140
    
slouken@768
   141
    ~AudioFileManager();
slouken@613
   142
    
slouken@613
   143
    
slouken@613
   144
    void                Connect (AudioConverterRef inConverter) 
slouken@613
   145
    {
slouken@613
   146
        mParentConverter = inConverter;
slouken@613
   147
        DoConnect();
slouken@613
   148
    }
slouken@613
   149
slouken@613
   150
        // this method should NOT be called by an object of this class
slouken@613
   151
        // as it is called by the parent's Disconnect() method
slouken@768
   152
    void                Disconnect ();
slouken@613
   153
slouken@768
   154
    OSStatus            Read(char *buffer, UInt32 *len);
slouken@613
   155
slouken@613
   156
    const char*         GetFileBuffer () { return mFileBuffer; }
slouken@613
   157
slouken@613
   158
    const AudioFilePlayer&  GetParent () const { return mParent; }
slouken@613
   159
    
slouken@768
   160
    void                SetPosition (SInt64 pos);  // seek/rewind in the file
slouken@613
   161
    
slouken@768
   162
    int                 GetByteCounter () { return mByteCounter; } // return actual bytes streamed to audio hardware
slouken@613
   163
    
slouken@768
   164
    void                SetEndOfFile (SInt64 pos);  // set the "EOF" (will behave just like it reached eof)
slouken@613
   165
   
slouken@613
   166
protected:
slouken@768
   167
    AudioFilePlayer&    mParent;
slouken@768
   168
    AudioConverterRef   mParentConverter;
slouken@768
   169
    SInt16              mForkRefNum;
slouken@768
   170
    SInt64              mAudioDataOffset;
slouken@613
   171
    
slouken@768
   172
    char*               mFileBuffer;
slouken@613
   173
slouken@768
   174
    int                 mByteCounter;
slouken@768
   175
slouken@768
   176
    bool                mReadFromFirstBuffer;
slouken@768
   177
    bool                mLockUnsuccessful;
slouken@768
   178
    bool                mIsEngaged;
slouken@768
   179
    
slouken@768
   180
    int                 mNumTimesAskedSinceFinished;
slouken@768
   181
slouken@768
   182
public:
slouken@768
   183
    const UInt32        mChunkSize;
slouken@768
   184
    SInt64              mFileLength;
slouken@768
   185
    SInt64              mReadFilePosition;
slouken@768
   186
    bool                mWriteToFirstBuffer;
slouken@768
   187
    bool                mFinishedReadingData;
slouken@768
   188
slouken@768
   189
protected:
slouken@613
   190
    OSStatus            Render (AudioBuffer &ioData);
slouken@613
   191
    
slouken@768
   192
    OSStatus            GetFileData (void** inOutData, UInt32 *inOutDataSize);
slouken@613
   193
    
slouken@768
   194
    void                DoConnect ();
slouken@613
   195
        
slouken@768
   196
    void                AfterRender ();
slouken@613
   197
slouken@613
   198
public:
slouken@613
   199
    static OSStatus     FileInputProc (void                             *inRefCon, 
slouken@613
   200
                                        AudioUnitRenderActionFlags      inActionFlags,
slouken@613
   201
                                        const AudioTimeStamp            *inTimeStamp, 
slouken@613
   202
                                        UInt32                          inBusNumber, 
slouken@613
   203
                                        AudioBuffer                     *ioData);
slouken@613
   204
    static OSStatus     ACInputProc (AudioConverterRef          inAudioConverter,
slouken@613
   205
                                            UInt32*                     outDataSize,
slouken@613
   206
                                            void**                      outData,
slouken@613
   207
                                            void*                       inUserData);
slouken@613
   208
};
slouken@613
   209
slouken@613
   210
slouken@613
   211
#endif