src/cdrom/macosx/AudioFilePlayer.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 02:06:30 +0000
changeset 935 f8d5ddc7aef1
parent 769 b8d311d90021
child 1143 71a2648acc75
permissions -rw-r--r--
Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
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@613
    34
#include <AudioUnit/AudioUnit.h>
slouken@613
    35
slouken@653
    36
#include "SDL_error.h"
slouken@613
    37
slouken@613
    38
const char* AudioFilePlayerErrorStr (OSStatus error);
slouken@613
    39
slouken@613
    40
void ThrowResult (OSStatus result, const char *str);
slouken@613
    41
slouken@613
    42
#define THROW_RESULT(str)                                       \
slouken@613
    43
    if (result) {                                               \
slouken@613
    44
        ThrowResult (result, str);                              \
slouken@613
    45
    }
slouken@613
    46
slouken@613
    47
typedef void (*AudioFilePlayNotifier)(void          *inRefCon,
slouken@613
    48
                                    OSStatus        inStatus);
slouken@613
    49
slouken@613
    50
enum {
slouken@613
    51
    kAudioFilePlayErr_FilePlayUnderrun = -10000,
slouken@613
    52
    kAudioFilePlay_FileIsFinished = -10001,
slouken@613
    53
    kAudioFilePlay_PlayerIsUninitialized = -10002
slouken@613
    54
};
slouken@613
    55
slouken@613
    56
slouken@613
    57
class AudioFileManager;
slouken@613
    58
slouken@613
    59
#pragma mark __________ AudioFilePlayer
slouken@613
    60
class AudioFilePlayer
slouken@613
    61
{
slouken@613
    62
public:
slouken@613
    63
    AudioFilePlayer (const FSRef    *inFileRef);
slouken@613
    64
    
slouken@613
    65
    ~AudioFilePlayer();
slouken@613
    66
slouken@935
    67
    void            SetDestination (AudioUnit &inDestUnit);
slouken@613
    68
    
slouken@613
    69
    void            SetNotifier (AudioFilePlayNotifier inNotifier, void *inRefCon)
slouken@613
    70
    {
slouken@613
    71
        mNotifier = inNotifier;
slouken@613
    72
        mRefCon = inRefCon;
slouken@613
    73
    }
slouken@613
    74
    
slouken@613
    75
    void            SetStartFrame (int frame); // seek in the file
slouken@613
    76
    
slouken@613
    77
    int             GetCurrentFrame (); // get the current frame position
slouken@613
    78
    
slouken@613
    79
    void            SetStopFrame (int frame);   // set limit in the file
slouken@613
    80
    
slouken@613
    81
    void            Connect();
slouken@613
    82
    
slouken@613
    83
    void            Disconnect();
slouken@613
    84
slouken@613
    85
    void            DoNotification (OSStatus inError) const;
slouken@613
    86
    
slouken@613
    87
    bool            IsConnected () const { return mConnected; }
slouken@613
    88
slouken@613
    89
    AudioUnit       GetDestUnit () const { return mPlayUnit; }
slouken@613
    90
    
slouken@613
    91
#if DEBUG    
slouken@613
    92
    void            Print() const 
slouken@613
    93
    {
slouken@613
    94
        printf ("Is Connected:%s\n", (IsConnected() ? "true" : "false"));
slouken@768
    95
        printf ("- - - - - - - - - - - - - - \n");
slouken@613
    96
    }
slouken@613
    97
#endif
slouken@613
    98
    
slouken@613
    99
private:
slouken@613
   100
    AudioUnit                       mPlayUnit;
slouken@768
   101
    SInt16                          mForkRefNum;
slouken@613
   102
    
slouken@613
   103
    AudioUnitInputCallback          mInputCallback;
slouken@613
   104
slouken@613
   105
    AudioStreamBasicDescription     mFileDescription;
slouken@613
   106
    
slouken@613
   107
    bool                            mConnected;
slouken@613
   108
    
slouken@613
   109
    AudioFileManager*               mAudioFileManager;
slouken@613
   110
    
slouken@613
   111
    AudioFilePlayNotifier           mNotifier;
slouken@613
   112
    void*                           mRefCon;
slouken@613
   113
    
slouken@613
   114
    int                             mStartFrame;
slouken@613
   115
    
slouken@613
   116
#pragma mark __________ Private_Methods
slouken@613
   117
    
slouken@613
   118
    void        OpenFile (const FSRef *inRef, SInt64& outFileSize);
slouken@613
   119
};
slouken@613
   120
