1

Edited the question due to progressive insights :-)

I am creating an app that is listening to the audio input. I want it to count peaks. (peaks will be at a max frequency of about 10 Hz.)

After a lot of searching, I ended up using the AudioQueue Service as that will be able to give me the raw input data. I am using a stripped down version (no playback) of the SpeakHere example, but instead of simply writing the buffer to the filesystem, I want to look at the individual sample data.

Think I am on the right track now, but I don't understand how to work with the buffers. I am trying to isolate the data of one sample. So that for loop in the following function, does that make any sense, and what should I put in there to get one sample?

void AQRecorder::MyInputBufferHandler( void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp *inStartTime, UInt32 inNumPackets, const AudioStreamPacketDescription* inPacketDesc)
{
    // AudioQueue callback function, called when an input buffers has been filled.

    AQRecorder *aqr = (AQRecorder *)inUserData;
    try {
        if (inNumPackets > 0) {
            /*          // write packets to file
            XThrowIfError(AudioFileWritePackets(aqr->mRecordFile,FALSE,inBuffer->mAudioDataByteSize,inPacketDesc,aqr->mRecordPacket,&inNumPackets,inBuffer->mAudioData),
                      "AudioFileWritePackets failed");*/

            SInt16 sample;
        for (UInt32 sampleIndex=0; sampleIndex < inNumPackets; ++sampleIndex) {


            // What do I put here to look at one sample at index sampleIndex ??


        }
        aqr->mRecordPacket += inNumPackets;
        }

    // if we're not stopping, re-enqueue the buffe so that it gets filled again
    if (aqr->IsRunning())
        XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL),
                      "AudioQueueEnqueueBuffer failed");
    } catch (CAXException e) {
    char buf[256];
    fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
}

(maybe I shouldn't have deleted so much of the original question... what is the policy?)

Originally I was thinking of using the AurioTouch example, but as was pointed out in a comment, that uses throughput and I only need input. It is also a much more complicated example than SpeakHere.

2 Answers2

0

you would probably want to apply some sort of smoothing to your peak power level, maybe am IIR filter, something like:

x_out = 0.9 * x_old + 0.1 * x_in;
:
x_old = x_out;

I haven't used this feature, so I don't know if it would do everything you want. if it doesn't, you can drop a level and use a RemoteIO audio unit, and catch sound as it comes in using the 'input callback' ( as opposed to the render callback which happens when the speakers are hungry for data )

note that in the input callback you have to create your own buffers, don't think just because you get a buffer pointer as the last parameter that that means it points to something valid. it doesn't.

anyway, you could use some vDSP function to get the magnitude squared for the vector of the entire buffer (1024 floats or whatever your buffer size / stream format is)

and then you could smooth that yourself

P i
  • 29,020
  • 36
  • 159
  • 267
  • It seems like I will be using a RemoteIO audio unit indeed. I am thinking of "reverse engineering" the AurioTouch sample code and using the data from the oscilloscope. I think with that data I can find the peaks I am looking for. – Joris van Liempd iDeveloper Jun 25 '11 at 08:03
  • What do you mean the buffer pointers don't point to something valid? That sounds alarming. I assume using AurioTouch as my starting point will point out how these buffers work... I was planning to get a block of samples and determine the average (absolute) power for this block. With the right block size I think I should be able to find peaks. thanks for helping – Joris van Liempd iDeveloper Jun 25 '11 at 08:11
  • http://stackoverflow.com/questions/6039291/simplest-way-to-capture-raw-audio-from-audio-input-for-real-time-processing-on-a/6468960#6468960 – P i Jun 25 '11 at 13:03
  • Note that AurioTouch actually implements passThru, so it uses the RENDER-callback. if you just want to listen, use INPUT-callback instead. IIRC there is some bug in AurioTouch which stops passthru from working, But the basic technique is sound -- inside the render callback it grabs data from the microphone buffer (if there is any waiting, the first pass there won't be anything waiting so watch out for that). for the RENDER callback, buffers preexist. the callback just demands that they be filled. but for the INPUT callback you need to make your own. – P i Jun 25 '11 at 13:11
0

This loops through all samples in the buffer.

    SInt16 sample;
    for (UInt32 sampleIndex=0; sampleIndex < inNumPackets; ++sampleIndex) {
        sample = buffer[sampleIndex]; // Get the power of one sample from the buffer
        aqr->AnalyseSample(sample);
    }

Was a tricky part: aqr points to the instance of the recorder. The callback is a static function and can't access the member variables or member functions directly.

In order to count the peaks, I keep track of a longterm average and a shortterm average. If the shortTerm average is a certain factor bigger than the longterm average, there is a peak. When the shortterm average goes down again, the peak has passed.