gpt4 book ai didi

c++ - CoreAudio AudioUnitSetProperty 总是设置采样率失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:27:23 34 4
gpt4 key购买 nike

我需要将输出采样率从 44.1 更改为 32.0,但它总是会引发错误,Out: AudioUnitSetProperty-SF=\217\325\377\377, -10865。我不知道为什么它会让我将其设置为输入,但又不将其设置为输出。

我的代码是:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

OSStatus MyRenderer(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData){
NSLog(@"Running...");
ioData->mBuffers[0].mDataByteSize = 2048;
ioData->mBuffers[0].mData = lbuf;
ioData->mBuffers[0].mNumberChannels = 1;

return noErr;
}

void CreateDefaultAU(){
OSStatus err = noErr;

// Open the default output unit
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = 0;

AudioComponent comp = AudioComponentFindNext(NULL, &desc);
if (comp == NULL) { printf ("FindNextComponent\n"); return; }

err = AudioComponentInstanceNew(comp, &gOutputUnit);
if (comp == NULL) { printf ("OpenAComponent=%ld\n", err); return; }

// Set up a callback function to generate output to the output unit
AURenderCallbackStruct input;
input.inputProc = MyRenderer;
input.inputProcRefCon = NULL;

err = AudioUnitSetProperty(gOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &input, sizeof(input));

if (err) { printf ("AudioUnitSetProperty-CB=%ld\n", err); return; }

AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = 32000.00; // the sample rate of the audio stream
streamFormat.mFormatID = kAudioFormatLinearPCM; // the specific encoding type of audio stream
streamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger;//kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsNonMixable;
streamFormat.mFramesPerPacket = 1;
streamFormat.mChannelsPerFrame = 1;
streamFormat.mBitsPerChannel = 16;
streamFormat.mBytesPerPacket = 2;
streamFormat.mBytesPerFrame = 2;

err = AudioUnitSetProperty(gOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &streamFormat, sizeof(streamFormat));
if (err) { printf ("In: AudioUnitSetProperty-SF=%4.4s, %ld\n", (char*)&err, err); return; }

err = AudioUnitSetProperty(gOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &streamFormat, sizeof(streamFormat));
if (err) { printf ("Out: AudioUnitSetProperty-SF=%4.4s, %ld\n", (char*)&err, err); return; }
}

void TestDefaultAU(){
OSStatus err = noErr;

// Initialize unit
err = AudioUnitInitialize(gOutputUnit);
if (err) { printf ("AudioUnitInitialize=%ld\n", err); return; }

Float64 outSampleRate;
UInt32 size = sizeof(Float64);
err = AudioUnitGetProperty(gOutputUnit, kAudioUnitProperty_SampleRate, kAudioUnitScope_Output, 0, &outSampleRate, &size);

printf("Out srate %f\n",outSampleRate);
if (err) { printf ("AudioUnitSetProperty-GF=%4.4s, %ld\n", (char*)&err, err); return; }
AudioOutputUnitStart (gOutputUnit);
if (err) { printf ("AudioOutputUnitStart=%ld\n", err); return; }
AudioUnitReset (gOutputUnit, kAudioUnitScope_Input, 0);
}

最佳答案

使用 DefaultOuput AudioUnit,您只需将 AudioUnit 的输入端设置为您希望呈现的格式。设备的输出侧将匹配您在输入侧指定的内容,但您不能自行设置。

在设置输入流格式后试试这个,你会发现你已经准备好了......

Float64 outSampleRate = 0.0;
UInt32 size = sizeof(Float64);
AudioUnitGetProperty (gOutputUnit,
kAudioUnitProperty_SampleRate,
kAudioUnitScope_Output,
0,
&outSampleRate,
&size);
NSLog(@"Output sample rate is now at %f Hz", outSampleRate);

您还可以查看 Audio Unit Component Services Reference查看错误代码 -10865 是 kAudioUnitErr_PropertyNotWritable。

关于c++ - CoreAudio AudioUnitSetProperty 总是设置采样率失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2170523/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com