nCine::N64AudioDevice class

Nintendo 64 implementation of IAudioDevice on top of libdragon's RSP mixer.

Mixing runs on the RSP; the CPU only assigns sources to mixer channels (effects from the bottom, music from the top, the longest-playing effect is evicted when nothing is free). Buffers are .wav64 files streamed from the cartridge (LoadNativeBuffer()) and music is a .xm64 module or a .wav64 recording (OpenNativeStream()), so no sample data is held in RAM. There is no mixer thread - the mixer is polled once per frame from UpdatePlayers().

Base classes

class AudioDeviceBase
Backend-independent part of an audio device.

Constructors, destructors, conversion operators

N64AudioDevice()
~N64AudioDevice() 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.
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 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 LoadNativeBuffer(std::uint32_t bufferId, StringView path, NativeAudioInfo& info) -> bool override
Makes a buffer play a file the device reads by itself instead of holding its decoded samples.
auto OpenNativeStream(StringView path, NativeAudioInfo& info) -> std::uint32_t override
Opens a file the device streams by itself, returning its id or 0 if it can't be played.
void CloseNativeStream(std::uint32_t streamId) override
Closes a stream returned by OpenNativeStream(), stopping it if it is playing.
void SetSourceNativeStream(std::uint32_t sourceId, std::uint32_t streamId) override
Binds a native stream to a source.
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 SuspendDevice() override
Suspends the audio device.
void ResumeDevice() override
Resumes the audio device.
auto ReserveExclusiveChannels(std::int32_t count) -> std::int32_t
Takes count consecutive mixer channels away from the engine, returning the first one or -1.
void ReleaseExclusiveChannels(std::int32_t firstChannel, std::int32_t count)
Gives channels taken by ReserveExclusiveChannels() back to the engine.
void PollMixer()
Runs the mixer if the AI queue has room, without the rest of UpdatePlayers().

Function documentation

bool nCine::N64AudioDevice::LoadNativeBuffer(std::uint32_t bufferId, StringView path, NativeAudioInfo& info) override

Makes a buffer play a file the device reads by itself instead of holding its decoded samples.

Used e.g. on Nintendo 64 to stream sounds from the cartridge. Returns false if the file can't be played.

void nCine::N64AudioDevice::SetSourceNativeStream(std::uint32_t sourceId, std::uint32_t streamId) override

Binds a native stream to a source.

The source controls then drive the stream (StopSource() also rewinds it). The binding lasts until the source is handed out again.

std::int32_t nCine::N64AudioDevice::ReserveExclusiveChannels(std::int32_t count)

Takes count consecutive mixer channels away from the engine, returning the first one or -1.

Used by the video player for its audio track. Whatever played there is stopped, and the channels stay reserved until ReleaseExclusiveChannels().

void nCine::N64AudioDevice::PollMixer()

Runs the mixer if the AI queue has room, without the rest of UpdatePlayers().

For a loop that owns the main thread for a while (the video player) and has to keep the audio fed.