-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
221 additions
and
183 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,56 @@ | ||
#[derive(Default)] | ||
// An envelope changes a sound's volume over time. | ||
// In the NES APU, it can set a constant volume or a decay. | ||
pub struct Envelope { | ||
pub constant_volume: bool, | ||
pub looping: bool, | ||
pub start: bool, | ||
pub divider: u8, | ||
pub decay_level: u8, | ||
pub volume: u8, | ||
} | ||
|
||
impl Envelope { | ||
pub fn start(&mut self) { | ||
self.start = true; | ||
} | ||
|
||
pub fn set_constant_volume(&mut self, constant_volume: bool) { | ||
self.constant_volume = constant_volume; | ||
} | ||
|
||
pub fn set_volume(&mut self, volume: u8) { | ||
self.volume = volume; | ||
} | ||
|
||
pub fn clock(&mut self) { | ||
if !self.start { | ||
self.clock_divider(); | ||
} else { | ||
self.start = false; | ||
self.decay_level = 15; | ||
self.divider = self.volume; | ||
} | ||
} | ||
|
||
pub fn clock_divider(&mut self) { | ||
if self.divider == 0 { | ||
self.divider = self.volume; | ||
if self.decay_level > 0 { | ||
self.decay_level -= 1; | ||
} else if self.looping { | ||
self.decay_level = 15; | ||
} | ||
} else { | ||
self.divider -= 1; | ||
} | ||
} | ||
|
||
pub fn volume(&self) -> u8 { | ||
if self.constant_volume { | ||
self.volume | ||
} else { | ||
self.decay_level | ||
} | ||
} | ||
} |
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
Oops, something went wrong.