View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vidéos

Ouvrir le menu Fermer le menu
  • Collections
  • Toutes les vidéos
  • À propos

Plus de vidéos

  • À propos
  • Code
  • Swan's Quest, Chapter 4: The sequence completes

    Swift Playgrounds presents "Swan's Quest,” an interactive adventure in four chapters for all ages. It's time for the grand finale: You've honed your skills with tones, but in this chapter our Hero needs to sequence multi-part harmony.

    Discover how to play pitched instruments with MIDI codes, and you just might help our Hero find the rhythm… and complete 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 quests.

    Ressources

    • Quest Create playground book
    • Swan's Quest: The sequence completes playground book
      • Vidéo HD
      • Vidéo SD

    Vidéos connexes

    WWDC20

    • Build a SwiftUI view in Swift Playgrounds
    • Create Swift Playgrounds content for iPad and Mac
    • Swan's Quest, Chapter 1: Voices in the dark
    • Swan's Quest, Chapter 2: A time for tones
    • Swan's Quest, Chapter 3: The notable scroll
  • Rechercher dans cette vidéo…
    • 2:26 - Barebones example of a sequencer

      // A barebones example of a sequencer
      
      let numberOfBeats = 8   // two bars of 4/4
      let duration = 4.0      // seconds
      
      let interval = duration / Double(numberOfBeats)
      
      var index = 0
      Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { timer in
          // Play each track's Instrument
          // ...
          
          index = (index + 1 < numberOfBeats) ? index + 1 : 0
      }
    • 3:16 - Introduction to playInstrument(_:note:volume:)

      // Sequencer.swift
      
      func playInstrument(_ kind: Instrument.Kind, note: MIDINoteProtocol, volume: Double = 75)
      
      
      // Instrument.swift
      
      public class Instrument {
      
          /// The kind of included instruments
          public enum Kind: String {
              case electricGuitar, bassGuitar, piano, warmBells, sevenSynth, 
                  bassSynth, crystalSynth
          }
          
          // ...
      }
    • 3:38 - MIDINoteProtocol

      // Sequencer.swift 
      
      protocol MIDINoteProtocol {
          
          /// note as an 8-bit MIDI code
          var midiCode: UInt8 { get }
      }
    • 3:48 - Example implementation for Notes

      // Example implementation for Notes
      
      enum MIDINotes: UInt8, MIDINoteProtocol {
          case rest = 0
          
          case C2 = 36
          case D2 = 38
          case E2 = 40
          case F2 = 41
          case G2 = 43
          case A2 = 45
          case B2 = 47
              
          var midiCode: UInt8 {
              return self.rawValue
          }
      }
    • 4:03 - TrackProtocol

      // Sequencer.swift
      
      protocol TrackProtocol {
          associatedtype NoteType : MidiNoteProtocol
          
          /// The kind of instrument that the track sequences
          var instrument: Instrument.Kind { get }
          
          /// Number of beats contained in the sequence
          var length: Int { get }
          
          /// MIDI code for the sequence frame
          func note(for frame: Int) -> NoteType
      }
    • 4:21 - Example implementation for Tracks

      // Example implementation for Tracks
      
      struct Track : TrackProtocol {
          var instrument: Instrument.Kind
          var length: Int
          
          var notes: [MIDINotes]? = nil
          
          func note(for frame: Int) -> MIDINotes {
              guard let n = notes, frame < n.count else {
                  return .rest
              }
              return n[frame]
          }
      }
    • 4:34 - Implementing a Sequencer

      // A barebones example of a sequencer
      
      let numberOfBeats = 8   // two bars of 4/4
      let duration = 4.0      // seconds
      
      var bass = Track(instrument: .bassGuitar, length: numberOfBeats)
      var piano = Track(instrument: .piano, length: numberOfBeats)
      let tracks = [bass, piano]
      
      bass.notes =  [.rest, .C2, .A2, .rest, .C2, .A2, .D2, .C2 ]
      piano.notes = [.A2, .A2, .C2, .F2, .A2, .C2, .none, .F2]
      
      let interval = duration / Double(numberOfBeats)
      var index = 0
      Timer.scheduledTimer(withTimeInterval: interval, repeats: true, block: { timer in
          for track in tracks {
              playInstrument(track.instrument, note: track.note(for: index))
          }
          index = (index + 1 < numberOfBeats) ? index + 1 : 0
      })
    • 5:00 - // Getting credit for our work

      // Getting credit for our work
      
      Timer.scheduledTimer(withTimeInterval: interval, repeats: true, block: { timer in
          for track in tracks {
              playInstrument(track.instrument, note: track.note(for: index))
          }
          
          if index + 1 < numberOfBeats {
              index = index + 1
          }
          
          else {
              index = 0
              owner.endPerformance()
          }
      })

Developer Footer

  • Vidéos
  • WWDC20
  • Swan's Quest, Chapter 4: The sequence completes
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines