coastline 0.0.4
Loading...
Searching...
No Matches
music.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <expected>
5#include <filesystem>
6#include <memory>
7#include <span>
8#include <string>
9#include <vector>
10
11#include <SDL3/SDL.h>
12
13namespace coastline {
14
15class N64Mixer;
16
36
38inline constexpr int N64_AUDIO_RATE = 21998;
39
44class N64Audio {
45public:
51 N64Audio(const N64Audio &) = delete;
53 N64Audio &operator=(const N64Audio &) = delete;
54
60 bool OpenDevice();
61
68
70 void Stop();
71
73 bool Pause();
74
76 bool Resume();
89 void SetProcessingEnabled(bool music, bool soundEffects);
90
96 void SetMusicGain(float gain);
97
106
107private:
108 std::vector<std::int16_t> RenderLocked(std::size_t frames);
109 void Tick(std::vector<std::int16_t> &output);
110 static void SDLCALL StreamCallback(void *userData,
111 SDL_AudioStream *stream,
112 int additional,
113 int total);
114
115 N64Mixer &m_mixer;
116 SDL_AudioStream *m_stream = nullptr;
118 std::uint32_t m_outputReadOffset = 0;
119 std::uint64_t m_frameRemainder = 0;
120 bool m_primed = false;
121 std::uint32_t m_soundEffectMixOffset = 0;
122 bool m_musicTick = false;
124 bool m_musicProcessing = true;
125 bool m_sfxProcessing = true;
126 bool m_paused = false;
127 bool m_devicePaused = true;
128 bool m_musicEnabled = true;
129 bool m_sfxEnabled = true;
130 // The music gain in 8.8 fixed point.
131 std::uint32_t m_musicGain = 256;
132};
133
141public:
147 N64MixerLock(const N64MixerLock &) = delete;
150
151private:
152 SDL_Mutex *m_mutex;
153};
154
160
161} // namespace coastline
void SetProcessingEnabled(bool music, bool soundEffects)
Enable each category independently of transient fades and playback pauses.
N64Audio()
Initialise channels in the active N64MixerContext, without opening an audio device.
void Stop()
Stop the module and silence its channels.
bool UpdateVolumeProcessing()
Suspend the device while paused or while both enabled audio categories are silent.
~N64Audio()
Destroy the playback stream before the mixer context is destroyed.
N64Audio & operator=(const N64Audio &)=delete
Prevent assignment of the playback stream and module storage.
std::vector< std::int16_t > Render(std::size_t frames)
Render stereo 16-bit frames without a device, as the device would receive them.
void Play(std::span< const std::uint8_t > module)
Load a module and start it from its first order.
bool OpenDevice()
Open the default playback device.
void SetMusicGain(float gain)
Scale the music of the modules, apart from the sound effects the player mixes with it.
N64Audio(const N64Audio &)=delete
Prevent copying the playback stream and module storage.
N64MixerContext()
Register the sole mixer context.
N64MixerContext(const N64MixerContext &)=delete
Prevent copying the registered mixer and its synchronisation resources.
N64MixerContext & operator=(const N64MixerContext &)=delete
Prevent assignment of the registered mixer and its synchronisation resources.
~N64MixerContext()
Unregister the mixer after all audio players and locks have been destroyed.
~N64MixerLock()
Release the mutex on the thread that acquired this lock.
N64MixerLock & operator=(const N64MixerLock &)=delete
Prevent assignment of a mutex acquisition.
N64MixerLock()
Acquire the active mixer context's recursive mutex on the calling thread.
N64MixerLock(const N64MixerLock &)=delete
Prevent copying a mutex acquisition.
Definition assetimport.h:11
constexpr int N64_AUDIO_RATE
The sample rate of the imported music, in stereo frames per second.
Definition music.h:38
void PublishN64Volumes()
Hand the volumes the game writes (g_bBgmVolume, g_bMusicVolume, g_bSfxMasterVolume,...