| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
/* |
| 8 |
* Digital audio support |
| 9 |
* Library-independent stub for dynamic selection of sound system |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <stdexcept> |
| 13 |
#include "dxxerror.h" |
| 14 |
#include "vecmat.h" |
| 15 |
#include "config.h" |
| 16 |
#include "digi.h" |
| 17 |
#include "sounds.h" |
| 18 |
#include "console.h" |
| 19 |
#include "rbaudio.h" |
| 20 |
#include "args.h" |
| 21 |
#include <stdlib.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <string.h> |
| 24 |
#include <digi.h> |
| 25 |
#include <digi_audio.h> |
| 26 |
|
| 27 |
#if DXX_USE_SDLMIXER |
| 28 |
#include <digi_mixer.h> |
| 29 |
#include <SDL_mixer.h> |
| 30 |
#endif |
| 31 |
#ifdef _WIN32 |
| 32 |
#include "hmp.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
namespace dcx { |
| 36 |
|
| 37 |
int digi_volume = SOUND_MAX_VOLUME; |
| 38 |
|
| 39 |
/* The values for these three defines are arbitrary and can be changed, |
| 40 |
* provided that they remain unique with respect to each other. |
| 41 |
*/ |
| 42 |
#define DXX_STS_MIXER_WITH_POINTER 0 |
| 43 |
#define DXX_STS_MIXER_WITH_COPY 1 |
| 44 |
#define DXX_STS_NO_MIXER 2 |
| 45 |
|
| 46 |
#if DXX_USE_SDLMIXER |
| 47 |
#ifndef DXX_SOUND_TABLE_STYLE |
| 48 |
#ifdef __PIE__ |
| 49 |
/* PIE -> paranoid checks |
| 50 |
* No PIE -> prefer speed |
| 51 |
*/ |
| 52 |
#define DXX_SOUND_TABLE_STYLE DXX_STS_MIXER_WITH_POINTER |
| 53 |
#else |
| 54 |
#define DXX_SOUND_TABLE_STYLE DXX_STS_MIXER_WITH_COPY |
| 55 |
#endif |
| 56 |
#endif |
| 57 |
#else |
| 58 |
#define DXX_SOUND_TABLE_STYLE DXX_STS_NO_MIXER |
| 59 |
#endif |
| 60 |
|
| 61 |
/* Sound system function pointers */ |
| 62 |
|
| 63 |
namespace { |
| 64 |
|
| 65 |
struct sound_function_table_t |
| 66 |
{ |
| 67 |
int (*init)(); |
| 68 |
void (*close)(); |
| 69 |
void (*set_channel_volume)(sound_channel, int); |
| 70 |
void (*set_channel_pan)(sound_channel, sound_pan); |
| 71 |
sound_channel (*start_sound)(sound_effect, fix, sound_pan, int, int, int, sound_object *); |
| 72 |
void (*stop_sound)(sound_channel); |
| 73 |
void (*end_sound)(sound_channel); |
| 74 |
int (*is_channel_playing)(sound_channel); |
| 75 |
void (*stop_all_channels)(); |
| 76 |
void (*set_digi_volume)(int); |
| 77 |
}; |
| 78 |
|
| 79 |
#if DXX_SOUND_TABLE_STYLE == DXX_STS_MIXER_WITH_POINTER |
| 80 |
[[noreturn]] |
| 81 |
dxx_compiler_attribute_cold |
| 82 |
void report_invalid_table() |
| 83 |
{ |
| 84 |
/* Out of line due to length of generated code */ |
| 85 |
throw std::logic_error("invalid sound table pointer"); |
| 86 |
} |
| 87 |
#endif |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
#if DXX_SOUND_TABLE_STYLE != DXX_STS_MIXER_WITH_COPY |
| 94 |
namespace dsx |
| 95 |
#else |
| 96 |
namespace dcx |
| 97 |
#endif |
| 98 |
{ |
| 99 |
|
| 100 |
namespace { |
| 101 |
|
| 102 |
class sound_function_pointers_t |
| 103 |
{ |
| 104 |
#if DXX_SOUND_TABLE_STYLE == DXX_STS_MIXER_WITH_COPY |
| 105 |
sound_function_table_t table; |
| 106 |
#elif DXX_SOUND_TABLE_STYLE == DXX_STS_MIXER_WITH_POINTER |
| 107 |
const sound_function_table_t *table; |
| 108 |
#endif |
| 109 |
public: |
| 110 |
inline const sound_function_table_t *operator->(); |
| 111 |
inline sound_function_pointers_t &operator=(const sound_function_table_t &t); |
| 112 |
}; |
| 113 |
|
| 114 |
#if DXX_SOUND_TABLE_STYLE == DXX_STS_MIXER_WITH_COPY |
| 115 |
const sound_function_table_t *sound_function_pointers_t::operator->() |
| 116 |
{ |
| 117 |
return &table; |
| 118 |
} |
| 119 |
|
| 120 |
sound_function_pointers_t &sound_function_pointers_t::operator=(const sound_function_table_t &t) |
| 121 |
{ |
| 122 |
table = t; |
| 123 |
return *this; |
| 124 |
} |
| 125 |
#elif DXX_SOUND_TABLE_STYLE == DXX_STS_MIXER_WITH_POINTER |
| 126 |
sound_function_pointers_t &sound_function_pointers_t::operator=(const sound_function_table_t &t) |
| 127 |
{ |
| 128 |
table = &t; |
| 129 |
/* Trap bad initial assignment */ |
| 130 |
operator->(); |
| 131 |
return *this; |
| 132 |
} |
| 133 |
#elif DXX_SOUND_TABLE_STYLE == DXX_STS_NO_MIXER |
| 134 |
sound_function_pointers_t &sound_function_pointers_t::operator=(const sound_function_table_t &) |
| 135 |
{ |
| 136 |
return *this; |
| 137 |
} |
| 138 |
#endif |
| 139 |
|
| 140 |
static sound_function_pointers_t fptr; |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
namespace dsx { |
| 147 |
|
| 148 |
namespace { |
| 149 |
|
| 150 |
/* Some of the functions are in dsx, so the definition and |
| 151 |
* initializer must be in dsx. |
| 152 |
*/ |
| 153 |
#if DXX_USE_SDLMIXER |
| 154 |
constexpr sound_function_table_t digi_mixer_table{ |
| 155 |
&digi_mixer_init, |
| 156 |
&digi_mixer_close, |
| 157 |
&digi_mixer_set_channel_volume, |
| 158 |
&digi_mixer_set_channel_pan, |
| 159 |
&digi_mixer_start_sound, |
| 160 |
&digi_mixer_stop_sound, |
| 161 |
&digi_mixer_end_sound, |
| 162 |
&digi_mixer_is_channel_playing, |
| 163 |
&digi_mixer_stop_all_channels, |
| 164 |
&digi_mixer_set_digi_volume, |
| 165 |
}; |
| 166 |
#endif |
| 167 |
|
| 168 |
constexpr sound_function_table_t digi_audio_table{ |
| 169 |
&digi_audio_init, |
| 170 |
&digi_audio_close, |
| 171 |
&digi_audio_set_channel_volume, |
| 172 |
&digi_audio_set_channel_pan, |
| 173 |
&digi_audio_start_sound, |
| 174 |
&digi_audio_stop_sound, |
| 175 |
&digi_audio_end_sound, |
| 176 |
&digi_audio_is_channel_playing, |
| 177 |
&digi_audio_stop_all_channels, |
| 178 |
&digi_audio_set_digi_volume, |
| 179 |
}; |
| 180 |
|
| 181 |
#if DXX_SOUND_TABLE_STYLE == DXX_STS_MIXER_WITH_POINTER |
| 182 |
const sound_function_table_t *sound_function_pointers_t::operator->() |
| 183 |
{ |
| 184 |
if (table != &digi_audio_table && table != &digi_mixer_table) |
| 185 |
report_invalid_table(); |
| 186 |
return table; |
| 187 |
} |
| 188 |
#elif DXX_SOUND_TABLE_STYLE == DXX_STS_NO_MIXER |
| 189 |
const sound_function_table_t *sound_function_pointers_t::operator->() |
| 190 |
{ |
| 191 |
return &digi_audio_table; |
| 192 |
} |
| 193 |
#endif |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
void digi_select_system() |
| 198 |
{ |
| 199 |
#if DXX_USE_SDLMIXER |
| 200 |
if (!CGameArg.SndDisableSdlMixer) |
| 201 |
{ |
| 202 |
const auto vl = Mix_Linked_Version(); |
| 203 |
con_printf(CON_NORMAL, "Using SDL_mixer library v%u.%u.%u", vl->major, vl->minor, vl->patch); |
| 204 |
fptr = digi_mixer_table; |
| 205 |
return; |
| 206 |
} |
| 207 |
#endif |
| 208 |
con_puts(CON_NORMAL,"Using plain old SDL audio"); |
| 209 |
fptr = digi_audio_table; |
| 210 |
} |
| 211 |
|
| 212 |
/* Stub functions */ |
| 213 |
|
| 214 |
int digi_init() |
| 215 |
{ |
| 216 |
digi_init_sounds(); |
| 217 |
return fptr->init(); |
| 218 |
} |
| 219 |
|
| 220 |
void digi_close() { fptr->close(); } |
| 221 |
|
| 222 |
void digi_set_channel_volume(const sound_channel channel, int volume) |
| 223 |
{ |
| 224 |
fptr->set_channel_volume(channel, volume); |
| 225 |
} |
| 226 |
|
| 227 |
void digi_set_channel_pan(const sound_channel channel, const sound_pan pan) |
| 228 |
{ |
| 229 |
fptr->set_channel_pan(channel, pan); |
| 230 |
} |
| 231 |
|
| 232 |
sound_channel digi_start_sound(const sound_effect soundnum, const fix volume, const sound_pan pan, const int looping, const int loop_start, const int loop_end, sound_object *const soundobj) |
| 233 |
{ |
| 234 |
return fptr->start_sound(soundnum, volume, pan, looping, loop_start, loop_end, soundobj); |
| 235 |
} |
| 236 |
|
| 237 |
void digi_stop_sound(const sound_channel channel) |
| 238 |
{ |
| 239 |
fptr->stop_sound(channel); |
| 240 |
} |
| 241 |
|
| 242 |
void digi_end_sound(const sound_channel channel) |
| 243 |
{ |
| 244 |
fptr->end_sound(channel); |
| 245 |
} |
| 246 |
|
| 247 |
int digi_is_channel_playing(const sound_channel channel) |
| 248 |
{ |
| 249 |
return fptr->is_channel_playing(channel); |
| 250 |
} |
| 251 |
|
| 252 |
void digi_stop_all_channels() { fptr->stop_all_channels(); } |
| 253 |
void digi_set_digi_volume(int dvolume) { fptr->set_digi_volume(dvolume); } |
| 254 |
|
| 255 |
} |