2018-04-17 02:44:53 +02:00
|
|
|
/*
|
|
|
|
s_backend.c - sound hardware output
|
|
|
|
Copyright (C) 2009 Uncle Mike
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2019-01-07 02:07:01 +01:00
|
|
|
#include "platform/platform.h"
|
2018-04-17 02:44:53 +02:00
|
|
|
#if XASH_SOUND == SOUND_SDL
|
|
|
|
|
|
|
|
#include "sound.h"
|
|
|
|
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
#define SAMPLE_16BIT_SHIFT 1
|
|
|
|
#define SECONDARY_BUFFER_SIZE 0x10000
|
|
|
|
|
2019-10-25 16:11:27 +02:00
|
|
|
#if ! SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
#include <stdlib.h>
|
2021-01-03 02:28:45 +01:00
|
|
|
#define SDL_setenv setenv
|
2019-10-25 16:11:27 +02:00
|
|
|
#define SDL_GetCurrentAudioDriver() "legacysdl"
|
|
|
|
#define SDL_OpenAudioDevice( a, b, c, d, e ) SDL_OpenAudio( ( c ), ( d ) )
|
|
|
|
#define SDL_CloseAudioDevice( a ) SDL_CloseAudio()
|
|
|
|
#define SDL_PauseAudioDevice( a, b ) SDL_PauseAudio( ( b ) )
|
2019-10-26 03:36:43 +02:00
|
|
|
#define SDLash_IsAudioError( x ) ( x ) != 0
|
|
|
|
#else
|
|
|
|
#define SDLash_IsAudioError( x ) ( x ) == 0
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-17 02:44:53 +02:00
|
|
|
/*
|
|
|
|
=======================================================================
|
|
|
|
Global variables. Must be visible to window-procedure function
|
|
|
|
so it can unlock and free the data block after it has been played.
|
|
|
|
=======================================================================
|
|
|
|
*/
|
|
|
|
static int sdl_dev;
|
|
|
|
|
|
|
|
//static qboolean snd_firsttime = true;
|
|
|
|
//static qboolean primary_format_set;
|
|
|
|
|
|
|
|
void SDL_SoundCallback( void *userdata, Uint8 *stream, int len )
|
|
|
|
{
|
|
|
|
int size = dma.samples << 1;
|
|
|
|
int pos = dma.samplepos << 1;
|
|
|
|
int wrapped = pos + len - size;
|
|
|
|
|
2019-10-26 03:36:43 +02:00
|
|
|
#if ! SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
if( !dma.buffer )
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2018-04-17 02:44:53 +02:00
|
|
|
if( wrapped < 0 )
|
|
|
|
{
|
|
|
|
memcpy( stream, dma.buffer + pos, len );
|
|
|
|
dma.samplepos += len >> 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int remaining = size - pos;
|
|
|
|
memcpy( stream, dma.buffer + pos, remaining );
|
|
|
|
memcpy( stream + remaining, dma.buffer, wrapped );
|
|
|
|
dma.samplepos = wrapped >> 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
SNDDMA_Init
|
|
|
|
|
|
|
|
Try to find a sound device to mix for.
|
|
|
|
Returns false if nothing is found.
|
|
|
|
==================
|
|
|
|
*/
|
2019-07-11 23:32:09 +02:00
|
|
|
qboolean SNDDMA_Init( void )
|
2018-04-17 02:44:53 +02:00
|
|
|
{
|
|
|
|
SDL_AudioSpec desired, obtained;
|
|
|
|
int samplecount;
|
|
|
|
|
|
|
|
if( SDL_Init( SDL_INIT_AUDIO ) )
|
|
|
|
{
|
2018-10-27 23:39:29 +02:00
|
|
|
Con_Reportf( S_ERROR "Audio: SDL: %s \n", SDL_GetError( ) );
|
2018-04-17 02:44:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-07 02:09:55 +01:00
|
|
|
// even if we don't have PA
|
|
|
|
// we still can safely set env variables
|
2019-01-13 14:19:35 +01:00
|
|
|
SDL_setenv( "PULSE_PROP_application.name", GI->title, 1 );
|
|
|
|
SDL_setenv( "PULSE_PROP_media.role", "game", 1 );
|
2018-04-17 02:44:53 +02:00
|
|
|
|
|
|
|
memset( &desired, 0, sizeof( desired ) );
|
|
|
|
desired.freq = SOUND_DMA_SPEED;
|
|
|
|
desired.format = AUDIO_S16LSB;
|
|
|
|
desired.samples = 1024;
|
|
|
|
desired.channels = 2;
|
|
|
|
desired.callback = SDL_SoundCallback;
|
|
|
|
|
|
|
|
sdl_dev = SDL_OpenAudioDevice( NULL, 0, &desired, &obtained, 0 );
|
|
|
|
|
2019-10-26 03:36:43 +02:00
|
|
|
if( SDLash_IsAudioError( sdl_dev ))
|
2018-04-17 02:44:53 +02:00
|
|
|
{
|
|
|
|
Con_Printf( "Couldn't open SDL audio: %s\n", SDL_GetError( ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( obtained.format != AUDIO_S16LSB )
|
|
|
|
{
|
|
|
|
Con_Printf( "SDL audio format %d unsupported.\n", obtained.format );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( obtained.channels != 1 && obtained.channels != 2 )
|
|
|
|
{
|
|
|
|
Con_Printf( "SDL audio channels %d unsupported.\n", obtained.channels );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma.format.speed = obtained.freq;
|
|
|
|
dma.format.channels = obtained.channels;
|
|
|
|
dma.format.width = 2;
|
2021-03-09 21:59:29 +01:00
|
|
|
samplecount = s_samplecount.value;
|
2018-04-17 02:44:53 +02:00
|
|
|
if( !samplecount )
|
|
|
|
samplecount = 0x8000;
|
|
|
|
dma.samples = samplecount * obtained.channels;
|
2019-10-26 03:36:43 +02:00
|
|
|
dma.buffer = Z_Calloc( dma.samples * 2 );
|
2018-04-17 02:44:53 +02:00
|
|
|
dma.samplepos = 0;
|
|
|
|
|
|
|
|
Con_Printf( "Using SDL audio driver: %s @ %d Hz\n", SDL_GetCurrentAudioDriver( ), obtained.freq );
|
|
|
|
|
2021-03-10 11:44:25 +01:00
|
|
|
dma.initialized = true;
|
|
|
|
|
2019-10-24 11:43:17 +02:00
|
|
|
SNDDMA_Activate( true );
|
2018-04-17 02:44:53 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
SNDDMA_Shutdown( );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
SNDDMA_BeginPainting
|
|
|
|
|
|
|
|
Makes sure dma.buffer is valid
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void SNDDMA_BeginPainting( void )
|
|
|
|
{
|
|
|
|
SDL_LockAudio( );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
SNDDMA_Submit
|
|
|
|
|
|
|
|
Send sound to device if buffer isn't really the dma buffer
|
|
|
|
Also unlocks the dsound buffer
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void SNDDMA_Submit( void )
|
|
|
|
{
|
|
|
|
SDL_UnlockAudio( );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
SNDDMA_Shutdown
|
|
|
|
|
|
|
|
Reset the sound device for exiting
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void SNDDMA_Shutdown( void )
|
|
|
|
{
|
|
|
|
Con_Printf( "Shutting down audio.\n" );
|
|
|
|
dma.initialized = false;
|
|
|
|
|
|
|
|
if( sdl_dev )
|
|
|
|
{
|
2019-10-24 11:43:17 +02:00
|
|
|
SNDDMA_Activate( false );
|
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#if !XASH_EMSCRIPTEN
|
2018-04-17 02:44:53 +02:00
|
|
|
SDL_CloseAudioDevice( sdl_dev );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#if !XASH_EMSCRIPTEN
|
2018-04-17 02:44:53 +02:00
|
|
|
if( SDL_WasInit( SDL_INIT_AUDIO ) )
|
|
|
|
SDL_QuitSubSystem( SDL_INIT_AUDIO );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( dma.buffer )
|
|
|
|
{
|
|
|
|
Mem_Free( dma.buffer );
|
|
|
|
dma.buffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
2019-01-07 02:07:01 +01:00
|
|
|
SNDDMA_Activate
|
2018-04-17 02:44:53 +02:00
|
|
|
Called when the main window gains or loses focus.
|
|
|
|
The window have been destroyed and recreated
|
|
|
|
between a deactivate and an activate.
|
|
|
|
===========
|
|
|
|
*/
|
2019-01-07 02:07:01 +01:00
|
|
|
void SNDDMA_Activate( qboolean active )
|
2018-04-17 02:44:53 +02:00
|
|
|
{
|
2021-03-09 22:03:12 +01:00
|
|
|
if( !dma.initialized )
|
|
|
|
return;
|
|
|
|
|
2018-04-17 02:44:53 +02:00
|
|
|
SDL_PauseAudioDevice( sdl_dev, !active );
|
|
|
|
}
|
|
|
|
#endif // XASH_SOUND == SOUND_SDL
|