slouken@613
   121
#pragma mark __________ AudioFileManager
slouken@613
   122
class AudioFileManager
slouken@613
   123
{
slouken@613
   124
public:
slouken@768
   125
    AudioFileManager (AudioFilePlayer &inParent, 
slouken@768
   126
                      SInt16          inForkRefNum, 
slouken@768
   127
                      SInt64          inFileLength,
slouken@768
   128
                      UInt32          inChunkSize);
slouken@613
   129
    
slouken@768
   130
    ~AudioFileManager();
slouken@613
   131
    
slouken@613
   132
    
slouken@613
   133
        // this method should NOT be called by an object of this class
slouken@613
   134
        // as it is called by the parent's Disconnect() method
slouken@768
   135
    void                Disconnect ();
slouken@935
   136
    
slouken@935
   137
    void                DoConnect ();
slouken@613
   138
slouken@768
   139
    OSStatus            Read(char *buffer, UInt32 *len);
slouken@613
   140
slouken@613
   141
    const char*         GetFileBuffer () { return mFileBuffer; }
slouken@613
   142
slouken@613
   143
    const AudioFilePlayer&  GetParent () const { return mParent; }
slouken@613
   144
    
slouken@768
   145
    void                SetPosition (SInt64 pos);  // seek/rewind in the file
slouken@613
   146
    
slouken@768
   147
    int                 GetByteCounter () { return mByteCounter; } // return actual bytes streamed to audio hardware
slouken@613
   148
    
slouken@768
   149
    void                SetEndOfFile (SInt64 pos);  // set the "EOF" (will behave just like it reached eof)
slouken@613
   150
   
slouken@613
   151
protected:
slouken@768
   152
    AudioFilePlayer&    mParent;
slouken@768
   153
    SInt16              mForkRefNum;
slouken@768
   154
    SInt64              mAudioDataOffset;
slouken@613
   155
    
slouken@768
   156
    char*               mFileBuffer;
slouken@613
   157
slouken@768
   158
    int                 mByteCounter;
slouken@768
   159
slouken@768
   160
    bool                mReadFromFirstBuffer;
slouken@768
   161
    bool                mLockUnsuccessful;
slouken@768
   162
    bool                mIsEngaged;
slouken@768
   163
    
slouken@768
   164
    int                 mNumTimesAskedSinceFinished;
slouken@768
   165
slouken@935
   166
slouken@935
   167
	void*               mTmpBuffer;
slouken@935
   168
	UInt32              mBufferSize;
slouken@935
   169
	UInt32              mBufferOffset;
slouken@768
   170
public:
slouken@768
   171
    const UInt32        mChunkSize;
slouken@768
   172
    SInt64              mFileLength;
slouken@768
   173
    SInt64              mReadFilePosition;
slouken@768
   174
    bool                mWriteToFirstBuffer;
slouken@768
   175
    bool                mFinishedReadingData;
slouken@768
   176
slouken@768
   177
protected:
slouken@613
   178
    OSStatus            Render (AudioBuffer &ioData);
slouken@613
   179
    
slouken@768
   180
    OSStatus            GetFileData (void** inOutData, UInt32 *inOutDataSize);
slouken@613
   181
        
slouken@768
   182
    void                AfterRender ();
slouken@613
   183
slouken@613
   184
public:
slouken@613
   185
    static OSStatus     FileInputProc (void                             *inRefCon, 
slouken@613
   186
                                        AudioUnitRenderActionFlags      inActionFlags,
slouken@613
   187
                                        const AudioTimeStamp            *inTimeStamp, 
slouken@613
   188
                                        UInt32                          inBusNumber, 
slouken@613
   189
                                        AudioBuffer                     *ioData);
slouken@613
   190
};
slouken@613
   191
slouken@613
   192
slouken@613
   193
#endif