2014-06-06 18 views
9

Tôi đang cố gắng truy cập AVAudioPCMBuffer.floatChannelData bằng Swift nhưng thuộc loại UnsafePointer<UnsafePointer<CFloat>> (trong Mục tiêu-C, @property(nonatomic, readonly) float *const *floatChannelData) và mọi nỗ lực tôi thực hiện để truy cập kết quả trong quá trình thực hiện không thành công.Từ UnsafePointer <UnsafePointer <CFloat>> đến một mảng float trong Swift?

Mẫu mã để thiết lập một AVAudioPCMBuffer nhanh chóng trong một sân chơi Swift được bao gồm trong một câu hỏi trước:

Getting AVAudioPCMBuffer working (AVAudioFile.mm error code -50)

Trả lời

6

Liệu công việc này?

let channels = UnsafeBufferPointer(start: myAudioBuffer.floatChannelData, count: Int(myAudioBuffer.format.channelCount)) 
let floats = UnsafeBufferPointer(start: channels[0], count: Int(myAudioBuffer.frameLength)) 
+0

Nope: Tôi nhận được một "lỗi nghiêm trọng: Không thể unwrap Tùy chọn.None Thực thi sân chơi không thành công: lỗi: Thực thi bị gián đoạn, lý do: EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode = 0x0). " – Matthew

+0

Tôi gặp lỗi đó với dòng '' let channels = ... ''. Phần '' Int (myAudioBuffer.format.channelCount) '' đánh giá chính xác. – Matthew

+0

Cập nhật: Tính năng này hoạt động! Cảm ơn vì điều này. Nó gây ra sự cố trong Swift Playground nhưng không hoạt động trong ứng dụng. – Matthew

3

gì bạn cũng có thể làm là để truy cập vào AVAudioBuffer:

for var i = 0; i < Int(audioBuffer.frameLength); i+=Int(audioMixerNode.outputFormatForBus(0).channelCount) { 
    self.audioBuffer.floatChannelData.memory[i] = 0.0f; // or whatever 
} 

Bạn có thể tìm thêm về điều đó trong Apple pre-release documentation:

The floatChannelData property returns pointers to the buffer’s audio samples, if the buffer’s format is 32-bit float. It returns nil if it is another format. The returned pointer is to format.channelCount pointers to float. Each of these pointers is to frameLength valid samples, which are spaced by stride samples.

Các vấn đề liên quan