swift AVAudioEngine convert multichannel non interleaved signal to single channel - swift

I am using AVAudioEngine to take a measurement. I play a stimulus sound out of my interface, and use a micTap to record the returned signal.
I am now looking at different Audio Interfaces which support a multitude of different formats. I am converting the input format of the inputNode via a mixer for two different reasons:
to downsample from the interfaces' preferred sampleRate to the sampleRate at which my app is working
to convert the incoming channel count to a single mono channel
I try this, however it does not always seem to work as expected. If my interface is running 96k and my app is running 48k, doing a format change via a mixer ends up with the following:
This looks like it is only getting one side of a stereo interleaved channel. Below is my audioEngine code:
func initializeEngine(inputSweep:SweepFilter) {
buf1current = 0
buf2current = 0
in1StartTime = 0
in2startTime = 0
in1firstRun = true
in2firstRun = true
in1Buf = Array(repeating:0, count:1000000)
in2Buf = Array(repeating:0, count:1000000)
engine.stop()
engine.reset()
engine = AVAudioEngine()
numberOfSamples = 0
var time:Int = 0
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
try AVAudioSession.sharedInstance()
.setPreferredSampleRate(Double(sampleRate))
} catch {
assertionFailure("AVAudioSession setup failed")
}
let format = engine.outputNode.inputFormat(forBus: 0)
let stimulusFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32,
sampleRate: Double(sampleRate),
channels: 1,
interleaved: false)
let outputFormat = engine.outputNode.inputFormat(forBus: 0)
let inputFormat = engine.inputNode.outputFormat(forBus: 0)
let srcNode = AVAudioSourceNode { _, timeStamp, frameCount, AudioBufferList -> OSStatus in
let ablPointer = UnsafeMutableAudioBufferListPointer(AudioBufferList)
if self.in2firstRun == true {
let start2 = CACurrentMediaTime()
self.in2startTime = Double(CACurrentMediaTime())
self.in2firstRun = false
}
if Int(frameCount) + time >= inputSweep.stimulus.count{
self.running = false
print("AUDIO ENGINE STOPPED")
}
if (Int(frameCount) + time) <= inputSweep.stimulus.count {
for frame in 0..<Int(frameCount) {
let value = inputSweep.stimulus[frame + time] * Float(outputVolume)
for buffer in ablPointer {
let buf: UnsafeMutableBufferPointer<Float> = UnsafeMutableBufferPointer(buffer)
buf[frame] = value
}
}
time += Int(frameCount)
} else {
for frame in 0..<Int(frameCount) {
let value = 0
for buffer in ablPointer {
let buf: UnsafeMutableBufferPointer<Float> = UnsafeMutableBufferPointer(buffer)
buf[frame] = Float(value)
}
}
}
return noErr
}
engine.attach(srcNode)
engine.connect(srcNode, to: engine.mainMixerNode, format: stimulusFormat)
engine.connect(engine.mainMixerNode, to: engine.outputNode, format: format)
let requiredFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32,
sampleRate: Double(sampleRate),
channels: 1,
interleaved: false)
let formatMixer = AVAudioMixerNode()
engine.attach(formatMixer)
engine.connect(engine.inputNode, to: formatMixer, format: inputFormat)
let MicSinkNode = AVAudioSinkNode() { (timeStamp, frames, audioBufferList) ->
OSStatus in
if self.in1firstRun == true {
let start1 = CACurrentMediaTime()
self.in1StartTime = Double(start1)
self.in1firstRun = false
}
let ptr = audioBufferList.pointee.mBuffers.mData?.assumingMemoryBound(to: Float.self)
var monoSamples = [Float]()
monoSamples.append(contentsOf: UnsafeBufferPointer(start: ptr, count: Int(frames)))
if self.buf1current >= 100000 {
self.running = false
}
for frame in 0..<frames {
self.in1Buf[self.buf1current + Int(frame)] = monoSamples[Int(frame)]
}
self.buf1current = self.buf1current + Int(frames)
return noErr
}
engine.attach(MicSinkNode)
engine.connect(formatMixer, to: MicSinkNode, format: requiredFormat)
engine.prepare()
assert(engine.inputNode != nil)
running = true
try! engine.start()
}
My sourceNode is an array of Floats synthesised to use the stimulusFormat. If I listen to this audioEngine with my interface at 96k, the output stimulus sounds completely clean. However this broken up signal is what is coming from the micTap. Physically the output of the interface is routed. directly to the input, so not going through any other device.
Further to this, I have the following function, which records my arrays to WAV files so that I can visually inspect in a DAW.
func writetoFile(buff:[Float], name:String){
let SAMPLE_RATE = sampleRate
let outputFormatSettings = [
AVFormatIDKey:kAudioFormatLinearPCM,
AVLinearPCMBitDepthKey:32,
AVLinearPCMIsFloatKey: true,
AVLinearPCMIsBigEndianKey: true,
AVSampleRateKey: SAMPLE_RATE,
AVNumberOfChannelsKey: 1
] as [String : Any]
let fileName = name
let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("wav")
//print("FilePath: \(url.path)")
let audioFile = try? AVAudioFile(forWriting: url, settings: outputFormatSettings, commonFormat: AVAudioCommonFormat.pcmFormatFloat32, interleaved: false)
let bufferFormat = AVAudioFormat(settings: outputFormatSettings)
let outputBuffer = AVAudioPCMBuffer(pcmFormat: bufferFormat!, frameCapacity: AVAudioFrameCount(buff.count))
for i in 0..<buff.count {
outputBuffer?.floatChannelData!.pointee[i] = Float(( buff[i] ))
}
outputBuffer!.frameLength = AVAudioFrameCount( buff.count )
do{
try audioFile?.write(from: outputBuffer!)
} catch let error as NSError {
print("error:", error.localizedDescription)
}
}
If I set my interface to be 48k, and my app is working at 48k, if I inspect my reference signal and. my measurement signal, i get the following:
The measured signal is clearly a lot longer than the original stimulus. The physical file size. is the same as it is initialised as an empty array of fixed size. However at some point doing the format conversion, it is not correct.
If I put my interface at 44.1k and my app runs at 48k, I can see the regular 'glitches' in audio. So the format convert here is not working as it should do.
Can anyone see anything obviously wrong?

