2

When i try to read an audio file in swift using AVAudioFile.read() all the amplitude values are between -1 and 1.But when i read the values in python using librosa library i get different amplitude values.I think some kind of normalization is done while reading the contents in iOS. I would like to know what and how it is done so that in python i can make the same adjustments

ios sample code:

    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 sample code:

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

i am resampling in python because by default printing sr after librosa.read() gives 22050. So the values printed by both the codes are different.why? TIA

  • 1
    Did you ever find an answer here? I noticed the same thing, even in python. If I use scipy.io.wavfile and then normalize between -1 and 1 I get the same values as I do in iOS. – MScottWaller Jul 24 '18 at 02:30

0 Answers0