No problem, Ash.
Be aware that that solution is very slow - it'll use up a big chunk of your CPU doing all those scalar operations. You'll get much better performance using the vector processor.
Here's what I'm doing, using Accelerate:
1. Create a scratch buffer that's an adequately large number of frames long, using floats
malloc(sizeof(float) * kScratchBufferSize).
2. When you have some output you want to monitor, loop though the data in lengths of kScratchBufferSize (or not, if you make your scratch buffer big enough so that you don't have to worry about it - although maybe add an assert to be sure you're not going to overrun in case the hardware buffer duration changes.
3. Convert the block into float: If it's 16-bit integer:
vDSP_vflt16(buffer->mBuffers[i].mData, 1, scratchBuffer, 1, numberOfFrames).
If it's 32-bit integer, you can use vDSP_vflt32. If it's already float, you don't have to worry about that, or the scratch buffer, for that matter.
4. Determine the max value:
float peak = 0.0; vDSP_maxmgv(scratchBuffer, 1, &peak, numberOfFrames); if ( peak > _monitorRecord->peak ) _monitorRecord->peak = peak;
5. Determine the average:
float avg = 0.0; vDSP_meamgv(scratchBuffer, 1, &avg, numberOfFrames); _monitorRecord->meanAccumulator += avg; _monitorRecord->meanAccumulator->meanBlockCount++; _monitorRecord->average = _monitorRecord->meanAccumulator / _monitorRecord->meanBlockCount;
6. When queried, convert the peak/average into dB:
averagePower = 10.0 * log10((double)_monitorRecord->average / (audioDescription.mBitsPerChannel == 16 ? INT16_MAX : INT32_MAX));
peakLevel = 10.0 * log10((double)_monitorRecord->peak / (audioDescription.mBitsPerChannel == 16 ? INT16_MAX : INT32_MAX));
7. After querying, reset the values (the most thread-safe way to do this is to set a "reset" flag which causes the monitoring code to reset when it sees it).
Thanks for your help Michael. Glad to hear that someone else has had the issue.
I'll take a look at the accelerate framework but in the meantime I found a solution that works well for me.
Cheers,
Ash
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
|