Skip to content

Commit 876e3b6

Browse files
committed
fix: check all offline context options in constructor
1 parent 0a50410 commit 876e3b6

File tree

6 files changed

+110
-98
lines changed

6 files changed

+110
-98
lines changed

src/buffer.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@
22
use std::sync::Arc;
33

44
use crate::{
5-
assert_valid_channel_number, assert_valid_number_of_channels, assert_valid_sample_rate,
5+
assert_valid_buffer_length, assert_valid_channel_number, assert_valid_number_of_channels,
6+
assert_valid_sample_rate,
67
};
78

8-
#[track_caller]
9-
#[inline(always)]
10-
pub(crate) fn assert_valid_length(length: usize) {
11-
assert!(
12-
length > 0,
13-
"NotSupportedError - Invalid length: {:?} is less than or equal to minimum bound (0)",
14-
length,
15-
);
16-
}
17-
189
/// Options for constructing an [`AudioBuffer`]
1910
// dictionary AudioBufferOptions {
2011
// unsigned long numberOfChannels = 1;
@@ -91,7 +82,7 @@ impl AudioBuffer {
9182
/// 32 being defined by the MAX_CHANNELS constant.
9283
pub fn new(options: AudioBufferOptions) -> Self {
9384
assert_valid_sample_rate(options.sample_rate);
94-
assert_valid_length(options.length);
85+
assert_valid_buffer_length(options.length);
9586
assert_valid_number_of_channels(options.number_of_channels);
9687

9788
let silence = ChannelData::new(options.length);

src/context/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ mod tests {
153153

154154
#[test]
155155
fn test_audio_context_registration_traits() {
156-
let context = OfflineAudioContext::new(1, 0, 44100.);
156+
let context = OfflineAudioContext::new(1, 1, 44100.);
157157
let registration = context.mock_registration();
158158

159159
// we want to be able to ship AudioNodes to another thread, so the Registration should be
@@ -163,7 +163,7 @@ mod tests {
163163

164164
#[test]
165165
fn test_offline_audio_context_send_sync() {
166-
let context = OfflineAudioContext::new(1, 0, 44100.);
166+
let context = OfflineAudioContext::new(1, 1, 44100.);
167167
require_send_sync_static(context);
168168
}
169169

@@ -176,7 +176,7 @@ mod tests {
176176

177177
#[test]
178178
fn test_decode_audio_data() {
179-
let context = OfflineAudioContext::new(1, 0, 44100.);
179+
let context = OfflineAudioContext::new(1, 1, 44100.);
180180
let file = std::fs::File::open("samples/sample.wav").unwrap();
181181
let audio_buffer = context.decode_audio_data_sync(file).unwrap();
182182

@@ -195,15 +195,15 @@ mod tests {
195195
// disabled: symphonia cannot handle empty WAV-files
196196
#[allow(dead_code)]
197197
fn test_decode_audio_data_empty() {
198-
let context = OfflineAudioContext::new(1, 0, 44100.);
198+
let context = OfflineAudioContext::new(1, 1, 44100.);
199199
let file = std::fs::File::open("samples/empty_2c.wav").unwrap();
200200
let audio_buffer = context.decode_audio_data_sync(file).unwrap();
201201
assert_eq!(audio_buffer.length(), 0);
202202
}
203203

204204
#[test]
205205
fn test_decode_audio_data_decoding_error() {
206-
let context = OfflineAudioContext::new(1, 0, 44100.);
206+
let context = OfflineAudioContext::new(1, 1, 44100.);
207207
let file = std::fs::File::open("samples/corrupt.wav").unwrap();
208208
assert!(context.decode_audio_data_sync(file).is_err());
209209
}
@@ -214,7 +214,7 @@ mod tests {
214214
let length = 2000;
215215
let sample_rate = 96_000.;
216216

217-
let context = OfflineAudioContext::new(1, 0, 44100.);
217+
let context = OfflineAudioContext::new(1, 1, 44100.);
218218
let buffer = context.create_buffer(number_of_channels, length, sample_rate);
219219

220220
assert_eq!(buffer.number_of_channels(), 3);

src/context/offline.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use crate::events::{
99
Event, EventDispatch, EventHandler, EventPayload, EventType, OfflineAudioCompletionEvent,
1010
};
1111
use crate::render::RenderThread;
12-
use crate::{assert_valid_sample_rate, RENDER_QUANTUM_SIZE};
12+
use crate::{
13+
assert_valid_buffer_length, assert_valid_number_of_channels, assert_valid_sample_rate,
14+
RENDER_QUANTUM_SIZE,
15+
};
1316

1417
use crate::events::EventLoop;
1518
use futures_channel::{mpsc, oneshot};
@@ -72,6 +75,8 @@ impl OfflineAudioContext {
7275
#[must_use]
7376
#[allow(clippy::missing_panics_doc)]
7477
pub fn new(number_of_channels: usize, length: usize, sample_rate: f32) -> Self {
78+
assert_valid_number_of_channels(number_of_channels);
79+
assert_valid_buffer_length(length);
7580
assert_valid_sample_rate(sample_rate);
7681

7782
// communication channel to the render thread,

src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,23 @@ pub(crate) fn assert_valid_channel_number(channel_number: usize, number_of_chann
190190
);
191191
}
192192

193+
/// Assert that the given value number is a valid buffer length, i.e. greater than zero
194+
///
195+
/// # Panics
196+
///
197+
/// This function will panic if:
198+
/// - the given value is not lower than or equal to zero
199+
///
200+
#[track_caller]
201+
#[inline(always)]
202+
pub(crate) fn assert_valid_buffer_length(length: usize) {
203+
assert!(
204+
length > 0,
205+
"NotSupportedError - Invalid length: {:?} is less than or equal to minimum bound (0)",
206+
length,
207+
);
208+
}
209+
193210
/// Assert that the given value number is a valid time information, i.e. greater
194211
/// than or equal to zero and finite.
195212
///

src/node/dynamics_compressor.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -488,38 +488,37 @@ mod tests {
488488
use super::*;
489489

490490
#[test]
491-
fn test_constructor() {
492-
{
493-
let context = OfflineAudioContext::new(1, 0, 44_100.);
494-
let compressor = DynamicsCompressorNode::new(&context, Default::default());
495-
496-
assert_float_eq!(compressor.attack().value(), 0.003, abs <= 0.);
497-
assert_float_eq!(compressor.knee().value(), 30., abs <= 0.);
498-
assert_float_eq!(compressor.ratio().value(), 12., abs <= 0.);
499-
assert_float_eq!(compressor.release().value(), 0.25, abs <= 0.);
500-
assert_float_eq!(compressor.threshold().value(), -24., abs <= 0.);
501-
}
491+
fn test_constructor_default() {
492+
let context = OfflineAudioContext::new(1, 1, 44_100.);
493+
let compressor = DynamicsCompressorNode::new(&context, Default::default());
502494

503-
{
504-
let context = OfflineAudioContext::new(1, 0, 44_100.);
505-
let compressor = DynamicsCompressorNode::new(
506-
&context,
507-
DynamicsCompressorOptions {
508-
attack: 0.5,
509-
knee: 12.,
510-
ratio: 1.,
511-
release: 0.75,
512-
threshold: -60.,
513-
..DynamicsCompressorOptions::default()
514-
},
515-
);
516-
517-
assert_float_eq!(compressor.attack().value(), 0.5, abs <= 0.);
518-
assert_float_eq!(compressor.knee().value(), 12., abs <= 0.);
519-
assert_float_eq!(compressor.ratio().value(), 1., abs <= 0.);
520-
assert_float_eq!(compressor.release().value(), 0.75, abs <= 0.);
521-
assert_float_eq!(compressor.threshold().value(), -60., abs <= 0.);
522-
}
495+
assert_float_eq!(compressor.attack().value(), 0.003, abs <= 0.);
496+
assert_float_eq!(compressor.knee().value(), 30., abs <= 0.);
497+
assert_float_eq!(compressor.ratio().value(), 12., abs <= 0.);
498+
assert_float_eq!(compressor.release().value(), 0.25, abs <= 0.);
499+
assert_float_eq!(compressor.threshold().value(), -24., abs <= 0.);
500+
}
501+
502+
#[test]
503+
fn test_constructor_non_default() {
504+
let context = OfflineAudioContext::new(1, 1, 44_100.);
505+
let compressor = DynamicsCompressorNode::new(
506+
&context,
507+
DynamicsCompressorOptions {
508+
attack: 0.5,
509+
knee: 12.,
510+
ratio: 1.,
511+
release: 0.75,
512+
threshold: -60.,
513+
..DynamicsCompressorOptions::default()
514+
},
515+
);
516+
517+
assert_float_eq!(compressor.attack().value(), 0.5, abs <= 0.);
518+
assert_float_eq!(compressor.knee().value(), 12., abs <= 0.);
519+
assert_float_eq!(compressor.ratio().value(), 1., abs <= 0.);
520+
assert_float_eq!(compressor.release().value(), 0.75, abs <= 0.);
521+
assert_float_eq!(compressor.threshold().value(), -60., abs <= 0.);
523522
}
524523

525524
#[test]

0 commit comments

Comments
 (0)