use anyhow::{Context, Result}; use cpal::Sample; use tracing::{error, info, trace, warn}; use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::mpsc::error::TryRecvError; use crate::task_msg::{RawEthFrameMessage, PcmSampleMessage}; use crate::dsp_common::carrier::{Carrier, TwoStateCarrier}; pub async fn dsp_outb_main( tx_for_wire: Sender, mut rx_for_mod: Receiver, ) -> Result<()> { trace!("pcm_outb task started"); let carrier_phase = 0_f64; let carrier_low = 240; let carrier_high = 250; // TODO: fetch real sample rate let mut carrier = TwoStateCarrier::new(carrier_low, carrier_high, 384000); loop { let eth_frame = match rx_for_mod.try_recv() { Ok(x) => x, Err(TryRecvError::Empty) => { trace!("no eth frames to modulate!"); //tx_for_wire.send(i64::EQUILIBRIUM).await?; //continue; vec![0, 0, 0, 0, 0, 0, 0, 0,] } Err(TryRecvError::Disconnected) => { Err(anyhow::Error::msg( format!("rx_for_mod closed?! the tap task likely crashed"), ))? } }; trace!("network frame came in"); let mut txed = 0; let mut bytes = 0; for byte in eth_frame { for samp in carrier.byte_into_fsk_samples(&byte) { tx_for_wire.send(samp).await?; txed += 1; } bytes += 1; } trace!("eth frame modulated and sent ({} bytes, {} samps)", bytes, txed); //(*carrier_phase * TAU * freq / sample_rate).sin() } }