From 54bf7a3085025776a6847dbb51a02786846e9755 Mon Sep 17 00:00:00 2001 From: transistor Date: Wed, 26 Jan 2022 11:14:52 -0800 Subject: [PATCH] Renamed moa-ym2612 to moa-synth, and put CircularBuffer into its own file --- frontends/moa-common/src/audio.rs | 104 +----------------- frontends/moa-common/src/circularbuf.rs | 101 +++++++++++++++++ frontends/moa-common/src/lib.rs | 2 + .../src/bin/{moa-ym2612.rs => moa-synth.rs} | 0 4 files changed, 105 insertions(+), 102 deletions(-) create mode 100644 frontends/moa-common/src/circularbuf.rs rename frontends/moa-minifb/src/bin/{moa-ym2612.rs => moa-synth.rs} (100%) diff --git a/frontends/moa-common/src/audio.rs b/frontends/moa-common/src/audio.rs index 3e53d11..7c60782 100644 --- a/frontends/moa-common/src/audio.rs +++ b/frontends/moa-common/src/audio.rs @@ -1,112 +1,12 @@ -use moa::host::traits::{HostData, Audio}; use cpal::{Sample, Stream, SampleRate, SampleFormat, StreamConfig, traits::{DeviceTrait, HostTrait, StreamTrait}}; +use moa::host::traits::{HostData, Audio}; +use crate::circularbuf::CircularBuffer; const SAMPLE_RATE: usize = 48000; -#[derive(Clone)] -pub struct CircularBuffer { - pub inp: usize, - pub out: usize, - pub init: T, - pub buffer: Vec, -} - -impl CircularBuffer { - pub fn new(size: usize, init: T) -> Self { - Self { - inp: 0, - out: 0, - init, - buffer: vec![init; size], - } - } - - pub fn len(&self) -> usize { - self.buffer.len() - } - - pub fn clear(&mut self) { - self.inp = 0; - self.out = 0; - } - - pub fn resize(&mut self, newlen: usize) { - if self.buffer.len() != newlen { - self.buffer = vec![self.init; newlen]; - self.clear(); - } - } - - pub fn insert(&mut self, item: T) { - let next = self.next_in(); - if next != self.out { - self.buffer[self.inp] = item; - self.inp = next; - } - } - - pub fn drop_next(&mut self, mut count: usize) { - let avail = self.used_space(); - if count > avail { - count = avail; - } - - self.out += count; - if self.out >= self.buffer.len() { - self.out -= self.buffer.len(); - } - } - - pub fn is_full(&self) -> bool { - self.next_in() == self.out - } - - pub fn used_space(&self) -> usize { - if self.inp >= self.out { - self.inp - self.out - } else { - self.buffer.len() - self.out + self.inp - } - } - - pub fn free_space(&self) -> usize { - if self.out > self.inp { - self.out - self.inp - 1 - } else { - self.buffer.len() - self.inp + self.out - 1 - } - } - - fn next_in(&self) -> usize { - if self.inp + 1 < self.buffer.len() { - self.inp + 1 - } else { - 0 - } - } -} - -impl Iterator for CircularBuffer { - type Item = T; - - fn next(&mut self) -> Option { - if self.out == self.inp { - None - } else { - let value = self.buffer[self.out]; - self.out += 1; - if self.out >= self.buffer.len() { - self.out = 0; - } - Some(value) - } - } -} - - pub struct AudioSource { sample_rate: usize, frame_size: usize, diff --git a/frontends/moa-common/src/circularbuf.rs b/frontends/moa-common/src/circularbuf.rs new file mode 100644 index 0000000..db886b1 --- /dev/null +++ b/frontends/moa-common/src/circularbuf.rs @@ -0,0 +1,101 @@ + +#[derive(Clone)] +pub struct CircularBuffer { + pub inp: usize, + pub out: usize, + pub init: T, + pub buffer: Vec, +} + +impl CircularBuffer { + pub fn new(size: usize, init: T) -> Self { + Self { + inp: 0, + out: 0, + init, + buffer: vec![init; size], + } + } + + pub fn len(&self) -> usize { + self.buffer.len() + } + + pub fn clear(&mut self) { + self.inp = 0; + self.out = 0; + } + + pub fn resize(&mut self, newlen: usize) { + if self.buffer.len() != newlen { + self.buffer = vec![self.init; newlen]; + self.clear(); + } + } + + pub fn insert(&mut self, item: T) { + let next = self.next_in(); + if next != self.out { + self.buffer[self.inp] = item; + self.inp = next; + } + } + + pub fn drop_next(&mut self, mut count: usize) { + let avail = self.used_space(); + if count > avail { + count = avail; + } + + self.out += count; + if self.out >= self.buffer.len() { + self.out -= self.buffer.len(); + } + } + + pub fn is_full(&self) -> bool { + self.next_in() == self.out + } + + pub fn used_space(&self) -> usize { + if self.inp >= self.out { + self.inp - self.out + } else { + self.buffer.len() - self.out + self.inp + } + } + + pub fn free_space(&self) -> usize { + if self.out > self.inp { + self.out - self.inp - 1 + } else { + self.buffer.len() - self.inp + self.out - 1 + } + } + + fn next_in(&self) -> usize { + if self.inp + 1 < self.buffer.len() { + self.inp + 1 + } else { + 0 + } + } +} + +impl Iterator for CircularBuffer { + type Item = T; + + fn next(&mut self) -> Option { + if self.out == self.inp { + None + } else { + let value = self.buffer[self.out]; + self.out += 1; + if self.out >= self.buffer.len() { + self.out = 0; + } + Some(value) + } + } +} + diff --git a/frontends/moa-common/src/lib.rs b/frontends/moa-common/src/lib.rs index d7491ff..61670ba 100644 --- a/frontends/moa-common/src/lib.rs +++ b/frontends/moa-common/src/lib.rs @@ -1,4 +1,6 @@ +pub mod circularbuf; + #[cfg(feature = "tty")] pub mod tty; diff --git a/frontends/moa-minifb/src/bin/moa-ym2612.rs b/frontends/moa-minifb/src/bin/moa-synth.rs similarity index 100% rename from frontends/moa-minifb/src/bin/moa-ym2612.rs rename to frontends/moa-minifb/src/bin/moa-synth.rs