我怎样才能产生彩车从斯威夫特的音频文件数组 [英] How can I generate an array of floats from an audio file in Swift

查看:242
本文介绍了我怎样才能产生彩车从斯威夫特的音频文件数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载MP3和WAV音频文件作为浮点或双精度,类似于SciPy的的io.wavfile.read功能的阵列。我可以与麦克风的数据或通过写音频数据流给缓冲器播放音频做到这一点。不过,我不知道如何加载所有的音频文件数据的一次。

- 更新

有关的人在未来音频信号数据的工作,下面是做的伎俩功能。它是基于艺术Fistman的答案。

  FUNC loadAudioSignal(audioURL:NSURL) -  GT; (信号:[浮点],速度:双,frameCount:智力){
        让文件=试试吧! AVAudioFile(ForReading的:audioURL)
        让格式= AVAudioFormat(commonFormat:.PCMFormatFloat32,采样率:file.fileFormat.sampleRate,渠道:file.fileFormat.channelCount,交错:FALSE)
        让BUF = AVAudioPCMBuffer(PCMFormat:格式,frameCapacity:UInt32的(file.length))
        尝试! file.readIntoBuffer(BUF)//你可能需要更好的错误处理
        让floatArray =阵列(UnsafeBufferPointer(启动:buf.floatChannelData [0],计数:INT(buf.frameLength)))
        回报(信号:floatArray,率:file.fileFormat.sampleRate,frameCount:INT(file.length))
    }


解决方案

AVAudioFile 内置到iOS(和OS X),非常方便,也将做格式为你转换:

 进口AVFoundation
// ...让URL = NSBundle.mainBundle()URLForResource(音频文件,withExtension:WAV)。
让文件=试试吧! AVAudioFile(ForReading的:URL)
让格式= AVAudioFormat(commonFormat:.PCMFormatFloat32,采样率:file.fileFormat.sampleRate,渠道:1,交错:FALSE)让BUF = AVAudioPCMBuffer(PCMFormat:格式,frameCapacity:1024)
尝试! file.readIntoBuffer(BUF)//这使得副本,您可能不希望出现这种情况
让floatArray =阵列(UnsafeBufferPointer(启动:buf.floatChannelData [0],计数:INT(buf.frameLength)))打印(floatArray \\(floatArray)\\ n)

不幸的是,双打它似乎并不足够替代 .PCMFormatFloat32 .PCMFormatFloat64 ,因为 AVAudioPCMBuffer 不具有 float64ChannelData 方法。

更新,因为我不知道SWIFT以及

您可以通过使用 UnsafeBufferPointer ,这是一个完美的集合类型的工作避免复制数组:

 让floatArray = UnsafeBufferPointer(启动:buf.floatChannelData [0],计数:INT(buf.frameLength))

I would like to load mp3 and wav audio files as arrays of floats or doubles, similar to the io.wavfile.read function in scipy. I can do this with microphone data or playing audio by writing the audio stream to a buffer. However, I'm not sure how to load all of an audio file's data at once.

-- Update

For anyone working with audio signal data in the future, here's a function that does the trick. It's based on Rhythmic Fistman's answer.

    func loadAudioSignal(audioURL: NSURL) -> (signal: [Float], rate: Double, frameCount: Int) {
        let file = try! AVAudioFile(forReading: audioURL)
        let format = AVAudioFormat(commonFormat: .PCMFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false)
        let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: UInt32(file.length))
        try! file.readIntoBuffer(buf) // You probably want better error handling
        let floatArray = Array(UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength)))
        return (signal: floatArray, rate: file.fileFormat.sampleRate, frameCount: Int(file.length))
    }

解决方案

AVAudioFile built-in to iOS (and OS X), is very convenient and will also do format conversions for you:

import AVFoundation
// ...

let url = NSBundle.mainBundle().URLForResource("your audio file", withExtension: "wav")
let file = try! AVAudioFile(forReading: url!)
let format = AVAudioFormat(commonFormat: .PCMFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false)

let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: 1024)
try! file.readIntoBuffer(buf)

// this makes a copy, you might not want that
let floatArray = Array(UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength)))

print("floatArray \(floatArray)\n")

Sadly, for doubles it doesn't seem to be enough to substitute .PCMFormatFloat32 with .PCMFormatFloat64 because AVAudioPCMBuffer doesn't have a float64ChannelData method.

update because I don't know swift well

You can avoid copying the array by working with the UnsafeBufferPointer, which is a perfectly good collection type:

let floatArray = UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength))

这篇关于我怎样才能产生彩车从斯威夫特的音频文件数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