Chapter 3 Performance at Swan Hall

I am trying to run my code for the Performance at Swan Hall.

I think I have a relevant solution for music.swift. In the main file, there is a func performance(owner: Assessable) function with content to setup up tones/pitches/timer as described in the video.

When I run the code, it thinks for a few minutes and then stops. The run code button becomes available, but no animation is shown.

I can't see any errors or warnings in the code windows.

Any thoughts on what might be preventing the code from running?
Could you share your code in Music.swift and your page code so we can help you debug?
The code is included below. When I said 'minutes' in my original post, I was mistaken. I find that it returns quickly now after a restart. With the following code, I press the Run my code button. It starts to animate as if it is running. It then quickly returns with no apparent change to the display. Probably something silly in the code - I'd appreciate a pointer to try and get this working. Thanks.

From music.swift:

Code Block swift
public protocol PitchProtocol {
    var frequency: Double { get }
}
public protocol NoteProtocol {
    associatedtype PitchType: PitchProtocol
    var tone: Tone { get }
    var length: Float { get }
    static var shortestSupportedNoteLength: Float { get }
    
    func subdivide() -> [PitchType]
}
public enum Note : NoteProtocol {
    case quarter(pitch: Pitch)
    case half(pitch: Pitch)
    case dottedHalf(pitch: Pitch)
    public var tone: Tone {
        switch self {
        case .quarter(let pitch):
            return Tone(pitch: pitch.frequency, volume: 0.3)
        case .half(let pitch):
            return Tone(pitch: pitch.frequency, volume: 0.3)
        case .dottedHalf(let pitch):
            return Tone(pitch: pitch.frequency, volume: 0.3)
        }
    }
    public var length: Float {
        switch self {
        case .quarter(_):
            return 1.0
        case .half(_):
            return 2.0
        case .dottedHalf(_):
            return 3.0
        }
    }
    public static var shortestSupportedNoteLength: Float {
        return Note.quarter(pitch: .a4).length
    }
    public func subdivide() -> [Pitch] {
        var notePitch: Pitch
        switch self { 
        case .quarter(let pitch): 
            notePitch = pitch
        case .half(let pitch):
            notePitch = pitch
        case .dottedHalf(let pitch):
            notePitch = pitch
        } 
        return [Pitch](repeating: notePitch, count: Int(self.length))
    }
}
public enum Pitch : Double, PitchProtocol {
    case c3 = 261.63
    case d3 = 293.66
    case e3 = 329.63
    case f3 = 349.23
    case g3 = 392.00
    case a4 = 440.0
    case b4 = 493.88
    case c4 = 523.25
    public var frequency: Double {
        return self.rawValue
    }
}



The performance code:

Code Block swift
func performance(owner: Assessable) {
    let toneOutput = ToneOutput()
    let notes: [Note] = [
        Note.half(pitch: .e3), 
        Note.quarter(pitch: .f3), 
        Note.half(pitch: .g3),
        Note.quarter(pitch: .f3),
        Note.quarter(pitch: .e3),
        Note.quarter(pitch: .d3),
        Note.half(pitch: .c3),
        Note.quarter(pitch: .d3),
        Note.half(pitch: .e3),
        Note.dottedHalf(pitch: .d3)
    ]
    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
    }
 }

Hitting this as well, tried running on the macOS and iPadOS betas.
Same problem here...
Chapter 3 Performance at Swan Hall
 
 
Q