Swift3 AudioToolbox: PCM playback how to AudioQueu

2019-08-17 19:58发布

I am following https://github.com/AlesTsurko/LearningCoreAudioWithSwift2.0/tree/master/CH05_Player to playback a frequency but it is with Swift2. Get microphone input using Audio Queue in Swift 3 has resolved many of the issues but it is for recording.

I am stuck at allocating a buffer to audio queue

var ringBuffers = [AudioQueueBufferRef](repeating:nil, count:3)
AudioQueueAllocateBuffer(inQueue!, bufferSize, &ringBuffers[0])

It gives an error

main.swift:152:29: Expression type '[AudioQueueBufferRef]' is ambiguous without more context
main.swift:153:20: Cannot pass immutable value as inout argument: implicit conversion from 'AudioQueueBufferRef' to 'AudioQueueBufferRef?' requires a temporary

--After Spads' answer--

var ringBuffers = [AudioQueueBufferRef?](repeating:nil, count:3)
let status = AudioQueueAllocateBuffer(inQueue!, bufferSize, &ringBuffers[0])
print("\(status.description)")

prints

vm_map failed: 0x4 ((os/kern) invalid argument)
4

audio description I have used is

inFormat = AudioStreamBasicDescription(
            mSampleRate:        Double(sampleRate),
            mFormatID:          kAudioFormatLinearPCM,
            mFormatFlags:       kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
            mBytesPerPacket:    UInt32(numChannels * MemoryLayout<UInt16>.size),
            mFramesPerPacket:   1,
            mBytesPerFrame:     UInt32(numChannels * MemoryLayout<UInt16>.size),
            mChannelsPerFrame:  UInt32(numChannels),
            mBitsPerChannel:    UInt32(8 * (MemoryLayout<UInt16>.size)),
            mReserved:          UInt32(0)
        )
AudioQueueNewOutput(&inFormat, AQOutputCallback, &player, nil, nil, 0, &inQueue)

1条回答
我只想做你的唯一
2楼-- · 2019-08-17 20:29

Should you not have an array of AudioQueueBufferRef? instead of AudioQueueBufferRef

var ringBuffers = [AudioQueueBufferRef?](repeating:nil, count:3)
AudioQueueAllocateBuffer(inQueue!, bufferSize, &ringBuffers[0])
查看更多
登录 后发表回答