krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDayTests/VoiceClipTests.swift

main
daybyday/DayByDayTests/VoiceClipTests.swift history · blame · raw

70 lines · 3116 bytes

8 symbols in this file
 1import AVFoundation
 2import Foundation
 3import Testing
 4@testable import DayByDay
 5
 6/// The bundled clips match the spoken-line catalog exactly.
 7struct VoiceClipTests {
 8    struct Entry: Decodable {
 9        let text: String
10        let file: String
11        let seconds: Double
12    }
13
14    static let manifestURL = Bundle(for: SpeechSynthesizer.self).url(forResource: "voice-manifest", withExtension: "json")
15    static let entries: [Entry] = manifestURL
16        .flatMap { try? Data(contentsOf: $0) }
17        .flatMap { try? JSONDecoder().decode([Entry].self, from: $0) } ?? []
18
19    @Test func manifestIsBundled() {
20        #expect(Self.manifestURL != nil)
21        #expect(ClipLibrary.bundled != nil)
22    }
23
24    @Test func catalogMatchesManifest() {
25        let texts = Set(Self.entries.map(\.text))
26        let missing = SpokenLines.all.subtracting(texts)
27        let stale = texts.subtracting(SpokenLines.all)
28        #expect(missing.isEmpty, "no clip for: \(missing.sorted().prefix(10))")
29        #expect(stale.isEmpty, "not in catalog: \(stale.sorted().prefix(10))")
30        #expect(Self.entries.count == texts.count)
31    }
32
33    @Test func bundleHasEveryClip() throws {
34        let dir = try #require(Self.manifestURL).deletingLastPathComponent()
35        let missing = Self.entries.filter { !FileManager.default.fileExists(atPath: dir.appendingPathComponent($0.file).path) }
36        #expect(missing.isEmpty, "\(missing.prefix(5).map(\.file))")
37    }
38
39    @Test func manifestDurations() throws {
40        let dir = try #require(Self.manifestURL).deletingLastPathComponent()
41        var off: [String] = []
42        for e in Self.entries {
43            let file = try AVAudioFile(forReading: dir.appendingPathComponent(e.file))
44            let seconds = Double(file.length) / file.fileFormat.sampleRate
45            if abs(seconds - e.seconds) > 0.05 { off.append("\(e.text) \(e.seconds) vs \(seconds)") }
46        }
47        #expect(off.isEmpty, "\(off.prefix(5))")
48    }
49
50    /// Playback skips `SpeechSynthesizer.leadIn`; none of it may hold speech.
51    @Test func leadInIsQuiet() throws {
52        let dir = try #require(Self.manifestURL).deletingLastPathComponent()
53        var loud: [String] = []
54        for e in Self.entries {
55            let file = try AVAudioFile(forReading: dir.appendingPathComponent(e.file))
56            let buffer = try #require(AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity: AVAudioFrameCount(file.length)))
57            try file.read(into: buffer)
58            let samples = UnsafeBufferPointer(start: buffer.floatChannelData![0], count: Int(buffer.frameLength))
59            let peak = samples.map(abs).max() ?? 0
60            let skipped = Int(SpeechSynthesizer.leadIn * file.processingFormat.sampleRate)
61            if samples.prefix(skipped).contains(where: { abs($0) > 0.01 * peak }) { loud.append(e.text) }
62        }
63        #expect(loud.isEmpty, "\(loud.prefix(5))")
64    }
65
66    @Test func onlyListedNamesHaveGreetings() {
67        #expect(Set(Self.entries.map(\.text).filter { $0.hasPrefix("Hi, ") })
68                == Set(Lines.spokenNames.map { "Hi, \($0)!" }))
69    }
70}