2016-08-29 14 views
1

録音した音声ファイルを[Double]タイプの生のPCMデータに変換しようとしています。音声を[Double]タイプの生のPCMデータに変換します

// load audio from path 
    let file = try! AVAudioFile(forReading: self.soundFileURL) 
    // declare format 
    let format = AVAudioFormat(commonFormat: .PCMFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false) 
    // initialize audiobuffer with the length of the audio file 
    let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: UInt32(file.length)) 
    // write to file 
    try! file.readIntoBuffer(buf) 
    // copy to array 
    let floatArray = Array(UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength))) 

問題は、[ダブル]とAVAudioPCMBuffer()のみ.PCMFormatFloat32を知っているように私がデータを必要とする、である:私はこのような[FLOAT32]ロードおよびタイプのPCMデータへの音声に変換する方法を見つけました。誰かが回避策を知っていますか? ありがとうございます。

答えて

1

しかしAVAudioFormat.PCMFormatFloat64を知っているん:

let format = AVAudioFormat(commonFormat: .PCMFormatFloat64, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false) 

たぶん、あなたはAVAudioPCMBufferfloat64ChannelData便利なプロパティを持っていない意味ですか?フルで

let abl = buf.audioBufferList.memory 
let doubles = UnsafePointer<Double>(abl.mBuffers.mData) 
doubles[0] // etc... 

let file = try! AVAudioFile(forReading: self.soundFileURL) 
let format = AVAudioFormat(commonFormat: .PCMFormatFloat64, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false) 

let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: UInt32(file.length)) 
try! file.readIntoBuffer(buf) 
let abl = buf.audioBufferList.memory 
let doubles = UnsafePointer<Float64>(abl.mBuffers.mData) 
+0

?わたしにはできる。私はコード全体を表示するために質問を更新しました。 –

+0

答えをありがとう、私の問題を解決!私はまだAVAudioPCMBufferを使用していたとは思えませんでした。 – user967493

0

大丈夫です。つまり、あなたがAVAudioPCMBufferのスーパークラスを使用することができ、AVAudioBufferはあなたが生Double/Float64サンプルで取得できるようにする必要がありeveryingを持っていますそのコードはもう動作しません。
ここに新しい作業コードがあります。

スウィフト3.2

あなた `readIntoBuffer()`何が起こる
let file = try! AVAudioFile(forReading: soundFileURL) 
let format = AVAudioFormat(commonFormat: .PCMFormatFloat64, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false) 
let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: AVAudioFrameCount(file.length)) 
try! file.readIntoBuffer(buf) 
let abl = Array(UnsafeBufferPointer(start: buf.audioBufferList, count: Int(buf.audioBufferList.pointee.mNumberBuffers))) 
let buffer = audioBufferList[0].mBuffers 
let mDataList = Array(UnsafeMutableRawBufferPointer(start: buffer.mData, count: Int(buffer.mDataByteSize))) 
関連する問題