Opl3Chip

Struct Opl3Chip 

Source
pub struct Opl3Chip { /* private fields */ }
Expand description

The Opl3Chip struct provides a safe interface for interacting with the Nuked-OPL3 library.

Implementations§

Source§

impl Opl3Chip

Source

pub fn new(sample_rate: u32) -> Self

Creates a new OPL3 chip instance. The chip is initialized with the given sample rate. The internal chip device is Pinned to ensure that it is not moved in memory. The Nuked-OPL3 instance contains many self-referencing pointers, which would be invalidated if moved.

§Arguments
  • sample_rate - The sample rate to initialize the OPL3 chip with.
§Returns

The new Opl3Chip instance.

§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
Source

pub fn reset(&mut self, sample_rate: u32)

Reinitialize the OPL3 chip instance.

§Arguments
  • sample_rate - The sample rate to initialize the OPL3 chip with. I have not tested the effects of reinitializing the chip with a different sample rate than the one initially used.
§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
chip.reset(44100);
Source

pub fn generate(&mut self, sample: &mut [i16]) -> Result<(), OplError>

Generate an audio sample.

Internally, this calls Opl3Generate4Ch and returns samples for the first 2 channels.

§Arguments
  • sample - A mutable slice of 2 elements that will receive the sample.
§Returns

A Result containing either () on success or an OplError on failure.

§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
let mut buffer = [0i16; 2];
_ = chip.generate(&mut buffer);
Source

pub fn generate_resampled(&mut self, sample: &mut [i16]) -> Result<(), OplError>

Generate a resampled audio sample.

§Arguments
  • sample - A mutable slice of 2 elements that will receive the sample.
§Returns

A Result containing either () on success or an OplError on failure.

§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
let mut buffer = [0i16; 2];
_ = chip.generate_resampled(&mut buffer);
Source

pub fn write_register(&mut self, reg: u16, value: u8)

Writes a value to an OPL register.

§Arguments
  • reg - The register to write to.
  • value - The value to write to the register.
§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
chip.write_register(0x20, 0x01);
Source

pub fn write_register_buffered(&mut self, reg: u16, value: u8)

Write a value to an OPL register, in buffered mode.

The OPL3 normally requires a delay between register writes. This function will queue the write operation and execute it after any necessary delay.

§Arguments
  • reg - The register to write to.
  • value - The value to write to the register.
§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
chip.write_register_buffered(0x20, 0x01);
Source

pub fn generate_stream(&mut self, buffer: &mut [i16]) -> Result<(), OplError>

Generates a stream of resampled audio samples.

The number of samples generated is determined by the size of the buffer provided.

§Arguments
  • buffer - A mutable reference to a slice of i16 that will be filled with resampled audio samples.
§Returns

A Result containing either () on success or an OplError on failure.

§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
let mut buffer = [0i16; 1024 * 2];
_ = chip.generate_stream(&mut buffer);
Source

pub fn generate_4ch(&mut self, sample: &mut [i16]) -> Result<(), OplError>

Generate a 4 channel audio sample.

§Arguments
  • sample - A mutable, 4-element slice of i16 that will receive the sample.
§Returns

A Result containing either () on success or an OplError on failure.

§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
let mut buffer = [0i16; 4];
_ = chip.generate_4ch(&mut buffer);
Source

pub fn generate_4ch_resampled( &mut self, sample: &mut [i16], ) -> Result<(), OplError>

Generate a 4-channel resampled audio sample.

§Arguments
  • sample - A mutable, 4-element slice of i16 that will receive the sample.
§Returns

A Result containing either () on success or an OplError on failure.

§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
let mut buffer = [0i16; 4];
_ = chip.generate_4ch_resampled(&mut buffer);
Source

pub fn generate_4ch_stream( &mut self, buffer1: &mut [i16], buffer2: &mut [i16], ) -> Result<(), OplError>

Generates a stream of 4-channel audio samples, resampled to the configured sample rate. The OPL3 was capable of 4-channel output, although this feature was not widely used. Most cards simply didn’t provide 4-channel outputs, although there now exist modern reproduction cards that do.

The number of samples is determined by the size of the input buffers.

§Arguments
  • buffer1 - A mutable reference to a slice that will be filled with the first stereo audio samples, interleaved between left and right channels.
  • buffer2 - A mutable reference to a slice that will be filled with audio samples for the channels 2 and 3. The length of buffer1 should equal the length of buffer2.
§Returns

A Result containing either () on success or an OplError on failure.

§Example
use opl3_rs::Opl3Chip;

let mut chip = Opl3Chip::new(44100);
let mut buffer1 = [0i16; 1024 * 2];
let mut buffer2 = [0i16; 1024 * 2];
_ = chip.generate_4ch_stream(&mut buffer1, &mut buffer2);

Trait Implementations§

Source§

impl Drop for Opl3Chip

Source§

fn drop(&mut self)

Drop the Opl3Chip instance by deallocating the memory used by the Nuked-OPL3 instance.

Source§

impl Send for Opl3Chip

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.