put the non-interleaved option "AVLinearPCMIsNonInterleaved" inside the format settings:
let outputFormatSettings = [
**AVLinearPCMIsNonInterleaved: 0,**
AVFormatIDKey:kAudioFormatLinearPCM,
AVLinearPCMBitDepthKey:32,
AVLinearPCMIsFloatKey: true,
AVLinearPCMIsBigEndianKey: true,
AVSampleRateKey: SAMPLE_RATE,
AVNumberOfChannelsKey: 1
] as [String : Any]
it worked for me, let me know

Related

Am trying to add an AVAudioMixerNode with custom formatting, but I keep crashing on installTap

I have a very simple app that taps the microphone and records the audio into multiple files. Working code at the very bottom.
However the files are large and would like to change the sampling of the microphone buffer, which with my current Mac, is sampled at 44100.
If I understand correctly:
The formatting of the incoming audio can be different depending on hardware (i.e. bluetooth headset, etc.)
I can attach mixer nodes to the AVAudioEngine and change the audio formatting specific to a node.
So I make two changes:
After starting the AVAudioEngine, I add:
audioEngine.attach(downMixer)
audioEngine.connect(node, to: downMixer, format: format16KHzMono)
I change the installTap to downMixer and use the lower formatting
downMixer.installTap(onBus: 0, bufferSize: 8192, format: format16KHzMono, block:
However the error I get on .installTap is:
required condition is false: NULL != engine"
The only two things I have noticed is:
That my user defined format16KHzMono has only 7 key values, rather than 8. The one missing key is : AVChannelLayoutKey However, the crash occurs even if I switch to the old format.
The line audioEngine.attach(downMixer) gives me nine "throwing -10878" errors. According to other posts I have found, this "supposedly" can be ignored?
I can only assume I am connecting the mixer incorrectly, or at the wrong time, etc. If anyone can figure out what I am doing wrong I would appreciate it.
class MyAudio {
let audioEngine = AVAudioEngine()
var audioFile : AVAudioFile?
var node : AVAudioInputNode
var recordingFormat : AVAudioFormat
var downMixer = AVAudioMixerNode()
let format16KHzMono = AVAudioFormat.init(commonFormat: .pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: true)
init() {
node = audioEngine.inputNode
recordingFormat = node.inputFormat(forBus: 0)
let format16KHzMono = AVAudioFormat.init(commonFormat: .pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: true) }
func startRecording() {
audioBuffs = []
x = -1
node.installTap(onBus: 0, bufferSize: 8192, format: recordingFormat, block: {
[self]
(buffer, _) in
x += 1
audioBuffs.append(buffer)
if x >= 100 {
var cumSilence = 0
var topRange = 0
audioFile = makeFile(format: recordingFormat, index: fileCount)
fileCount += 1
for i in 50...100 {
let curVol = getVolume(from: audioBuffs[i], bufferSize: audioBuffs[i].frameLength)
if curVol < voxGate {
cumSilence += 1
} else if curVol >= voxGate && cumSilence < 4 {
cumSilence = 0
} else if curVol >= voxGate && cumSilence > 4 {
topRange = i
break
}
}
for i in 0...(topRange - Int(cumSilence/2)){
do {
try audioFile!.write(from: audioBuffs[i]);
} catch {
mainView?.setLabelText(tag: 4, text: "write error")
stopRecording()
}
}
for _ in 0...(topRange - Int(cumSilence/2)){
audioBuffs.remove(at: 0) }
x = 0
}
})
audioEngine.prepare()
do {
try audioEngine.start()
} catch let error { print ("oh catch \(error)") }
}

Converting AvAudioInputNode to S16LE PCM

I'm trying to convert input node format to S16LE format. I've tried it with AVAudioMixerNode
First I create audio session
do {
try audioSession.setCategory(.record)
try audioSession.setActive(true)
} catch {
...
}
//Define formats
let inputNodeOutputFormat = audioEngine.inputNode.outputFormat(forBus: 0)
guard let wantedFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: false) else {
return;
}
//Create mixer node and attach it to the engine
audioEngine.attach(mixerNode)
//Connect the input node to mixer node and mixer node to mainMixerNode
audioEngine.connect(audioEngine.inputNode, to: mixerNode, format: inputNodeOutputFormat)
audioEngine.connect(mixerNode, to: audioEngine.mainMixerNode, format: wantedFormat)
//Install the tab on the output of the mixerNode
mixerNode.installTap(onBus: 0, bufferSize: bufferSize, format: wantedFormat) { (buffer, time) in
let theLength = Int(buffer.frameLength)
var bufferData: [Int16] = []
for i in 0 ..< theLength
{
let char = Int16((buffer.int16ChannelData?.pointee[i])!)
bufferData.append(char)
}
}
I get the following error.
Exception 'I[busArray objectAtindexedSubscript:
(NSUlnteger)element] setFormat:format
error:&nsErr]: returned false, error Error
Domain=NSOSStatusErrorDomain Code=-10868
"(null)"' was thrown
What part of the graph did I mess up?
You have to set the format of nodes to match the actual format of the data. Setting the node's format doesn't cause any conversions to happen, except that mixer nodes can convert sample rates (but not data formats). You'll need to use an AVAudioConverter in your tap to do the conversion.
As an example of what this code would look like, to handle arbitrary conversions:
let inputNode = audioEngine.inputNode
let inputFormat = inputNode.inputFormat(forBus: 0)
let outputFormat = AVAudioFormat(... define your format ...)
guard let converter = AVAudioConverter(from: inputFormat, to: outputFormat) else {
throw ...some error...
}
inputNode.installTap(onBus: 0, bufferSize: 1024, format: inputFormat) {[weak self] (buffer, time) in
let inputBlock: AVAudioConverterInputBlock = {inNumPackets, outStatus in
outStatus.pointee = AVAudioConverterInputStatus.haveData
return buffer
}
let targetFrameCapacity = AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate)
if let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: targetFrameCapacity) {
var error: NSError?
let status = converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputBlock)
assert(status != .error)
let sampleCount = convertedBuffer.frameLength
let rawData = convertedBuffer.int16ChannelData![0]
// ... and here you have your data ...
}
}
If you don't need to change the sample rate, and you're converting from uncompressed audio to uncompressed audio, you may be able to use the simpler convert(to:from:) method in your tap.
Since iOS 13, you can also do this with AVAudioSinkNode rather than a tap, which can be more convenient.

