如何清除或不设置kLinearPCMFormatFlagIsNonInterleaved

时间:2012-03-15 02:12:46

标签: ios core-audio

在toneRender中,我在频率上填充一个音调,在频率/ 3处填充另一个音调。

当我运行代码时,看起来好像输出被读取,因为缓冲区没有交错。实际上它是在createToneUnit中设置的。 声音仅在左扬声器中播放。当两个频率都写入缓冲器时,两个音调都在左扬声器中播放。当频率没有写入缓冲器时,例如leftON = 0,他们没有被播放。所以缓冲区编写代码似乎没问题。

由于我怀疑我不应该在createToneUnit中设置kLinearPCMFormatFlagIsNonInterleaved,所以我试图“清除”该标志。我读了几个小时的文件,但从来没有找到办法做到这一点。试验只会导致应用启动时崩溃。

如何清除kLinearPCMFormatFlagIsNonInterleaved?

或者我怎么能不首先设置kLinearPCMFormatFlagIsNonInterleaved? (注释掉streamFormat.mFormatFlags也会造成崩溃。)

可能某些其他设置会影响创建交错播放。

OSStatus RenderTone(
                void *inRefCon, 
                AudioUnitRenderActionFlags  *ioActionFlags, 
                const AudioTimeStamp        *inTimeStamp, 
                UInt32                      inBusNumber, 
                UInt32                      inNumberFrames,
                AudioBufferList             *ioData)
{        
    *)inRefCon;
    float sampleRate = viewController->sampleRate;
    float frequency = viewController->frequency;
    // etc.

    float theta_increment = 2.0 * M_PI * frequency /sampleRate;

    float wave;       
    float theta2;
    float wave2;

    float theta_increment2 =0.3 * theta_increment;

    const int channel = 0;
    Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;

    for (UInt32 frame = 0; frame < inNumberFrames;) 
    {                
            theta += theta_increment;
            wave = sin(theta) * playVolume;

            theta2 += theta_increment2;
            wave2 = sin(theta2) * playVolume;

            buffer[frame++] = wave * leftON;    // leftON = 1 or 0
            buffer[frame++] = wave2 * rightON;  // rightON = 1 or 0

            if (theta > 2.0 * M_PI)
            {
                    theta -= 2.0 * M_PI;
            }
    }

    // etc.
}


- (void)createToneUnit
{
    AudioComponentDescription defaultOutputDescription;
    defaultOutputDescription.componentType = kAudioUnitType_Output;
    defaultOutputDescription.componentSubType = kAudioUnitSubType_RemoteIO;
    defaultOutputDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
    defaultOutputDescription.componentFlags = 0;
    defaultOutputDescription.componentFlagsMask = 0;

    // etc.
                               kAudioUnitProperty_SetRenderCallback,
                               kAudioUnitScope_Input,
                               0,
                               &input,
                               sizeof(input));

    const int four_bytes_per_float = 4;
    const int eight_bits_per_byte = 8;

    AudioStreamBasicDescription streamFormat;
    streamFormat.mSampleRate = sampleRate;
    streamFormat.mFormatID = kAudioFormatLinearPCM;
    streamFormat.mFormatFlags =
    kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsNonInterleaved;
    streamFormat.mBytesPerPacket = four_bytes_per_float;
    streamFormat.mFramesPerPacket = 1;   
    streamFormat.mBytesPerFrame = four_bytes_per_float;       
    streamFormat.mChannelsPerFrame = 2;   // 2= stereo /
    streamFormat.mBitsPerChannel = four_bytes_per_float * eight_bits_per_byte;
    err = AudioUnitSetProperty (toneUnit,
                                kAudioUnitProperty_StreamFormat,
                                kAudioUnitScope_Input,
                                0,
                                &streamFormat,
                                sizeof(AudioStreamBasicDescription));
}

1 个答案:

答案 0 :(得分:1)

在您的情况下,您可以通过执行以下操作来设置标志:

streamFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;

因此不会将另一个标志位或计入标志 (查找C位运算符及其工作原理)。