ADC Home > Reference Library > Technical Q&As > QuickTime > Movie Creation >

Sequence Grabber - Using the SGDataProc for Sound


Q: I'm currently working with the SGDataProcSample and I'd like to know if it's possible to use the same technique with a Sound SGChannel to process audio as it's being captured just as the sample does for a Video SGChannel.

A: Yes, you certainly can. When capturing video and sound, the SGChannel parameter passed to the SGDataProc will either be the Video SGChannel, or the Sound SGChannel. You can check which of these was passed each time through the callback and proceed accordingly.

When you register your SGDataProc callback, you are handed the buffers just as they will be written to disk. By default, the Sound SGChannel (SoundMediaType) will internally buffer its audio data in approximately 1 second increments. This means your SGDataProc will be called approximately once a second with audio data. You can alter this behavior by calling SGSetSoundRecordChunkSize and set the seconds per chunk parameter to a higher or lower value depending on your needs.

You will also need to be aware of the writeType parameter passed to your SGDataProc. When working with a Video SGChannel, the writeType parameter will always equal seqGrabWriteAppend. However, this is not the case for sound. The Sound SGChannel writes data differently that the Video SGChannel, it first reserves a block, and then writes a block during the next SGIdle call. This means that when writeType equals seqGrabWriteReserve, the pointer (Ptr p) passed to your SGDataProc should be disregarded and you can return immediately without doing any work. When writeType equals seqGrabWriteFill, the data can be processed.

Note:
SGSetSoundRecordChunkSize takes a long for "number of seconds per chunk". If you want to record chunks in durations shorter than one second, you must pass in a negative Fixed point value (rather than an SInt32). For example, if you want 0.5 second chunks, you should pass 0xFFFF8000 (-0.5 in Fixed point).

Remember that the Sound SGChannel uses the Sound Manager to acquire its audio data and the data will always be presented in interleaved buffers in whatever format was selected using an API such as SGSettingsDialog. The default format is 16-bit twos complement ('twos' - see note below) and is designated by the "NONE" compression type. If some other format such as 32-bit float or 24-bit integer was chosen, the data presented in the DataProc will be different. If a compressed format such as QDesign was chosen, the data will already have been compressed into packets.

If you would like to process the data in some interesting way, maybe by accessing the left and right channel separately or by passing the data to some AudioUnit, you will always need to de-interleave the data yourself. This could be done using an AudioConverter (see AudioConverter.h). In this case you're much better off grabbing from the Sound SGChannel using the default 'twos' format, then letting the conversion happen once. This can be done by using a single AudioConverter and converting from 16-bit signed integer interleaved, to 32-bit Float deinterleaved which can then be consumed by Core Audio.

As mentioned above, SGSetSoundRecordChunkSize can be used to get lower latency by setting the seconds parameter to a lower value. The SGDataProc will then be passed smaller chunks of audio data more frequently.

Note:
The default capture format is 16-bit twos complement, the samples range from a minimum value of 0x8000 (-32768) to a maximum value 0x7fff (32767).


References:


[Apr 06, 2004]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.