Re: quick and dirty way to play some data?
Re: quick and dirty way to play some data?
- Subject: Re: quick and dirty way to play some data?
- From: Doug Wyatt <email@hidden>
- Date: Wed, 16 Feb 2005 09:51:39 -0800
On Feb 16, 2005, at 8:58, Ben Dougall wrote:
hello,
what's the quickest and easiest way (either c or obj-c, not c++ if
possible, preferably using coreaudio) to take some data (32bit floats
who's values are all between -1.0 and 1.0), call that data a sound,
probably have to provide some meta info details (probably Hz, number
of channels, length amongst maybe others) along with it and play it?
#include <AudioUnit/AudioUnit.h>
#include <unistd.h>
#define kNumChannels 2 // how many channels to render
// AudioUnit rendering proc to generate a sine wave
// this is NOT an efficient way to do this but does illustrate very
basic synthesis and
// how to use the render callback.
#include <math.h>
static const double kAmplitude = 0.25;
static const double kToneFrequency = 440.;
static double gFrameCount = 0.;
static Float64 gSampleRate = 44100.; // you might want to get
this from the AU instead
static OSStatus MyRenderer( void
*inRefCon,
AudioUnitRenderActionFlags
*inActionFlags,
const AudioTimeStamp
*inTimeStamp,
UInt32
inBusNumber,
UInt32
inNumFrames,
AudioBufferList *ioData)
{
Float32 *p0 = (Float32 *)ioData->mBuffers[0].mData;
Float32 *p1 = (Float32 *)ioData->mBuffers[1].mData;
double j = gFrameCount;
double cycleLength = gSampleRate / kToneFrequency;
for (UInt32 i = 0; i < inNumFrames; ++i) {
*p0++ = *p1++ = sin(j / cycleLength * (M_PI * 2.0)) *
kAmplitude;
j += 1.0;
if (j > cycleLength)
j -= cycleLength;
}
gFrameCount = j;
return noErr;
}
int main(int argc, char *argv[])
{
ComponentDescription compDesc;
Component comp;
AudioUnit outputAU;
AudioStreamBasicDescription streamDesc;
// open output unit
compDesc.componentType = kAudioUnitType_Output;
compDesc.componentSubType = kAudioUnitSubType_DefaultOutput;
compDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
compDesc.componentFlags = 0;
compDesc.componentFlagsMask = 0;
comp = FindNextComponent(NULL, &compDesc);
verify_noerr(OpenAComponent(comp, &outputAU));
// set stream format to stereo deinterleaved float32
streamDesc.mSampleRate = gSampleRate;
streamDesc.mFormatID = kAudioFormatLinearPCM;
streamDesc.mFormatFlags = kAudioFormatFlagsNativeFloatPacked |
kAudioFormatFlagIsNonInterleaved;
streamDesc.mFramesPerPacket = 1;
streamDesc.mBytesPerPacket = sizeof(Float32);
streamDesc.mBytesPerFrame = sizeof(Float32);
streamDesc.mChannelsPerFrame = kNumChannels;
streamDesc.mBitsPerChannel = 32;
verify_noerr(AudioUnitSetProperty(outputAU,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&streamDesc,
sizeof(streamDesc)));
// set input proc
AURenderCallbackStruct input;
input.inputProc = MyRenderer;
input.inputProcRefCon = NULL;
verify_noerr(AudioUnitSetProperty(outputAU,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Global,
0,
&input,
sizeof(input)));
// initialize
verify_noerr(AudioUnitInitialize(outputAU));
// start playing
verify_noerr(AudioOutputUnitStart(outputAU));
// sleep forever
while (1)
sleep(1);
}
_______________________________________________
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