Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2020 Swift

WWDC20 · 6 min · Swift

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.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 8 snippets

Example Pitch Implementation swift · at 2:03 ↗
// Example Pitch implementation

public enum Pitch: Double, PitchProtocol {
    case a4 = 440.0
    
    var frequency: Double {
        return self.rawValue
    }
}
NoteProtocol swift · at 2:09 ↗
// 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 }
}
Example Note implementation swift · at 2:24 ↗
// 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
        }
    }
}
Play more than one tone redux swift · at 2:51 ↗
// 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
}
Updating NoteProtocol swift · at 3:18 ↗
// 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 }
}
Updating the Timer interval swift · at 3:36 ↗
// 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
}
Adding subdivide to NoteProtocol swift · at 4:15 ↗
// 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]
}
Putting it all together swift · at 4:30 ↗
// 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
}

Resources