AVAudioFIle阅读iOS swift规范化

时间:2018-02-22 05:10:53

标签: python ios swift audio avaudiofile

当我尝试使用AVAudioFile.read()在swift中读取音频文件时,所有幅度值都介于-1和1.但是当我使用librosa库读取python中的值时,我得到不同的幅度值。我想一些在iOS中阅读内容时完成了一种规范化。我想知道它是什么以及如何完成的,以便在python中我可以进行相同的调整

ios示例代码:

    let audioPath = Bundle.main.path(forResource:"example" , ofType:"mp3")
    let fileURL = NSURL(fileURLWithPath : audioPath!)
    let audio =  try! AVAudioFile(forReading : fileURL as URL)

    print(audio.fileFormat.channelCount,audio.fileFormat.sampleRate)

    let format = AVAudioFormat(commonFormat:.pcmFormatFloat32, sampleRate:audio.fileFormat.sampleRate, channels: audio.fileFormat.channelCount,  interleaved: false)
    var audioBuffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: UInt32(audio.length))!
    try! audio.read(into : audioBuffer, frameCount:UInt32(audio.length))
    let arraySize = Int(audioBuffer.frameLength)
    let samples = Array(UnsafeBufferPointer(start: audioBuffer.floatChannelData![0], count:arraySize))
    print(samples[0...2048])

python示例代码:

import librosa
y, sr = librosa.load('/Users/myname/Desktop/example.mp3')
y_new = librosa.resample(y, sr, 44100)
print(y_new[0:2048])  

我在python中进行重新采样,因为在librosa.read()给出22050后默认打印sr。因此两个代码打印的值都不同。为什么? TIA

0 个答案:

没有答案