Skip to content

Commit 7d10ff7

Browse files
authored
Merge pull request #316 from uklotzde/msrv-1.70
Update MSRV to 1.70 and remove lazy_static and once_cell
2 parents bfd09f7 + 61a3a35 commit 7d10ff7

File tree

12 files changed

+114
-49
lines changed

12 files changed

+114
-49
lines changed

.github/workflows/msrv.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow
2+
3+
name: msrv
4+
5+
# read-only repo token, no access to secrets
6+
permissions:
7+
contents: read
8+
9+
on:
10+
push:
11+
branches: [main]
12+
pull_request:
13+
14+
env:
15+
CARGO_TERM_COLOR: always
16+
17+
jobs:
18+
check-workspace:
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- name: Install ALSA and Jack dependencies
23+
run: |
24+
sudo apt-get update && sudo apt-get install -y libasound2-dev libjack-jackd2-dev cmake
25+
26+
- name: Install Rust toolchain
27+
# Aligned with `rust-version` in `Cargo.toml`
28+
uses: dtolnay/[email protected]
29+
30+
- name: Check out repository
31+
uses: actions/checkout@v3
32+
33+
- name: Generate Cargo.lock
34+
run: cargo generate-lockfile
35+
36+
- name: Cache Rust toolchain and build artifacts
37+
uses: Swatinem/rust-cache@v2
38+
with:
39+
# Distinguished by the action name to avoid sharing!
40+
shared-key: "msrv"
41+
42+
- name: Check workspace
43+
run: cargo check --locked --workspace --verbose --all-targets --all-features

.github/workflows/pre-commit.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Same name as the file for consistent identification
44
name: pre-commit
55

6+
# read-only repo token, no access to secrets
67
permissions:
78
contents: read
89

.github/workflows/rust.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
name: Build
44

5-
# read-only repo token
5+
# read-only repo token, no access to secrets
6+
permissions:
7+
contents: read
8+
69
# no access to secrets
710
on:
811
push:
@@ -35,6 +38,9 @@ jobs:
3538
# restore cargo cache from previous runs
3639
- name: Rust Cache
3740
uses: Swatinem/rust-cache@v2
41+
with:
42+
# Distinguished by the action name to avoid sharing!
43+
shared-key: "rust"
3844

3945
# check it builds
4046
- name: Build

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ repos:
4242
- id: codespell
4343
args: [--ignore-words=.codespellignore]
4444
- repo: https://github.com/sirosen/check-jsonschema
45-
rev: 0.23.2
45+
rev: 0.23.3
4646
hooks:
4747
- id: check-github-actions
4848
- id: check-github-workflows
4949
- repo: https://github.com/pre-commit/mirrors-prettier
50-
rev: v3.0.0-alpha.9-for-vscode
50+
rev: v3.0.0
5151
hooks:
5252
- id: prettier
5353
types_or:

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ keywords = ["web-audio-api", "audio", "sound", "dsp"]
1010
license = "MIT"
1111
categories = ["multimedia::audio"]
1212
exclude = ["/samples", "/showcase", "/.github"]
13+
rust-version = "1.70"
1314

1415
[dependencies]
1516
arc-swap = "1.6"
@@ -22,10 +23,8 @@ dasp_sample = "0.11"
2223
float_eq = "1.0"
2324
hound = "3.5"
2425
hrtf = "0.8"
25-
lazy_static = "1.4"
2626
log = "0.4"
2727
num-complex = "0.4"
28-
once_cell = "1.10"
2928
realfft = "3.0"
3029
rubato = "0.14"
3130
rustc-hash = "1.1"

src/node/audio_buffer_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use once_cell::sync::OnceCell;
1+
use std::cell::OnceCell;
22
use std::sync::atomic::{AtomicBool, Ordering};
33
use std::sync::Arc;
44

src/node/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
//! The AudioNode interface and concrete types
22
use std::f32::consts::PI;
33
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
4-
use std::sync::Arc;
4+
use std::sync::{Arc, OnceLock};
55

66
use crate::context::{AudioContextRegistration, ConcreteBaseAudioContext};
77
use crate::events::{ErrorEvent, EventHandler, EventPayload, EventType};
88
use crate::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum, RenderScope};
99
use crate::AudioBufferIter;
1010
use crate::Event;
1111

