How to manipulate AudioBufferList / AudioBuffer's mData?
How to manipulate AudioBufferList / AudioBuffer's mData?
- Subject: How to manipulate AudioBufferList / AudioBuffer's mData?
- From: Dave Della Costa <email@hidden>
- Date: Thu, 31 Jan 2008 18:54:27 -0500
Hi folks,
The subject line is a bit misleading. There is a whole host of
confusion going on in my head right now that I'm hoping you can give me
a hand with. Thank you in advance!
Let me say that my C++ is not what it could (will) be...so please
forgive me for any obvious errors that an experienced coder wouldn't
make. While I've got plenty of other programming experience, memory
management and pointer manipulation are coming a bit more slowly to me.
First of all, I'm writing an AU with a Cocoa View and I'm doing some FFT
processing to boot. I've taken the CASpectralProcessor class that is
provided in Public Utility and I've taken some code as well from the
SonogramViewDemo project.
Right now, I'm overriding the Render() function (method? whatever) of
the AudioUnit class, as is done in SonogramViewDemo. Here's what I've got:
ComponentResult AtomProto::Render( AudioUnitRenderActionFlags
&ioActionFlags,
const AudioTimeStamp & inTimeStamp,
UInt32 inFramesToProcess )
{
UInt32 actionFlags = 0;
ComponentResult err = PullInput(0, actionFlags, inTimeStamp,
inFramesToProcess);
if (err) return err;
AUInputElement* inputBus = GetInput(0);
AUOutputElement* outputBus = GetOutput(0);
outputBus->PrepareBuffer(inFramesToProcess); // prepare the output
buffer list
AudioBufferList& inputBufList = inputBus->GetBufferList();
if (
mSpectralProcessor->ProcessForwards(inFramesToProcess, &inputBufList)
){
mMinMag = 0.0; mMaxMag = 0.0;
// This AudioBufferList will hold buffers of amplitude (magnitude)?
AudioBufferList* sdBufferList =
&mSpectralDataBufferList->GetModifiableBufferList();
mSpectralProcessor->GetMagnitude(sdBufferList, mMinMag, mMaxMag);
// Here we set the FrequencyValue dynamically:
for (UInt32 i=0; i<mNumBins; ++i) {
// Crashes AU...why, 'cause topFreq isn't malloc'ed correctly?
// *topFreq = (Float32) sdBufferList->mBuffers[i].mData;
}
// Set it!
SetParameter(kParam_FrequencyValue, mMaxMag);
// Notification for Cocoa View
AudioUnitParameter freshParam;
memset(&freshParam, 0, sizeof(freshParam));
freshParam.mAudioUnit = GetComponentInstance();
freshParam.mParameterID = kParam_FrequencyValue;
freshParam.mScope = kAudioUnitScope_Global;
freshParam.mElement = 0;
AUParameterListenerNotify(NULL, NULL, &freshParam);
}
return AUEffectBase::Render(ioActionFlags, inTimeStamp, inFramesToProcess);
}
(If you know SonogramViewDemo you'll see that much of this is the same).
I tried at first to create, allocate memory for, and destroy the
Float32* topFreq (see inside the loop) but that seemed to crash the AU
every time; so it worked when I allocated and destroyed it outside of
the Render() function. This is in the Initialize() function now:
topFreq = (Float32 *)malloc (sizeof (Float32));
...and I'm freeing that memory in the Cleanup() function. However, it
seems to me that I've got something pretty wrong here since I really
don't have any idea what is in the AudioBuffer mData; I was assuming at
first it was a Float or Int as that is what I get in the mMinAmp and
mMaxAmp of the GetMagnitude() function of CASpectralProcessor, but I
guess not. I would imagine to do this properly I would need to allocate
as much memory as is in the AudioBuffer->mDataByteSize member, right, or no?
What I am trying to do, basically, is find the frequency of the given
FFT chunk based on what has the greatest magnitude, and print that to
the Cocoa View. I thought that sdBufferList would contain an array of
magnitude values after passing it into GetMagnitude(), based on my
(admittedly poor) comprehension of CASpectralProcessor, but I don't
understand how to get at those magnitudes. I would imagine it shouldn't
be hard to match the indexes of the array of magnitudes up to the array
of frequencies returned by GetFrequencies() once I get that, is this
correct? Of course there is a lot I'm trying to figure out about FFTs
at the same time...
Anyways, help set this DSP noob straight...please!
Cheers,
Dave
_______________________________________________
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