i got crash when record : "required condition is false: format.sampleRate == hwFormat.sampleRate" afterweb rtc call

my record work normally, but the problem is after WebRTC call, i got crash
required condition is false: format.sampleRate == hwFormat.sampleRate
here is how i start crash and installTap:
func startRecord() {
self.filePath = nil
print("last format: \(audioEngine.inputNode.inputFormat(forBus: 0).sampleRate)")
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(.playAndRecord, options: .mixWithOthers)
} catch {
print("======== Error setting setCategory \(error.localizedDescription)")
}
do {
try session.setPreferredSampleRate(44100.0)
} catch {
print("======== Error setting rate \(error.localizedDescription)")
}
do {
try session.setPreferredIOBufferDuration(0.005)
} catch {
print("======== Error IOBufferDuration \(error.localizedDescription)")
}
do {
try session.setActive(true, options: .notifyOthersOnDeactivation)
} catch {
print("========== Error starting session \(error.localizedDescription)")
}
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16,
sampleRate: 44100.0,
// sampleRate: audioEngine.inputNode.inputFormat(forBus: 0).sampleRate,
channels: 1,
interleaved: true)
audioEngine.connect(audioEngine.inputNode, to: mixer, format: format)
audioEngine.connect(mixer, to: audioEngine.mainMixerNode, format: format)
let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
filePath = dir.appending("/\(UUID.init().uuidString).wav")
_ = ExtAudioFileCreateWithURL(URL(fileURLWithPath: filePath!) as CFURL,
kAudioFileWAVEType,(format?.streamDescription)!,nil,AudioFileFlags.eraseFile.rawValue,&outref)
mixer.installTap(onBus: 0, bufferSize: AVAudioFrameCount((format?.sampleRate)!), format: format, block: { (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
let audioBuffer : AVAudioBuffer = buffer
_ = ExtAudioFileWrite(self.outref!, buffer.frameLength, audioBuffer.audioBufferList)
})
try! audioEngine.start()
startMP3Rec(path: filePath!, rate: 128)
}
func stopRecord() {
self.audioFilePlayer.stop()
self.audioEngine.stop()
self.mixer.removeTap(onBus: 0)
self.stopMP3Rec()
ExtAudioFileDispose(self.outref!)
try? AVAudioSession.sharedInstance().setActive(false)
}
func startMP3Rec(path: String, rate: Int32) {
self.isMP3Active = true
var total = 0
var read = 0
var write: Int32 = 0
let mp3path = path.replacingOccurrences(of: "wav", with: "mp3")
var pcm: UnsafeMutablePointer<FILE> = fopen(path, "rb")
fseek(pcm, 4*1024, SEEK_CUR)
let mp3: UnsafeMutablePointer<FILE> = fopen(mp3path, "wb")
let PCM_SIZE: Int = 8192
let MP3_SIZE: Int32 = 8192
let pcmbuffer = UnsafeMutablePointer<Int16>.allocate(capacity: Int(PCM_SIZE*2))
let mp3buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(MP3_SIZE))
let lame = lame_init()
lame_set_num_channels(lame, 1)
lame_set_mode(lame, MONO)
lame_set_in_samplerate(lame, 44100)
lame_set_brate(lame, rate)
lame_set_VBR(lame, vbr_off)
lame_init_params(lame)
DispatchQueue.global(qos: .default).async {
while true {
pcm = fopen(path, "rb")
fseek(pcm, 4*1024 + total, SEEK_CUR)
read = fread(pcmbuffer, MemoryLayout<Int16>.size, PCM_SIZE, pcm)
if read != 0 {
write = lame_encode_buffer(lame, pcmbuffer, nil, Int32(read), mp3buffer, MP3_SIZE)
fwrite(mp3buffer, Int(write), 1, mp3)
total += read * MemoryLayout<Int16>.size
fclose(pcm)
} else if !self.isMP3Active {
_ = lame_encode_flush(lame, mp3buffer, MP3_SIZE)
_ = fwrite(mp3buffer, Int(write), 1, mp3)
break
} else {
fclose(pcm)
usleep(50)
}
}
lame_close(lame)
fclose(mp3)
fclose(pcm)
self.filePathMP3 = mp3path
}
}
func stopMP3Rec() {
self.isMP3Active = false
}
as first time run app, i log the last format using
print("last format: \(audioEngine.inputNode.inputFormat(forBus: 0).sampleRate)")
--> return 0 -> record normally
next time return 44100 -> record normally
but after webrtc call, i got 48000, then it make crash in this line
self.audioEngine.connect(self.audioEngine.inputNode, to: self.mixer, format: format)
i spend 4 hour in stackoverflow but no solution work for me.
i dont want 48000 format, because i have set the sample to
sampleRate: audioEngine.inputNode.inputFormat(forBus: 0).sampleRate,
-> my output is hard to hear, i can recognize my voice :(
So i think 44100 is the best
can someone give me some advices? Thanks
The down sample part , more vivid on your case.
let bus = 0
let inputNode = audioEngine.inputNode
let inputFormat = inputNode.outputFormat(forBus: bus)
let outputFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: true)!
let converter = AVAudioConverter(from: inputFormat, to: outputFormat)!
inputNode.installTap(onBus: bus, bufferSize: 1024, format: inputFormat){ (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
var newBufferAvailable = true
let inputCallback: AVAudioConverterInputBlock = { inNumPackets, outStatus in
if newBufferAvailable {
outStatus.pointee = .haveData
newBufferAvailable = false
return buffer
} else {
outStatus.pointee = .noDataNow
return nil
}
}
let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate))!
var error: NSError?
let status = converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputCallback)
// 44100 Hz buffer
print(convertedBuffer.format)
}
This line bugs.
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, ...
AVAudioCommonFormat.pcmFormatInt16 not works by default.
You should use .pcmFormatFloat32
And the xcode tip is obvious,
the crash line
self.audioEngine.connect(self.audioEngine.inputNode, to: self.mixer, format: format)
You know it by print mixer.inputFormat(forBus: 0 )
then you got sample rate 48000 by the actual device. you can get 44100 by converting
just use AVAudioConverter to do down sample audio buffer.
let input = engine.inputNode
let bus = 0
let inputFormat = input.outputFormat(forBus: bus )
guard let outputFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: true), let converter = AVAudioConverter(from: inputFormat, to: outputFormat) else{
return
}
if let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate)){
var error: NSError?
let status = converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputCallback)
assert(status != .error)
print(convertedBuffer.format)
}
Only saw this in the iOS simulator:
I spent over an hour going down a rat hole on this. I had worked on a Logic audio session with some headphones (at 48K) on and then went over to my iOS simulator to work on my audio code for my app and started getting this crash. Unplugged my headphones, still crashed. Rebooted simulator, deleted app from the simulator, restarted XCode and machine, still crashed.
Finally I went to system preference on my mac, selected:
Sound & Input, plugged in my headphones so it says "External Microphone".
Also went to the simulator I/O settings for audio input set to "Internal Microphone"
Now my app was able to startup in the simulator without crashing while trying to create an AKMicrophone()...
I tried the accepted answer but it didn't work for me.
I was able to fix it by declaring audioEngine instance variable as optional. Right before when I need to monitor or record sound. I used to assign a new object of type AVAudioEngine to it.
Upon ending the recording session. I call audioEngine!.stop and then assign it to nil to deallocate the object.

