Skip to main content
← PSoXide SDK guides and examples · SDK 80d1c329e977 · All Cargo features enabled; check feature gates before use.

Player

Struct Player 

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

Plays XA-ADPCM songs. Owns the CD token: nothing else drives the controller while a player exists.

Implementations§

Source§

impl Player

Source

pub const fn new(cd: Cd) -> Self

A player with nothing playing. The SPU’s CD input must be on and have a volume (psx_spu::enable_cd_audio, psx_spu::set_cd_volume), and the drive’s own mixer must pass audio (set_volume).

Source

pub fn release(self) -> Cd

Stop and give the token back.

Source

pub fn play(&mut self, song: Song, looping: bool) -> Result<(), Error>

Start song from the top (or restart it). With looping it restarts by itself when poll sees it end. Blocks for the five drive commands, a few milliseconds on a warm drive.

Source

pub fn stop(&mut self)

Pause the drive and forget the song. Waits for the drive to settle.

Source

pub fn suspend(&mut self) -> bool

Suspend the current song without releasing the CD token or forgetting its filter, looping policy or elapsed position. Returns false when the drive refuses Pause during a seek; keep polling to retry it. An idle player or an already suspended song returns true.

Source

pub fn resume(&mut self) -> Result<(), Error>

Continue a suspended song where it stopped. Safe after the drive was used for something else in between (see with_cd_suspended): it sends Demute, Setmode (XA, filter and the file’s speed), Setfilter (the file and channel) and Setloc to the position the pause left, then ReadS, the same steps play takes except that it aims at the saved position and not at the top. Costs a short seek.

The position is the head at the pause, read back from the drive, so the music continues within about one sector of where it stopped. A channel gets one of its sectors every few sectors of the file (every fourth for four 37.8 kHz stereo songs, 53 ms of sound each), so the first sector after a resume can repeat or skip up to that much sound. This is not a sample-exact cursor.

A refused step returns its Error and leaves the song suspended for an explicit retry. Cancels a still-pending suspension; idle and running players are no-ops.

Source

pub fn with_cd_suspended<R>( &mut self, f: impl FnOnce(Cd) -> (Cd, R), ) -> Result<R, PauseRefused>

Lend the drive to f while the song is suspended, for a data read (a level load) that must not run on top of the music. Suspends the song first; if the drive refuses the pause (it does during the seek at the start of a read), f is not called and the error says to keep calling poll and try again. f gets the token and hands it back with its result. Afterwards call resume to continue the song, or play to start another.

ⓘ
let loaded = player.with_cd_suspended(|cd| {
    let mut reader = SectorReader::with_cd(cd);
    let ok = load_level(&mut reader);
    (reader.release(), ok)
})?;
player.resume()?;
Source

pub fn is_suspended(&self) -> bool

Whether the drive has acknowledged suspension. False while Pause is still being retried. The retained song still counts as is_playing.

Source

pub fn poll(&mut self) -> Event

Call about once per frame: notices the end of the song and loops or stops, or retries a pending suspension. A suspended song returns Event::Playing without querying or advancing the head.

Source

pub fn is_playing(&self) -> bool

Whether a song is started and has not finished.

Source

pub fn is_streaming(&self) -> bool

Whether the head has been seen inside the song file since the last start, so the audio is flowing and not still seeking.

Source

pub fn song(&self) -> Option<Song>

The song being played.

Source

pub fn elapsed_millis(&self) -> u32

Time since the start of the song file at the last poll, in milliseconds (the head runs a sector or two ahead of the sound). The value only moves when poll reads the head; for a time that advances every frame see SongClock.

Source

pub fn head_sample(&self) -> Option<HeadSample>

The newest head reading, while the song is streaming (None while it seeks, is suspended or is not playing). The input of SongClock.

Source

pub fn set_volume(&mut self, left: u8, right: u8)

Set the level of the drive’s audio output (CD-DA too). 0x80 is unity, 0xFF about twice that; (left, right) feed the left and right outputs of a stereo song.

Auto Trait Implementations§

§

impl Freeze for Player

§

impl RefUnwindSafe for Player

§

impl Send for Player

§

impl Sync for Player

§

impl Unpin for Player

§

impl UnsafeUnpin for Player

§

impl UnwindSafe for Player

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.