diff --git a/src/audio_transmitter/mod.rs b/src/audio_transmitter/mod.rs index 3ac1af1..5b5aa9a 100644 --- a/src/audio_transmitter/mod.rs +++ b/src/audio_transmitter/mod.rs @@ -51,13 +51,13 @@ pub fn audio_transmitter_main( // TODO: from cli let mut carrier = TwoStateCarrier::new( - 3200, // freq_low - 3840, // freq_high + 320, // freq_low + 340, // freq_high cpal_output_config.sample_rate().0, // sample_rate - 48, // symbol_rate + 20, // symbol_rate ); - let mut leftovers: Vec = vec![]; + let mut leftovers: VecDeque = vec![].into(); // TODO: config .clone()s should be obsolete let output_stream = match cpal_output_config.sample_format() { @@ -178,60 +178,51 @@ fn give_output_pcm( pcm: &mut [T], rx_for_mod: &mut Receiver, carrier: &mut TwoStateCarrier, - leftovers: &mut Vec, + leftovers: &mut VecDeque, ) where T: Sample + std::fmt::Debug + FromSample, { trace!("ALSA demands {} samples...", pcm.len()); let mut output = pcm.iter_mut().peekable(); - let mut leftovers_owned = leftovers.clone(); - let mut leftovers_iter = leftovers_owned.into_iter(); - while let Some(samp) = leftovers_iter.next() { + while let Some(samp) = leftovers.pop_front() { if let Some(sample_to_write) = output.next() { *sample_to_write = samp.to_sample::(); } else { - *leftovers = leftovers_iter.collect(); - //leftovers.extend(leftovers_iter); - trace!("ALSA demand fulfilled (last samp with leftovers)! collected {} leftovers", leftovers.len()); + trace!("ALSA demand fulfilled (last samp with leftovers), {} leftovers remain", leftovers.len()); return; } } while let Ok(byte) = rx_for_mod.try_recv() { - //trace!("recv byte {}", byte); let samps = carrier.byte_into_fsk_samples(&byte); - let mut samps_iter = samps.iter(); - //trace!("Samples for this byte: {:?}", samps_iter); + trace!("new ether samps {:?}", samps); + let mut samps_iter = samps.iter().cloned(); while let Some(samp) = samps_iter.next() { if let Some(sample_to_write) = output.next() { *sample_to_write = samp.to_sample::(); } else { - *leftovers = leftovers_iter.collect(); - //leftovers.extend(samps_iter); - trace!("{:?}", leftovers); + let mut collected = samps_iter.collect::>(); + leftovers.append(&mut collected); trace!("ALSA demand fulfilled (last samp with modulated eth data)! collected {} leftovers", leftovers.len()); return; } } } - //trace!("No more eth frame bytes, idling carrier"); while let Some(_) = output.peek() { let idle_samps = carrier.idle(); - let mut idle_samps_iter = idle_samps.iter(); + let mut idle_samps_iter = idle_samps.iter().cloned(); while let Some(samp) = idle_samps_iter.next() { if let Some(sample_to_write) = output.next() { *sample_to_write = samp.to_sample::(); } else { - *leftovers = leftovers_iter.collect(); - //leftovers.extend(idle_samps_iter); + let mut collected = idle_samps_iter.collect::>(); + leftovers.append(&mut collected); trace!("ALSA demand fulfilled (last samp with empty carrier)! collected {} leftovers", leftovers.len()); return; } } } - //*leftovers = leftovers_iter.collect(); - leftovers.extend(leftovers_iter); - trace!("ALSA demand fulfilled! collected {} leftovers", leftovers.len()); + trace!("Alsa demand fulfilled with no leftovers, apparently. (leftovers={})", leftovers.len()); }