2016-12-13 14 views
3

へのPCM:負荷私はこのコードを持っているAVAudioPCMBuffer

func loadSoundfont(_ pitch : String) { 
    let path: String = Bundle.main.path(forResource: "\(self.id)/\(pitch)", ofType: "f32")! 
    let url = URL(fileURLWithPath: path) 

    do { 
     let file = try AVAudioFile(forReading: url, commonFormat: AVAudioCommonFormat.pcmFormatFloat32, interleaved: true) 
     let format = file.processingFormat 
     let capacity = AVAudioFrameCount(file.length) 
     self.buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: capacity) 
     try file.read(into: self.buffer!) 
    } catch let error as NSError { 
     print("ERROR HERE", error.localizedDescription) 
    } 
} 

をそして、私は次のエラーを取得する:1954115647次のとおりです。kAudioFileUnsupportedFileTypeError

マイA4.f32ファイルはPCMフロート32があります含まれています私はここで行方不明の何か?私のファイルにヘッダーはありません。この問題につながる可能性がありますか?コメント欄でリズムFistmanへ

+0

あなたのオーディオファイルは、生のfloat32データのですか?私は 'AVFoundation'があなたのためにそれをロードするとは思わない。自分で読み込んだり、ヘッダーを付けたりしてください。 –

答えて

1

おかげで、私は、そのように自分自身を行って、それをロード:

func loadSoundfont(_ pitch : String) { 
    let path: String = Bundle.main.path(forResource: "\(self.id)/\(pitch)", ofType: "f32")! 
    let url = URL(fileURLWithPath: path) 

    do { 
     let data = try Data(contentsOf: url) 
     let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 2, interleaved: true) 

     self.buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(data.count)) 

     self.buffer!.floatChannelData!.pointee.withMemoryRebound(to: UInt8.self, capacity: data.count) { 
      let stream = OutputStream(toBuffer: $0, capacity: data.count) 
      stream.open() 
      _ = data.withUnsafeBytes { 
       stream.write($0, maxLength: data.count) 
      } 
      stream.close() 
     } 

    } catch let error as NSError { 
     print("ERROR HERE", error.localizedDescription) 
    } 
} 
関連する問題