pub struct Wavetable {
pub num_tables: usize,
pub num_octaves: usize,
pub num_values: usize,
pub num_samples: usize,
pub table: Vec<Vec<Float>>,
}
Fields§
§num_tables: usize
§num_octaves: usize
§num_values: usize
§num_samples: usize
§table: Vec<Vec<Float>>
Implementations§
Source§impl Wavetable
impl Wavetable
Sourcepub fn new(
num_tables: usize,
num_octaves: usize,
num_samples: usize,
) -> Wavetable
pub fn new( num_tables: usize, num_octaves: usize, num_samples: usize, ) -> Wavetable
Creates a new Wavetable instance.
This function allocates the memory required for storing the given number of tables.
use wavetable::Wavetable;
// Allocate a table for 4 waveshapes, each containing bandlimited
// sub-tables for 11 octaves, with each table holding 2048 samples.
let wt = Wavetable::new(4, 11, 2048);
Sourcepub fn new_from_vector(
num_tables: usize,
num_octaves: usize,
num_samples: usize,
table: Vec<Vec<Float>>,
) -> WavetableRef
pub fn new_from_vector( num_tables: usize, num_octaves: usize, num_samples: usize, table: Vec<Vec<Float>>, ) -> WavetableRef
Create a new Wavetable using the provided sample memory.
use wavetable::Wavetable;
let num_tables = 4;
let num_octaves = 11;
let num_samples = 2048;
let num_values = num_samples + 1;
let table = vec!(vec!(0.0; num_values * num_octaves); num_tables);
let wt = Wavetable::new_from_vector(num_tables, num_octaves, num_samples, table);
Sourcepub fn get_wave(&self, wave_id: usize) -> &Vec<Float> ⓘ
pub fn get_wave(&self, wave_id: usize) -> &Vec<Float> ⓘ
Return the waveshape at the given index.
This will return the vector containing all octave tables for a single waveshape in the Wavetable.
use wavetable::Wavetable;
let wt = Wavetable::new(4, 11, 2048); // Allocate a Wavetable for 4 waveshapes
let first_wave = wt.get_wave(0); // Get the vector holding the first waveshape
Sourcepub fn get_wave_mut(&mut self, wave_id: usize) -> &mut Vec<Float> ⓘ
pub fn get_wave_mut(&mut self, wave_id: usize) -> &mut Vec<Float> ⓘ
Return a mutable vector for the selected waveshape.
This will return a mutable vector containing all octave tables for a single waveshape in the Wavetable.
use wavetable::Wavetable;
let mut wt = Wavetable::new(4, 11, 2048); // Allocate a Wavetable for 4 waveshapes
let mut second_wave = wt.get_wave_mut(1); // Get the vector holding the second waveshape
Sourcepub fn calc_num_harmonics(base_freq: Float, sample_freq: Float) -> usize
pub fn calc_num_harmonics(base_freq: Float, sample_freq: Float) -> usize
Calculates the number of non-aliasing harmonics for one octave.
Calculates all the harmonics for the octave starting at base_freq that do not exceed the Nyquist frequency.
use wavetable::Wavetable;
let num_harmonics = Wavetable::calc_num_harmonics(20.0, 100.0);
assert_eq!(num_harmonics, 1);
Sourcepub fn convert_to_harmonics(&self, num_harmonics: usize) -> Vec<Vec<Float>>
pub fn convert_to_harmonics(&self, num_harmonics: usize) -> Vec<Vec<Float>>
Convert a wavetable to a vector of harmonics lists.
Creates one harmonic list for each waveshape in the table. The result will contain at most <num_harmonics> harmonics in the list.
use wavetable::Wavetable;
use wavetable::WtManager;
let mut wt_manager = WtManager::new(44100.0, "data");
wt_manager.add_basic_tables(0);
let wt = if let Some(table) = wt_manager.get_table(0) { table } else { panic!(); };
let harmonics = wt.convert_to_harmonics(1300);
Sourcepub fn add_sine_wave(table: &mut [Float], freq: Float, amplitude: Float)
pub fn add_sine_wave(table: &mut [Float], freq: Float, amplitude: Float)
Add a sine wave with given frequency and amplitude to the buffer.
use wavetable::Wavetable;
let mut wt = Wavetable::new(4, 11, 2048);
let mut first_wave = wt.get_wave_mut(0);
let frequency = 1.0; // wavelength = buffer size
let amplitude = 1.0;
Wavetable::add_sine_wave(&mut first_wave, frequency, amplitude);
Sourcepub fn add_cosine_wave(table: &mut [Float], freq: Float, amplitude: Float)
pub fn add_cosine_wave(table: &mut [Float], freq: Float, amplitude: Float)
Add a cosine wave with given frequency and amplitude to the buffer.
use wavetable::Wavetable;
let mut wt = Wavetable::new(4, 11, 2048);
let mut first_wave = wt.get_wave_mut(0);
let frequency = 1.0; // wavelength = buffer size
let amplitude = 1.0;
Wavetable::add_cosine_wave(&mut first_wave, frequency, amplitude);
Sourcepub fn insert_tables(
&mut self,
table_id: usize,
start_freq: Float,
sample_freq: Float,
insert_wave: fn(&mut [Float], Float, Float),
)
pub fn insert_tables( &mut self, table_id: usize, start_freq: Float, sample_freq: Float, insert_wave: fn(&mut [Float], Float, Float), )
Create octave tables with given insert function.
Divides the given table into NUM_TABLES subtables and uses the given insert function to insert waveforms into them. Each table serves the frequency range of one octave.
Sourcepub fn get_start_frequency(base_freq: Float) -> Float
pub fn get_start_frequency(base_freq: Float) -> Float
Calculate the start frequency to use for wave creation.
Sourcepub fn insert_harmonics(
&mut self,
harmonics: &[Vec<Float>],
sample_freq: Float,
) -> Result<(), ()>
pub fn insert_harmonics( &mut self, harmonics: &[Vec<Float>], sample_freq: Float, ) -> Result<(), ()>
Insert a wave from a list of harmonic amplitudes.
The list of harmonics containes their relative amplitude. After adding the harmonics to the wavetable, the total amplitude will be normalized.
use wavetable::Wavetable;
let harmonics = vec![vec![0.0; 2048]];
let mut wt = Wavetable::new(1, 11, 2048);
wt.insert_harmonics(&harmonics, 44100.0);
Sourcepub fn combine_tables(
&mut self,
table_id: usize,
table_a: &[Float],
table_b: &[Float],
offset_b: Float,
)
pub fn combine_tables( &mut self, table_id: usize, table_a: &[Float], table_b: &[Float], offset_b: Float, )
Combine two tables by subtracting one from the other.
\param table_id ID of the target table to write to \param table_a Source table that is subtracted from \param table_b Source table that gets subtracted from table_a \param offset_b Offset into source table_b (0.0 - 1.0)
Sourcepub fn normalize(table: &mut [Float])
pub fn normalize(table: &mut [Float])
Normalizes samples in a table to the range [-1.0,1.0].
Searches the maximum absolute value and uses it to calculate the required scale. Assumes that the values are centered around 0.0.
Sourcepub fn shift(table: &mut [Float], num_values: usize, offset: usize)
pub fn shift(table: &mut [Float], num_values: usize, offset: usize)
Shifts all values in a table by the given offset.