Skip to content

Commit

Permalink
Merge pull request #3 from aprowe/speaker-changes
Browse files Browse the repository at this point in the history
More Explicit Sound Config
  • Loading branch information
Kezii authored Mar 27, 2024
2 parents 9a00850 + b29e82e commit 362ad89
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
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> {
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

0 comments on commit 362ad89

Please sign in to comment.