Skip to content

Instantly share code, notes, and snippets.

@tion-low
Created August 6, 2019 04:50
Show Gist options
  • Save tion-low/f425e2002fa1ea75bb8277dae17cd776 to your computer and use it in GitHub Desktop.
Save tion-low/f425e2002fa1ea75bb8277dae17cd776 to your computer and use it in GitHub Desktop.
import UIKit
import AVFoundation
let player = AVAudioPlayerNode()
let engine = AVAudioEngine()
engine.attach(player)
engine.connect(player, to: engine.mainMixerNode, format: player.outputFormat(forBus: 0))
try! engine.start()
player.play()
let format = player.outputFormat(forBus: 0)
let sampleRate = format.sampleRate
var buf = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(sampleRate))!
buf.frameLength = AVAudioFrameCount(sampleRate)
for ch in 0..<format.channelCount {
let samples = buf.floatChannelData![Int(ch)]
for n in 0..<buf.frameLength {
samples[Int(n)] = sinf(Float(2.0 * Double.pi) * 440.0 * Float(n) / Float(sampleRate))
}
}
player.scheduleBuffer(buf) {
print("complete")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment