PulseCanvas / app · pulsecanvas.ai / scripts / AudioSmoke.swift
| 1 | import Foundation |
| 2 | import AVFoundation |
| 3 | import Combine |
| 4 | |
| 5 | @main struct AudioSmoke { |
| 6 | @MainActor static func main() async throws { |
| 7 | let directory = URL(fileURLWithPath: CommandLine.arguments[1]) |
| 8 | let url = directory.appendingPathComponent("test-tone.caf") |
| 9 | let format = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 2)! |
| 10 | let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 132300)! |
| 11 | buffer.frameLength = buffer.frameCapacity |
| 12 | for channel in 0..<2 { for frame in 0..<Int(buffer.frameLength) { buffer.floatChannelData![channel][frame] = 0.4 * sin(Float(frame) * 2 * .pi * 440 / 44100) } } |
| 13 | do { let file = try AVAudioFile(forWriting: url, settings: format.settings); try file.write(from: buffer) } |
| 14 | let defaultAudio = AudioController() |
| 15 | precondition(defaultAudio.loadDefaultSoundtrackIfNeeded()) |
| 16 | precondition(defaultAudio.selectedURL?.pathExtension == "mp3" && defaultAudio.title == "PulseCanvas") |
| 17 | precondition(abs(defaultAudio.duration - 174.08) < 0.1 && !defaultAudio.isPlaying && defaultAudio.loopEnabled) |
| 18 | defaultAudio.seek(fraction: 0.5) |
| 19 | try await Task.sleep(nanoseconds: 100_000_000) |
| 20 | precondition(defaultAudio.spectrum.max()! > 0.01, "Bundled MP3 must decode and drive visual analysis") |
| 21 | let defaultPosition = defaultAudio.playbackSeconds |
| 22 | precondition(defaultAudio.loadDefaultSoundtrackIfNeeded() && defaultAudio.playbackSeconds == defaultPosition) |
| 23 | let bundledIdentifier = BundledSoundtrack.identifier(for: defaultAudio.selectedURL) |
| 24 | precondition(bundledIdentifier == "pulsecanvas") |
| 25 | let portableURL = try BundledSoundtrack.resolve(identifier: bundledIdentifier, externalURL: nil) |
| 26 | precondition(portableURL == defaultAudio.selectedURL) |
| 27 | let externalURL = try BundledSoundtrack.resolve(identifier: nil, externalURL: url) |
| 28 | precondition(externalURL == url && BundledSoundtrack.identifier(for: url) == nil) |
| 29 | do { |
| 30 | _ = try BundledSoundtrack.resolve(identifier: "missing-track", externalURL: nil) |
| 31 | preconditionFailure("Unknown bundled soundtracks must be rejected") |
| 32 | } catch {} |
| 33 | defaultAudio.clear() |
| 34 | precondition(!defaultAudio.loadDefaultSoundtrackIfNeeded() && defaultAudio.selectedURL == nil, |
| 35 | "Clearing the default track must persist for this session") |
| 36 | print("PASS bundled soundtrack: 174-second MP3 loads paused, decodes PCM, resolves portable project references and respects removal") |
| 37 | let audio = AudioController() |
| 38 | precondition(audio.loopEnabled, "Loop must start on") |
| 39 | precondition(audio.load(url: url)); precondition(abs(audio.duration - 3) < 0.001) |
| 40 | precondition(audio.loadDefaultSoundtrackIfNeeded() && audio.selectedURL == url && audio.duration == 3, |
| 41 | "Default loading must retain an imported or restored soundtrack") |
| 42 | try await Task.sleep(nanoseconds: 100_000_000) |
| 43 | precondition(audio.spectrum.max()! > 0.3, "Paused seek/load must analyze actual PCM") |
| 44 | audio.playPause() |
| 45 | var editorInvalidations = 0 |
| 46 | var visualizationFrames = 0 |
| 47 | let editorObserver = audio.objectWillChange.sink { editorInvalidations += 1 } |
| 48 | let frameObserver = audio.visualizationUpdated.sink { visualizationFrames += 1 } |
| 49 | try await Task.sleep(nanoseconds: 500_000_000) |
| 50 | precondition(editorInvalidations == 0, "Audio telemetry must not invalidate the editor or preset buttons") |
| 51 | precondition(visualizationFrames >= 10, "Renderer and Studio must still receive the audio clock") |
| 52 | editorObserver.cancel(); frameObserver.cancel() |
| 53 | print("PASS playback updates: renderer clock continues without editor/preset invalidations") |
| 54 | precondition(audio.isPlaying && audio.playbackSeconds > 0.2 && audio.spectrum.max()! > 0.3) |
| 55 | audio.pause(); let paused = audio.playbackSeconds |
| 56 | try await Task.sleep(nanoseconds: 150_000_000) |
| 57 | precondition(abs(paused - audio.playbackSeconds) < 0.01) |
| 58 | audio.seek(fraction: 0.5) |
| 59 | precondition(abs(audio.playbackSeconds - 1.5) < 0.01) |
| 60 | audio.playPause() |
| 61 | try await Task.sleep(nanoseconds: 300_000_000) |
| 62 | precondition(audio.playbackSeconds > 1.6) |
| 63 | audio.pause() |
| 64 | precondition(audio.loopEnabled, "Loading and pausing must retain the default Loop setting") |
| 65 | audio.loopEnabled = false |
| 66 | audio.seek(fraction: 0.96); audio.playPause() |
| 67 | try await Task.sleep(nanoseconds: 650_000_000) |
| 68 | precondition(!audio.isPlaying && audio.progress == 1, "Playback with Loop off must stop at the end") |
| 69 | audio.loopEnabled = true |
| 70 | audio.seek(fraction: 0.96); audio.playPause() |
| 71 | try await Task.sleep(nanoseconds: 650_000_000) |
| 72 | precondition(audio.isPlaying && audio.playbackSeconds < 1, "Loop must restart at the beginning") |
| 73 | precondition(audio.spectrum.max()! > 0.3, "Loop restart must resume PCM analysis") |
| 74 | audio.pause(); let loopPaused = audio.playbackSeconds |
| 75 | try await Task.sleep(nanoseconds: 200_000_000) |
| 76 | precondition(!audio.isPlaying && abs(audio.playbackSeconds - loopPaused) < 0.01, "Pause must hold while Loop is on") |
| 77 | audio.loopEnabled = false |
| 78 | audio.seek(fraction: 0.96); audio.playPause() |
| 79 | try await Task.sleep(nanoseconds: 650_000_000) |
| 80 | precondition(!audio.isPlaying && audio.progress == 1, "Turning Loop off must stop at the next end") |
| 81 | let boundary = 0.472 |
| 82 | audio.pause(); audio.seekToSection(start: boundary) |
| 83 | precondition(audio.playbackSeconds >= boundary && audio.playbackSeconds < boundary + 0.00005, "Section seeking cannot land in the card to the left") |
| 84 | for fullTrackLoop in [false, true] { |
| 85 | audio.loopEnabled = fullTrackLoop |
| 86 | var wraps = 0, previous = boundary |
| 87 | let loopObserver = audio.visualizationUpdated.sink { |
| 88 | if audio.playbackSeconds < previous - 0.1 { wraps += 1 } |
| 89 | previous = audio.playbackSeconds |
| 90 | } |
| 91 | audio.playSection(start: boundary, end: 0.733) |
| 92 | try await Task.sleep(nanoseconds: 1_000_000_000) |
| 93 | loopObserver.cancel() |
| 94 | precondition(audio.isPlaying && audio.previewSectionStart == boundary && wraps >= 2, |
| 95 | "Studio sections must repeat continuously, independently of the full-track Loop switch") |
| 96 | precondition(audio.playbackSeconds >= boundary && audio.playbackSeconds < 0.733 && audio.spectrum.max()! > 0.3) |
| 97 | audio.pause(); let sectionPaused = audio.playbackSeconds |
| 98 | try await Task.sleep(nanoseconds: 350_000_000) |
| 99 | precondition(!audio.isPlaying && audio.playbackSeconds == sectionPaused, "Pausing a section must prevent pending callbacks from restarting it") |
| 100 | } |
| 101 | audio.playSection(start: boundary, end: 0.733) |
| 102 | try await Task.sleep(nanoseconds: 80_000_000) |
| 103 | audio.playSection(start: boundary, end: 0.733) |
| 104 | precondition(!audio.isPlaying, "Section Play must toggle pause independently") |
| 105 | audio.playPause() |
| 106 | try await Task.sleep(nanoseconds: 450_000_000) |
| 107 | precondition(audio.isPlaying && audio.previewSectionStart == nil && audio.playbackSeconds > 0.733, "Normal playback must resume beyond the section") |
| 108 | audio.pause() |
| 109 | audio.playSection(start: boundary, end: 0.733) |
| 110 | audio.seekToSection(start: 1.123) |
| 111 | try await Task.sleep(nanoseconds: 450_000_000) |
| 112 | precondition(audio.isPlaying && audio.playbackSeconds > 1.123 && audio.previewSectionStart == nil, "Seeking must invalidate the previous section's pending stop") |
| 113 | audio.pause() |
| 114 | print("PASS Studio audio: repeated section loops with full-track Loop off/on, fractional boundaries, PCM analysis, independent pause, normal resume and stale callback invalidation") |
| 115 | audio.loopEnabled = false |
| 116 | audio.selectSection(start: 1.123, end: 1.433) |
| 117 | precondition(!audio.isPlaying && audio.playbackSeconds >= 1.123 && audio.playbackSeconds < 1.12305) |
| 118 | let selectedPaused = audio.playbackSeconds |
| 119 | try await Task.sleep(nanoseconds: 350_000_000) |
| 120 | precondition(!audio.isPlaying && audio.playbackSeconds == selectedPaused, "Selecting a paused card must not start playback") |
| 121 | audio.playSection(start: boundary, end: 0.733) |
| 122 | try await Task.sleep(nanoseconds: 80_000_000) |
| 123 | audio.selectSection(start: 1.123, end: 1.433) |
| 124 | precondition(audio.isPlaying && audio.previewSectionStart == 1.123 && audio.playbackSeconds >= 1.123 && audio.playbackSeconds < 1.12305) |
| 125 | try await Task.sleep(nanoseconds: 750_000_000) |
| 126 | precondition(audio.isPlaying && audio.previewSectionStart == 1.123 && audio.playbackSeconds >= 1.123 && audio.playbackSeconds < 1.433 && audio.spectrum.max()! > 0.3, |
| 127 | "Selecting another card must loop its audio and invalidate the previous loop's callbacks") |
| 128 | audio.selectSection(start: 1.123, end: 1.433) |
| 129 | precondition(audio.isPlaying && audio.previewSectionStart == 1.123, "Selecting the playing card must not toggle it off") |
| 130 | audio.pause(); audio.selectSection(start: boundary, end: 0.733) |
| 131 | try await Task.sleep(nanoseconds: 350_000_000) |
| 132 | precondition(!audio.isPlaying && audio.playbackSeconds >= boundary && audio.playbackSeconds < boundary + 0.00005 && !audio.loopEnabled) |
| 133 | audio.playFullSoundtrack(); audio.selectSection(start: 1.123, end: 1.433) |
| 134 | precondition(audio.isPlaying && audio.previewSectionStart == 1.123, "Selecting during full playback must keep playing the chosen section") |
| 135 | audio.selectSection(start: .nan, end: 1.433) |
| 136 | precondition(audio.isPlaying && audio.previewSectionStart == 1.123, "Invalid selection must leave playback unchanged") |
| 137 | audio.pause(); audio.loopEnabled = true |
| 138 | print("PASS Studio card selection: paused preview stays paused, running playback switches and loops the chosen card, same-card selection stays playing, PCM/bounds and stale callbacks verified") |
| 139 | audio.playSection(start: boundary, end: 0.733, restart: true) |
| 140 | precondition(audio.isPlaying && audio.previewSectionStart == boundary && audio.loopEnabled) |
| 141 | try await Task.sleep(nanoseconds: 80_000_000) |
| 142 | precondition(audio.playbackSeconds > boundary + 0.02) |
| 143 | audio.playSection(start: boundary, end: 0.733, restart: true) |
| 144 | precondition(audio.isPlaying && audio.previewSectionStart == boundary && audio.playbackSeconds >= boundary && audio.playbackSeconds < boundary + 0.00005, "Dice/shuffle must restart a playing section, not toggle it off") |
| 145 | try await Task.sleep(nanoseconds: 80_000_000) |
| 146 | audio.playSection(start: boundary, end: 0.733, restart: true) |
| 147 | precondition(audio.isPlaying && audio.playbackSeconds < boundary + 0.00005, "Repeated rolls must continue to restart and play") |
| 148 | try await Task.sleep(nanoseconds: 450_000_000) |
| 149 | precondition(audio.isPlaying && audio.previewSectionStart == boundary && audio.playbackSeconds >= boundary && audio.playbackSeconds < 0.733, "Restart retains and loops the selected section bounds") |
| 150 | print("PASS Studio replay: rolls restart paused/playing sections from their exact start, preserve Loop and invalidate stale completion callbacks") |
| 151 | |
| 152 | audio.pause() |
| 153 | audio.loopEnabled = false |
| 154 | audio.playSection(start: boundary, end: 0.733) |
| 155 | audio.playFullSoundtrack() |
| 156 | precondition(audio.isPlaying && audio.previewSectionStart == nil && !audio.loopEnabled, "Presentation must clear bounded section playback and retain Loop") |
| 157 | try await Task.sleep(nanoseconds: 450_000_000) |
| 158 | precondition(audio.isPlaying && audio.playbackSeconds > 0.733, "Presentation must continue across Studio boundaries") |
| 159 | audio.pause(); audio.seek(fraction: 0.99); audio.playFullSoundtrack() |
| 160 | try await Task.sleep(nanoseconds: 250_000_000) |
| 161 | precondition(!audio.isPlaying && audio.progress == 1, "Presentation must finish at track end with Loop off") |
| 162 | audio.playFullSoundtrack() |
| 163 | precondition(audio.isPlaying && audio.playbackSeconds < 0.05, "Presentation must restart an ended soundtrack") |
| 164 | audio.pause() |
| 165 | print("PASS presentation audio: resumes full soundtrack beyond section, retains Loop, stops at end and restarts") |
| 166 | audio.loopEnabled = true; audio.seek(fraction: 0.99); audio.playPause() |
| 167 | audio.clear(); precondition(audio.selectedURL == nil && audio.duration == 0 && !audio.isPlaying) |
| 168 | try await Task.sleep(nanoseconds: 350_000_000) |
| 169 | precondition(!audio.isPlaying && audio.selectedURL == nil, "Clear must invalidate pending loop callbacks") |
| 170 | print("PASS Loop: on by default, end stop when disabled, repeat with PCM analysis, pause, disable, clear") |
| 171 | print("PASS soundtrack: load, paused analysis, real FFT, playback, pause, seek, resume, clear") |
| 172 | } |
| 173 | } |