我试图让以下示例在Xamarin.Mac项目中工作。
它是一个AUGraph
,它将混音器连接到默认输出。混音器有一个输入,它是一个生成正弦波的渲染回调。
var graph = new AUGraph();
var output = graph.AddNode(AudioComponentDescription.CreateOutput(AudioTypeOutput.Default));
var mixer = graph.AddNode(AudioComponentDescription.CreateMixer(AudioTypeMixer.MultiChannel));
if (graph.TryOpen() != 0)
{
throw new Exception();
}
var mixNode = graph.GetNodeInfo(mixer);
// configure mixer
if (mixNode.SetElementCount(AudioUnitScopeType.Input, 1) != AudioUnitStatus.OK)
{
throw new Exception();
}
if (mixNode.SetRenderCallback(HandleRenderDelegate, AudioUnitScopeType.Global, 0) != AudioUnitStatus.OK)
{
throw new Exception();
}
var outNode = graph.GetNodeInfo(output);
// define stream description
var desc = new AudioStreamBasicDescription();
desc.BitsPerChannel = 32;
desc.BytesPerFrame = 4;
desc.BytesPerPacket = 4;
desc.Format = AudioFormatType.LinearPCM;
desc.FormatFlags = AudioStreamBasicDescription.AudioFormatFlagsAudioUnitNativeFloat;
desc.FramesPerPacket = 1;
desc.ChannelsPerFrame = 2;
desc.SampleRate = 44100;
// set mixer input format
if (mixNode.SetFormat(desc, AudioUnitScopeType.Input, 0) != AudioUnitStatus.OK)
{
throw new Exception();
}
// connect mixer's output to the output
if (graph.ConnnectNodeInput(mixer, 0, output, 0) != AUGraphError.OK)
{
throw new Exception();
}
// set format of mixer's output
desc = mixNode.GetAudioFormat(AudioUnitScopeType.Output);
desc.SampleRate = 44100;
if (outNode.SetFormat(desc, AudioUnitScopeType.Input,0) != AudioUnitStatus.OK)
{
throw new Exception();
}
if (mixNode.SetFormat(desc, AudioUnitScopeType.Output) != AudioUnitStatus.OK)
{
throw new Exception();
}
if (graph.Initialize() != AUGraphError.OK)
{
throw new Exception();
}
if (graph.Start() != AUGraphError.OK)
{
throw new Exception();
}
回调:
int sample = 0;
unsafe AudioUnitStatus HandleRenderDelegate(AudioUnitRenderActionFlags actionFlags, AudioTimeStamp timeStamp, uint busNumber, uint numberFrames, AudioBuffers data)
{
var left = (float*)data[0].Data;
var right = (float*)data[1].Data;
for (var i = 0; i < numberFrames; i++)
{
float sampleValue = (float)Math.Sin(sample * 2 * Math.PI * 440 / 44100);
left[i] = right[i] = sampleValue;
sample++;
}
return AudioUnitStatus.OK;
}
回调正在运行且缓冲区正在填充但未生成声音。该示例适用于iOS,AudioTypeOutput.Remote
代替AudioTypeOutput.Default
,但由于某种原因,macOS不播放声音。有什么想法吗?
答案 0 :(得分:0)
混音器的输出音量必须手动设置,因为默认为0。