-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from Amjad50/improve_audio
Improve audio
- Loading branch information
Showing
11 changed files
with
123 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::{ | ||
collections::VecDeque, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
pub trait APUChannel: Serialize + for<'de> Deserialize<'de> { | ||
fn get_output(&mut self) -> f32; | ||
} | ||
|
||
pub trait TimedAPUChannel: APUChannel { | ||
fn timer_clock(&mut self); | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct BufferedChannel { | ||
buffer: VecDeque<f32>, | ||
} | ||
|
||
impl BufferedChannel { | ||
pub fn new() -> Self { | ||
Self { | ||
buffer: VecDeque::new(), | ||
} | ||
} | ||
|
||
pub fn recored_sample(&mut self, sample: f32) { | ||
self.buffer.push_back(sample); | ||
self.buffer.push_back(sample); | ||
} | ||
|
||
pub fn take_buffer(&mut self) -> Vec<f32> { | ||
self.buffer.drain(..).collect() | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(bound = "C: APUChannel")] | ||
pub struct Dac<C: APUChannel> { | ||
capacitor: f32, | ||
channel: C, | ||
} | ||
|
||
impl<C: APUChannel> Dac<C> { | ||
pub fn new(channel: C) -> Self { | ||
Self { | ||
capacitor: 0., | ||
channel, | ||
} | ||
} | ||
|
||
pub fn dac_output(&mut self) -> f32 { | ||
let dac_in = self.channel.get_output() / 2.2; | ||
let dac_out = dac_in - self.capacitor; | ||
|
||
self.capacitor = dac_in - dac_out * 0.996; | ||
|
||
dac_out | ||
} | ||
} | ||
|
||
impl<C: APUChannel> Deref for Dac<C> { | ||
type Target = C; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.channel | ||
} | ||
} | ||
|
||
impl<C: APUChannel> DerefMut for Dac<C> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.channel | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.