12-
use lazy_static::lazy_static;
13-
1412
mod analyser;
1513
pub use analyser::*;
1614
mod audio_buffer_source;
@@ -58,14 +56,15 @@ pub(crate) const TABLE_LENGTH_BY_4_USIZE: usize = TABLE_LENGTH_USIZE / 4;
5856
pub(crate) const TABLE_LENGTH_F32: f32 = TABLE_LENGTH_USIZE as f32;
5957
pub(crate) const TABLE_LENGTH_BY_4_F32: f32 = TABLE_LENGTH_BY_4_USIZE as f32;
6058

61-
// Compute one period sine wavetable of size TABLE_LENGTH
62-
lazy_static! {
63-
pub(crate) static ref SINETABLE: Vec<f32> = {
64-
let table: Vec<f32> = (0..TABLE_LENGTH_USIZE)
59+
/// Precomputed sine table
60+
pub(crate) fn precomputed_sine_table() -> &'static [f32] {
61+
static INSTANCE: OnceLock<Vec<f32>> = OnceLock::new();
62+
INSTANCE.get_or_init(|| {
63+
// Compute one period sine wavetable of size TABLE_LENGTH
64+
(0..TABLE_LENGTH_USIZE)
6565
.map(|x| ((x as f32) * 2.0 * PI * (1. / (TABLE_LENGTH_F32))).sin())
66-
.collect();
67-
table
68-
};
66+
.collect()
67+
})
6968
}
7069

7170
/// How channels must be matched between the node's inputs and outputs.

src/node/oscillator.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum, Render
99
use crate::RENDER_QUANTUM_SIZE;
1010

1111
use super::{
12-
AudioNode, AudioScheduledSourceNode, ChannelConfig, ChannelConfigOptions, SINETABLE,
13-
TABLE_LENGTH_USIZE,
12+
precomputed_sine_table, AudioNode, AudioScheduledSourceNode, ChannelConfig,
13+
ChannelConfigOptions, TABLE_LENGTH_USIZE,
1414
};
1515

1616
/// Options for constructing an [`OscillatorNode`]
@@ -222,6 +222,7 @@ impl OscillatorNode {
222222
started: false,
223223
periodic_wave: None,
224224
ended_triggered: false,
225+
sine_table: precomputed_sine_table(),
225226
};
226227

