2022-11-09 19:14:43 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-11-12 18:48:57 +01:00
|
|
|
use core::f32::consts::PI;
|
|
|
|
|
2022-11-19 00:29:05 +01:00
|
|
|
use defmt::{error, info};
|
2022-11-09 19:14:43 +01:00
|
|
|
use embassy_executor::Spawner;
|
2022-12-08 20:22:50 +01:00
|
|
|
use embassy_nrf::i2s::{self, Channels, Config, DoubleBuffering, MasterClock, Sample as _, SampleWidth, I2S};
|
2023-03-05 20:40:13 +01:00
|
|
|
use embassy_nrf::{bind_interrupts, peripherals};
|
2022-11-09 19:14:43 +01:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2022-11-19 19:18:20 +01:00
|
|
|
type Sample = i16;
|
|
|
|
|
2022-11-29 01:09:47 +01:00
|
|
|
const NUM_SAMPLES: usize = 50;
|
2022-11-19 19:18:20 +01:00
|
|
|
|
2023-03-05 20:40:13 +01:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
I2S => i2s::InterruptHandler<peripherals::I2S>;
|
|
|
|
});
|
|
|
|
|
2022-11-09 19:14:43 +01:00
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
2022-11-19 00:29:05 +01:00
|
|
|
|
2022-11-19 19:18:20 +01:00
|
|
|
let master_clock: MasterClock = i2s::ExactSampleRate::_50000.into();
|
2022-11-09 19:14:43 +01:00
|
|
|
|
2022-11-19 19:18:20 +01:00
|
|
|
let sample_rate = master_clock.sample_rate();
|
2022-11-12 18:48:57 +01:00
|
|
|
info!("Sample rate: {}", sample_rate);
|
2022-11-09 21:58:56 +01:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
let mut config = Config::default();
|
|
|
|
config.sample_width = SampleWidth::_16bit;
|
|
|
|
config.channels = Channels::MonoLeft;
|
2022-11-17 00:19:22 +01:00
|
|
|
|
2022-12-08 20:22:50 +01:00
|
|
|
let buffers = DoubleBuffering::<Sample, NUM_SAMPLES>::new();
|
|
|
|
let mut output_stream =
|
2023-03-05 20:40:13 +01:00
|
|
|
I2S::new_master(p.I2S, Irqs, p.P0_25, p.P0_26, p.P0_27, master_clock, config).output(p.P0_28, buffers);
|
2022-11-12 18:48:57 +01:00
|
|
|
|
2022-11-19 19:18:20 +01:00
|
|
|
let mut waveform = Waveform::new(1.0 / sample_rate as f32);
|
2022-11-13 01:41:32 +01:00
|
|
|
|
2022-12-08 20:22:50 +01:00
|
|
|
waveform.process(output_stream.buffer());
|
2022-11-13 01:41:32 +01:00
|
|
|
|
2022-12-08 20:22:50 +01:00
|
|
|
output_stream.start().await.expect("I2S Start");
|
2022-11-13 01:41:32 +01:00
|
|
|
|
|
|
|
loop {
|
2022-12-08 20:22:50 +01:00
|
|
|
waveform.process(output_stream.buffer());
|
2022-11-29 01:09:47 +01:00
|
|
|
|
2022-12-08 20:22:50 +01:00
|
|
|
if let Err(err) = output_stream.send().await {
|
2022-11-12 18:48:57 +01:00
|
|
|
error!("{}", err);
|
|
|
|
}
|
2022-11-19 19:18:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Waveform {
|
|
|
|
inv_sample_rate: f32,
|
|
|
|
carrier: SineOsc,
|
|
|
|
freq_mod: SineOsc,
|
|
|
|
amp_mod: SineOsc,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Waveform {
|
|
|
|
fn new(inv_sample_rate: f32) -> Self {
|
2022-11-29 01:09:47 +01:00
|
|
|
let mut carrier = SineOsc::new();
|
|
|
|
carrier.set_frequency(110.0, inv_sample_rate);
|
2022-11-19 19:18:20 +01:00
|
|
|
|
|
|
|
let mut freq_mod = SineOsc::new();
|
2022-12-08 20:22:50 +01:00
|
|
|
freq_mod.set_frequency(1.0, inv_sample_rate);
|
2022-11-19 19:18:20 +01:00
|
|
|
freq_mod.set_amplitude(1.0);
|
|
|
|
|
|
|
|
let mut amp_mod = SineOsc::new();
|
|
|
|
amp_mod.set_frequency(16.0, inv_sample_rate);
|
|
|
|
amp_mod.set_amplitude(0.5);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
inv_sample_rate,
|
|
|
|
carrier,
|
|
|
|
freq_mod,
|
|
|
|
amp_mod,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process(&mut self, buf: &mut [Sample]) {
|
|
|
|
for sample in buf.chunks_mut(1) {
|
|
|
|
let freq_modulation = bipolar_to_unipolar(self.freq_mod.generate());
|
|
|
|
self.carrier
|
|
|
|
.set_frequency(110.0 + 440.0 * freq_modulation, self.inv_sample_rate);
|
|
|
|
|
|
|
|
let amp_modulation = bipolar_to_unipolar(self.amp_mod.generate());
|
|
|
|
self.carrier.set_amplitude(amp_modulation);
|
|
|
|
|
|
|
|
let signal = self.carrier.generate();
|
|
|
|
|
|
|
|
sample[0] = (Sample::SCALE as f32 * signal) as Sample;
|
|
|
|
}
|
2022-11-12 18:48:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SineOsc {
|
|
|
|
amplitude: f32,
|
|
|
|
modulo: f32,
|
|
|
|
phase_inc: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SineOsc {
|
|
|
|
const B: f32 = 4.0 / PI;
|
|
|
|
const C: f32 = -4.0 / (PI * PI);
|
|
|
|
const P: f32 = 0.225;
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
amplitude: 1.0,
|
|
|
|
modulo: 0.0,
|
|
|
|
phase_inc: 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_frequency(&mut self, freq: f32, inv_sample_rate: f32) {
|
|
|
|
self.phase_inc = freq * inv_sample_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_amplitude(&mut self, amplitude: f32) {
|
|
|
|
self.amplitude = amplitude;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate(&mut self) -> f32 {
|
|
|
|
let signal = self.parabolic_sin(self.modulo);
|
|
|
|
self.modulo += self.phase_inc;
|
|
|
|
if self.modulo < 0.0 {
|
|
|
|
self.modulo += 1.0;
|
|
|
|
} else if self.modulo > 1.0 {
|
|
|
|
self.modulo -= 1.0;
|
|
|
|
}
|
|
|
|
signal * self.amplitude
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parabolic_sin(&mut self, modulo: f32) -> f32 {
|
|
|
|
let angle = PI - modulo * 2.0 * PI;
|
|
|
|
let y = Self::B * angle + Self::C * angle * abs(angle);
|
|
|
|
Self::P * (y * abs(y) - y) + y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn abs(value: f32) -> f32 {
|
|
|
|
if value < 0.0 {
|
|
|
|
-value
|
|
|
|
} else {
|
|
|
|
value
|
2022-11-09 19:14:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 18:48:57 +01:00
|
|
|
#[inline]
|
|
|
|
fn bipolar_to_unipolar(value: f32) -> f32 {
|
|
|
|
(value + 1.0) / 2.0
|
2022-11-09 19:14:43 +01:00
|
|
|
}
|