Sound samples from Float32 to SInt16 lead to severe clipping

I am new to iOS and its C basics, but not for programming in general. My dilemma is this. I implement the echo effect in a complex application based on AudioUnits. Among other things, the application requires reverb, echo and compression. However, echo only works when I use the specific AudioStreamBasicDescription format for audio samples generated in my application. However, this format does not work with other AudioUnits. Although there are other ways to solve this problem, fixing bit-twiddling in the echo algorithm may be the most direct way.

* AudioStreamBasicDescription *, which works with echo, has mFormatFlag of: kAudioFormatFlagsAudioUnitCanonical; These are the features:

AudioUnit Stream Format (ECHO works, NO AUDIO UNITS)
Sample Rate:              44100
Format ID:                 lpcm
Format Flags:              3116 = kAudioFormatFlagsAudioUnitCanonical
Bytes per Packet:             4
Frames per Packet:            1
Bytes per Frame:              4
Channels per Frame:           2
Bits per Channel:            32
Set ASBD on input
Set ASBD on  output
au SampleRate rate: 0.000000, 2 channels, 12 formatflags, 1819304813 mFormatID, 16 bits per channel

The stream format that works with AudioUnits is the same except for mFormatFlag : kAudioFormatFlagIsFloat | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved - Its specifics:

AudioUnit Stream Format (NO ECHO, AUDIO UNITS WORK)
Sample Rate:              44100
Format ID:                 lpcm
Format Flags:                41 
Bytes per Packet:             4
Frames per Packet:            1
Bytes per Frame:              4
Channels per Frame:           2
Bits per Channel:            32
Set ASBD on input
Set ASBD on  output
au SampleRate rate: 44100.000000, 2 channels, 41 formatflags, 1819304813 mFormatID, 32 bits per channel

, , SInt16, . , kAudioFormatFlagsAudioUnitCanonical, . , , . , , , Float32.

// convert sample vector from fixed point 8.24 to SInt16
void fixedPointToSInt16( SInt32 * source, SInt16 * target, int length ) {
    int i;
    for(i = 0;i < length; i++ ) {
        target[i] =  (SInt16) (source[i] >> 9);
        //target[i] *= 0.003;

    }
}

* , , - , .

// convert sample vector from SInt16 to fixed point 8.24 
void SInt16ToFixedPoint( SInt16 * source, SInt32 * target, int length ) {
    int i;
    for(i = 0;i < length; i++ ) {
        target[i] =  (SInt32) (source[i] << 9);
        if(source[i] < 0) { 
            target[i] |= 0xFF000000;
        }
        else {
            target[i] &= 0x00FFFFFF;
        }
    }
}

kAudioFormatFlagIsFloat | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved, . , . CoreAudio , , CoreAudioTypes.h, mFormatFlag (s) 8.24 . , - , , .

, , , .

+5
1

kAudioFormatFlagIsFloat , . mBitsPerChannel 32, float ( Float32), 64, double.

kAudioFormatFlagsNativeEndian , , .

kAudioFormatFlagIsPacked , . , 24- 32 , .

kAudioFormatFlagIsNonInterleaved , . , L R: LRLRLRLR. DSP .

, , . [-1, +1). float SInt16, 16- (1u << 15, 32768), [-32768, 32767].

+11

All Articles