Hi Roland, and others.
The standard clipping level is +/- 1.0, and that's what CoreAudio uses everywhere. As long as all of your samples have an absolute value of less than 1.0, you're not going to clip.
abs(x) < 1.0
However, the tricky part becomes what to do when to need to pick a scaling factor. It is perfectly legal, on nearly all devices, to have a sample value of -1.0 without clipping. This is due to signed integer formats when converted from float format for the DAC stage. While -1.0 is valid and will not clip, the same is not true of +1.0 and the tough thing is that it depends upon whether the output is 16-bit, 24-bit, or even something like 20-bit, if you want to find the exact maximum positive float value.
In many cases, depending upon how you generate your data, and the specifics of your output device, you'll need to consider your math carefully. 24-bit devices will be able to get closer to +1.0 without clipping, but if you generate these values as float and then play back on 16-bit or 20-bit hardware, you'll still get clipping on the positive excursion of the waveform.
I think the safest maximum would be something like:
#define CA_FLOAT_POS_MAX (32767./32768.)
That formula gives you the largest 16-bit value that will not clip. You will still clip on devices with less than 16-bit precision, but that's probably nearly nonexistent these days. You can do a similar calculation for 24-bit, if you can be certain that your output device will always be 24-bit.
Obviously, certain waveform generation algorithms will not need to be scaled in the same way, but these values should help. You can always just scan for potentially clipped values with (abs(x) >= 1.0) and sort them out later if you find any, adjusting your algorithm as appropriate.
P.S. If all you're going to do is manually clip the samples yourself, there is no need to do so. The device driver will clamp the samples for you. The only thing you should be concerned about is generating samples that are scaled so they fall within the range so you don't have to clamp them.
Brian Willoughby Sound Consulting
On Jan 11, 2008, at 20:24, Roland Silver wrote: If I use AudioQueue as the back end between my sound-generating program and the Mac sound output, and my program generates linear PCM data at 44.1 kHz with float format, is there a clipping level? Ie, a float value sigMax (considerably short of infinity) such that if I specify a signal with amplitude greater than sigMax, it won't be rendered properly?
|