nCine::Ps2AudioDevice class

PlayStation 2 implementation of IAudioDevice on top of audsrv.

Software mixer whose output goes through audsrv_play_audio() into the ring audsrv keeps on the IOP. Threads are off on this console, so UpdatePlayers() tops the ring up once per frame without ever blocking, and music chunks are decoded in slices from RunIdleWork() while the frame waits for the vertical blank. Mixes at 22050 Hz by default; the SPU2 resamples to 48 kHz in hardware.

Base classes

class AudioDeviceBase
Backend-independent part of an audio device.

Public static functions

static auto InitializeModules() -> bool
Brings the IOP side up - rom0:LIBSD and the embedded audsrv.irx.

Constructors, destructors, conversion operators

Ps2AudioDevice()
~Ps2AudioDevice() override

Public functions

auto IsValid() const -> bool override
Returns true if the device was initialized successfully.
auto GetName() const -> const char* override
Returns the name of the underlying device.
void SetGain(float gain) override
Sets the listener gain (master volume).
void UpdateListener(const Vector3f& position, const Vector3f& velocity) override
Updates the position and velocity of the listener.
auto GetNativeFrequency() -> std::int32_t override
Returns the native sample rate of the device.
void SetMixingFrequency(std::int32_t frequency) override
Changes the rate the device mixes at, if the backend has one to change.
auto RegisterPlayer(IAudioPlayer* player) -> std::uint32_t override
Registers a player so it receives state and buffer queue updates, returning its source id.
void UpdatePlayers() override
Updates the state of every registered player, including the buffer queue of stream players.
auto SubmitStreamDecode(const std::shared_ptr<StreamDecodeRequest>& request) -> bool override
Submits a decode request to be executed asynchronously on the decoding thread.
void DrainStreamDecode(const std::shared_ptr<StreamDecodeRequest>& request) override
Ensures the specified request is neither queued nor being executed when this method returns.
auto RunIdleWork(float availableMs) -> bool override
Does one short step of background work in time the caller would otherwise spend waiting.
auto CreateBuffer(BufferUsage usage) -> std::uint32_t override
Creates an empty backend buffer, returning its id or 0 on failure.
void DeleteBuffer(std::uint32_t bufferId) override
Destroys a buffer previously returned by CreateBuffer().
auto UploadBuffer(std::uint32_t bufferId, BufferFormat format, const void* data, std::int32_t size, std::int32_t frequency) -> bool override
Replaces the contents of a buffer with the specified samples.
void SetSourceBuffer(std::uint32_t sourceId, std::uint32_t bufferId) override
Attaches a buffer to a source for non-streamed playback, 0 detaches the current one.
void SetSourceGain(std::uint32_t sourceId, float gain) override
Sets the gain of a source.
void SetSourcePitch(std::uint32_t sourceId, float pitch) override
Sets the pitch of a source, as a multiplier of its natural playback rate.
void SetSourceLooping(std::uint32_t sourceId, bool looping) override
Sets whether a source repeats its attached buffer.
void SetSourceRelative(std::uint32_t sourceId, bool relative) override
Sets whether the position of a source is relative to the listener.
void SetSourcePosition(std::uint32_t sourceId, const Vector3f& position) override
Sets the position of a source, in physical units.
void SetSourceLowPass(std::uint32_t sourceId, float value) override
Sets the low-pass amount of a source, 1.0f disables the filter.
auto GetSourceSampleOffset(std::uint32_t sourceId) -> std::int32_t override
Returns the playback position of a source in samples.
void SetSourceSampleOffset(std::uint32_t sourceId, std::int32_t offset) override
Sets the playback position of a source in samples.
void PlaySource(std::uint32_t sourceId) override
Starts or resumes a source.
void PauseSource(std::uint32_t sourceId) override
Pauses a source at its current position.
void StopSource(std::uint32_t sourceId) override
Stops a source.
auto IsSourcePlaying(std::uint32_t sourceId) -> bool override
Returns true if a source is still producing sound.
void QueueBuffer(std::uint32_t sourceId, std::uint32_t bufferId) override
Appends a buffer to the streaming queue of a source.
auto GetProcessedBufferCount(std::uint32_t sourceId) -> std::int32_t override
Returns the number of queued buffers a source has finished playing.
void UnqueueBuffers(std::uint32_t sourceId, std::int32_t count, std::uint32_t* bufferIds) override
Removes the specified number of played buffers from the front of the queue.
void OnBlockingOperationBegan() override
Called when the outermost blocking operation begins.
void OnBlockingOperationEnded() override
Called when the last outstanding blocking operation ends (see OnBlockingOperationBegan()).
void SuspendDevice() override
Suspends the audio device.
void ResumeDevice() override
Resumes the audio device.

Function documentation

static bool nCine::Ps2AudioDevice::InitializeModules()

Brings the IOP side up - rom0:LIBSD and the embedded audsrv.irx.

Returns false if the modules could not be loaded, which makes the backend report itself invalid

Idempotent, because an IRX can only be loaded once per process but the device may be created more than once.

void nCine::Ps2AudioDevice::SetMixingFrequency(std::int32_t frequency) override

Changes the rate the device mixes at, if the backend has one to change.

Honoured only by software mixers (PSP, Amiga), GetNativeFrequency() then reports the new rate. An unsupported rate or 0 is ignored, a no-op on other backends.

bool nCine::Ps2AudioDevice::SubmitStreamDecode(const std::shared_ptr<StreamDecodeRequest>& request) override

Submits a decode request to be executed asynchronously on the decoding thread.

Returns false when no decoding thread is available and the caller has to decode synchronously

The request state must be StreamDecodeRequest::State::Pending when submitted.

void nCine::Ps2AudioDevice::DrainStreamDecode(const std::shared_ptr<StreamDecodeRequest>& request) override

Ensures the specified request is neither queued nor being executed when this method returns.

Required before touching the request's reader. A queued request is reset to StreamDecodeRequest::State::Idle, a running one is waited for.

bool nCine::Ps2AudioDevice::RunIdleWork(float availableMs) override

Does one short step of background work in time the caller would otherwise spend waiting.

Parameters
availableMs Time left, a step that would not fit is not started
Returns true if a step was done and more remains

Called while waiting for vertical blank, for devices that decode streams without a thread.

void nCine::Ps2AudioDevice::UnqueueBuffers(std::uint32_t sourceId, std::int32_t count, std::uint32_t* bufferIds) override

Removes the specified number of played buffers from the front of the queue.

Parameters
sourceId
count
bufferIds Receives the ids of the removed buffers, must hold count entries

void nCine::Ps2AudioDevice::OnBlockingOperationBegan() override

Called when the outermost blocking operation begins.

Blocking operations can nest, so backends override this instead of BeginBlockingOperation().