将M4A文件转换为原始数据

时间:2015-08-11 21:39:02

标签: ios swift audio pcm

我正在尝试读取声音文件的原始值。我对IOS开发很新。我最终试图对音频文件进行快速傅里叶变换。数据的输出看起来像是一个声波,但当我接受这里提供的哔哔声时enter link description here我从fft中得不到明显的频率,这让我相信我没有得到真实的原始数据。我使用几个堆栈溢出帖子的组合构造了以下代码。我是否错误地阅读了该文件?

class AudioAnalyzer {
init(file_path: NSURL) {
    var assetOptions = [
        AVURLAssetPreferPreciseDurationAndTimingKey : 1,
        AVFormatIDKey : kAudioFormatLinearPCM
    ]
    println(file_path)
    var videoAsset=AVURLAsset(URL: file_path, options: assetOptions)
    var error:NSError?

    var videoAssetReader=AVAssetReader(asset: videoAsset, error: &error)

    if error != nil
    {
        println(error)
    }

    var tracksArray=videoAsset?.tracksWithMediaType(AVMediaTypeAudio)
    var videotrack = tracksArray?[0] as! AVAssetTrack
    var fps = videotrack.nominalFrameRate

    var videoTrackOutput=AVAssetReaderTrackOutput(track:videotrack as AVAssetTrack , outputSettings: nil)

    if videoAssetReader.canAddOutput(videoTrackOutput)
    {
        videoAssetReader.addOutput(videoTrackOutput)
        videoAssetReader.startReading()
    }

    if videoAssetReader.status == AVAssetReaderStatus.Reading {

        var sampleBuffer  = videoTrackOutput.copyNextSampleBuffer()
        var audioBuffer = CMSampleBufferGetDataBuffer(sampleBuffer)
        let samplesInBuffer = CMSampleBufferGetNumSamples(sampleBuffer)
        var currentZ = Double(samplesInBuffer)

        let buffer: CMBlockBufferRef = CMSampleBufferGetDataBuffer(sampleBuffer)

        var lengthAtOffset: size_t = 0
        var totalLength: size_t = 0
        var data: UnsafeMutablePointer<Int8> = nil
        var output:  Array<Float> = [];
        if( CMBlockBufferGetDataPointer( buffer, 0, &lengthAtOffset, &totalLength, &data ) != noErr ) {
            println("some sort of error happened")
        } else {
            for i in stride(from: 0, to: totalLength, by: 2) {
                var myint = Int16(data[i]) << 8 | Int16(data[i+1])
                var myFloat = Float(myint)
                output.append(myFloat);
            }
            println(output)
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

您的AVAssetReaderTrackOutput正在为您提供原始数据包数据。对于LPCM输出,传入一些outputSettings

var settings = [NSObject : AnyObject]()
settings[AVFormatIDKey] = kAudioFormatLinearPCM
settings[AVLinearPCMBitDepthKey] = 16
settings[AVLinearPCMIsFloatKey] = false

var videoTrackOutput=AVAssetReaderTrackOutput(track:videotrack as AVAssetTrack , outputSettings: settings)

P.S。如果您将videoTrackOutput重命名为audioTrackOutput,我会感觉好多了。