AVAudioPCMBuffer able to get audio with static/white noise only

Thank you for taking the time and reading my question.
Target:
I am getting some audio data from UDP server and I have to convert them into PCM format to play the actual audio realtime.
Query:
For some reason using below code did not work for the format of my
AVAudioPCMBuffer and yielded no sound, or a memory error. However, I
am able to get white noise/static to play from the speakers by
converting the NSData to a AVAudioPCMBuffer. I have a feeling that I
am getting close to the answer, because whenever I speak into the
microphone I hear a different frequency of static.
Here is my code:
1. Converting received data to PCM Buffer:
func toPCMBuffer(format: AVAudioFormat, data: NSData) -> AVAudioPCMBuffer {
let PCMBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: UInt32(data.count) / format.streamDescription.pointee.mBytesPerFrame)
PCMBuffer!.frameLength = PCMBuffer!.frameCapacity
let channels = UnsafeBufferPointer(start: PCMBuffer?.int16ChannelData, count: Int(PCMBuffer!.format.channelCount))
data.getBytes(UnsafeMutableRawPointer(channels[0]) , length: data.count)
return PCMBuffer!
}
2. Playing the converted buffer into player.
if let receivedData = notification.userInfo?["data"] as? NSData {
let inputFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 8000, channels: 1, interleaved: false)
let pcmBufferData = self.toPCMBuffer(format: inputFormat!, data: receivedData) // converting raw data to pcm buffer
let mainMixer = audioEngine.inputNode
audioEngine.attach(audioFilePlayer)
let outputFormat = mainMixer.outputFormat(forBus: 1)
audioEngine.connect(audioFilePlayer, to:audioEngine.outputNode, format: outputFormat)
audioFilePlayer.scheduleBuffer(pcmBufferData, at: nil, completionHandler: nil)
audioEngine.prepare()
do {
try audioEngine.start()
} catch _{
print("error")
}
audioFilePlayer.play()
}
I don't understand what the issue is? Your help will be much appreciated. Thanks :)
Edit 1:
As # Vladimir's suggestion, I updated my code as below:
func configAudioEngine(){ // Config audio Engine only once.
let mainMixer = audioEngine.inputNode
audioEngine.attach(audioFilePlayer)
let outputFormat = mainMixer.outputFormat(forBus: 0)
// let outputFormat = mainMixer.outputFormat(forBus: bus)
audioEngine.connect(audioFilePlayer, to:audioEngine.outputNode, format: outputFormat)
audioEngine.prepare()
do {
try audioEngine.start()
} catch _{
print("error")
}
}
// Receiving Data in notification from UDP socket delegate, converting it to pcm buffer and then playing.
// Note : This callback gets called everytime I receive data from UDP
#objc func didReceiveData(notification:NSNotification) {
if let receivedData = notification.userInfo?["data"] as? NSData
{
let inputFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 80000, channels: 1, interleaved: false)
let pcmBufferData = self.toPCMBuffer(format: inputFormat!, data: receivedData)
audioFilePlayer.play()
}
}
// Still getting the white noise without any improvement in Audio.
//Here is my received Data in UDP
//incoming message: 1600 bytes
<48003c00 36003400 36002800 1600f5ff d2ffb8ff afffb6ff d1fff3ff 19003900 45004200 26000600 e1ffc2ff b7ffbbff c8ffd5ff dfffe3ff e1ffe9ff f4ff1200 3c006f00 93009900 88006000 2400e1ff a9ff8aff 87ff8eff a9ffc1ff d1ffdaff dcffe9ff 04002c00 52006c00 74005e00 2900e3ff 9fff76ff 78ffa2ff ebff2d00 5e007400 69005700 3e003000 2d002f00 28000a00 d7ff93ff 51ff24ff 23ff59ff a8ff1200 6700a100 af009500 77004a00 36002100 1e001700 0300deff abff77ff 5eff6fff 9cffebff 2d006800 80007500 4f001900 f3ffdbff d1ffd6ff d2ffd5ff ceffc8ff d1ffe0ff 0c003800 6e009900 b000a800 7e003e00 f8ffb6ff 7cff55ff 3fff3fff 50ff71ff 99ffc8ff 00003000 64008b00 ab00b000 a0008000 55002b00 f7ffdbff b2ffb1ff c2ffe2ff 0b002800 41003c00 30000b00 e8ffbdff 96ff86ff 74ff74ff 6bff72ff 7eff9aff daff1900 6500a500 da00f200 f400dc00 af007900 44001700 efffcaff a7ff87ff 77ff71ff 75ff87ff 9bffbbff d8fff4ff fbfff4ff daffc3ff bdffc1ff ddfff4ff 17003d00 5a007800 88009900 a0009f00 9a008500 69003700 0000caff 95ff79ff 66ff5fff 69ff77ff 8affa6ff bdffd5ff e9fffbff 12001f00 2d002900 2b002700 27003000 36004200 46004900 47003800 30002300 1a000b00 f7ffe3ff c8ffc0ff b1ffb4ff b8ffbeff ceffdeff f4ff0000 10001700 19001300 0c000300 fffffdff f7fff9ff fcff0c00 12001500 11001500 11001500 19001200 11000100 f3ffe8ff e2ffe7ff f2ff0700 1c002900 2d002700 20001100 0500fcff f2ffecff e5ffd7ff c7ffbaff b1ffb3ff bcffd7ff f4ff1500 2b003600 3b002e00 2e002600 2b003200 34003600 27001b00 0400f4ff ecffecff faff0100 0b000900 f9ffe9ff d9ffcdff c7ffc3ff c6ffc3ff c3ffc3ff c3ffd2ff eaff0f00 35005600 73007800 75006100 4c003600 23001100 ffffefff e1ffd6ff cbffc7ff c4ffd2ff dbffebff efffedff e5ffd9ff d9ffd1ff daffe3ff eefff8ff 06001100 17002500 32004400 57006300 68005f00 48002c00 0c00edff cdffb8ff a7ff9dff 96ff97ff a0ffb2ff d1fff0ff 0a002100 2e003200 2d001f00 17000c00 0a000f00 15001f00 24002400 25002600 25002400 1f001800 0b00fbff e7ffceff bbffacff a2ffa2ff acffbdff d5fff2ff 0f002400 31003900 3f004200 42004300 42003a00 28001200 ffffefff deffd1ff d1ffd5ff e0ffe6ff effff5ff f7fff5ff f5fff3ff effff0ff ecffedff effff2ff fbff0200 0d001e00 2c003b00 45004a00 42003300 1d000500 efffdbff c6ffb8ff a7ff9fff a1ffafff cefff0ff 1a003e00 5d006b00 69005e00 49002d00 0f00f3ff deffcdff c6ffc3ff c6ffcfff ddfff4ff 09002100 2e002c00 1e000600 e9ffd3ff bfffadff a9ffacff bbffdcff 05003c00 65008700 a100a600 a3008d00 65002c00 f0ffb6ff 82ff5eff 4aff47ff 56ff76ff a5ffd1ff 06003000 51006200 67005c00 47003100 1700ffff e6ffdeff e4fff1ff 0a002600 3b004e00 5b006000 5e004d00 2a00feff cbff97ff 6aff53ff 42ff45ff 59ff79ff aeffeaff 2c006100 84009d00 a300a800 95008200 5d003100 0700e3ff cbffc4ff c4ffc5ff ceffd2ff e5fff4ff 0d001300 0a00f4ff d7ffc3ff b6ffadff adffa7ff b3ffc1ff e9ff1600 3b006000 70007f00 7e008200 7b006700 4b002600 feffdcff bbffadff 9eff94ff 8fff95ff adffc4ff e2ffedff f7ffffff 02000d00 1b002200 1e001800 12001300 16001e00 28003000 38003900 42003a00 36002900 12000100 e8ffd5ff bbffabff 9cff99ff 9bffa2ff b5ffc9ff ddfff3ff 06002100 37004800 59005d00 5c005500 48004000 2b002b00 19001300 0100f0ff eaffd4ff dfffc2ff dfffbeff d1ffb6ff afffb1ff 9fffbdff b3ffe4ff e4ff1800 24004200 63005d00 80007100 83007500 6a005f00 41003100 0500e8ff baff9bff 85ff76ff 76ff7eff 89ff97ff a6ffc1ff d1fff5ff 04002300 32003d00 51004700 56004f00 5b005800 56006200 4b006200 3f003c00 1f000100 e3ffbcff a3ff7dff 76ff62ff 67ff6aff 7bff91ff acffc7ff eaff0300 28004000 68008300 9e00b000 a200a000 7a006700 3f002700 0d00edff d8ffb5ff a4ff93ff 8cff95ff 92ffa4ff b7ffc7ff d8ffedff eefff6ff f7ffebff edfff0ff 0c002000 42005a00 73008500 8c008c00 76006400 45002000 ffffd7ff b4ff97ff 83ff79ff 75ff70ff 7eff94ff b4ffe1ff ffff2b00>
Edit 2:
As # Vladimir's suggestion, I updated my configAudioEngine() as below:
func configAudioEngine(){
let mainMixer = audioEngine.outputNode
audioEngine.attach(audioFilePlayer)
let outputFormat = mainMixer.outputFormat(forBus: 0)
// let outputFormat = mainMixer.outputFormat(forBus: bus)
audioEngine.connect(audioFilePlayer, to:mainMixer, format: outputFormat)
audioEngine.prepare()
do {
try audioEngine.start()
} catch _{
print("error")
}
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveData), name: Global.inSocket.notificationAlertMessage, object: nil)
}
This has resolved my exception and I am receiving data. But No sound is coming. It's mute.

