SPB Git

Repository Public

Fixtures.swift 3.1 KB 59 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/CoreTests/Fixtures.swift3// Description: Tests: audio fixtures generated by ffmpeg and signal measurements.4import Foundation5import AVFoundation6import XCTest7@testable import SPMusicCore89/// Real audio files made by ffmpeg (sine waves: nothing commercial), one per format the app claims to play.10enum Fixtures {11    static let ffmpeg = ["/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg"].first { FileManager.default.isExecutableFile(atPath: $0) }12    static let dir: URL = {13        let d = FileManager.default.temporaryDirectory.appendingPathComponent("spmusic-fixtures-\(ProcessInfo.processInfo.processIdentifier)")14        try? FileManager.default.createDirectory(at: d, withIntermediateDirectories: true)15        return d16    }()1718    @discardableResult19    static func run(_ tool: String, _ args: [String]) throws -> Int32 {20        let p = Process(); p.executableURL = URL(fileURLWithPath: tool); p.arguments = args21        p.standardOutput = FileHandle.nullDevice; p.standardError = FileHandle.nullDevice22        try p.run(); p.waitUntilExit(); return p.terminationStatus23    }2425    /// `name.ext` holding a sine wave. `codecArgs` are ffmpeg's encoder options.26    static func sine(_ name: String, hz: Int, seconds: Double, rate: Int = 44100, codecArgs: [String]) throws -> URL {27        guard let ffmpeg else { throw XCTSkip("ffmpeg is not installed") }28        let url = dir.appendingPathComponent(name)29        if FileManager.default.fileExists(atPath: url.path) { return url }30        let status = try run(ffmpeg, ["-v", "error", "-nostdin", "-y", "-f", "lavfi", "-i", "sine=frequency=\(hz):duration=\(seconds):sample_rate=\(rate)", "-ac", "2"] + codecArgs + [url.path])31        guard status == 0 else { throw XCTSkip("ffmpeg could not encode \(name)") }32        return url33    }3435    static let formats: [(name: String, args: [String], lossless: Bool)] = [36        ("flac16.flac", ["-c:a", "flac", "-sample_fmt", "s16"], true),37        ("flac24.flac", ["-c:a", "flac", "-sample_fmt", "s32", "-bits_per_raw_sample", "24"], true),38        ("alac.m4a", ["-c:a", "alac"], true),39        ("wav16.wav", ["-c:a", "pcm_s16le"], true),40        ("aiff16.aiff", ["-c:a", "pcm_s16be"], true),41        ("mp3.mp3", ["-c:a", "libmp3lame", "-b:a", "192k"], false),42        ("aac.m4a", ["-c:a", "aac", "-b:a", "192k"], false),43    ]44}4546enum Signal {47    static func samples(_ b: AVAudioPCMBuffer, channel: Int = 0) -> [Float] {48        guard let d = b.floatChannelData else { return [] }49        return Array(UnsafeBufferPointer(start: d[channel], count: Int(b.frameLength)))50    }51    static func rms(_ x: ArraySlice<Float>) -> Float { x.isEmpty ? 0 : (x.reduce(0) { $0 + $1 * $1 } / Float(x.count)).squareRoot() }52    /// Dominant frequency by counting upward zero crossings.53    static func frequency(_ x: ArraySlice<Float>, rate: Double) -> Double {54        var n = 0; var prev: Float = 0; var first = true55        for v in x { if !first, prev < 0, v >= 0 { n += 1 }; prev = v; first = false }56        return Double(n) * rate / Double(x.count)57    }58}59
Files main
Menu

Theme