Instantly share code, notes, and snippets.

Embed
What would you like to do?
Grab the max and avg values of an audio sample
class WorkSpace: CanvasController {
var max0 = 0.0
var max1 = 0.0
var avg0 = 0.0
var avg1 = 0.0
var player = AudioPlayer("BlackVelvet.m4a")!
var timer: Timer?
override func setup() {
player.meteringEnabled = true
timer = Timer(interval: 1/60.0) {
self.player.updateMeters()
self.updateValues()
}
player.play()
timer?.start()
}
func updateValues() {
if max0 < abs(player.peakPower(0)) {
max0 = abs(player.peakPower(0))
}
if max1 < abs(player.peakPower(1)) {
max1 = abs(player.peakPower(1))
}
if avg0 < abs(player.averagePower(0)) {
avg0 = abs(player.averagePower(0))
}
if avg1 < abs(player.averagePower(1)) {
avg1 = abs(player.averagePower(1))
}
print("\(max0)\t\(max1)\t\(avg0)\t\(avg1)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment