May 23, 2021

Pure Tone Generation

Posted in Uncategorized at 2:41 am by tetontech

It has been a long time since I last wrote a posting for this blog.

I recently had an opportunity to create some Swift 5 code for generating and playing pure tones using AVFoundation. I put the code in a gist and thought I’d share it here.

The code uses a semaphore to block until the tone completes playing and plays the tone on a background thread. Even so, realize that the thread you are calling from will be blocked so you may need to wrap a call to the playPureTone(frequencyInHz, amplitude, durationInMillis, completion) function in a closure passed to a DispatchQue.

Enjoy.

//
//
// Swift Pure Tone Generation
//
/*
Copyright (c) 2021 Lee Barney
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import Foundation
import AVFoundation
func playPureTone(frequencyInHz: Int, amplitude: Float, durationInMillis: Int, completion: @escaping ()->Void) {
//Use a semaphore to block until the tone completes playing
let semaphore = DispatchSemaphore(value: 1)
//Run async in the background so as not to block the current thread
DispatchQueue.global().async {
//Build the player and its engine
let audioPlayer = AVAudioPlayerNode()
let audioEngine = AVAudioEngine()
semaphore.wait()//Claim the semphore for blocking
audioEngine.attach(audioPlayer)
let mixer = audioEngine.mainMixerNode
let sampleRateHz = Float(mixer.outputFormat(forBus: 0).sampleRate)
guard let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatFloat32, sampleRate: Double(sampleRateHz), channels: AVAudioChannelCount(1), interleaved: false) else {
return
}
// Connect the audio engine to the audio player
audioEngine.connect(audioPlayer, to: mixer, format: format)
let numberOfSamples = AVAudioFrameCount((Float(durationInMillis) / 1000 * sampleRateHz))
//create the appropriatly sized buffer
guard let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: numberOfSamples) else {
return
}
buffer.frameLength = numberOfSamples
//get a pointer to the buffer of floats
let channels = UnsafeBufferPointer(start: buffer.floatChannelData, count: Int(format.channelCount))
let floats = UnsafeMutableBufferPointer<Float>(start: channels[0], count: Int(numberOfSamples))
//calculate the angular frequency
let angularFrequency = Float(frequencyInHz * 2) * .pi
// Generate and store the sequential samples representing the sine wave of the tone
for i in 0 ..< Int(numberOfSamples) {
let waveComponent = sinf(Float(i) * angularFrequency / sampleRateHz)
floats[i] = waveComponent * amplitude
}
do {
try audioEngine.start()
}
catch{
print("Error: Engine start failure")
return
}
// Play the pure tone represented by the buffer
audioPlayer.play()
audioPlayer.scheduleBuffer(buffer, at: nil, options: .interrupts){
DispatchQueue.main.async {
completionBlock()
semaphore.signal()//Release one claim of the semiphore
}
}
semaphore.wait()//Wait for the semiphore so the function doesn't end before the playing of the tone completes
semaphore.signal()//Release the other claim of the semiphore
}
}
view raw pure_tone.swift hosted with ❤ by GitHub
Advertisement

%d bloggers like this: