Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Explicit Sound Config #3

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions src/bin/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ use cardputer::{
};
use esp_idf_hal::{io::Write, peripherals};

const SAMPLE_RATE: f32 = 48000.0;
const FREQUENCY: f32 = 440.0;
const AMPLITUDE: f32 = 127.0;

fn generate_sine_wave(duration_secs: f32) -> Vec<u8> {
let num_samples = (duration_secs * SAMPLE_RATE) as usize;
let mut samples = Vec::with_capacity(num_samples);

let sample_period = 1.0 / SAMPLE_RATE;

for i in 0..num_samples {
let t = i as f32 * sample_period;
let angular_freq = 2.0 * PI * FREQUENCY + t * 200.0;
let sample_value = (AMPLITUDE * (angular_freq * t).sin()) as u8;
samples.push(sample_value);
}

samples
}

#[allow(clippy::approx_constant)]
fn main() {
esp_idf_svc::sys::link_patches();
Expand All @@ -47,6 +27,12 @@ fn main() {

let mut typing = Typing::new();

// Enable the speaker,
// TODO: is there reason to not do this in hal.rs?
p.speaker.tx_enable().unwrap();

let wav = generate_sine_wave(1.0, 880.0);

loop {
let evt = p.keyboard.read_events();
if let Some(evt) = evt {
Expand All @@ -59,27 +45,23 @@ fn main() {
terminal.command_line.pop();
}
KeyboardEvent::Enter => {
terminal.enter();

let text = terminal.command_line.get();

match text {
"b" => {
terminal.println("Beep");
p.speaker.tx_enable().unwrap();
p.speaker
.write_all(
&generate_sine_wave(1.0),
esp_idf_hal::delay::TickType::new_millis(2000).into(),
&wav,
esp_idf_hal::delay::TickType::new_millis(100).into(),
)
.unwrap();
p.speaker.flush().unwrap();
p.speaker.tx_disable().unwrap();
}
_ => {
terminal.println("?");
terminal.println("Commands: b to Beep");
}
}

terminal.enter();
}
KeyboardEvent::ArrowUp => {
terminal.command_line.arrow_up();
Expand All @@ -92,3 +74,22 @@ fn main() {
terminal.draw();
}
}

fn generate_sine_wave(duration_secs: f32, frequency: f32) -> Vec<u8> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still not ideal to have this kind of unbounded dynamic memory allocation

But I'm not familiar with the i2s driver, maybe it can be done in a more streaming way with a smaller, statically allocated buffer?

const SAMPLE_RATE: f32 = 48000.0;
const AMPLITUDE: f32 = 127.0;

let num_samples = (duration_secs * SAMPLE_RATE) as usize;
let mut samples = Vec::with_capacity(num_samples);

let sample_period = 1.0 / SAMPLE_RATE;

for i in 0..num_samples {
let t = i as f32 * sample_period;
let angular_freq = 2.0 * PI * frequency;
let sample_value = (AMPLITUDE * (angular_freq * t).sin()) as u8;
samples.push(sample_value);
}

samples
}
12 changes: 9 additions & 3 deletions src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,15 @@ pub fn cardputer_peripherals<'a>(

let speaker = esp_idf_hal::i2s::I2sDriver::new_std_tx(
i2s,
&esp_idf_hal::i2s::config::StdConfig::philips(
48000,
esp_idf_hal::i2s::config::DataBitWidth::Bits8,
&esp_idf_hal::i2s::config::StdConfig::new(
// TODO Move these options into a config parameter
esp_idf_hal::i2s::config::Config::default().auto_clear(true),
esp_idf_hal::i2s::config::StdClkConfig::from_sample_rate_hz(48000),
esp_idf_hal::i2s::config::StdSlotConfig::philips_slot_default(
esp_idf_hal::i2s::config::DataBitWidth::Bits8,
esp_idf_hal::i2s::config::SlotMode::Mono,
),
Default::default(),
),
pins.gpio41,
pins.gpio42,
Expand Down
Loading