Swan’s Quest is a series of challenges where each chapter has a specific programming challenge for you that will build on the prior chapters.
Download the .playgroundbook here.
Tones
For this first challenge, a SPCAudio PlaygroundModule has been built, this module is part of the Create Quest Playground Book.
In this challenge you will need to play a tone with the secret code given in the previous quest. All of this is done via a Timer and a SPCAudio‘s’ Tone.
Solution
Advice from the Lizard
let toneOutput = ToneOutput()
func performance(owner: Assessable) {
let tones = [
Tone(pitch: 440.00, volume: 0.3),
Tone(pitch: 493.88, volume: 0.3),
Tone(pitch: 523.25, volume: 0.3)
]
var toneIndex = 0
Timer.scheduledTimer(withTimeInterval: 0.4, repeats: true) { timer in
guard toneIndex < tones.count else {
toneOutput.stopTones()
timer.invalidate()
owner.endPerformance()
return
}
toneOutput.play(tone: tones[toneIndex])
toneIndex += 1
}
// Let it be known your performance has ended.
owner.endPerformance()
}Performance at Swan Hall
func performance(owner: Assessable) {
let toneOutput = ToneOutput()
// TODO: Code your performance here.
let tones = [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25].map({ Tone(pitch: $0, volume: 0.3)})
var toneIndex = 0
Timer.scheduledTimer(withTimeInterval: 0.4, repeats: true) { timer in
guard toneIndex < tones.count else {
toneOutput.stopTones()
timer.invalidate()
owner.endPerformance()
return
}
toneOutput.play(tone: tones[toneIndex])
toneIndex += 1
}
// Let it be known your performance has ended.
owner.endPerformance()
}