How to play RAW PCM data using CoreAudio?
How to play RAW PCM data using CoreAudio?
- Subject: How to play RAW PCM data using CoreAudio?
- From: malcom <email@hidden>
- Date: Thu, 23 Apr 2009 17:13:42 +0200
Hi, I've finally made my own chirp signal and I've written it into a
file (the code is below).
At this time I need to get it played by Core Audio. I've successfully
imported it in Audacity and it works perfectly
(44,100/16bit/mono/endianess)..
I've tried with this code but it does works because seems that CA
can't open raw PCM data file (and inside the list of formats there is
not mention to raw pcm data format).
Anyone can suggest me a way to play it?
(the sounds generated can be downloaded here
http://www.box.net/shared/0q36px4tpn
#import "MainController.h"
@implementation MainController
void makeChirp(double freq_start,double freq_end,int
duration_secs,long sample_rate, int bits_persample);
void AudioOutputCallback(
void* inUserData,
AudioQueueRef outAQ,
AudioQueueBufferRef outBuffer)
{
PlayState* playState = (PlayState*)inUserData;
if(!playState->playing)
{
printf("Not playing, returning\n");
return;
}
printf("Queuing buffer %d for playback\n", playState->currentPacket);
AudioStreamPacketDescription* packetDescs;
UInt32 bytesRead;
UInt32 numPackets = 8000;
OSStatus status;
status = AudioFileReadPackets(
playState->audioFile,
false,
&bytesRead,
packetDescs,
playState->currentPacket,
&numPackets,
outBuffer->mAudioData);
if(numPackets)
{
outBuffer->mAudioDataByteSize = bytesRead;
status = AudioQueueEnqueueBuffer(
playState->queue,
outBuffer,
0,
packetDescs);
playState->currentPacket += numPackets;
}
else
{
if(playState->playing)
{
AudioQueueStop(playState->queue, false);
AudioFileClose(playState->audioFile);
playState->playing = false;
}
AudioQueueFreeBuffer(playState->queue, outBuffer);
}
}
- (void) awakeFromNib {
[self startPlayback];
// makeChirp(1000, 10000, 1, 44100, 16);
}
- (void)setupAudioFormat:(AudioStreamBasicDescription*)format
{
format->mSampleRate = 44100.0;
format->mFormatID = kAudioFormatLinearPCM;
format->mFramesPerPacket = 1;
format->mChannelsPerFrame = 1;
format->mBytesPerFrame = 2;
format->mBytesPerPacket = 2;
format->mBitsPerChannel = 16;
format->mReserved = 0;
format->mFormatFlags = kLinearPCMFormatFlagIsBigEndian |
kLinearPCMFormatFlagIsSignedInteger |
kLinearPCMFormatFlagIsPacked;
}
- (void)startPlayback
{
playState.currentPacket = 0;
[self setupAudioFormat:&playState.dataFormat];
OSStatus status;
fUrl = [NSURL URLWithString:@"file:///Users/malcom/Desktop/file.pcm"];
CFURLRef fileURL = (CFURLRef)fUrl;
status = AudioFileOpenURL(fileURL, fsRdPerm, kAudioFileWAVEType,
&playState.audioFile);
if(status == 0)
{
status = AudioQueueNewOutput(
&playState.dataFormat,
AudioOutputCallback,
&playState,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes,
0,
&playState.queue);
if(status == 0)
{
playState.playing = true;
for(int i = 0; i < NUM_BUFFERS && playState.playing; i++)
{
if(playState.playing)
{
AudioQueueAllocateBuffer(playState.queue, 16000,
&playState.buffers[i]);
AudioOutputCallback(&playState, playState.queue,
playState.buffers[i]);
}
}
if(playState.playing)
{
status = AudioQueueStart(playState.queue, NULL);
if(status == 0)
{
NSLog(@"ok");
// labelStatus.text = @"Playing";
}
}
}
}
if(status != 0)
{
NSLog(@"failed");
// labelStatus.text = @"Play failed";
}
}
#define MIN_INT -32768
#define MAX_INT 32767
#define PI 3.141592653
void makeChirp(double freq_start,double freq_end,int
duration_secs,long sample_rate, int bits_persample) {
long len_array = (long)duration_secs*sample_rate; // this is the
number of samples to generate
short wavesamples[len_array]; // this is our array of samples
int i; for (i=0; i < len_array; i++) // some funky initializations...
wavesamples[i] = 0;
double k = (freq_end-freq_start)/len_array;
double freq = freq_start; // this is our time-to-time frequency value
double omega = (double)(PI / sample_rate);
long t;
for (t=0; t < len_array; t++) {
freq += k; // increase frequency over the time with the omega value
double c_sample = sin(omega*freq*t)*MAX_INT;
wavesamples[t] = (short)c_sample;
}
NSData *d = [[NSData alloc] initWithBytes: wavesamples length:len_array];
[d writeToFile: @"/Users/malcom/Desktop/file.pcm" atomically:YES];
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden