I thought I'd set some standards here, no Windows XP installations, no crying about my personal misdemeanours. Instead here's some obscene class for having an audio player (for Mac OSX libs) in the background for a 3d engine, I'll post bits and pieces for the engine on from time to time.
implementation would be :
SlxAudioPlayer * player1 = new SlxAudioPlayer () ;
player1->PlayFile ( "/home/anthony/classic/dazed_and_confused.mp3" ) ;
/*
* slxaudioplayer.cpp
* slx
*
* Created by Anthony Shaw on 15/08/2006.
*
*/
#include "slxaudioplayer.h"
#include <AudioToolbox/AudioToolbox.h>
void SlxAudioPlayer::PlayFile (char * const fileName)
{
AudioFileID audioFile;
const char* inputFile = fileName;
FSRef theRef;
//xit
FSPathMakeRef ((const UInt8 *)inputFile, &theRef, NULL);
// xit
AudioFileOpen (&theRef, fsRdPerm, 0, &audioFile);
// get the number of channels of the file
AudioStreamBasicDescription fileFormat;
UInt32 propsize = sizeof(AudioStreamBasicDescription);
//xit
AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &propsize, &fileFormat);
printf ("playing file: %s\n", inputFile);
// lets set up our playing state now
AUGraph theGraph;
AudioUnit fileAU;
// this makes the graph, the file AU and sets it all up for playing
MakeSimpleGraph (theGraph, fileAU, fileFormat, audioFile);
// now we load the file contents up for playback before we start playing
// this has to be done the AU is initialized and anytime it is reset or uninitialized
Float64 fileDuration = PrepareFileAU (fileAU, fileFormat, audioFile);
printf ("file duration: %f secs\n", fileDuration);
// start playing
AUGraphStart (theGraph);
// lets clean up
AUGraphStop (theGraph);
AUGraphUninitialize (theGraph);
AudioFileClose (audioFile);
AUGraphClose (theGraph);
}
double SlxAudioPlayer::PrepareFileAU (AudioUnit &au, AudioStreamBasicDescription &fileFormat, AudioFileID audioFile)
{
//
// calculate the duration
UInt64 nPackets;
UInt32 propsize = sizeof(nPackets);
AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataPacketCount, &propsize, &nPackets);
Float64 fileDuration = (nPackets * fileFormat.mFramesPerPacket) / fileFormat.mSampleRate;
ScheduledAudioFileRegion rgn;
memset (&rgn.mTimeStamp, 0, sizeof(rgn.mTimeStamp));
rgn.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
rgn.mTimeStamp.mSampleTime = 0;
rgn.mCompletionProc = NULL;
rgn.mCompletionProcUserData = NULL;
rgn.mAudioFile = audioFile;
rgn.mLoopCount = 1;
rgn.mStartFrame = 0;
rgn.mFramesToPlay = UInt32(nPackets * fileFormat.mFramesPerPacket);
// prime the fp AU with default values
UInt32 defaultVal = 0;
// tell the fp AU when to start playing (this ts is in the AU's render time stamps; -1 means next render cycle)
AudioTimeStamp startTime;
memset (&startTime, 0, sizeof(startTime));
startTime.mFlags = kAudioTimeStampSampleTimeValid;
startTime.mSampleTime = -1;
//au.SetProperty(kAudioUnitProperty_ScheduleStartTimeStamp,
// kAudioUnitScope_Global, 0, &startTime, sizeof(startTime));
return fileDuration;
}
void SlxAudioPlayer::MakeSimpleGraph (AUGraph &theGraph, AudioUnit &fileAU, AudioStreamBasicDescription &fileFormat, AudioFileID audioFile)
{
NewAUGraph (&theGraph);
ComponentDescription cd;
// output node
cd.componentType = kAudioUnitType_Output;
cd.componentSubType = kAudioUnitSubType_DefaultOutput;
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
AUNode outputNode;
AUGraphNewNode (theGraph, &cd, 0, NULL, &outputNode);
// file AU node
AUNode fileNode;
cd.componentType = kAudioUnitType_Generator;
cd.componentSubType = kAudioUnitSubType_AudioFilePlayer;
AUGraphNewNode (theGraph, &cd, 0, NULL, &fileNode);
// connect & setup
AUGraphOpen (theGraph);
// install overload listener to detect when something is wrong
AudioUnit anAU;
AUGraphGetNodeInfo(theGraph, fileNode, NULL, NULL, NULL, &anAU);
AUGraphConnectNodeInput (theGraph, fileNode, 0, outputNode, 0);
AUGraphInitialize (theGraph);
}
And slxaudioplayer.h:
#ifndef SLXAUDIOPLAYERH
#define SLXAUDIOPLAYERH
#include <AudioToolbox/AudioToolbox.h>
class SlxAudioPlayer {
private:
double PrepareFileAU (AudioUnit &au, AudioStreamBasicDescription &fileFormat, AudioFileID audioFile);
void MakeSimpleGraph (AUGraph &theGraph, AudioUnit &fileAU, AudioStreamBasicDescription &fileFormat, AudioFileID audioFile);
public :
SlxAudioPlayer () {} ;
void PlayFile ( char * fileName ) ;
};
#endif
No comments:
Post a Comment