Skip to content

Commit

Permalink
Work around annoying Rust warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ameobea committed Jan 31, 2024
1 parent b50f3e7 commit 58bdf0c
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 51 deletions.
5 changes: 5 additions & 0 deletions engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion engine/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static mut RNG: Pcg32 =
unsafe { std::mem::transmute([5573589319906701683u64, 1442695040888963407u64]) };

#[inline(always)]
pub fn rng() -> &'static mut Pcg32 { unsafe { &mut RNG } }
pub fn rng() -> &'static mut Pcg32 { ref_static_mut!(RNG) }

pub fn uuid_v4() -> Uuid {
let entropy: (u64, i64) = rng().gen();
Expand All @@ -34,3 +34,12 @@ pub fn set_raw_panic_hook(log_err: unsafe extern "C" fn(ptr: *const u8, len: usi

std::panic::set_hook(Box::new(hook))
}

/// Implements `&mut *std::ptr::addr_of_mut!(x)` to work around the annoying new Rust rules on
/// referencing static muts
#[macro_export]
macro_rules! ref_static_mut {
($x:expr) => {
unsafe { &mut *std::ptr::addr_of_mut!($x) }
};
}
4 changes: 2 additions & 2 deletions engine/compressor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn detect_level_peak(
buf: &CircularBuffer<MAX_LOOKAHEAD_SAMPLES>,
lookahead_samples: isize,
sample_ix_in_frame: usize,
old_max: f32,
_old_max: f32,
) -> f32 {
// Try to fast-path. If the old max hasn't been removed from the lookahead buffer yet and it's
// still the max, then we can just return it.
Expand Down Expand Up @@ -278,7 +278,7 @@ impl Compressor {
top_threshold_db: f32,
bottom_ratio: f32,
top_ratio: f32,
knee: f32,
_knee: f32,
sensing_method: SensingMethod,
post_gain: f32,
) -> f32 {
Expand Down
1 change: 1 addition & 0 deletions engine/event_scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
heapless = { version = "0.7", default-features = false }
float-ord = "0.3"
common = { path = "../common" }
9 changes: 5 additions & 4 deletions engine/event_scheduler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cmp::Reverse;

use common::ref_static_mut;
use float_ord::FloatOrd;
use heapless::binary_heap::{BinaryHeap, Min};

Expand Down Expand Up @@ -127,7 +128,7 @@ fn handle_event(evt: ScheduledEvent) {

#[no_mangle]
pub extern "C" fn run(raw_cur_time: f64, cur_beats: f64) {
let scheduled_events = unsafe { &mut SCHEDULED_EVENTS };
let scheduled_events = ref_static_mut!(SCHEDULED_EVENTS);
loop {
match scheduled_events.peek() {
None => break,
Expand All @@ -139,7 +140,7 @@ pub extern "C" fn run(raw_cur_time: f64, cur_beats: f64) {
handle_event(evt);
}

let scheduled_beat_events = unsafe { &mut SCHEDULED_BEAT_EVENTS };
let scheduled_beat_events = ref_static_mut!(SCHEDULED_BEAT_EVENTS);
loop {
match scheduled_beat_events.peek() {
None => break,
Expand Down Expand Up @@ -172,8 +173,8 @@ pub unsafe extern "C" fn alloc_ids_buffer(count: usize) -> *mut i32 {
#[no_mangle]
pub extern "C" fn cancel_events_by_ids() -> usize {
let ids = unsafe { &*IDS_BUFFER }.as_slice();
let scheduled_events = unsafe { &mut SCHEDULED_EVENTS };
let scheduled_beat_events = unsafe { &mut SCHEDULED_BEAT_EVENTS };
let scheduled_events = ref_static_mut!(SCHEDULED_EVENTS);
let scheduled_beat_events = ref_static_mut!(SCHEDULED_BEAT_EVENTS);

let mut actually_cancelled_evt_count = 0;

Expand Down
3 changes: 2 additions & 1 deletion engine/granular/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! Several aspects of this design draw significant inspiration from the Clouds eurorack module made
//! by Mutable Instruments and associated code which is available on Github: https://github.com/pichenettes/eurorack
use common::ref_static_mut;
use dsp::{clamp, filters::butterworth::ButterworthFilter, mix, read_interpolated, smooth};
use rand::prelude::*;

Expand All @@ -28,7 +29,7 @@ impl Default for ReverseState {
}

static mut SCRATCH: [(f32, f32); 8192] = [(0.0, 0.0); 8192];
fn scratch() -> &'static mut [(f32, f32); 8192] { unsafe { &mut SCRATCH } }
fn scratch() -> &'static mut [(f32, f32); 8192] { ref_static_mut!(SCRATCH) }

#[derive(Clone)]
pub struct GranularVoice {
Expand Down
1 change: 1 addition & 0 deletions engine/midi_quantizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
dsp = { path = "../dsp" }
common = { path = "../common" }
4 changes: 3 additions & 1 deletion engine/midi_quantizer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use common::ref_static_mut;

extern "C" {
fn play_note(note: usize);

Expand Down Expand Up @@ -37,7 +39,7 @@ impl MIDIQuantizerState {
}

static mut STATE: MIDIQuantizerState = MIDIQuantizerState::default();
fn state() -> &'static mut MIDIQuantizerState { unsafe { &mut STATE } }
fn state() -> &'static mut MIDIQuantizerState { ref_static_mut!(STATE) }

#[no_mangle]
pub extern "C" fn set_octave_range(low: isize, high: isize) {
Expand Down
1 change: 1 addition & 0 deletions engine/multiband_diode_ladder_distortion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
dsp = { path = "../dsp" }
common = { path = "../common" }
24 changes: 12 additions & 12 deletions engine/multiband_diode_ladder_distortion/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common::ref_static_mut;
use dsp::{band_splitter::BandSplitter, FRAME_SIZE};

static mut INPUT_BUFFER: [f32; FRAME_SIZE] = [0.0; FRAME_SIZE];
Expand All @@ -15,33 +16,32 @@ pub extern "C" fn init() {

#[no_mangle]
pub extern "C" fn get_input_buf_ptr() -> *mut f32 {
unsafe { &mut INPUT_BUFFER as *mut [f32; FRAME_SIZE] as *mut f32 }
unsafe { std::ptr::addr_of_mut!(INPUT_BUFFER) as *mut f32 }
}

#[no_mangle]
pub extern "C" fn get_low_output_buf_ptr() -> *mut f32 {
unsafe { &mut LOW_BAND_OUTPUT_BUFFER as *mut [f32; FRAME_SIZE] as *mut f32 }
unsafe { std::ptr::addr_of_mut!(LOW_BAND_OUTPUT_BUFFER) as *mut f32 }
}

#[no_mangle]
pub extern "C" fn get_mid_output_buf_ptr() -> *mut f32 {
unsafe { &mut MID_BAND_OUTPUT_BUFFER as *mut [f32; FRAME_SIZE] as *mut f32 }
unsafe { std::ptr::addr_of_mut!(MID_BAND_OUTPUT_BUFFER) as *mut f32 }
}

#[no_mangle]
pub extern "C" fn get_high_output_buf_ptr() -> *mut f32 {
unsafe { &mut HIGH_BAND_OUTPUT_BUFFER as *mut [f32; FRAME_SIZE] as *mut f32 }
unsafe { std::ptr::addr_of_mut!(HIGH_BAND_OUTPUT_BUFFER) as *mut f32 }
}

#[no_mangle]
pub extern "C" fn process() {
let splitter = unsafe { &mut *BAND_SPLITTER };
unsafe {
splitter.apply_frame(
&INPUT_BUFFER,
&mut LOW_BAND_OUTPUT_BUFFER,
&mut MID_BAND_OUTPUT_BUFFER,
&mut HIGH_BAND_OUTPUT_BUFFER,
);
}

splitter.apply_frame(
ref_static_mut!(INPUT_BUFFER),
ref_static_mut!(LOW_BAND_OUTPUT_BUFFER),
ref_static_mut!(MID_BAND_OUTPUT_BUFFER),
ref_static_mut!(HIGH_BAND_OUTPUT_BUFFER),
);
}
10 changes: 7 additions & 3 deletions engine/noise_gen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use common::rng;
use common::{ref_static_mut, rng};
use rand::prelude::*;

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -87,14 +87,18 @@ pub unsafe extern "C" fn generate() -> *const f32 {
NoiseType::Pink => todo!(),
NoiseType::Brown => todo!(),
};
for out in &mut OUTPUT {
for out in ref_static_mut!(OUTPUT) {
let sample = generator() * GAIN;

if QUANTIZATION_FACTOR > 0 {
LAST_VAL = dsp::quantize(-1., 1., QUANTIZATION_FACTOR as f32, sample);
} else {
if SMOOTHING_COEFFICIENT != 0. {
dsp::one_pole(&mut LAST_VAL, sample, 1. - SMOOTHING_COEFFICIENT);
dsp::one_pole(
ref_static_mut!(LAST_VAL),
sample,
1. - SMOOTHING_COEFFICIENT,
);
} else {
LAST_VAL = sample;
}
Expand Down
1 change: 1 addition & 0 deletions engine/oscilloscope/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
dsp = { path = "../dsp" }
canvas_utils = { path = "../canvas_utils" }
common = { path = "../common" }
23 changes: 12 additions & 11 deletions engine/oscilloscope/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use self::oscilloscope::{PreviousWindow, Viz, WindowLength};
use canvas_utils::VizView;
use common::ref_static_mut;
use f0_estimation::YinCtx;

pub(crate) mod conf;
Expand Down Expand Up @@ -84,21 +85,21 @@ pub extern "C" fn oscilloscope_renderer_set_view(
) {
maybe_set_panic_hook();

let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
viz.set_view(cur_bpm, VizView { width, height, dpr });
}

#[no_mangle]
pub extern "C" fn oscilloscope_renderer_set_window(window_mode: u8, window_length: f32) {
let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
let window = WindowLength::from_parts(window_mode, window_length);
viz.set_window(window);
}

/// Process all samples in `FRAME_DATA_BUFFER` and update the viz
#[no_mangle]
pub extern "C" fn oscilloscope_renderer_process(cur_bpm: f32, cur_beat: f32, cur_time: f32) {
let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
viz.process(cur_bpm, cur_beat, cur_time);
}

Expand All @@ -109,44 +110,44 @@ pub extern "C" fn oscilloscope_renderer_get_frame_data_ptr() -> *const f32 {

#[no_mangle]
pub extern "C" fn oscilloscope_renderer_commit_samples() {
let viz = unsafe { &mut VIZ };
let frame_data = unsafe { &FRAME_DATA_BUFFER };
let viz = ref_static_mut!(VIZ);
let frame_data = ref_static_mut!(FRAME_DATA_BUFFER);
viz.commit_samples(frame_data);
}

#[no_mangle]
pub extern "C" fn oscilloscope_get_image_data_buf_ptr() -> *const u8 {
let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
viz.get_image_data().as_ptr()
}

#[no_mangle]
pub extern "C" fn oscilloscope_get_image_data_buf_len() -> usize {
let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
viz.get_image_data().len()
}

#[no_mangle]
pub extern "C" fn oscilloscope_renderer_set_frozen(frozen: bool) {
let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
viz.set_frozen(frozen);
}

#[no_mangle]
pub extern "C" fn oscilloscope_renderer_set_frame_by_frame(frame_by_frame: bool) {
let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
viz.set_frame_by_frame(frame_by_frame);
}

/// Returns pointer to zero-terminated string
#[no_mangle]
pub extern "C" fn oscilloscope_renderer_get_detected_f0_display() -> *const u8 {
let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
viz.yin_ctx.get_detected_f0_display()
}

#[no_mangle]
pub extern "C" fn oscilloscope_renderer_set_snap_f0_to_midi(snap_f0_to_midi: bool) {
let viz = unsafe { &mut VIZ };
let viz = ref_static_mut!(VIZ);
viz.snap_f0_to_midi = snap_f0_to_midi;
}
5 changes: 2 additions & 3 deletions engine/polysynth/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! Synth state management. Handles keeping track of what each voice of each polyphonic synth
//! is playing and passing the correct commands through to the WebAudio synths.
#[cfg(feature = "wasm-bindgen")]
#[macro_use]
extern crate wasm_bindgen;
#[cfg(feature = "wasm-bindgen")]
extern crate common;
#[cfg(feature = "wasm-bindgen")]
extern crate js_sys;
#[cfg(feature = "wasm-bindgen")]
extern crate wasm_bindgen;

#[macro_use]
extern crate log;
Expand Down
1 change: 1 addition & 0 deletions engine/quantizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
dsp = { path = "../dsp" }
common = { path = "../common" }
5 changes: 3 additions & 2 deletions engine/quantizer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common::ref_static_mut;
use dsp::FRAME_SIZE;

#[repr(u8)]
Expand Down Expand Up @@ -53,7 +54,7 @@ static mut STATE: QuantizerState = QuantizerState {
quantization_interval: 1.,
mode: QuantizationMode::Round,
};
fn state() -> &'static mut QuantizerState { unsafe { &mut STATE } }
fn state() -> &'static mut QuantizerState { ref_static_mut!(STATE) }

#[no_mangle]
pub extern "C" fn set_quantization_state(quantization_interval: f32, mode: u8) {
Expand All @@ -63,7 +64,7 @@ pub extern "C" fn set_quantization_state(quantization_interval: f32, mode: u8) {
}

static mut IO_BUFFER: [f32; FRAME_SIZE] = [0.; FRAME_SIZE];
fn io_buf() -> &'static mut [f32; FRAME_SIZE] { unsafe { &mut IO_BUFFER } }
fn io_buf() -> &'static mut [f32; FRAME_SIZE] { ref_static_mut!(IO_BUFFER) }

#[no_mangle]
pub extern "C" fn get_io_buf_ptr() -> *mut f32 { io_buf().as_mut_ptr() }
Expand Down
3 changes: 2 additions & 1 deletion engine/wavegen/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::f32::consts::PI;

use common::ref_static_mut;
use waveform_renderer::WaveformRendererCtx;

extern "C" {
Expand Down Expand Up @@ -81,7 +82,7 @@ fn get_waveform_renderer_ctx() -> &'static mut WaveformRendererCtx {
pub extern "C" fn wavegen_render_waveform() -> *const u8 {
common::set_raw_panic_hook(log_err);

let state = unsafe { &mut ENCODED_STATE_BUF };
let state = ref_static_mut!(ENCODED_STATE_BUF);
let magnitudes = &mut state[..HARMONIC_COUNT];
// normalize magnitudes
let max_magnitude = magnitudes.iter().fold(0.0f32, |acc, x| acc.max(*x));
Expand Down
Loading

0 comments on commit 58bdf0c

Please sign in to comment.