227228
let node = Self {
@@ -327,6 +328,8 @@ struct OscillatorRenderer {
327328
periodic_wave: Option<PeriodicWave>,
328329
/// defines if the `ended` events was already dispatched
329330
ended_triggered: bool,
331+
/// Precomputed sine table
332+
sine_table: &'static [f32],
330333
}
331334

332335
impl AudioProcessor for OscillatorRenderer {
@@ -467,7 +470,7 @@ impl OscillatorRenderer {
467470

468471
// linear interpolation into lookup table
469472
let k = (position - floored) as f32;
470-
SINETABLE[prev_index].mul_add(1. - k, SINETABLE[next_index] * k)
473+
self.sine_table[prev_index].mul_add(1. - k, self.sine_table[next_index] * k)
471474
}
472475

473476
#[inline]

src/node/stereo_panner.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::param::{AudioParam, AudioParamDescriptor};
44
use crate::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum, RenderScope};
55

66
use super::{
7-
AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
8-
SINETABLE, TABLE_LENGTH_BY_4_F32, TABLE_LENGTH_BY_4_USIZE,
7+
precomputed_sine_table, AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode,
8+
ChannelInterpretation, TABLE_LENGTH_BY_4_F32, TABLE_LENGTH_BY_4_USIZE,
99
};
1010

1111
/// Options for constructing a [`StereoPannerOptions`]
@@ -69,10 +69,11 @@ fn assert_valid_channel_count_mode(mode: ChannelCountMode) {
6969
/// - `gain_left = (x * PI / 2.).cos()`
7070
/// - `gain_right = (x * PI / 2.).sin()`
7171
#[inline(always)]
72-
fn get_stereo_gains(x: f32) -> [f32; 2] {
72+
fn get_stereo_gains(sine_table: &[f32], x: f32) -> [f32; 2] {
7373
let idx = (x * TABLE_LENGTH_BY_4_F32) as usize;
74-
let gain_left = SINETABLE[idx + TABLE_LENGTH_BY_4_USIZE];
75-
let gain_right = SINETABLE[idx];
74+
75+
let gain_left = sine_table[idx + TABLE_LENGTH_BY_4_USIZE];
76+
let gain_right = sine_table[idx];
7677

7778
[gain_left, gain_right]
7879
}
@@ -183,7 +184,7 @@ impl StereoPannerNode {
183184

184185
pan_param.set_value(options.pan);
185186

186-
let renderer = StereoPannerRenderer { pan: pan_proc };
187+
let renderer = StereoPannerRenderer::new(pan_proc);
187188

188189
let node = Self {
189190
registration,
@@ -207,6 +208,16 @@ struct StereoPannerRenderer {
207208
/// Position of the input in the output’s stereo image.
208209
/// -1 represents full left, +1 represents full right.
209210
pan: AudioParamId,
211+
sine_table: &'static [f32],
212+
}
213+
214+
impl StereoPannerRenderer {
215+
fn new(pan: AudioParamId) -> Self {
216+
Self {
217+
pan,
218+
sine_table: precomputed_sine_table(),
219+
}
220+
}
210221
}
211222

212223
impl AudioProcessor for StereoPannerRenderer {
@@ -239,7 +250,7 @@ impl AudioProcessor for StereoPannerRenderer {
239250
if pan_values.len() == 1 {
240251
let pan = pan_values[0];
241252
let x = (pan + 1.) * 0.5;
242-
let [gain_left, gain_right] = get_stereo_gains(x);
253+
let [gain_left, gain_right] = get_stereo_gains(self.sine_table, x);
243254

244255
left.iter_mut()
245256
.zip(right.iter_mut())
@@ -255,7 +266,7 @@ impl AudioProcessor for StereoPannerRenderer {
255266
.zip(input.channel_data(0).iter())
256267
.for_each(|(((l, r), pan), input)| {
257268
let x = (pan + 1.) * 0.5;
258-
let [gain_left, gain_right] = get_stereo_gains(x);
269+
let [gain_left, gain_right] = get_stereo_gains(self.sine_table, x);
259270

260271
*l = input * gain_left;
261272
*r = input * gain_right;
@@ -266,7 +277,7 @@ impl AudioProcessor for StereoPannerRenderer {
266277
if pan_values.len() == 1 {
267278
let pan = pan_values[0];
268279
let x = if pan <= 0. { pan + 1. } else { pan };
269-
let [gain_left, gain_right] = get_stereo_gains(x);
280+
let [gain_left, gain_right] = get_stereo_gains(self.sine_table, x);
270281

271282
left.iter_mut()
272283
.zip(right.iter_mut())
@@ -290,13 +301,13 @@ impl AudioProcessor for StereoPannerRenderer {
290301
.for_each(|((((l, r), &pan), &input_left), &input_right)| {
291302
if pan <= 0. {
292303
let x = pan + 1.;
293-
let [gain_left, gain_right] = get_stereo_gains(x);
304+
let [gain_left, gain_right] = get_stereo_gains(self.sine_table, x);
294305

295306
*l = input_right.mul_add(gain_left, input_left);
296307
*r = input_right * gain_right;
297308
} else {
298309
let x = pan;
299-
let [gain_left, gain_right] = get_stereo_gains(x);
310+
let [gain_left, gain_right] = get_stereo_gains(self.sine_table, x);
300311

301312
*l = input_left * gain_left;
302313
*r = input_left.mul_add(gain_right, input_right);
@@ -345,11 +356,13 @@ mod tests {
345356

346357
#[test]
347358
fn test_get_stereo_gains() {
359+
let sine_table = precomputed_sine_table();
360+
348361
// check correctness of wavetable lookup
349362
for i in 0..1001 {
350363
let x = i as f32 / 1000.;
351364

352-
let [gain_left, gain_right] = get_stereo_gains(x);
365+
let [gain_left, gain_right] = get_stereo_gains(sine_table, x);
353366

354367
assert_float_eq!(
355368
gain_left,

src/node/waveshaper.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use std::sync::{
2-
atomic::{AtomicU32, Ordering},
3-
Arc,
1+
use std::{
2+
cell::OnceCell,
3+
sync::{
4+
atomic::{AtomicU32, Ordering},
5+
Arc,
6+
},
47
};
58

69
use crossbeam_channel::{Receiver, Sender};
7-
use once_cell::sync::OnceCell;
810
use rubato::{FftFixedInOut, Resampler as _};
911

1012
use crate::{

0 commit comments

Comments
 (0)