-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement textual waveform support on AVAudioPCMBuffer (#2941)
* Check errors after AudioUnit interactions I spent quite a bit of time debugging an issue where `AudioUnitSetProperty` was returning error `OSStatus`. Ideally, all of these would be marked as throws, so that call-site can react appropriatelly to these errors. But I thought this is a good first step to improve the current situation. * Implement textual waveform support on AVAudioPCMBuffer - Textual waveforms can be useful as a debugging (and testing) tool - Additionally, this PR adds the ability to quick look AVAudioPCMBuffer inside of Xcode For example: - https://goq2q.net/blog/tech/using-ascii-waveforms-to-test-real-time-audio-code - https://melatonin.dev/blog/audio-sparklines/
- Loading branch information
Showing
3 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
Tests/AudioKitTests/Extension Tests/AVAudioPCMBufferReduceToBucketsTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import AudioKit | ||
import AVFoundation | ||
import Foundation | ||
import XCTest | ||
|
||
class AVAudioPCMBufferReduceToBucketsTests: XCTestCase { | ||
let sampleRate: Double = 44100 | ||
lazy var capacity = UInt32(sampleRate) | ||
lazy var format = AVAudioFormat(standardFormatWithSampleRate: sampleRate, channels: 2)! | ||
lazy var buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: capacity)! | ||
lazy var data = buffer.floatChannelData! | ||
|
||
func testOneSamplePerBucket() { | ||
for index in 0..<capacity { | ||
data[0][Int(index)] = 1 | ||
} | ||
buffer.frameLength = capacity | ||
|
||
let buckets = buffer.reduce(bucketCount: 44100) | ||
|
||
XCTAssertEqual(buckets.0.count, 44100) | ||
XCTAssertTrue(buckets.0.allSatisfy { $0 == 1 }) | ||
} | ||
|
||
func testTwoSamplesPerBucketHigherSampleReturned() { | ||
for index in 0..<Int(capacity) { | ||
data[0][index] = index.isMultiple(of: 2) ? 1 : 0.5 | ||
} | ||
buffer.frameLength = capacity | ||
|
||
let buckets = buffer.reduce(bucketCount: 22050) | ||
|
||
XCTAssertEqual(buckets.0.count, 22050) | ||
XCTAssertTrue(buckets.0.allSatisfy { $0 == 1 }) | ||
} | ||
|
||
func testTwoSamplesPerBucketHigherAbsoluteSampleReturned() { | ||
for index in 0..<Int(capacity) { | ||
data[0][index] = index.isMultiple(of: 2) ? -1 : 0.5 | ||
} | ||
buffer.frameLength = capacity | ||
|
||
let buckets = buffer.reduce(bucketCount: 22050) | ||
|
||
XCTAssertEqual(buckets.0.count, 22050) | ||
XCTAssertTrue(buckets.0.allSatisfy { $0 == -1 }) | ||
} | ||
|
||
func testLessThenOneSamplePerBucketFallbacksToOneSamplePerBucket() { | ||
for index in 0..<Int(capacity) { | ||
data[0][index] = 1 | ||
} | ||
buffer.frameLength = capacity | ||
|
||
let buckets = buffer.reduce(bucketCount: 44102) | ||
|
||
XCTAssertEqual(buckets.0.count, 44102) | ||
XCTAssertTrue(buckets.0[..<44100].allSatisfy { $0 == 1 }) | ||
XCTAssertEqual(buckets.0[44100..<44102], [0, 0]) | ||
} | ||
|
||
func testAbsoluteMax() { | ||
for index in 0..<Int(capacity) { | ||
data[0][index] = Float(index + 1) | ||
} | ||
buffer.frameLength = capacity | ||
|
||
let buckets = buffer.reduce(bucketCount: 44102) | ||
|
||
XCTAssertEqual(buckets.1, 44100) | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
Tests/AudioKitTests/Node Tests/Generator Tests/PlaygroundOscillatorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright AudioKit. All Rights Reserved. Revision History at http://github.com/AudioKit/AudioKit/ | ||
|
||
import AudioKit | ||
import AVFoundation | ||
import Foundation | ||
import GameplayKit | ||
import XCTest | ||
|
||
class PlaygroundOscillatorTests: XCTestCase { | ||
let engine = AudioEngine() | ||
|
||
override func setUp() { | ||
Settings.sampleRate = 44100 | ||
} | ||
|
||
func testSine() { | ||
let data = test(waveform: .sine) | ||
|
||
XCTAssertEqual( | ||
data.visualDescription(), | ||
""" | ||
Format 2 ch, 44100.0 Hz, deinterleaved, Float32 | ||
Frame count 44100 | ||
Frame capacity 44100 | ||
7│ * | ||
6│ ****** ***** | ||
5│ ** ** | ||
4│ ** ** | ||
3│ * * | ||
2│ ** ** | ||
1│ * * | ||
0│ * ** * | ||
1│ * * | ||
2│ ** ** | ||
3│ * * | ||
4│ ** ** | ||
5│ ** ** | ||
6│ ***** ****** | ||
7│ * | ||
""" | ||
) | ||
} | ||
|
||
func testTriangle() { | ||
let data = test(waveform: .triangle) | ||
XCTAssertEqual( | ||
data.visualDescription(), | ||
""" | ||
Format 2 ch, 44100.0 Hz, deinterleaved, Float32 | ||
Frame count 44100 | ||
Frame capacity 44100 | ||
7│ ** | ||
6│ ** ** | ||
5│ ** ** | ||
4│ ** ** | ||
3│ ** ** | ||
2│ ** ** | ||
1│ ** ** | ||
0│ **** **** | ||
1│ ** ** | ||
2│ ** ** | ||
3│ ** ** | ||
4│ ** ** | ||
5│ ** ** | ||
6│ ** ** | ||
7│ * * | ||
""" | ||
) | ||
} | ||
|
||
func testPositiveSquare() { | ||
let data = test(waveform: .positiveSquare) | ||
XCTAssertEqual( | ||
data.visualDescription(), | ||
""" | ||
Format 2 ch, 44100.0 Hz, deinterleaved, Float32 | ||
Frame count 44100 | ||
Frame capacity 44100 | ||
7│ ******************************* | ||
6│ | ||
5│ | ||
4│ | ||
3│ | ||
2│ | ||
1│ | ||
0│ ***************************** | ||
1│ | ||
2│ | ||
3│ | ||
4│ | ||
5│ | ||
6│ | ||
7│ | ||
""" | ||
) | ||
} | ||
} | ||
|
||
private extension PlaygroundOscillatorTests { | ||
func test(waveform: TableType) -> AVAudioPCMBuffer { | ||
let oscillator = PlaygroundOscillator(waveform: Table(waveform), frequency: 1) | ||
engine.output = oscillator | ||
oscillator.start() | ||
|
||
let data = engine.startTest(totalDuration: 1) | ||
data.append(engine.render(duration: 1)) | ||
return data | ||
} | ||
} |