187 lines
7.4 KiB
Swift
187 lines
7.4 KiB
Swift
// RecordingTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
@testable import LyrebirdShoeGaze
|
|
|
|
@MainActor
|
|
@Suite("Recording")
|
|
struct RecordingTests {
|
|
|
|
// MARK: - Helpers
|
|
|
|
/// Creates a temporary directory and returns its URL. The caller is responsible for cleanup.
|
|
private func makeTempFolder() throws -> URL {
|
|
let url: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
|
|
return url
|
|
}
|
|
|
|
// MARK: - Tests
|
|
|
|
@Test func recordingWritesInputAndOutputFiles() throws {
|
|
let renderDuration: Double = 0.2
|
|
let (engine, cafURL): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
numAudioBuses: 64
|
|
)
|
|
// The NRT driver output CAF is not checked here — clean it up when done.
|
|
defer { try? FileManager.default.removeItem(at: cafURL) }
|
|
|
|
let tempFolder: URL = try makeTempFolder()
|
|
defer { try? FileManager.default.removeItem(atPath: tempFolder.path) }
|
|
|
|
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop)
|
|
let board: PedalBoard = try shoeGaze.addBoard()
|
|
try board.add(OverdrivePedal())
|
|
try shoeGaze.finalizeBoard(board)
|
|
|
|
shoeGaze.recordingFolder = tempFolder
|
|
try shoeGaze.startRecording()
|
|
|
|
// NRT render: engine.start() renders the full duration synchronously.
|
|
try engine.start()
|
|
|
|
// Stop recording: flushes DiskOut ring buffers and closes file handles.
|
|
shoeGaze.stopRecording()
|
|
|
|
// Assert both output and input WAV files were created and are non-empty.
|
|
let contents: [URL] = try FileManager.default.contentsOfDirectory(
|
|
at: tempFolder,
|
|
includingPropertiesForKeys: [.fileSizeKey],
|
|
options: []
|
|
)
|
|
|
|
let outputFiles: [URL] = contents.filter { $0.lastPathComponent.hasPrefix("output-") && $0.pathExtension == "wav" }
|
|
let inputFiles: [URL] = contents.filter { $0.lastPathComponent.hasPrefix("input-") && $0.pathExtension == "wav" }
|
|
|
|
#expect(outputFiles.count == 1, "Expected one output WAV file")
|
|
#expect(inputFiles.count == 1, "Expected one input WAV file")
|
|
|
|
if let outputFile: URL = outputFiles.first {
|
|
let attrs: [FileAttributeKey: Any] = try FileManager.default.attributesOfItem(atPath: outputFile.path)
|
|
let size: Int = (attrs[.size] as? Int) ?? 0
|
|
#expect(size > 0, "output WAV file is empty")
|
|
}
|
|
|
|
if let inputFile: URL = inputFiles.first {
|
|
let attrs: [FileAttributeKey: Any] = try FileManager.default.attributesOfItem(atPath: inputFile.path)
|
|
let size: Int = (attrs[.size] as? Int) ?? 0
|
|
#expect(size > 0, "input WAV file is empty")
|
|
}
|
|
}
|
|
|
|
@Test func startRecordingThrowsWhenNoFolder() throws {
|
|
let (engine, cafURL): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05)
|
|
defer { try? FileManager.default.removeItem(at: cafURL) }
|
|
|
|
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine)
|
|
// recordingFolder is nil by default — startRecording() must throw.
|
|
#expect(throws: ShoeGazeError.recordingFolderNotSet) {
|
|
try shoeGaze.startRecording()
|
|
}
|
|
}
|
|
|
|
@Test func isRecordingTogglesAroundStartStop() throws {
|
|
let renderDuration: Double = 0.1
|
|
let (engine, cafURL): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
numAudioBuses: 64
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: cafURL) }
|
|
|
|
let tempFolder: URL = try makeTempFolder()
|
|
defer { try? FileManager.default.removeItem(atPath: tempFolder.path) }
|
|
|
|
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop)
|
|
let board: PedalBoard = try shoeGaze.addBoard()
|
|
try shoeGaze.finalizeBoard(board)
|
|
|
|
#expect(!shoeGaze.isRecording, "isRecording should be false before startRecording()")
|
|
|
|
shoeGaze.recordingFolder = tempFolder
|
|
try shoeGaze.startRecording()
|
|
#expect(shoeGaze.isRecording, "isRecording should be true after startRecording()")
|
|
|
|
// Render so DiskOut has data to flush.
|
|
try engine.start()
|
|
|
|
shoeGaze.stopRecording()
|
|
#expect(!shoeGaze.isRecording, "isRecording should be false after stopRecording()")
|
|
}
|
|
|
|
@Test func removeBoardWhileRecordingStopsRecording() throws {
|
|
let renderDuration: Double = 0.1
|
|
let (engine, cafURL): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
numAudioBuses: 64
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: cafURL) }
|
|
|
|
let tempFolder: URL = try makeTempFolder()
|
|
defer { try? FileManager.default.removeItem(atPath: tempFolder.path) }
|
|
|
|
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop)
|
|
let board: PedalBoard = try shoeGaze.addBoard()
|
|
try shoeGaze.finalizeBoard(board)
|
|
|
|
shoeGaze.recordingFolder = tempFolder
|
|
try shoeGaze.startRecording()
|
|
#expect(shoeGaze.isRecording, "isRecording should be true after startRecording()")
|
|
|
|
// Removing a board mid-recording must stop the recording to prevent the input
|
|
// record tap from reading a released or reused head bus.
|
|
shoeGaze.removeBoard(board)
|
|
#expect(!shoeGaze.isRecording, "isRecording must be false after removeBoard(_:)")
|
|
}
|
|
|
|
@Test func monoOutputEngineWritesOutputFileOnly() throws {
|
|
let renderDuration: Double = 0.1
|
|
// Build a mono-output engine (numOutputChannels: 1) so the output tap channel count
|
|
// is clamped to 1 and bus 1 (which is the first input bus) is never read.
|
|
let (engine, cafURL): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
numOutputChannels: 1,
|
|
numAudioBuses: 64
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: cafURL) }
|
|
|
|
let tempFolder: URL = try makeTempFolder()
|
|
defer { try? FileManager.default.removeItem(atPath: tempFolder.path) }
|
|
|
|
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop)
|
|
let board: PedalBoard = try shoeGaze.addBoard()
|
|
try shoeGaze.finalizeBoard(board)
|
|
|
|
shoeGaze.recordingFolder = tempFolder
|
|
try shoeGaze.startRecording()
|
|
try engine.start()
|
|
shoeGaze.stopRecording()
|
|
|
|
let contents: [URL] = try FileManager.default.contentsOfDirectory(
|
|
at: tempFolder,
|
|
includingPropertiesForKeys: [.fileSizeKey],
|
|
options: []
|
|
)
|
|
let outputFiles: [URL] = contents.filter {
|
|
$0.lastPathComponent.hasPrefix("output-") && $0.pathExtension == "wav"
|
|
}
|
|
// Exactly one output file must be written and must be non-empty.
|
|
#expect(outputFiles.count == 1, "Expected exactly one output WAV file for mono engine")
|
|
if let outputFile: URL = outputFiles.first {
|
|
let attrs: [FileAttributeKey: Any] = try FileManager.default.attributesOfItem(
|
|
atPath: outputFile.path
|
|
)
|
|
let fileSize: Int = (attrs[.size] as? Int) ?? 0
|
|
#expect(fileSize > 0, "Output WAV file from mono engine must be non-empty")
|
|
}
|
|
}
|
|
}
|