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
impl Player
Sourcepub const fn new(cd: Cd) -> Self
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).
Sourcepub fn play(&mut self, song: Song, looping: bool) -> Result<(), Error>
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.
Sourcepub fn suspend(&mut self) -> bool
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.
Sourcepub fn resume(&mut self) -> Result<(), Error>
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.
Sourcepub fn with_cd_suspended<R>(
&mut self,
f: impl FnOnce(Cd) -> (Cd, R),
) -> Result<R, PauseRefused>
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()?;Sourcepub fn is_suspended(&self) -> bool
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.
Sourcepub fn poll(&mut self) -> Event
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.
Sourcepub fn is_playing(&self) -> bool
pub fn is_playing(&self) -> bool
Whether a song is started and has not finished.
Sourcepub fn is_streaming(&self) -> bool
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.
Sourcepub fn elapsed_millis(&self) -> u32
pub fn elapsed_millis(&self) -> u32
Sourcepub fn head_sample(&self) -> Option<HeadSample>
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.
Sourcepub fn set_volume(&mut self, left: u8, right: u8)
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.