Ber*_*aux 8 audio waveform swift
我在udacity.com上关注了一个非常好的教程,用Swift探索音频应用程序的基础.我想扩展其当前的功能,从显示WAV文件的波形开始.为此,我需要从WAV文件中检索幅度与样本.考虑到我已经录制了文件,我怎么能快速进行?
谢谢!
AudioToolBox 满足你的需要。
您可以使用AudioFileService从音频文件中获取音频样本,例如文件WAV,
然后你就可以得到每个样本的幅度。
\n // this is your desired amplitude data\n public internal(set) var packetsX = [Data]()\n\n public required init(src path: URL) throws {\n Utility.check(error: AudioFileOpenURL(path as CFURL, .readPermission, 0, &playbackFile) , // set on output to the AudioFileID\n operation: "AudioFileOpenURL failed")\n \n guard let file = playbackFile else {\n return\n }\n \n var numPacketsToRead: UInt32 = 0\n \n \n GetPropertyValue(val: &numPacketsToRead, file: file, prop: kAudioFilePropertyAudioDataPacketCount)\n \n var asbdFormat = AudioStreamBasicDescription()\n GetPropertyValue(val: &asbdFormat, file: file, prop: kAudioFilePropertyDataFormat)\n \n dataFormatD = AVAudioFormat(streamDescription: &asbdFormat)\n /// At this point we should definitely have a data format\n var bytesRead: UInt32 = 0\n GetPropertyValue(val: &bytesRead, file: file, prop: kAudioFilePropertyAudioDataByteCount)\n \n \n \n \n guard let dataFormat = dataFormatD else {\n return\n }\n \n \n let format = dataFormat.streamDescription.pointee\n let bytesPerPacket = Int(format.mBytesPerPacket)\n \n for i in 0 ..< Int(numPacketsToRead) {\n \n var packetSize = UInt32(bytesPerPacket)\n \n let packetStart = Int64(i * bytesPerPacket)\n let dataPt: UnsafeMutableRawPointer = malloc(MemoryLayout<UInt8>.size * bytesPerPacket)\n AudioFileReadBytes(file, false, packetStart, &packetSize, dataPt)\n let startPt = dataPt.bindMemory(to: UInt8.self, capacity: bytesPerPacket)\n let buffer = UnsafeBufferPointer(start: startPt, count: bytesPerPacket)\n let array = Array(buffer)\n packetsX.append(Data(array))\n }\n \n \n \n }\nRun Code Online (Sandbox Code Playgroud)\n例如,该WAV文件的通道一 \xe3\x80\x81 位深度为 Int16。
// buffer is of two Int8, to express an Int16\nlet buffer = UnsafeBufferPointer(start: startPt, count: bytesPerPacket)\n\nRun Code Online (Sandbox Code Playgroud)\n更多信息,你可以查看我的github repo
\n| 归档时间: |
|
| 查看次数: |
599 次 |
| 最近记录: |