AURenderCallback 中的 UnsafeMutablePointer

UnsafeMutablePointer in AURenderCallback

我不明白,我应该如何在Swift中使用UnsafeMutablePointer 3。尤其是在AURenderCallback中。

我试试下面的代码:

import Foundation
import AudioToolbox

let sineFrequency = 880.0

// MARK: User data struct
struct SineWavePlayer {
    var outputUnit: AudioUnit? = nil
    var startingFrameCount: Double = 0
}

// MARK: Callback function
let SineWaveRenderProc: AURenderCallback = {(inRefCon, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData) -> OSStatus in

    var player = UnsafeMutablePointer<SineWavePlayer>(inRefCon)


    var j = player.pointee.startingFrameCount
    let cycleLength = 44100 / sineFrequency

    for frame in 0..<inNumberFrames {
        var buffers = UnsafeMutableAudioBufferListPointer(ioData)

        UnsafeMutablePointer<Float32>(buffers[0].mData)[Int(frame)] = Float32(sin(2 * M_PI * (j / cycleLength)))
        UnsafeMutablePointer<Float32>(buffers[1].mData)[Int(frame)] = Float32(sin(2 * M_PI * (j / cycleLength)))

        // Or iterate through array:
//        for buffer in buffers {
//            UnsafeMutablePointer<Float32>(buffer.mData)[Int(frame)] = Float32(sin(2 * M_PI * (j / cycleLength)))
//        }

        j++
        if j > cycleLength {
            j -= cycleLength
        }
    }

    player.pointee.startingFrameCount = j
    return noErr
}

// MARK: Utility function
func CheckError(_ error: OSStatus, operation: String) {
    guard error != noErr else {
        return
    }

    var result: String = ""
    var char = Int(error.bigEndian)

    for _ in 0..<4 {
        guard isprint(Int32(char&255)) == 1 else {
            result = "\(error)"
            break
        }
        result.append(String(describing: UnicodeScalar(char&255)))
        char = char/256
    }

    print("Error: \(operation) (\(result))")

    exit(1)
}

func CreateAndConnectOutputUnit(_ player: inout SineWavePlayer) {
    // Generate a description that matches the output device (speakers)
    var outputcd = AudioComponentDescription(componentType: kAudioUnitType_Output, componentSubType: kAudioUnitSubType_DefaultOutput, componentManufacturer: kAudioUnitManufacturer_Apple, componentFlags: 0, componentFlagsMask: 0)

    let comp = AudioComponentFindNext(nil, &outputcd)

    if comp == nil {
        print("Can't get output unit")
        exit(-1)
    }

    CheckError(AudioComponentInstanceNew(comp!, &player.outputUnit),
        operation: "Couldn't open component for outputUnit")

    // Register the render callback
    var input = AURenderCallbackStruct(inputProc: SineWaveRenderProc, inputProcRefCon: &player)

    CheckError(AudioUnitSetProperty(player.outputUnit!, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &input, UInt32(MemoryLayout<AURenderCallbackStruct>.size)),
        operation: "AudioUnitSetProperty failed")

    // Initialize the unit
    CheckError(AudioUnitInitialize(player.outputUnit!),
        operation: "Couldn't initialize output unit")
}

func main() {
    var player = SineWavePlayer()

    // Set up output unit and callback
    CreateAndConnectOutputUnit(&player)

    // Start playing
    CheckError(AudioOutputUnitStart(player.outputUnit!),
        operation: "Couldn't start output unit")

    // Play for 5 seconds
    sleep(5)

    // Clean up
    AudioOutputUnitStop(player.outputUnit!)
    AudioUnitUninitialize(player.outputUnit!)
    AudioComponentInstanceDispose(player.outputUnit!)
}

main()

但是这行代码

var player = UnsafeMutablePointer<SineWavePlayer>(inRefCon) 

不工作。如何将此行翻译成 Swift 3?

请帮帮我。

