Skip to content

Commit f7551f7

Browse files
committed
Implement audio file strategy
1 parent 7088df6 commit f7551f7

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Snapshot audio tests are snapshot tested itself. Please find many examples in: [
6666
- [x] Add a link to swift snapshot testing
6767
- [ ] Multi level comparison (first hash, then data, then image)
6868
- [ ] Use accelerate in downsampling
69-
- [ ] Add file strategy
69+
- [x] Add file strategy
7070

7171
## Contributing
7272

Sources/AudioSnapshotTesting/AudioSnapshotTesting.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,45 @@ typealias PlatformColor = UIColor
2525
typealias PlatformHostingView = _UIHostingView
2626
#endif
2727

28+
/// Generates a audio file snapshot of the given `AVAudioPCMBuffer.
29+
extension Snapshotting where Value == AVAudioPCMBuffer, Format == Data {
30+
public static func audio() -> Snapshotting<AVAudioPCMBuffer, Data> {
31+
return Snapshotting<AVAudioPCMBuffer, Data>(
32+
pathExtension: "wav",
33+
diffing: Diffing(
34+
toData: { $0 },
35+
fromData: { $0 },
36+
diff: { old, new in
37+
old == new ? nil : ("Audio buffers differ", [])
38+
}
39+
),
40+
asyncSnapshot: { buffer in
41+
Async<Data> { callback in
42+
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory())
43+
.appendingPathComponent(UUID().uuidString)
44+
.appendingPathExtension("wav")
45+
46+
do {
47+
let file = try AVAudioFile(forWriting: tempURL, settings: buffer.format.settings)
48+
try file.write(from: buffer)
49+
if #available(iOS 18.0, macOS 15, *) {
50+
file.close()
51+
}
52+
defer {
53+
try? FileManager.default.removeItem(at: tempURL)
54+
}
55+
56+
let data = try Data(contentsOf: tempURL)
57+
callback(data)
58+
} catch {
59+
fatalError("Failed to create audio snapshot: \(error)")
60+
}
61+
}
62+
}
63+
)
64+
}
65+
}
66+
2867
@MainActor
2968
public extension Snapshotting where Format == PlatformImage, Value == (AVAudioPCMBuffer, AVAudioPCMBuffer) {
3069
/// Generates a overlayed waveform snapshot of the given tuple of `AVAudioPCMBuffer`s.

Tests/AudioSnapshotTestingTests/AudioSnapshotTestingTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ private func synthesizeSignal(
184184
return signal
185185
}
186186

187+
@Test("Audio snapshot of synthesized signal", .snapshots(record: false))
188+
func audioSnapshotSynthesized() async throws {
189+
let signal = synthesizeSignal(
190+
frequencyAmplitudePairs: [(440, 0.5), (880, 0.3)],
191+
count: 4410
192+
)
193+
let buffer = createBuffer(from: signal)
194+
195+
assertSnapshot(
196+
of: buffer,
197+
as: .audio(),
198+
named: "440-880hz"
199+
)
200+
}
201+
187202
private func createBuffer(from samples: [Float]) -> AVAudioPCMBuffer {
188203
let format = AVAudioFormat(standardFormatWithSampleRate: 32768, channels: 1)!
189204
let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(samples.count))!

0 commit comments

Comments
 (0)