我正在尝试使用AudioStreamBasicDescription在AudioUnit中设置属性。我正在尝试设置声音数据的格式。返回的错误代码是-10868。它仅发生在iOS 3.1.3或更低版本上,但适用于3.2或更高版本。因此它可以在模拟器中运行,在运行4.2的iPod touch上运行,但不适用于第一代iPod touch。
当我尝试设置AudioUnit音调单元的格式时发生错误。代码看起来像这样(来自cocoawithlove.com)
// Set the format to 32 bit, single channel, floating point, linear PCM
const int four_bytes_per_float = 4;
const int eight_bits_per_byte = 8;
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = sampleRate;
streamFormat.mFormatID = kAudioFormatLinearPCM;
streamFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved;
streamFormat.mBytesPerPacket = four_bytes_per_float;
streamFormat.mFramesPerPacket = 1;
streamFormat.mBytesPerFrame = four_bytes_per_float;
streamFormat.mChannelsPerFrame = 1;
streamFormat.mBitsPerChannel = four_bytes_per_float * eight_bits_per_byte;
err = AudioUnitSetProperty (toneUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&streamFormat,
sizeof(AudioStreamBasicDescription));
// err now has error code -10868
我在标题中检查过,此错误代码对应于错误“kAudioUnitErr_FormatNotSupported”。所以我想iOS 3.1上不支持我试图设置的格式?我在哪里可以找到支持的格式?我可以尝试其他格式的指针吗?
使用渲染回调:
OSStatus RenderTone(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
// Fixed amplitude is good enough for our purposes
const double amplitude = 0.25;
// Get the tone parameters out of the view controller
ToneGeneratorViewController *viewController =
(ToneGeneratorViewController *)inRefCon;
double theta = viewController->theta;
double theta_increment =
2.0 * M_PI * viewController->frequency / viewController->sampleRate;
// This is a mono tone generator so we only need the first buffer
const int channel = 0;
Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;
// Generate the samples
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
buffer[frame] = sin(theta) * amplitude;
theta += theta_increment;
if (theta > 2.0 * M_PI)
{
theta -= 2.0 * M_PI;
}
}
// Store the updated theta back in the view controller
viewController->theta = theta;
return noErr;
}
答案 0 :(得分:5)
我认为主要问题是版本4.0之前的iOS上不支持浮点音频。我不确定ARM浮点支持是什么,但我建议使用其中一种固定点格式进行输入和输出。