Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Introduce helper function creating sample
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Horacek <[email protected]>
  • Loading branch information
phoracek committed Sep 28, 2023
1 parent 5777bc2 commit effb9a5
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions instruments/traky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use proton_ui::state::*;
const NAME: &str = "Traky";
const VOLUME_ATTRIBUTE: &str = "volume";

const MAX_SAMPLE_LENGTH_IN_SECONDS: u32 = 10;
const MAX_SAMPLE_LENGTH_IN_SECONDS: u32 = 20;

pub struct Instrument {
sample: Sample,
Expand All @@ -36,11 +36,7 @@ impl InstrumentTrait for Instrument {
sd: &mut impl BlockDevice<Error = impl core::fmt::Debug>,
) -> Self {
defmt::info!("Allocating buffer");
let mut sample = Sample::from_buffer(
memory_manager
.allocate(upper_power_of_two(sample_rate * MAX_SAMPLE_LENGTH_IN_SECONDS) as usize)
.unwrap(),
);
let mut sample = prepare_empty_sample(memory_manager, sample_rate);

defmt::info!("Loading sample from SD");
load_sample_from_sd(sd, &mut sample);
Expand Down Expand Up @@ -72,6 +68,14 @@ impl InstrumentTrait for Instrument {
fn update_control(&mut self, _snapshot: InputSnapshot) {}
}

fn prepare_empty_sample(memory_manager: &mut MemoryManager, sample_rate: u32) -> Sample {
Sample::from_buffer(
memory_manager
.allocate(upper_power_of_two(2 * sample_rate * MAX_SAMPLE_LENGTH_IN_SECONDS) as usize)
.unwrap(),
)
}

fn load_sample_from_sd(
sd: &mut impl BlockDevice<Error = impl core::fmt::Debug>,
sample: &mut Sample,
Expand Down

0 comments on commit effb9a5

Please sign in to comment.