I have an audio file with the following information
File type ID: WAVE
Num Tracks: 1
----
Data format: 1 ch, 16000 Hz, Int16
no channel layout.
estimated duration: 5.680063 sec
audio bytes: 181762
audio packets: 90881
bit rate: 256000 bits per second
packet size upper bound: 2
maximum packet size: 2
audio data file offset: 44
optimized
source bit depth: I16
I would like to convert the audio data into a waveform for processing in iOS, as shown in the code below
let audio = Bundle.main.url(forResource: "00001", withExtension: "wav")
let file = try! AVAudioFile(forReading: audio!)
let format = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false)
let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: AVAudioFrameCount(file.length))
try! file.read(into: buffer!)
var array = Array(UnsafeBufferPointer(start: buffer?.int16ChannelData![0], count:Int(buffer!.frameLength)))
print(array)
When I look at the array that was printed out, the values the values seem to be rather odd and I can’t figure out why is that the case.
[0, 15327, 0, 15314, 0, 15266, 0, 15044, 0, 15184, 0, 15250, 0, 15028, 0, -18144, 0, 14968, 0, 15317, 0, 15269, 0, -17848, 0, -17660, 0, 15016, 0, 15262, 0, 15246, 0, 15237, 0, 15328, 0, 15338, 0, 15228, 0, -17968, 0, -17648, 0, -17660, 0, 14920, 0, 14864, 0, -17664, 0, -17808, 0, -17760, 0, -17896, 0, -17658, 0, -17594, 0, -17602, 0, -17668, 0, -17748, 0, -17652, 0, 14656, 0, 14952, 0, 15164, 0, 15112, 0, 15004, 0, 15134, 0, 15210, 0, 15247, 0, 15160, 0, 15164, 0, 15228, 0, 15244, 0, 15216, 0, 14800, 0, 15032, 0, 15257, 0, 15265, 0, 15210, 0, 15204, 0, 15286, 0, 15272, 0, 15092, 0, -18176, 0, 15092, 0, 15307, 0, 15204, 0, 14736, 0, 15028, 0, 15116, 0, 15152, 0, -17772, 0, -17630, 0, 14920, 0, 15242, 0, 15297, 0, 15278, 0, 15297, 0, 15304, 0, 15258, 0, 15220, 0, 15024, 0, 14996, 0, 15239, 0, 15322, 0, 15398, 0, 15392, 0, 15302, 0, 15212, 0, 15152, 0, 15198, 0, 15273, 0, 15375, -32768, 15399, -32768, 15412, -32768, 15366, 0, 15295, 0, 15249, 0, 15251, 0, 15112, 0, -17840, 0, 14872, 0, 15274, -32768, 15382, 0, 15396, -32768, 15420, -32768, 15394, 0, 15315, 0, 15122, 0, 15036, 0, 15226, 0, 15176, 0, 15164, 0, 15256, -32768, 15361, -32768, 15379, 0, 15298, 0, 15124, 0, -17724, 0, -17724, 0, 14752, 0, 14656, 0, 15142, 0, 15260, 0, 15263, 0, 15235, 0, -17776, 0, -17527, 0, -17473, 0, -17519, 0, -17640, 0, 14592, 0, 15236, 0, 15309, 0, 15355, 0, 15268, 0, 14976, 0, 14856, 0, -18432, 0, 14872, 0, 14872, 0, 15120, 0, 15280, 0, 15254, 0, 15244, 0, 15080, 0, 14872, 0, 15192, 0, 15243, 0, 15256, 0, 15253, 0, 15325, 0, 15345, 0, 15345, 0, 15265, 0, 15166, 0, 15294, 0, 15299, 0, 15269, 0, 15254, 0, 15326, 0, 15380, 0, 15348, 0, 15289, 0, 15220, 0, 15283, 0, 15302, 0, 15289, 0, 15268, 0, 15316, -32768, 15398, 0, 15378, 0, 15260, 0, 15122, 0, 15267, 0, 15314, 0, 15254, 0, 15233, 0, 15251, 0, 15322, 0, 15295, 0, 15192, 0, 15174, 0, 15056, 0, 15108, 0, 15244, 0, 15311, 0, 15327, 0, 15308, -32768, 15363, 0, 15379, 0, 15326, 0, 15246, 0, 15202, 0, 15362, -32768, 15380, 0, 15367, 0, 15271, 0, 15287, 0, 15356, 0, 15359, 0, 15374, -32768, 15417, -32768, 15440, -32768, 15427, -32768, 15386, 0, 15289, 0, 15258, 0, 15251, 0, 15237, 0, 15224, 0, 15180, 0, 15300, 0, 15359, 0, 15330, 0, 15106, 0, 14752, 0, 14988, 0, 15004, 0, -18240, 0, 14688, 0, 15202, 0, 15206, 0, -17824, 0, -17608, 0, -17600, 0, -17684, 0, -17604, 0, -17654, 0, 15088, 0, 15336, 0, 15253, 0, -17848, 0, -17598, 0, -17712, 0, 14888, 0, -17968, 0, 14864, 0, 15188, 0, 15164, 0, -17880, 0, -17700, 0, 14936, 0, 14988, 0, 15000, 0, -17744, 0, 14952, 0, 14920, 0, -17808, 0, 14624, 0, 14872, 0, 15118, 0, -17688, 0, -17403, -32768, -17388, 0, -17390, 0, -17445, 0, -17485, 0, -17584, 0, -17864, 0, -17720, 0, -17696, 0, -17568, 0, -17574, 0, -17447, 0, -17462, 0, 14736, 0, 15116, 0, 15084, 0, -17616, 0, -17562, 0, -17554, ...]
New contributor
newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.