Hi, there
I'm not good at osx and coreaudio programming.
I decoded MP3 to signed 16 PCM(each frame decoded 4608 byte size)
and stored PCM data as a file "pcm.pcm".
And I find an example(http://developer.apple.com/samplecode/
PlayAudioFileLite/index.html).
this source looks somewhat simple, so I decide to modify this
source like this.
==================================================================
/*
SYSTEM: MacMini PPC
Sample Rate:48000.000000
Format ID:lpcm
Format Flags:B
Bytes per Packet:8
Frames per Packet:1
Bytes per Frame:8
Channels per Frame:2
Bits per Channel:32
*/
#include <CoreServices/CoreServices.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioToolbox.h>
#include <unistd.h> //for usleep
#define checkStatus( err) \
if(err) {\
printf("Error: %s -> %s: %d\n", (char *)&err,__FILE__, __LINE__);\
fflush(stdout);\
return err; \
}
int FD;
AudioConverterRef converter;
AudioUnit gOutputUnit;
AudioStreamBasicDescription sourceASBD, outputASBD;
void *gSourceBuffer;
char pcm_buffer[10000];
AudioFileID *gSourceAudioFileID;
UInt64 gTotalPacketCount=0;
UInt64 gFileByteCount =0;
UInt32 gMaxPacketSize =0;
UInt64 gPacketOffset=0;
Boolean gIsPlaying = FALSE;
OSStatus ACComplexInputProc
( AudioConverterRef inAudioConverter,
UInt32
*ioNumberDataPackets,
AudioBufferList *ioData,
AudioStreamPacketDescription **outDataPacketDescription,
void*
inUserData)
{
OSStatus err = noErr;
UInt32 bytesReturned = 0;
// initialize in case of failure
ioData->mBuffers[0].mData = NULL;
ioData->mBuffers[0].mDataByteSize = 0;
bytesReturned = read(FD, pcm_buffer, 4608);
gSourceBuffer = (void*)(pcm_buffer);
*ioNumberDataPackets = 1;;
printf("bytesReturned:%ld, ioNumberDataPackets:%ld,
gSourceBuffer=%p\n", bytesReturned, *ioNumberDataPackets,
gSourceBuffer);
if(bytesReturned == 0){
gIsPlaying = 0;
}
ioData->mBuffers[0].mData = gSourceBuffer;
ioData->mBuffers[0].mDataByteSize = bytesReturned;
return err;
}
OSStatus streamRenderProc(void *inRefCon,
AudioUnitRenderActionFlags *inActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumFrames,
AudioBufferList *ioData)
{
OSStatus err= noErr;
void *inInputDataProcUserData=NULL;
AudioStreamPacketDescription* outPacketDescription =NULL;
err = AudioConverterFillComplexBuffer(converter,
ACComplexInputProc ,inInputDataProcUserData ,
&inNumFrames, ioData, outPacketDescription);
return err;
}
void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
{
if (!inDesc) {
printf ("Can't print a NULL desc!\n");
return;
}
printf ("- - - - - - - - - - - - - - - - - - - -\n");
printf (" Sample Rate:%f\n", inDesc->mSampleRate);
printf (" Format ID:%s\n", (char*)&inDesc->mFormatID);
printf (" Format Flags:%lX\n", inDesc->mFormatFlags);
printf (" Bytes per Packet:%ld\n", inDesc->mBytesPerPacket);
printf (" Frames per Packet:%ld\n", inDesc->mFramesPerPacket);
printf (" Bytes per Frame:%ld\n", inDesc->mBytesPerFrame);
printf (" Channels per Frame:%ld\n", inDesc->mChannelsPerFrame);
printf (" Bits per Channel:%ld\n", inDesc->mBitsPerChannel);
printf ("- - - - - - - - - - - - - - - - - - - -\n");
}
OSStatus setupAudioUnit(AudioUnit *gOutputUnit, AudioConverterRef
*conv){
OSStatus err;
//An Audio Unit is a OS component
//The component description must be setup, then used to
//initialize an AudioUnit
ComponentDescription desc;
UInt32 size = sizeof (AudioStreamBasicDescription);
Boolean outWritable;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
Component comp = FindNextComponent(NULL, &desc);
if (comp == NULL) exit (-1);
err = OpenAComponent(comp, gOutputUnit);
if (err) exit (-1);
// Initialize AudioUnit
verify_noerr(AudioUnitInitialize(*gOutputUnit));
// MatchAUFormats
memset(&outputASBD, 0, size);
//Gets the size of the Stream Format Property and if it is writable
AudioUnitGetPropertyInfo(*gOutputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
0,
&size,
&outWritable);
//Get the current stream format of the output
OSStatus result = AudioUnitGetProperty (*gOutputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
0,
&outputASBD,
&size);
checkStatus(result);
//Set the stream format of the output to match the input
result = AudioUnitSetProperty (*gOutputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&outputASBD,
size);
err = AudioConverterNew(&sourceASBD, &outputASBD, conv);
printf("conv:%p\n", *conv);
printf("Source\n");
PrintStreamDesc(&sourceASBD);
return err;
}
OSStatus setupCallbacks(AudioUnit *gOutputUnit,
AURenderCallbackStruct *renderCallback){
OSStatus err= noErr;
memset(renderCallback, 0, sizeof(AURenderCallbackStruct));
renderCallback->inputProc = streamRenderProc;
renderCallback->inputProcRefCon =0;
//Sets the callback for the Audio Unit
err = AudioUnitSetProperty (*gOutputUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
renderCallback,
sizeof(AURenderCallbackStruct));
return err;
}
void CleanUp(AudioUnit *theOutputUnit){
printf("finished playing\n");
//Cleaning anything allocated.
close(FD);
AudioConverterDispose(converter);
AudioOutputUnitStop(*theOutputUnit);//you must stop the audio unit
AudioUnitUninitialize (*theOutputUnit);
CloseComponent(*theOutputUnit);
}
int main(int argc, char *argv[])
{
AURenderCallbackStruct renderCallback;
FD = open("/Users/jwlee/data/pcm.pcm", O_RDONLY, 0);
printf ("%d\n", FD);
// source PCM data information
sourceASBD.mFormatID = kAudioFormatLinearPCM;
sourceASBD.mFormatFlags = kAudioFormatFlagIsSignedInteger;//|
kAudioFormatFlagIsPacked;
sourceASBD.mSampleRate = 441000; //frequency
sourceASBD.mBitsPerChannel = 16; // bitspersample -> fix
sourceASBD.mBytesPerFrame = 16/8*2;// (bitspersample/8)*channels
sourceASBD.mChannelsPerFrame = 2;
sourceASBD.mFramesPerPacket = 1;
sourceASBD.mBytesPerPacket = 16/8*2; //(bitspersample/8)*channels
sourceASBD.mReserved = 0;
setupAudioUnit(&gOutputUnit, &converter);
printf("Destination\n");
PrintStreamDesc(&outputASBD);
setupCallbacks(&gOutputUnit, &renderCallback);
AudioOutputUnitStart(gOutputUnit);
gIsPlaying = TRUE;
while(gIsPlaying){
usleep(250000);
}
CleanUp(&gOutputUnit);
return 0;
}
=====================================================================
=========
result is some beep and beep,,,
What's wrong with this code?
please give me advice.
Thanks in advance.
---------------------------------------
JongWoong Lee
Seoul Korea
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40deepseasoftware.com
This email sent to email@hidden