vkQuake2 doxygen  1.0 dev
snd_miniaudio.h File Reference

Go to the source code of this file.

Functions

void Miniaudio_Init (void)
 
void Miniaudio_Play (int track, qboolean looping)
 
void Miniaudio_Stop (void)
 
void Miniaudio_Update (void)
 
void Miniaudio_Shutdown (void)
 

Function Documentation

◆ Miniaudio_Init()

void Miniaudio_Init ( void  )

Definition at line 195 of file snd_miniaudio.c.

196 {
197  cd_nocd = Cvar_Get("cd_nocd", "0", CVAR_ARCHIVE);
198  cd_loopcount = Cvar_Get("cd_loopcount", "4", 0);
199  cd_looptrack = Cvar_Get("cd_looptrack", "11", 0);
200  enabled = true;
201  paused = false;
202 
203 #ifdef __APPLE__
204  bufferSizeInFrames = Cvar_VariableValue("s_chunksize") * sizeof(float) / GetBytesPerSampleFrame();
205 #endif
206  Cmd_AddCommand("miniaudio", Miniaudio_f);
207 }

Referenced by CL_Init().

◆ Miniaudio_Play()

void Miniaudio_Play ( int  track,
qboolean  looping 
)

Definition at line 225 of file snd_miniaudio.c.

226 {
227  ma_result result;
228  ma_device_config deviceConfig;
229 
230  if (!enabled || playTrack == track)
231  return;
232 
233  Miniaudio_Stop();
234 
235  // ignore invalid tracks
236  if (track < 1)
237  return;
238 
239  result = LoadTrack(FS_Gamedir(), track);
240 
241  // try the baseq2 folder if loading the track from a custom gamedir failed
242  if (result != MA_SUCCESS && Q_stricmp(FS_Gamedir(), "./"BASEDIRNAME) != 0)
243  {
244  result = LoadTrack(BASEDIRNAME, track);
245  }
246 
247  if (result != MA_SUCCESS)
248  {
249  Com_Printf("Failed to open %s/music/track%s%i.[ogg/flac/mp3/wav]: error %i\n", FS_Gamedir(), track < 10 ? "0" : "", track, result);
250  return;
251  }
252 
254  deviceConfig.playback.format = decoder.outputFormat;
255  deviceConfig.playback.channels = decoder.outputChannels;
256  deviceConfig.sampleRate = decoder.outputSampleRate;
257  deviceConfig.dataCallback = data_callback;
258  deviceConfig.pUserData = &decoder;
259 #ifdef __APPLE__
260  deviceConfig.bufferSizeInFrames = bufferSizeInFrames;
261  deviceConfig.periods = 1;
262 #endif
263 
264  if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
265  Com_Printf("Failed to open playback device: error %i\n", result);
267  return;
268  }
269 
270  if (ma_device_start(&device) != MA_SUCCESS) {
271  Com_Printf("Failed to start playback device: error %i\n", result);
274  return;
275  }
276 
277  loopcounter = 0;
278  playTrack = track;
279  playLooping = looping;
280  paused = false;
281  trackFinished = false;
282 
283  if (Cvar_VariableValue("cd_nocd"))
284  Miniaudio_Pause();
285 }

Referenced by CL_ParseConfigString(), CL_PrepRefresh(), Miniaudio_f(), and Miniaudio_Update().

◆ Miniaudio_Shutdown()

void Miniaudio_Shutdown ( void  )

Definition at line 352 of file snd_miniaudio.c.

353 {
354  Miniaudio_Stop();
355  Cmd_RemoveCommand("miniaudio");
356 }

Referenced by CL_Shutdown().

◆ Miniaudio_Stop()

void Miniaudio_Stop ( void  )

Definition at line 287 of file snd_miniaudio.c.

288 {
290  return;
291 
292  paused = false;
293  playTrack = 0;
294 
297 }

Referenced by Miniaudio_f(), Miniaudio_Play(), Miniaudio_Shutdown(), Miniaudio_Update(), SCR_BeginLoadingPlaque(), and SCR_PlayCinematic().

◆ Miniaudio_Update()

void Miniaudio_Update ( void  )

Definition at line 299 of file snd_miniaudio.c.

300 {
301  if (cd_nocd->value != !enabled)
302  {
303  if (cd_nocd->value)
304  {
305  Miniaudio_Pause();
306  enabled = false;
307  }
308  else
309  {
310  enabled = true;
311  int track = atoi(cl.configstrings[CS_CDTRACK]);
312  if (!paused || playTrack != track)
313  {
314  if ((playTrack == 0 && !playLooping) || (playTrack > 0 && playTrack != track))
315  Miniaudio_Play(track, true);
316  else
318  }
319  else
321  }
322  }
323 
325  {
326  trackFinished = false;
327  if (playLooping)
328  {
329  // if the track has played the given number of times,
330  // go to the ambient track
331  if (++loopcounter >= cd_loopcount->value)
332  {
333  // ambient track will be played for the first time, so we need to load it
335  {
336  Miniaudio_Stop();
338  }
339  else
340  {
342  }
343  }
344  else
345  {
347  }
348  }
349  }
350 }

