2016-12-13 2 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) 
    } 
} 
관련 문제