Created
August 6, 2019 04:50
-
-
Save tion-low/f425e2002fa1ea75bb8277dae17cd776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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