Referenced by CL_Frame().

LoadTrack
static ma_result LoadTrack(const char *gamedir, int track)
Definition: snd_miniaudio.c:209
trackFinished
static qboolean trackFinished
Definition: snd_miniaudio.c:53
decoder
static ma_decoder decoder
Definition: snd_miniaudio.c:46
Miniaudio_Pause
static void Miniaudio_Pause(void)
Definition: snd_miniaudio.c:107
CS_CDTRACK
#define CS_CDTRACK
Definition: q_shared.h:1102
BASEDIRNAME
#define BASEDIRNAME
Definition: qcommon.h:28
Miniaudio_Play
void Miniaudio_Play(int track, qboolean looping)
Definition: snd_miniaudio.c:225
device
static ma_device device
Definition: snd_miniaudio.c:47
ma_device_config_init
ma_device_config ma_device_config_init(ma_device_type deviceType)
ma_device_config::pUserData
void * pUserData
Definition: miniaudio.h:1901
ma_device_start
ma_result ma_device_start(ma_device *pDevice)
ma_device_config::dataCallback
ma_device_callback_proc dataCallback
Definition: miniaudio.h:1899
Cvar_Get
cvar_t * Cvar_Get(char *var_name, char *var_value, int flags)
Definition: cvar.c:127
cd_nocd
static cvar_t * cd_nocd
Definition: snd_miniaudio.c:55
paused
static qboolean paused
Definition: snd_miniaudio.c:51
ma_decoder::outputFormat
ma_format outputFormat
Definition: miniaudio.h:3031
Cmd_RemoveCommand
void Cmd_RemoveCommand(char *cmd_name)
Definition: cmd.c:724
Miniaudio_Resume
static void Miniaudio_Resume(void)
Definition: snd_miniaudio.c:116
ma_device_uninit
void ma_device_uninit(ma_device *pDevice)
Cmd_AddCommand
void Cmd_AddCommand(char *cmd_name, xcommand_t function)
Definition: cmd.c:691
playTrack
static int playTrack
Definition: snd_miniaudio.c:49
CVAR_ARCHIVE
#define CVAR_ARCHIVE
Definition: q_shared.h:316
cvar_s::value
float value
Definition: q_shared.h:331
ma_device_init
ma_result ma_device_init(ma_context *pContext, const ma_device_config *pConfig, ma_device *pDevice)
cd_loopcount
static cvar_t * cd_loopcount
Definition: snd_miniaudio.c:56
enabled
static qboolean enabled
Definition: snd_miniaudio.c:50
NULL
#define NULL
Definition: q_shared.h:67
Miniaudio_Stop
void Miniaudio_Stop(void)
Definition: snd_miniaudio.c:287
MA_SUCCESS
#define MA_SUCCESS
Definition: miniaudio.h:673
Q_stricmp
int Q_stricmp(char *s1, char *s2)
Definition: q_shared.c:1180
ma_device_type_playback
@ ma_device_type_playback
Definition: miniaudio.h:1806
ma_device_config::bufferSizeInFrames
ma_uint32 bufferSizeInFrames
Definition: miniaudio.h:1895
ma_device_is_started
ma_bool32 ma_device_is_started(ma_device *pDevice)
ma_device_config::format
ma_format format
Definition: miniaudio.h:1905
playLooping
static qboolean playLooping
Definition: snd_miniaudio.c:52
loopcounter
static int loopcounter
Definition: snd_miniaudio.c:48
ma_device_config::periods
ma_uint32 periods
Definition: miniaudio.h:1897
FS_Gamedir
char * FS_Gamedir(void)
Definition: files.c:559
data_callback
static void data_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount)
Definition: snd_miniaudio.c:91
ma_decoder_uninit
ma_result ma_decoder_uninit(ma_decoder *pDecoder)
ma_device_config::channels
ma_uint32 channels
Definition: miniaudio.h:1906
ma_decoder::outputChannels
ma_uint32 outputChannels
Definition: miniaudio.h:3032
client_state_t::configstrings
char configstrings[MAX_CONFIGSTRINGS][MAX_QPATH]
Definition: client.h:158
ma_device_config
Definition: miniaudio.h:1891
ma_device_config::sampleRate
ma_uint32 sampleRate
Definition: miniaudio.h:1894
Com_Printf
void Com_Printf(char *fmt,...)
Definition: common.c:104
ma_result
int ma_result
Definition: miniaudio.h:672
cd_looptrack
static cvar_t * cd_looptrack
Definition: snd_miniaudio.c:57
cl
client_state_t cl
Definition: cl_main.c:91
Miniaudio_f
static void Miniaudio_f(void)
Definition: snd_miniaudio.c:125
ma_device_config::playback
struct ma_device_config::@28 playback
ma_decoder_seek_to_pcm_frame
ma_result ma_decoder_seek_to_pcm_frame(ma_decoder *pDecoder, ma_uint64 frameIndex)
ma_decoder::outputSampleRate
ma_uint32 outputSampleRate
Definition: miniaudio.h:3033
Cvar_VariableValue
float Cvar_VariableValue(char *var_name)
Definition: cvar.c:63