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/SoundSynthTests.swift

062549ad26607ce9e973ed2a9a7ef6f853885f83
daybyday/DayByDayTests/SoundSynthTests.swift history · blame · raw

104 lines · 4173 bytes

14 symbols in this file
  1import Foundation
  2import Testing
  3@testable import DayByDay
  4
  5struct SoundSynthTests {
  6    func seconds(_ samples: [Float]) -> Double { Double(samples.count) / SoundSynth.sampleRate }
  7    func rms(_ s: [Float]) -> Float { (s.reduce(0) { $0 + $1 * $1 } / Float(s.count)).squareRoot() }
  8
  9    @Test(arguments: SoundEffect.allCases) func noClipping(_ e: SoundEffect) {
 10        #expect(SoundSynth.render(e).map(abs).max()! <= 0.9)
 11    }
 12
 13    @Test(arguments: SoundEffect.allCases) func noClicks(_ e: SoundEffect) {
 14        let s = SoundSynth.render(e)
 15        #expect(s.prefix(64).allSatisfy { abs($0) < 0.01 })
 16        #expect(s.suffix(64).allSatisfy { abs($0) < 0.01 })
 17    }
 18
 19    @Test(arguments: SoundEffect.allCases) func notSilent(_ e: SoundEffect) {
 20        #expect(rms(SoundSynth.render(e)) > 0.02)
 21    }
 22
 23    @Test func durations() {
 24        #expect((0.3...0.6).contains(seconds(SoundSynth.render(.success))))
 25        #expect((0.25...0.5).contains(seconds(SoundSynth.render(.gentleMiss))))
 26        #expect((0.9...1.6).contains(seconds(SoundSynth.render(.roundComplete))))
 27    }
 28
 29    @Test func missIsQuieterThanSuccess() {
 30        #expect(rms(SoundSynth.render(.gentleMiss)) < rms(SoundSynth.render(.success)))
 31    }
 32
 33    func zeroCrossingRate(_ s: [Float]) -> Double {
 34        let crossings = zip(s, s.dropFirst()).filter { ($0 < 0) != ($1 < 0) }.count
 35        return Double(crossings) / Double(s.count)
 36    }
 37
 38    /// Number of 10 ms windows whose level rises above 0.1 after falling below 0.02.
 39    func onsets(_ s: [Float]) -> Int {
 40        let window = Int(SoundSynth.sampleRate / 100)
 41        var count = 0, quiet = true
 42        for start in stride(from: 0, to: s.count - window, by: window) {
 43            let level = rms(Array(s[start..<start + window]))
 44            if quiet, level > 0.1 { count += 1; quiet = false }
 45            if level < 0.02 { quiet = true }
 46        }
 47        return count
 48    }
 49
 50    @Test(arguments: SoundEffect.allCases) func noDCOffset(_ e: SoundEffect) {
 51        let s = SoundSynth.render(e)
 52        #expect(abs(s.reduce(0, +) / Float(s.count)) < 0.01)
 53    }
 54
 55    @Test func cueDurations() {
 56        for e in [SoundEffect.moo, .quack, .baa, .oink, .meow, .woof, .tweet] {
 57            #expect((0.35...1.6).contains(seconds(SoundSynth.render(e))), "\(e)")
 58        }
 59        for e in [SoundEffect.rain, .wind, .thunder] {
 60            #expect((1.5...2.6).contains(seconds(SoundSynth.render(e))), "\(e)")
 61        }
 62        for n in 1...5 {
 63            #expect(abs(seconds(SoundSynth.render(.beeps(n))) - (0.42 * Double(n - 1) + 0.12)) < 0.02)
 64        }
 65        #expect((0.6...1.2).contains(seconds(SoundSynth.render(.whistle))))
 66        #expect(SoundSynth.duration(.moo) == seconds(SoundSynth.render(.moo)))
 67    }
 68
 69    @Test(arguments: 1...5) func beepCountMatches(_ n: Int) {
 70        #expect(onsets(SoundSynth.render(.beeps(n))) == n)
 71    }
 72
 73    @Test func allCasesListsFiveBeeps() {
 74        #expect(SoundEffect.allCases.filter { if case .beeps = $0 { true } else { false } }.count == 5)
 75    }
 76
 77    @Test func brightAndDarkCues() {
 78        #expect(zeroCrossingRate(SoundSynth.render(.tweet)) > 5 * zeroCrossingRate(SoundSynth.render(.moo)))
 79        #expect(zeroCrossingRate(SoundSynth.render(.thunder)) < zeroCrossingRate(SoundSynth.render(.rain)) / 5)
 80    }
 81
 82    @Test func arpeggioNotDrownedByChord() {
 83        let s = SoundSynth.render(.roundComplete)
 84        let split = Int(0.38 * SoundSynth.sampleRate)
 85        let first = s[..<split].map(abs).max()!, rest = s[split...].map(abs).max()!
 86        #expect(first >= 0.5 * rest)
 87    }
 88
 89    @Test func thunderIsGentle() {
 90        let s = SoundSynth.render(.thunder)
 91        #expect(s.prefix(Int(0.05 * SoundSynth.sampleRate)).allSatisfy { abs($0) <= 0.2 })
 92        #expect(s.map(abs).max()! <= 0.6)
 93    }
 94
 95    /// Level above ~300 Hz, roughly what a phone speaker reproduces.
 96    func speakerLevel(_ s: [Float]) -> Float {
 97        var low: Float = 0
 98        return rms(s.map { x in low += 0.042 * (x - low); return x - low })
 99    }
100
101    @Test func thunderAudibleOnSpeakers() {
102        #expect(speakerLevel(SoundSynth.render(.thunder)) >= 0.5 * speakerLevel(SoundSynth.render(.wind)))
103    }
104}