在Swift3中,初始化器不能用来转换指针类型。在你的例子中,inRefCon的类型是UnsafeMutableRawPointer,所以你需要使用assumingMemoryBound(to:)方法。

还有一个,传给回调的player的地址需要在播放声音的时候保持稳定,取自inout参数的地址(由&前缀指定)不满足这个要求。

上面的两件事已经解决了,你的代码应该是这样的:

import Foundation
import AudioToolbox

let sineFrequency = 880.0

// MARK: User data struct
struct SineWavePlayer {
    var outputUnit: AudioUnit? = nil
    var startingFrameCount: Double = 0
}

// MARK: Callback function
let SineWaveRenderProc: AURenderCallback = {(inRefCon, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData) -> OSStatus in

    var player = inRefCon.assumingMemoryBound(to: SineWavePlayer.self)


    var j = player.pointee.startingFrameCount
    let cycleLength = 44100 / sineFrequency

    for frame in 0..<inNumberFrames {
        var buffers = UnsafeMutableAudioBufferListPointer(ioData)

        buffers?[0].mData?.assumingMemoryBound(to: Float32.self)[Int(frame)] = Float32(sin(2 * M_PI * (j / cycleLength)))
        buffers?[1].mData?.assumingMemoryBound(to: Float32.self)[Int(frame)] = Float32(sin(2 * M_PI * (j / cycleLength)))

        j += 1
        if j > cycleLength {
            j -= cycleLength
        }
    }

    player.pointee.startingFrameCount = j
    return noErr
}

// MARK: Utility function
func CheckError(_ error: OSStatus, operation: String) {
    guard error != noErr else {
        return
    }

    var result: String = ""
    var char = Int(error.bigEndian)

    for _ in 0..<4 {
        guard isprint(Int32(char&255)) == 1 else {
            result = "\(error)"
            break
        }
        result.append(String(describing: UnicodeScalar(char&255)))
        char = char/256
    }

    print("Error: \(operation) (\(result))")

    exit(1)
}

func CreateAndConnectOutputUnit(_ playerPtr: UnsafeMutablePointer<SineWavePlayer>) {
    // Generate a description that matches the output device (speakers)
    var outputcd = AudioComponentDescription(componentType: kAudioUnitType_Output, componentSubType: kAudioUnitSubType_DefaultOutput, componentManufacturer: kAudioUnitManufacturer_Apple, componentFlags: 0, componentFlagsMask: 0)

    let comp = AudioComponentFindNext(nil, &outputcd)

    if comp == nil {
        print("Can't get output unit")
        exit(-1)
    }

    CheckError(AudioComponentInstanceNew(comp!, &playerPtr.pointee.outputUnit),
               operation: "Couldn't open component for outputUnit")

    // Register the render callback
    var input = AURenderCallbackStruct(inputProc: SineWaveRenderProc, inputProcRefCon: playerPtr)

    CheckError(AudioUnitSetProperty(playerPtr.pointee.outputUnit!, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &input, UInt32(MemoryLayout<AURenderCallbackStruct>.size)),
               operation: "AudioUnitSetProperty failed")

    // Initialize the unit
    CheckError(AudioUnitInitialize(playerPtr.pointee.outputUnit!),
               operation: "Couldn't initialize output unit")
}

func main() {
    let playerPtr = UnsafeMutablePointer<SineWavePlayer>.allocate(capacity: 1)
    defer {playerPtr.deallocate(capacity: 1)}
    playerPtr.initialize(to: SineWavePlayer())
    defer {playerPtr.deinitialize()}

    // Set up output unit and callback
    CreateAndConnectOutputUnit(playerPtr)

    // Start playing
    CheckError(AudioOutputUnitStart(playerPtr.pointee.outputUnit!),
               operation: "Couldn't start output unit")

    // Play for 5 seconds
    sleep(5)

    // Clean up
    AudioOutputUnitStop(playerPtr.pointee.outputUnit!)
    AudioUnitUninitialize(playerPtr.pointee.outputUnit!)
    AudioComponentInstanceDispose(playerPtr.pointee.outputUnit!)
}