我在AUGraph上呆了一段时间,如果我的问题得到解决,我将不胜感激。现在正在尝试播放来自UDP的数据(字节)。我已经成功实现了如何使用AUGraph播放数据,但是我不知道如何更改其播放速度。
我当前的情况是从UDP获取数据,并将其传递给转换器-> newTimePitch->转换器-> ioUnit。
转换器:转换器,将48000的ASBD转换为timePitch所需的格式,然后再次将其转换回48000以从ioUnit播放。
IOUnit目前,为了演示,我已删除UDP并通过使用2种不同的ioUnit(一个用于录制,另一个用于播放)来还原播放。
CircularBuffer在Recording回调中,我将数据推送到循环缓冲区中,在回放回调中,我从中弹出数据,并从[[memcpy弹出数据到我的[[ioData
不要被看到的代码所淹没。这很简单。 启动AUGraph并记录AudioUnit。private func startRecordingUnit()
{
check(error: AUGraphStart(graph!), description: "Failed to start AUGraph")
check(error: AudioOutputUnitStart(audioUnit!), description: "Failed to start recording
audio unit.")
}
设置AUGraph
private func setupRecordingUnit(){
var description = AudioComponentDescription(
componentType: OSType(kAudioUnitType_Output),
componentSubType: OSType(kAudioUnitSubType_VoiceProcessingIO),
componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
componentFlags: 0,
componentFlagsMask: 0
)
let inputComponent = AudioComponentFindNext(nil, &description)
check(error: AudioComponentInstanceNew(inputComponent!, &audioUnit), description:
"Failed to init recording audio unit.")
check(error: AudioUnitSetProperty(
audioUnit!,
AudioUnitPropertyID(kAudioOutputUnitProperty_EnableIO),
AudioUnitScope(kAudioUnitScope_Input),
kInputBus,
&flag,
MemoryLayoutStride.SizeOf32(flag)
),
description: "Failed to set enable IO for recording."
)
check(error: AudioUnitSetProperty(
audioUnit!,
AudioUnitPropertyID(kAudioUnitProperty_StreamFormat),
AudioUnitScope(kAudioUnitScope_Output),
kInputBus,
&ioFormat!,
MemoryLayoutStride.SizeOf32(ioFormat)
),
description: "Failed to set stream format on output unit with scope input."
)
check(error: NewAUGraph(&graph), description: "Failed to create AU Graph")
check(error: AUGraphOpen(graph!), description: "Failed to open AUGraph")
var outputDesc = AudioComponentDescription(
componentType: OSType(kAudioUnitType_Output),
componentSubType: OSType(kAudioUnitSubType_VoiceProcessingIO),
componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
componentFlags: 0,
componentFlagsMask: 0
)
var firstConverterDesc = AudioComponentDescription(
componentType: OSType(kAudioUnitType_FormatConverter),
componentSubType: OSType(kAudioUnitSubType_AUConverter),
componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
componentFlags: 0,
componentFlagsMask: 0
)
var pitchConverterDesc = AudioComponentDescription(
componentType: OSType(kAudioUnitType_FormatConverter),
componentSubType: OSType(kAudioUnitSubType_NewTimePitch),
componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
componentFlags: 0,
componentFlagsMask: 0
)
var secondConverterDesc = AudioComponentDescription(
componentType: OSType(kAudioUnitType_FormatConverter),
componentSubType: OSType(kAudioUnitSubType_AUConverter),
componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
componentFlags: 0,
componentFlagsMask: 0
)
check(error: AUGraphAddNode(graph!, &firstConverterDesc, &firstConverterNode),
description: "Failed to Add Node of First desc")
check(error: AUGraphAddNode(graph!, &pitchConverterDesc, &newTimePitchNode),
description: "Failed to Add Node of new Time Desc")
check(error: AUGraphAddNode(graph!, &secondConverterDesc, &secondConverterNode),
description: "Failed to Add Node of Second desc")
check(error: AUGraphAddNode(graph!, &outputDesc, &outputNode), description: "Failed to
add node of output desc")
check(error: AUGraphNodeInfo(graph!, firstConverterNode, nil, &firstConverterUnit),
description: "Failed to get Node Info of FIRST Unit")
check(error: AUGraphNodeInfo(graph!, newTimePitchNode, nil, &newTimePitchUnit),
description: "Failed to get Node Info of new Time Unit")
check(error: AUGraphNodeInfo(graph!, secondConverterNode, nil, &secondConverterUnit),
description: "Failed to get Node Info of SECOND Unit")
check(error: AUGraphNodeInfo(graph!, outputNode, nil, &outputUnit), description: "Failed
to get Node Info of output Unit");
var sizeASBD = MemoryLayoutStride.SizeOf32(AudioStreamBasicDescription())
var ioASBDin = AudioStreamBasicDescription()
var ioASBDout = AudioStreamBasicDescription()
AudioUnitGetProperty(newTimePitchUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, kOutputBus, &ioASBDin, &sizeASBD);
AudioUnitGetProperty(newTimePitchUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, kOutputBus, &ioASBDout, &sizeASBD);
check(error: AudioUnitSetProperty(firstConverterUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, kOutputBus, &ioFormat, MemoryLayoutStride.SizeOf32(ioFormat)),
description: "Failed to set property of FIRST Unit")
check(error: AudioUnitSetProperty(firstConverterUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, kOutputBus, &ioASBDin, MemoryLayoutStride.SizeOf32(ioASBDin)),
description: "Failed to set property first unit to temp format")
check(error: AudioUnitSetProperty(newTimePitchUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, kOutputBus, &ioASBDin, MemoryLayoutStride.SizeOf32(ioASBDin)),
description: "Failed to set property of new Time Pitch")
check(error: AudioUnitSetProperty(newTimePitchUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, kOutputBus, &ioASBDout, MemoryLayoutStride.SizeOf32(ioASBDout)),
description: "Failed to set property of new Time Pitch OUTOUT")
check(error: AudioUnitSetProperty(secondConverterUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, kOutputBus, &ioASBDout, MemoryLayoutStride.SizeOf32(ioASBDout)),
description: "Failed to set property of Second Converter Unit")
check(error: AudioUnitSetProperty(secondConverterUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, kOutputBus, &ioFormat, MemoryLayoutStride.SizeOf32(ioFormat)),
description: "Failed to set property of Second Converter Unit to io Format")
check(error: AudioUnitSetProperty(outputUnit!, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, kOutputBus, &ioFormat, MemoryLayoutStride.SizeOf32(ioFormat)),
description: "Failed to set property of OUTPUT Unit")
// ************************************* RECORDING
****************************************************
var recordingCallback = AURenderCallbackStruct(
inputProc: AudioController_RecordingCallback,
inputProcRefCon: UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
)
check(error: AudioUnitSetProperty(
audioUnit!,
AudioUnitPropertyID(kAudioOutputUnitProperty_SetInputCallback),
AudioUnitScope(kAudioUnitScope_Global),
kInputBus,
&recordingCallback,
MemoryLayout<AURenderCallbackStruct>.size.ui
),
description: "Failed to set property on recording callback."
)
// *************************************** RECORDING END
****************************************************
var playbackCallback = AURenderCallbackStruct(
inputProc: AudioController_PlaybackCallback,
inputProcRefCon: UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
)
check(error: AUGraphConnectNodeInput(graph!, firstConverterNode, 0, newTimePitchNode,
0), description: "Failed to connect FIRST AND timePitch Nodes")
check(error: AUGraphConnectNodeInput(graph!, newTimePitchNode, 0, secondConverterNode,
0), description: "Failed to connect timePitch AND Second Nodes")
check(error: AUGraphConnectNodeInput(graph!, secondConverterNode, 0, outputNode, 0),
description: "Failed to connect Second AND OUTPUT Nodes")
check(error: AudioUnitSetProperty(outputUnit!, kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Output, kOutputBus, &playbackCallback,
MemoryLayout<AURenderCallbackStruct>.size.ui), description: "Failed to set Property for
varispeed Unit 1")
check(error: AUGraphInitialize(graph!), description: "Unable to initialize AUGraph")
}
录制工作正常,但是在我将其与AUConvertor
连接后,无法运行playbackCalback。
AUGraphConnectNodeInput
,并开始播放。我认为连接有问题,如果我了解导致问题的原因,我将不胜感激。https://stackoverflow.com/users/1313967/dave234