1
0
Fork 0

Add a pitch parameter to Puberty Simulator

Because might as well.
This commit is contained in:
Robbert van der Helm 2022-03-07 20:21:20 +01:00
parent e575514543
commit 951cac51d7

View file

@ -49,7 +49,10 @@ unsafe impl Send for Plan {}
unsafe impl Sync for Plan {} unsafe impl Sync for Plan {}
#[derive(Params)] #[derive(Params)]
struct PubertySimulatorParams {} struct PubertySimulatorParams {
#[id = "pitch"]
pitch_octaves: FloatParam,
}
impl Default for PubertySimulator { impl Default for PubertySimulator {
fn default() -> Self { fn default() -> Self {
@ -68,10 +71,20 @@ impl Default for PubertySimulator {
} }
} }
#[allow(clippy::derivable_impls)]
impl Default for PubertySimulatorParams { impl Default for PubertySimulatorParams {
fn default() -> Self { fn default() -> Self {
Self {} Self {
pitch_octaves: FloatParam::new(
"Pitch",
-1.0,
FloatRange::Linear {
min: -5.0,
max: 5.0,
},
)
.with_unit(" Octaves")
.with_value_to_string(formatters::f32_rounded(2)),
}
} }
} }
@ -111,15 +124,15 @@ impl Plugin for PubertySimulator {
true true
} }
fn process( fn process(&mut self, buffer: &mut Buffer, context: &mut impl ProcessContext) -> ProcessStatus {
&mut self,
buffer: &mut Buffer,
_context: &mut impl ProcessContext,
) -> ProcessStatus {
// Compensate for the window function, the overlap, and the extra gain introduced by the // Compensate for the window function, the overlap, and the extra gain introduced by the
// IDFT operation // IDFT operation
const GAIN_COMPENSATION: f32 = 1.0 / OVERLAP_TIMES as f32 / WINDOW_SIZE as f32; const GAIN_COMPENSATION: f32 = 1.0 / OVERLAP_TIMES as f32 / WINDOW_SIZE as f32;
// Negated because pitching down should cause us to take values from higher frequency bins
let frequency_multiplier = 2.0f32.powf(-self.params.pitch_octaves.value);
let sample_rate = context.transport().sample_rate;
self.stft.process_overlap_add( self.stft.process_overlap_add(
buffer, buffer,
&self.window_function, &self.window_function,
@ -134,47 +147,46 @@ impl Plugin for PubertySimulator {
) )
.unwrap(); .unwrap();
// This simply takes the complex sinusoid from the frequency bin for double this // This simply interpolates between the complex sinusoids from the frequency bins
// bin's frequency // for this bin's frequency scaled by the octave pitch multiplies. The iteration
for bin_idx in 0..self.complex_fft_scratch_buffer.len() { // order dependson the pitch shifting direction since we're doing it in place.
// TODO: Since we're always doubling now this can be a lot simpler, but it may let num_bins = self.complex_fft_scratch_buffer.len();
// be interesting to add some more options here later let mut process_bin = |bin_idx| {
// let frequency = bin_idx as f32 / WINDOW_SIZE as f32 * sample_rate; let frequency = bin_idx as f32 / WINDOW_SIZE as f32 * sample_rate;
// let target_frequency = frequency * 2.0; let target_frequency = frequency * frequency_multiplier;
// // Simple linear interpolation // Simple linear interpolation
// let target_bin = target_frequency / sample_rate * WINDOW_SIZE as f32; let target_bin = target_frequency / sample_rate * WINDOW_SIZE as f32;
// let target_bin_low = target_bin.floor() as usize; let target_bin_low = target_bin.floor() as usize;
// let target_bin_high = target_bin.ceil() as usize; let target_bin_high = target_bin.ceil() as usize;
// let target_low_t = target_bin % 1.0; let target_low_t = target_bin % 1.0;
// let target_high_t = 1.0 - target_low_t; let target_high_t = 1.0 - target_low_t;
// let target_low = self let target_low = self
// .complex_fft_scratch_buffer
// .get(target_bin_low)
// .copied()
// .unwrap_or_default();
// let target_high = self
// .complex_fft_scratch_buffer
// .get(target_bin_high)
// .copied()
// .unwrap_or_default();
// self.complex_fft_scratch_buffer[bin_idx] = (target_low * target_low_t
// + target_high * target_high_t)
// * 4.0 // Random extra gain, not sure
// * GAIN_COMPENSATION;
let target_bin = bin_idx * 2;
let target = self
.complex_fft_scratch_buffer .complex_fft_scratch_buffer
.get(target_bin) .get(target_bin_low)
.copied()
.unwrap_or_default();
let target_high = self
.complex_fft_scratch_buffer
.get(target_bin_high)
.copied() .copied()
.unwrap_or_default(); .unwrap_or_default();
self.complex_fft_scratch_buffer[bin_idx] = target self.complex_fft_scratch_buffer[bin_idx] = (target_low * target_low_t
+ target_high * target_high_t)
* 6.0 // Random extra gain, not sure * 6.0 // Random extra gain, not sure
* GAIN_COMPENSATION; * GAIN_COMPENSATION;
};
if frequency_multiplier >= 1.0 {
for bin_idx in 0..num_bins {
process_bin(bin_idx);
} }
} else {
for bin_idx in (0..num_bins).rev() {
process_bin(bin_idx);
}
};
// Inverse FFT back into the scratch buffer. This will be added to a ring buffer // Inverse FFT back into the scratch buffer. This will be added to a ring buffer
// which gets written back to the host at a one block delay. // which gets written back to the host at a one block delay.