Get all sound frequencies of a WAV-file using Swift and AVFoundation

I would like to capture all frequencies between given timespans in a Wav-file. The intent is to do some audio analysis in a later step. For test, I’ve used the application “Sox” to generate a 1 second long Wav-file which includes only a single tone at 13000Hz. I want to read the file and find that frequency.
I’m using AVFoundation (which is important) to read the file. Since the input data is in PCM, I need to use FFT to get the actual frequencies which I do using the Accelerate framework. However, I don’t get the expected result (13000Hz), but rather a lot of values I don’t understand. I’m new to audio development, so any hint about where my code is failing is appreciated. The code includes a few comments where the issue occurs.
Thanks in advance!
Code:
import AVFoundation
import Accelerate
class Analyzer {
// This function is implemented using the code from the following tutorial:
// https://developer.apple.com/documentation/accelerate/vdsp/fast_fourier_transforms/finding_the_component_frequencies_in_a_composite_sine_wave
func fftTransform(signal: [Float], n: vDSP_Length) -> [Int] {
let observed: [DSPComplex] = stride(from: 0, to: Int(n), by: 2).map {
return DSPComplex(real: signal[$0],
imag: signal[$0.advanced(by: 1)])
}
let halfN = Int(n / 2)
var forwardInputReal = [Float](repeating: 0, count: halfN)
var forwardInputImag = [Float](repeating: 0, count: halfN)
var forwardInput = DSPSplitComplex(realp: &forwardInputReal,
imagp: &forwardInputImag)
vDSP_ctoz(observed, 2,
&forwardInput, 1,
vDSP_Length(halfN))
let log2n = vDSP_Length(log2(Float(n)))
guard let fftSetUp = vDSP_create_fftsetup(
log2n,
FFTRadix(kFFTRadix2)) else {
fatalError("Can't create FFT setup.")
}
defer {
vDSP_destroy_fftsetup(fftSetUp)
}
var forwardOutputReal = [Float](repeating: 0, count: halfN)
var forwardOutputImag = [Float](repeating: 0, count: halfN)
var forwardOutput = DSPSplitComplex(realp: &forwardOutputReal,
imagp: &forwardOutputImag)
vDSP_fft_zrop(fftSetUp,
&forwardInput, 1,
&forwardOutput, 1,
log2n,
FFTDirection(kFFTDirection_Forward))
let componentFrequencies = forwardOutputImag.enumerated().filter {
$0.element < -1
}.map {
return $0.offset
}
return componentFrequencies
}
func run() {
// The frequencies array is a array of frequencies which is then converted to points on sinus curves (signal)
let n = vDSP_Length(4*4096)
let frequencies: [Float] = [1, 5, 25, 30, 75, 100, 300, 500, 512, 1023]
let tau: Float = .pi * 2
let signal: [Float] = (0 ... n).map { index in
frequencies.reduce(0) { accumulator, frequency in
let normalizedIndex = Float(index) / Float(n)
return accumulator + sin(normalizedIndex * frequency * tau)
}
}
// These signals are then restored using the fftTransform function above, giving the exact same values as in the "frequencies" variable
let frequenciesRestored = fftTransform(signal: signal, n: n).map({Float($0)})
assert(frequenciesRestored == frequencies)
// Now I want to do the same thing, but reading the frequencies from a file (which includes a constant tone at 13000 Hz)
let file = { PATH TO A WAV-FILE WITH A SINGLE TONE AT 13000Hz RUNNING FOR 1 SECOND }
let asset = AVURLAsset(url: URL(fileURLWithPath: file))
let track = asset.tracks[0]
do {
let reader = try AVAssetReader(asset: asset)
let sampleRate = 48000.0
let outputSettingsDict: [String: Any] = [
AVFormatIDKey: kAudioFormatLinearPCM,
AVSampleRateKey: Int(sampleRate),
AVLinearPCMIsNonInterleaved: false,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsFloatKey: false,
AVLinearPCMIsBigEndianKey: false,
]
let output = AVAssetReaderTrackOutput(track: track, outputSettings: outputSettingsDict)
output.alwaysCopiesSampleData = false
reader.add(output)
reader.startReading()
typealias audioBuffertType = Int16
autoreleasepool {
while (reader.status == .reading) {
if let sampleBuffer = output.copyNextSampleBuffer() {
var audioBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
var blockBuffer: CMBlockBuffer?
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
sampleBuffer,
bufferListSizeNeededOut: nil,
bufferListOut: &audioBufferList,
bufferListSize: MemoryLayout<AudioBufferList>.size,
blockBufferAllocator: nil,
blockBufferMemoryAllocator: nil,
flags: kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
blockBufferOut: &blockBuffer
);
let buffers = UnsafeBufferPointer<AudioBuffer>(start: &audioBufferList.mBuffers, count: Int(audioBufferList.mNumberBuffers))
for buffer in buffers {
let samplesCount = Int(buffer.mDataByteSize) / MemoryLayout<audioBuffertType>.size
let samplesPointer = audioBufferList.mBuffers.mData!.bindMemory(to: audioBuffertType.self, capacity: samplesCount)
let samples = UnsafeMutableBufferPointer<audioBuffertType>(start: samplesPointer, count: samplesCount)
let myValues: [Float] = samples.map {
let value = Float($0)
return value
}
// Here I would expect my array to include multiple "13000" which is the frequency of the tone in my file
// I'm not sure what the variable 'n' does in this case, but changing it seems to change the result.
// The value should be twice as high as the highest measurable frequency (Nyquist frequency) (13000),
// but this crashes the application:
let mySignals = fftTransform(signal: myValues, n: vDSP_Length(2 * 13000))
assert(mySignals[0] == 13000)
}
}
}
}
}
catch {
print("error!")
}
}
}
The test clip can be generated using:
sox -G -n -r 48000 ~/outputfile.wav synth 1.0 sine 13000