pub struct YM2149<C>where
C: CommandOutput,{
pub channel_data: [AudioChannelData; 3],
pub last_used_channel: Option<usize>,
/* private fields */
}Expand description
A YM2149 chip struct.
The master_clock_frequency value is used to convert a frequency into a tone period by .tone_hz()
Example code:
use ym2149_core::{
command::{Command, CommandOutput},
io::IoPortMixerSettings,
chip::YM2149
};
struct DebugWriter;
impl CommandOutput for DebugWriter {
fn execute(&mut self, command: Command) {
print!("Register 0b{:08b} ({:?}), ", command.register, command.register);
println!("0b{:08b} ({:?})", command.value, command.value);
}
}
let mut chip = YM2149::new(
DebugWriter{},
2_000_000,
).expect("Error building chip");
chip.setup_io_and_mixer(
IoPortMixerSettings {
tone_ch_a: true,
..Default::default()
}
);Fields§
§channel_data: [AudioChannelData; 3]§last_used_channel: Option<usize>Implementations§
Source§impl<C> YM2149<C>where
C: CommandOutput,
impl<C> YM2149<C>where
C: CommandOutput,
Sourcepub fn new(
command_output: C,
master_clock_frequency: u32,
) -> Result<Self, Error>
pub fn new( command_output: C, master_clock_frequency: u32, ) -> Result<Self, Error>
Create a new struct for the YM2149.
Sourcepub fn setup_io_and_mixer(&mut self, settings: IoPortMixerSettings)
pub fn setup_io_and_mixer(&mut self, settings: IoPortMixerSettings)
Setup the IO ports and the internal mixer according to the IoPortMixerSettings specified.
Sourcepub fn write_io(&mut self, port: IoPort, value: u8)
pub fn write_io(&mut self, port: IoPort, value: u8)
Write a value to one of the chip’s GPIO ports.
Note: This is a simple helper function, equivalent to self.command(port as u8, value);
Sourcepub fn set_envelope_frequency(
&mut self,
frequency: EnvelopeFrequency,
) -> Result<u16, Error>
pub fn set_envelope_frequency( &mut self, frequency: EnvelopeFrequency, ) -> Result<u16, Error>
Set the envelope generator’s frequency.
Sourcepub fn set_envelope_shape(&mut self, envelope: &Envelope)
pub fn set_envelope_shape(&mut self, envelope: &Envelope)
Set the envelope generator’s shape.
Sourcepub fn tone(&mut self, channel: AudioChannel, period: u16) -> Result<(), Error>
pub fn tone(&mut self, channel: AudioChannel, period: u16) -> Result<(), Error>
Play a tone with a TP of period on an AudioChannel.
The formula for the frequency is
f = fMaster / (16 * TP), where:
- f: target frequency
- fMaster: master clock frequency
- TP: tone period
Sourcepub fn tone_hz(
&mut self,
channel: AudioChannel,
frequency: u32,
) -> Result<(), Error>
pub fn tone_hz( &mut self, channel: AudioChannel, frequency: u32, ) -> Result<(), Error>
Play a tone of a given frequency in Hz on an AudioChannel.
Sourcepub fn play_note(
&mut self,
channel: AudioChannel,
note: &Note,
) -> Result<(), Error>
pub fn play_note( &mut self, channel: AudioChannel, note: &Note, ) -> Result<(), Error>
Play a Note on an AudioChannel.
Sourcepub fn pitch_bend(
&mut self,
channel: AudioChannel,
byte1: u8,
byte2: u8,
replay_last: bool,
) -> Result<f32, Error>
pub fn pitch_bend( &mut self, channel: AudioChannel, byte1: u8, byte2: u8, replay_last: bool, ) -> Result<f32, Error>
Set an AudioChannel’s pitch bend (takes a MIDI command).
Sourcepub fn replay_last_note(&mut self, channel: AudioChannel) -> Result<(), Error>
pub fn replay_last_note(&mut self, channel: AudioChannel) -> Result<(), Error>
Replay the last played note on a given channel.
Sourcepub fn play_note_with_envelope(
&mut self,
channel: AudioChannel,
note: &Note,
with_envelope: &Envelope,
) -> Result<(), Error>
pub fn play_note_with_envelope( &mut self, channel: AudioChannel, note: &Note, with_envelope: &Envelope, ) -> Result<(), Error>
Play a Note on an AudioChannel with a given Envelope.
Sourcepub fn set_noise_freq(&mut self, frequency: u8) -> Result<(), Error>
pub fn set_noise_freq(&mut self, frequency: u8) -> Result<(), Error>
Set the frequency of the noise generator.
Mask: 0x1F
Sourcepub fn level(&mut self, channel: AudioChannel, level: u8)
pub fn level(&mut self, channel: AudioChannel, level: u8)
Set the volume of an AudioChannel.
Note: The channel level registers store 5 bits of data per channel.
From the datasheet:
- Mode M selects whether the level is fixed (when M = 0) or variable (M = 1).
- When M = 0, the level is determined from one of 16 by level selection signals L3, L2, L1, and L0 which compromise the lower four bits.
- When M = 1, the level is determined by the 5 bit output of E4, E3, E2, E1, and E0 of the envelope generator of the SSG.
| B7 (MSB) | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
|---|---|---|---|---|---|---|---|
| N/A | N/A | N/A | M | L3 | L2 | L1 | L0 |