1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
//! PeriodicWave interface
use std::f32::consts::PI;
use std::sync::Arc;
use crate::context::BaseAudioContext;
use crate::node::TABLE_LENGTH_USIZE;
/// Options for constructing a [`PeriodicWave`]
#[derive(Debug, Default, Clone)]
pub struct PeriodicWaveOptions {
/// The real parameter represents an array of cosine terms of Fourier series.
///
/// The first element (index 0) represents the DC-offset.
/// This offset has to be given but will not be taken into account
/// to build the custom periodic waveform.
///
/// The following elements (index 1 and more) represent the fundamental and
/// harmonics of the periodic waveform.
pub real: Option<Vec<f32>>,
/// The imag parameter represents an array of sine terms of Fourier series.
///
/// The first element (index 0) will not be taken into account
/// to build the custom periodic waveform.
///
/// The following elements (index 1 and more) represent the fundamental and
/// harmonics of the periodic waveform.
pub imag: Option<Vec<f32>>,
/// By default PeriodicWave is build with normalization enabled (disable_normalization = false).
/// In this case, a peak normalization is applied to the given custom periodic waveform.
///
/// If disable_normalization is enabled (disable_normalization = true), the normalization is
/// defined by the periodic waveform characteristics (img, and real fields).
pub disable_normalization: bool,
}
/// `PeriodicWave` represents an arbitrary periodic waveform to be used with an `OscillatorNode`.
///
/// - MDN documentation: <https://developer.mozilla.org/en-US/docs/Web/API/PeriodicWave>
/// - specification: <https://webaudio.github.io/web-audio-api/#PeriodicWave>
/// - see also: [`BaseAudioContext::create_periodic_wave`]
/// - see also: [`OscillatorNode`](crate::node::OscillatorNode)
///
/// # Usage
///
/// ```no_run
/// use web_audio_api::context::{BaseAudioContext, AudioContext};
/// use web_audio_api::{PeriodicWave, PeriodicWaveOptions};
/// use web_audio_api::node::{AudioNode, AudioScheduledSourceNode};
///
/// let context = AudioContext::default();
///
/// // generate a simple waveform with 2 harmonics
/// let options = PeriodicWaveOptions {
/// real: Some(vec![0., 0., 0.]),
/// imag: Some(vec![0., 0.5, 0.5]),
/// disable_normalization: false,
/// };
///
/// let periodic_wave = PeriodicWave::new(&context, options);
///
/// let mut osc = context.create_oscillator();
/// osc.set_periodic_wave(periodic_wave);
/// osc.connect(&context.destination());
/// osc.start();
/// ```
/// # Examples
///
/// - `cargo run --release --example oscillators`
///
// Basically a wrapper around Arc<Vec<f32>>, so `PeriodicWave`s are cheap to clone
#[derive(Debug, Clone, Default)]
pub struct PeriodicWave {
wavetable: Arc<Vec<f32>>,
}
impl PeriodicWave {
/// Returns a `PeriodicWave`
///
/// # Arguments
///
/// * `real` - The real parameter represents an array of cosine terms of Fourier series.
/// * `imag` - The imag parameter represents an array of sine terms of Fourier series.
/// * `constraints` - The constraints parameter specifies the normalization mode of the `PeriodicWave`
///
/// # Panics
///
/// Will panic if:
///
/// * `real` is defined and its length is less than 2
/// * `imag` is defined and its length is less than 2
/// * `real` and `imag` are defined and theirs lengths are not equal
/// * `PeriodicWave` is more than 8192 components
//
// @notes:
// - Current implementation is very naive and could be improved using inverse
// FFT or table lookup on SINETABLE. Such performance improvements should be
// however tested also against this implementation.
// - Built-in types of the `OscillatorNode` should use periodic waves
// c.f. https://webaudio.github.io/web-audio-api/#oscillator-coefficients
// - The question of bandlimited oscillators should also be handled
// e.g. https://www.dafx12.york.ac.uk/papers/dafx12_submission_69.pdf
pub fn new<C: BaseAudioContext>(_context: &C, options: PeriodicWaveOptions) -> Self {
let PeriodicWaveOptions {
real,
imag,
disable_normalization,
} = options;
let (real, imag) = match (real, imag) {
(Some(r), Some(i)) => {
if r.len() != i.len() {
panic!("IndexSizeError: `real` and `imag` length should be equal");
} else if r.len() < 2 {
// i and r have same length
panic!("IndexSizeError: `real` and `imag` length should at least 2");
}
(r, i)
}
(Some(r), None) => {
if r.len() < 2 {
panic!("IndexSizeError: `real` and `imag` length should at least 2");
}
let len = r.len();
(r, vec![0.; len])
}
(None, Some(i)) => {
if i.len() < 2 {
panic!("IndexSizeError: `real` and `imag` length should at least 2");
}
let len = i.len();
(vec![0.; len], i)
}
// Defaults to sine wave
// [spec] Note: When setting this PeriodicWave on an OscillatorNode,
// this is equivalent to using the built-in type "sine".
_ => (vec![0., 0.], vec![0., 1.]),
};
let normalize = !disable_normalization;
// [spec] A conforming implementation MUST support PeriodicWave up to at least 8192 elements.
let wavetable = Self::generate_wavetable(&real, &imag, normalize, TABLE_LENGTH_USIZE);
Self {
wavetable: Arc::new(wavetable),
}
}
pub(crate) fn as_slice(&self) -> &[f32] {
&self.wavetable[..]
}
// cf. https://webaudio.github.io/web-audio-api/#waveform-generation
fn generate_wavetable(reals: &[f32], imags: &[f32], normalize: bool, size: usize) -> Vec<f32> {
let mut wavetable = Vec::with_capacity(size);
let pi_2 = 2. * PI;
for i in 0..size {
let mut sample = 0.;
let phase = pi_2 * i as f32 / size as f32;
for j in 1..reals.len() {
let freq = j as f32;
let real = reals[j];
let imag = imags[j];
let rad = phase * freq;
let contrib = real * rad.cos() + imag * rad.sin();
sample += contrib;
}
wavetable.push(sample);
}
if normalize {
Self::normalize(&mut wavetable);
}
wavetable
}
fn normalize(wavetable: &mut [f32]) {
let mut max = 0.;
for sample in wavetable.iter() {
let abs = sample.abs();
if abs > max {
max = abs;
}
}
// prevent division by 0. (nothing to normalize anyway...)
if max > 0. {
let norm_factor = 1. / max;
for sample in wavetable.iter_mut() {
*sample *= norm_factor;
}
}
}
}
#[cfg(test)]
mod tests {
use float_eq::assert_float_eq;
use std::f32::consts::PI;
use crate::context::AudioContext;
use crate::node::{TABLE_LENGTH_F32, TABLE_LENGTH_USIZE};
use super::{PeriodicWave, PeriodicWaveOptions};
#[test]
#[should_panic]
fn fails_to_build_when_only_real_is_defined_and_too_short() {
let context = AudioContext::default();
let options = PeriodicWaveOptions {
real: Some(vec![0.]),
imag: None,
disable_normalization: false,
};
let _periodic_wave = PeriodicWave::new(&context, options);
}
#[test]
#[should_panic]
fn fails_to_build_when_only_imag_is_defined_and_too_short() {
let context = AudioContext::default();
let options = PeriodicWaveOptions {
real: None,
imag: Some(vec![0.]),
disable_normalization: false,
};
let _periodic_wave = PeriodicWave::new(&context, options);
}
#[test]
#[should_panic]
fn fails_to_build_when_imag_and_real_not_equal_length() {
let context = AudioContext::default();
let options = PeriodicWaveOptions {
real: Some(vec![0., 0., 0.]),
imag: Some(vec![0., 0.]),
disable_normalization: false,
};
let _periodic_wave = PeriodicWave::new(&context, options);
}
#[test]
#[should_panic]
fn fails_to_build_when_imag_and_real_too_shorts() {
let context = AudioContext::default();
let options = PeriodicWaveOptions {
real: Some(vec![0.]),
imag: Some(vec![0.]),
disable_normalization: false,
};
let _periodic_wave = PeriodicWave::new(&context, options);
}
#[test]
fn wavetable_generate_sine() {
let reals = [0., 0.];
let imags = [0., 1.];
let result = PeriodicWave::generate_wavetable(&reals, &imags, true, TABLE_LENGTH_USIZE);
let mut expected = Vec::new();
for i in 0..TABLE_LENGTH_USIZE {
let sample = (i as f32 / TABLE_LENGTH_F32 * 2. * PI).sin();
expected.push(sample);
}
assert_float_eq!(result[..], expected[..], abs_all <= 1e-6);
}
#[test]
fn wavetable_generate_2f_not_norm() {
let reals = [0., 0., 0.];
let imags = [0., 0.5, 0.5];
let result = PeriodicWave::generate_wavetable(&reals, &imags, false, TABLE_LENGTH_USIZE);
let mut expected = Vec::new();
for i in 0..TABLE_LENGTH_USIZE {
let mut sample = 0.;
// fundamental frequency
sample += 0.5 * (1. * i as f32 / TABLE_LENGTH_F32 * 2. * PI).sin();
// 1rst partial
sample += 0.5 * (2. * i as f32 / TABLE_LENGTH_F32 * 2. * PI).sin();
expected.push(sample);
}
assert_float_eq!(result[..], expected[..], abs_all <= 1e-6);
}
#[test]
fn normalize() {
{
let mut signal = [-0.5, 0.2];
PeriodicWave::normalize(&mut signal);
let expected = [-1., 0.4];
assert_float_eq!(signal[..], expected[..], abs_all <= 0.);
}
{
let mut signal = [0.5, -0.2];
PeriodicWave::normalize(&mut signal);
let expected = [1., -0.4];
assert_float_eq!(signal[..], expected[..], abs_all <= 0.);
}
}
#[test]
fn wavetable_generate_2f_norm() {
let reals = [0., 0., 0.];
let imags = [0., 0.5, 0.5];
let result = PeriodicWave::generate_wavetable(&reals, &imags, true, TABLE_LENGTH_USIZE);
let mut expected = Vec::new();
for i in 0..TABLE_LENGTH_USIZE {
let mut sample = 0.;
// fundamental frequency
sample += 0.5 * (1. * i as f32 / TABLE_LENGTH_F32 * 2. * PI).sin();
// 1rst partial
sample += 0.5 * (2. * i as f32 / TABLE_LENGTH_F32 * 2. * PI).sin();
expected.push(sample);
}
PeriodicWave::normalize(&mut expected);
assert_float_eq!(result[..], expected[..], abs_all <= 1e-6);
}
}