Skip to content

Instantly share code, notes, and snippets.

@yenrab
Last active October 4, 2024 07:02
Show Gist options
  • Save yenrab/4076b391b6c50b3d195c42ab8cf3b486 to your computer and use it in GitHub Desktop.
Save yenrab/4076b391b6c50b3d195c42ab8cf3b486 to your computer and use it in GitHub Desktop.
An MIT licensed pure tone generator in Swift. A single function is provided with internal comments. Complexity calculation is not done, but is at least O(n) with the duration being the data.
//
//
// 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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment