Skip to content

Commit 5fa5bc8

Browse files
committed
Remove RENDER_QUANTUM_SIZE, add render_size_hint to AudioContextOptions
In the future `RENDER_QUANTUM_SIZE` will not be a constant, but a property of the BaseAudioContext instead that is based on the `render_size_hint`
1 parent 2dcaa1b commit 5fa5bc8

File tree

7 files changed

+39
-23
lines changed

7 files changed

+39
-23
lines changed

examples/toy_webrtc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::net::UdpSocket;
2727
use web_audio_api::context::{AudioContext, BaseAudioContext};
2828
use web_audio_api::media::Microphone;
2929
use web_audio_api::node::AudioNode;
30-
use web_audio_api::{AudioBuffer, AudioBufferOptions, RENDER_QUANTUM_SIZE};
30+
use web_audio_api::{AudioBuffer, AudioBufferOptions};
3131

3232
const MAX_UDP_SIZE: usize = 508;
3333
const SERVER_ADDR: &str = "0.0.0.0:1234";
@@ -127,7 +127,7 @@ impl Iterator for SocketStream {
127127
// construct empty buffer
128128
let options = AudioBufferOptions {
129129
number_of_channels: 1,
130-
length: RENDER_QUANTUM_SIZE,
130+
length: 128,
131131
sample_rate: self.sample_rate,
132132
};
133133
return Some(Ok(AudioBuffer::new(options)));

src/context/online.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ impl Default for AudioContextLatencyCategory {
4848
}
4949
}
5050

51+
#[derive(Copy, Clone, Debug)]
52+
#[non_exhaustive]
53+
/// This allows users to ask for a particular render quantum size.
54+
///
55+
/// Currently, only the default value is available
56+
pub enum AudioContextRenderSizeCategory {
57+
/// The default value of 128 frames
58+
Default,
59+
}
60+
61+
impl Default for AudioContextRenderSizeCategory {
62+
fn default() -> Self {
63+
Self::Default
64+
}
65+
}
66+
5167
/// Specify the playback configuration for the [`AudioContext`] constructor.
5268
///
5369
/// All fields are optional and will default to the value best suited for interactive playback on
@@ -63,7 +79,7 @@ impl Default for AudioContextLatencyCategory {
6379
/// sample_rate: Some(44100.),
6480
/// ..AudioContextOptions::default()
6581
/// };
66-
#[derive(Clone, Debug)]
82+
#[derive(Clone, Debug, Default)]
6783
pub struct AudioContextOptions {
6884
/// Identify the type of playback, which affects tradeoffs between audio output latency and
6985
/// power consumption.
@@ -77,16 +93,9 @@ pub struct AudioContextOptions {
7793
/// - use `"none"` to process the audio graph without playing through an audio output device.
7894
/// - use `"sinkId"` to use the specified audio sink id, obtained with [`enumerate_devices`]
7995
pub sink_id: String,
80-
}
8196

82-
impl Default for AudioContextOptions {
83-
fn default() -> Self {
84-
Self {
85-
latency_hint: AudioContextLatencyCategory::default(),
86-
sample_rate: None,
87-
sink_id: String::from(""),
88-
}
89-
}
97+
/// Option to request a default, optimized or specific render quantum size. It is a hint that might not be honored.
98+
pub render_size_hint: AudioContextRenderSizeCategory,
9099
}
91100

92101
/// This interface represents an audio graph whose `AudioDestinationNode` is routed to a real-time
@@ -270,6 +279,7 @@ impl AudioContext {
270279
sample_rate: Some(self.sample_rate()),
271280
latency_hint: AudioContextLatencyCategory::default(), // todo reuse existing setting
272281
sink_id,
282+
render_size_hint: AudioContextRenderSizeCategory::default(), // todo reuse existing setting
273283
};
274284
*backend_manager_guard = io::build_output(options, self.render_thread_init.clone());
275285

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
4141

4242
/// Render quantum size, the audio graph is rendered in blocks of RENDER_QUANTUM_SIZE samples
4343
/// see. <https://webaudio.github.io/web-audio-api/#render-quantum>
44-
pub const RENDER_QUANTUM_SIZE: usize = 128;
44+
pub(crate) const RENDER_QUANTUM_SIZE: usize = 128;
4545

4646
/// Maximum number of channels for audio processing
4747
pub const MAX_CHANNELS: usize = 32;

src/render/processor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ impl<'a> AudioParamValues<'a> {
9696

9797
/// Get the computed values for the given [`crate::param::AudioParam`]
9898
///
99-
/// For k-rate params or if the (a-rate) parameter is constant for this block, it will
100-
/// provide a slice of length 1. In other cases, i.e. a-rate param with scheduled
101-
/// automations it will provide a slice of length [`crate::RENDER_QUANTUM_SIZE`]
99+
/// For k-rate params or if the (a-rate) parameter is constant for this block, it will provide
100+
/// a slice of length 1. In other cases, i.e. a-rate param with scheduled automations it will
101+
/// provide a slice of length equal to the render quantum size (default: 128)
102102
#[allow(clippy::missing_panics_doc)]
103103
pub fn get(&self, index: &AudioParamId) -> impl std::ops::Deref<Target = [f32]> + '_ {
104104
DerefAudioRenderQuantumChannel(self.nodes.get(&index.into()).unwrap().borrow())

src/render/quantum.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ impl AllocInner {
7575

7676
/// Render thread channel buffer
7777
///
78-
/// Basically wraps a `Rc<[f32; RENDER_QUANTUM_SIZE]>`, which means it derefs to a (mutable) slice
78+
/// Basically wraps a `Rc<[f32; render_quantum_size]>`, which means it derefs to a (mutable) slice
7979
/// of `[f32]` sample values. Plus it has copy-on-write semantics, so it is cheap to clone.
8080
///
81+
/// The `render_quantum_size` is equal to 128 by default, but in future versions it may be equal to
82+
/// the hardware preferred render quantum size or any other value.
83+
///
8184
/// # Usage
8285
///
8386
/// Audio buffers are managed with a dedicated allocator per render thread, hence there are no
@@ -165,9 +168,12 @@ impl std::ops::Drop for AudioRenderQuantumChannel {
165168

166169
/// Render thread audio buffer, consisting of multiple channel buffers
167170
///
168-
/// Internal fixed length audio asset of `RENDER_QUANTUM_SIZE` sample frames for
169-
/// block rendering, basically a matrix of `channels * AudioRenderQuantumChannel`
170-
/// cf. <https://webaudio.github.io/web-audio-api/#render-quantum>
171+
/// This is a fixed length audio asset of `render_quantum_size` sample frames for block rendering,
172+
/// basically a matrix of `channels * AudioRenderQuantumChannel` cf.
173+
/// <https://webaudio.github.io/web-audio-api/#render-quantum>
174+
///
175+
/// The `render_quantum_size` is equal to 128 by default, but in future versions it may be equal to
176+
/// the hardware preferred render quantum size or any other value.
171177
///
172178
/// An `AudioRenderQuantum` has copy-on-write semantics, so it is cheap to clone.
173179
///

tests/graph.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use web_audio_api::context::{AudioContextRegistration, BaseAudioContext, OfflineAudioContext};
22
use web_audio_api::node::{AudioNode, ChannelConfig};
33
use web_audio_api::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum, RenderScope};
4-
use web_audio_api::RENDER_QUANTUM_SIZE;
54

65
use std::collections::HashMap;
76
use std::sync::{Arc, Mutex};
@@ -29,7 +28,7 @@ fn test_ordering_with_cycle_breakers(
2928
nodes.shuffle(&mut rng);
3029
edges.shuffle(&mut rng);
3130

32-
let context = OfflineAudioContext::new(1, RENDER_QUANTUM_SIZE, 44_100.);
31+
let context = OfflineAudioContext::new(1, 128, 44_100.);
3332
let collect = Arc::new(Mutex::new(vec![]));
3433

3534
let map: HashMap<_, _> = nodes

tests/offline.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use web_audio_api::context::OfflineAudioContext;
44
use web_audio_api::node::{
55
AudioNode, AudioScheduledSourceNode, OscillatorNode, OscillatorOptions, OscillatorType,
66
};
7-
use web_audio_api::RENDER_QUANTUM_SIZE;
7+
8+
const RENDER_QUANTUM_SIZE: usize = 128;
89

910
#[test]
1011
fn test_offline_render() {

0 commit comments

Comments
 (0)