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"
|
2021-05-09 15:32:53 +02:00
|
|
|
#include "voice.h"
|
2018-04-17 02:44:53 +02:00
|
|
|
|
|
|
|
#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 ) )
|
2022-08-31 05:50:06 +02:00
|
|
|
#define SDL_LockAudioDevice( x ) SDL_LockAudio()
|
|
|
|
#define SDL_UnlockAudioDevice( x ) SDL_UnlockAudio()
|
2022-08-31 06:13:02 +02:00
|
|
|
#define SDLash_IsAudioError( x ) (( x ) != 0)
|
2019-10-26 03:36:43 +02:00
|
|
|
#else
|
2022-08-31 06:13:02 +02:00
|
|
|
#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;
|
2022-08-20 02:17:51 +02:00
|
|
|
static SDL_AudioDeviceID in_dev = 0;
|
2021-05-09 15:32:53 +02:00
|
|
|
static SDL_AudioFormat sdl_format;
|
2022-08-20 11:59:14 +02:00
|
|
|
static char sdl_backend_name[32];
|
2018-04-17 02:44:53 +02:00
|
|
|
|
|
|
|
//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;
|
|
|
|
|
2021-05-09 15:32:53 +02:00
|
|
|
sdl_format = obtained.format;
|
|
|
|
|
2018-04-17 02:44:53 +02:00
|
|
|
Con_Printf( "Using SDL audio driver: %s @ %d Hz\n", SDL_GetCurrentAudioDriver( ), obtained.freq );
|
2022-08-20 11:59:14 +02:00
|
|
|
Q_snprintf( sdl_backend_name, sizeof( sdl_backend_name ), "SDL (%s)", SDL_GetCurrentAudioDriver( ));
|
2021-03-10 11:44:25 +01:00
|
|
|
dma.initialized = true;
|
2022-08-20 11:59:14 +02:00
|
|
|
dma.backendName = sdl_backend_name;
|
2021-03-10 11:44:25 +01:00
|
|
|
|
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 )
|
|
|
|
{
|
2022-09-20 20:29:19 +02:00
|
|
|
// SDL_LockAudioDevice( sdl_dev );
|
2018-04-17 02:44:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
SNDDMA_Submit
|
|
|
|
|
|
|
|
Send sound to device if buffer isn't really the dma buffer
|
|
|
|
Also unlocks the dsound buffer
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void SNDDMA_Submit( void )
|
|
|
|
{
|
2022-09-20 20:29:19 +02:00
|
|
|
// SDL_UnlockAudioDevice( sdl_dev );
|
2018-04-17 02:44:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
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 );
|
|
|
|
}
|
2021-05-09 15:32:53 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
|
|
|
SDL_SoundInputCallback
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
void SDL_SoundInputCallback( void *userdata, Uint8 *stream, int len )
|
|
|
|
{
|
2022-08-31 05:50:06 +02:00
|
|
|
int size = Q_min( len, sizeof( voice.input_buffer ) - voice.input_buffer_pos );
|
2021-05-09 15:32:53 +02:00
|
|
|
|
2022-08-31 05:50:06 +02:00
|
|
|
// engine can't keep up, skip audio
|
|
|
|
if( !size )
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy( voice.input_buffer + voice.input_buffer_pos, stream, size );
|
2022-08-15 09:42:06 +02:00
|
|
|
voice.input_buffer_pos += size;
|
2021-05-09 15:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
|
|
|
VoiceCapture_Init
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
qboolean VoiceCapture_Init( void )
|
|
|
|
{
|
|
|
|
SDL_AudioSpec wanted, spec;
|
|
|
|
|
2022-08-31 05:50:06 +02:00
|
|
|
if( !SDLash_IsAudioError( in_dev ))
|
|
|
|
{
|
|
|
|
VoiceCapture_Shutdown();
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:32:53 +02:00
|
|
|
SDL_zero( wanted );
|
|
|
|
wanted.freq = voice.samplerate;
|
|
|
|
wanted.format = AUDIO_S16LSB;
|
2022-08-31 05:50:06 +02:00
|
|
|
wanted.channels = VOICE_PCM_CHANNELS;
|
|
|
|
wanted.samples = voice.frame_size;
|
2021-05-09 15:32:53 +02:00
|
|
|
wanted.callback = SDL_SoundInputCallback;
|
|
|
|
|
|
|
|
in_dev = SDL_OpenAudioDevice( NULL, SDL_TRUE, &wanted, &spec, 0 );
|
|
|
|
|
2022-08-20 02:17:51 +02:00
|
|
|
if( SDLash_IsAudioError( in_dev ))
|
2021-05-09 15:32:53 +02:00
|
|
|
{
|
|
|
|
Con_Printf( "VoiceCapture_Init: error creating capture device (%s)\n", SDL_GetError() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Con_Printf( S_NOTE "VoiceCapture_Init: capture device creation success (%i: %s)\n", in_dev, SDL_GetAudioDeviceName( in_dev, SDL_TRUE ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
2022-08-31 05:50:06 +02:00
|
|
|
VoiceCapture_Activate
|
2021-05-09 15:32:53 +02:00
|
|
|
===========
|
|
|
|
*/
|
2022-08-31 05:50:06 +02:00
|
|
|
qboolean VoiceCapture_Activate( qboolean activate )
|
2021-05-09 15:32:53 +02:00
|
|
|
{
|
2022-08-31 05:50:06 +02:00
|
|
|
if( SDLash_IsAudioError( in_dev ))
|
|
|
|
return false;
|
2021-05-09 15:32:53 +02:00
|
|
|
|
2022-08-31 05:50:06 +02:00
|
|
|
SDL_PauseAudioDevice( in_dev, activate ? SDL_FALSE : SDL_TRUE );
|
2021-05-09 15:32:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
2022-08-31 05:50:06 +02:00
|
|
|
VoiceCapture_Lock
|
2021-05-09 15:32:53 +02:00
|
|
|
===========
|
|
|
|
*/
|
2022-08-31 05:50:06 +02:00
|
|
|
qboolean VoiceCapture_Lock( qboolean lock )
|
2021-05-09 15:32:53 +02:00
|
|
|
{
|
2022-08-31 05:50:06 +02:00
|
|
|
if( SDLash_IsAudioError( in_dev ))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( lock ) SDL_LockAudioDevice( in_dev );
|
|
|
|
else SDL_UnlockAudioDevice( in_dev );
|
|
|
|
|
|
|
|
return true;
|
2022-08-20 02:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==========
|
|
|
|
VoiceCapture_Shutdown
|
|
|
|
==========
|
|
|
|
*/
|
|
|
|
void VoiceCapture_Shutdown( void )
|
|
|
|
{
|
2022-08-31 05:50:06 +02:00
|
|
|
if( SDLash_IsAudioError( in_dev ))
|
2022-08-20 02:17:51 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
SDL_CloseAudioDevice( in_dev );
|
2021-05-09 15:32:53 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 02:44:53 +02:00
|
|
|
#endif // XASH_SOUND == SOUND_SDL
|