读取WAV文件并将其转换为Swift中的振幅数组

时间:2015-02-16 18:26:39

标签: swift audio waveform

我在udacity.com上有一个非常好的教程,用Swift探索音频应用程序的基础。我想扩展其当前的功能,从显示WAV文件的波形开始。为此,我需要从WAV文件中检索幅度与样本。考虑到我已经录制了文件,我怎么能快速进行?

谢谢!

1 个答案:

答案 0 :(得分:0)

SELECT well.well_name, wellbore.wellbore_name, operation.operation_name, COUNT(*) FROM well INNER JOIN wellbore ON wellbore.well_uid = well.well_uid INNER JOIN operation ON wellbore.well_uid = operation.well_uid GROUP BY well.well_name,wellbore.wellbore_name HAVING COUNT(*) > 1 满足您的需求。

您可以使用 AudioToolBox 从音频文件中获取音频样本,例如 AudioFileService 文件,

然后您可以从每个样本中获得幅度。

WAV

例如 // this is your desired amplitude data public internal(set) var packetsX = [Data]() public required init(src path: URL) throws { Utility.check(error: AudioFileOpenURL(path as CFURL, .readPermission, 0, &playbackFile) , // set on output to the AudioFileID operation: "AudioFileOpenURL failed") guard let file = playbackFile else { return } var numPacketsToRead: UInt32 = 0 GetPropertyValue(val: &numPacketsToRead, file: file, prop: kAudioFilePropertyAudioDataPacketCount) var asbdFormat = AudioStreamBasicDescription() GetPropertyValue(val: &asbdFormat, file: file, prop: kAudioFilePropertyDataFormat) dataFormatD = AVAudioFormat(streamDescription: &asbdFormat) /// At this point we should definitely have a data format var bytesRead: UInt32 = 0 GetPropertyValue(val: &bytesRead, file: file, prop: kAudioFilePropertyAudioDataByteCount) guard let dataFormat = dataFormatD else { return } let format = dataFormat.streamDescription.pointee let bytesPerPacket = Int(format.mBytesPerPacket) for i in 0 ..< Int(numPacketsToRead) { var packetSize = UInt32(bytesPerPacket) let packetStart = Int64(i * bytesPerPacket) let dataPt: UnsafeMutableRawPointer = malloc(MemoryLayout<UInt8>.size * bytesPerPacket) AudioFileReadBytes(file, false, packetStart, &packetSize, dataPt) let startPt = dataPt.bindMemory(to: UInt8.self, capacity: bytesPerPacket) let buffer = UnsafeBufferPointer(start: startPt, count: bytesPerPacket) let array = Array(buffer) packetsX.append(Data(array)) } } 文件有通道一、位深为Int16。

WAV

更多信息,您可以查看my github repo