-
Swan's Quest, Chapter 3: The notable scroll
Swift Playgrounds presents "Swan's Quest,” an interactive adventure in four chapters for all ages. Calling all musicians! In this chapter, our Hero has found a mysterious scroll of music, and only you can help decode it. (Don't worry if you can't read music, our clever Lizard is standing by to assist. It's sure to be a note-worthy experience.)
By learning a little theory, and mastering time to create tones of different lengths, you just might help our Hero face the music… and move onto the next part of their quest.
Swan's Quest was created for Swift Playgrounds on iPad and Mac, combining frameworks and resources which power the educational experiences in many of our playgrounds, including Sonic Workshop, Sensor Arcade, and Augmented Reality. To learn more about building your own playgrounds, be sure to watch "Create Swift Playgrounds content for iPad and Mac".
And don't forget to stop by the Developer Forums and share your solution for our side quest.Recursos
Vídeos relacionados
WWDC20
-
Buscar neste vídeo...
-
-
2:03 - Example Pitch Implementation
// Example Pitch implementation public enum Pitch: Double, PitchProtocol { case a4 = 440.0 var frequency: Double { return self.rawValue } } -
2:09 - NoteProtocol
// Music.swift public protocol NoteProtocol { /// Play this Note through a ToneOutput var tone: Tone { get } /// The duration of this Note as a multiple of quarter notes, /// e.g., a half note is 2.0, an eighth note is 0.5 var length: Float { get } } -
2:24 - Example Note implementation
// Example Note implementation public enum Note: NoteProtocol { case quarter(pitch: Pitch) var tone: Tone { switch self { case .quarter(let pitch): return Tone(pitch: pitch.frequency, volume: 0.3) } } var length: Float { switch self { case .quarter(_): return 1.0 } } } -
2:51 - Play more than one tone redux
// Play more than one tone redux let toneOutput = ToneOutput() let notes = [Note.quarter(pitch: .a4), .half(pitch: .c4), .quarter(pitch: .a4)] var index = 0 Timer.scheduledTimer(withTimeInterval: 0.4, repeats: true) { timer in guard index < tones.count else { timer.invalidate() owner.endPerformance() return } toneOutput.play(tone: tones[toneIndex].tone) index += 1 } -
3:18 - Updating NoteProtocol
// Music.swift public protocol NoteProtocol { /// Play this Note through a ToneOutput var tone: Tone { get } /// The duration of this Note as a multiple of quarter notes, /// e.g., a half note is 2.0, an eighth note is 0.5 var length: Float { get } /// Length of the smallest Note supported static var shortestSupportedNoteLength: Float { get } } -
3:36 - Updating the Timer interval
// Play more than one tone redux let toneOutput = ToneOutput() let notes = [Note.quarter(pitch: .a4), .half(pitch: .c4), .quarter(pitch: .a4)] var index = 0 let interval = TimeInterval(Note.shortestSupportedNoteLength * 0.5) // 120 BPM Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { timer in guard index < tones.count else { timer.invalidate() owner.endPerformance() return } toneOutput.play(tone: tones[toneIndex].tone) index += 1 } -
4:15 - Adding subdivide to NoteProtocol
// Music.swift public protocol NoteProtocol { associatedtype PitchType: PitchProtocol /// Play this Note through a ToneOutput var tone: Tone { get } /// The duration of this Note as a multiple of quarter notes, /// e.g., a half note is 2.0, an eighth note is 0.5 var length: Float { get } /// Length of the smallest Note supported static var shortestSupportedNoteLength: Float { get } /// Subdivide into a series pitches, according to the shortest /// supported note func subdivide() -> [PitchType] } -
4:30 - Putting it all together
// Play more than one tone redux let toneOutput = ToneOutput() let notes = [Note.quarter(pitch: .a4), .half(pitch: .a4), .quarter(pitch: .a4)] var pitches = [Pitch]() for note in notes { pitches.append(contentsOf: note.subdivide()) } var index = 0 let interval = TimeInterval(Note.shortestSupportedNoteLength * 0.5) Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { timer in guard index < pitches.count else { timer.invalidate() owner.endPerformance() return } toneOutput.play(tone: Tone(pitch: pitches[index].frequency, volume: 0.3)) index += 1 }
-