| 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 |
* This is an alternate backend for the sound effect system. |
| 9 |
* It uses SDL_mixer to provide a more reliable playback, |
| 10 |
* and allow processing of multiple audio formats. |
| 11 |
* |
| 12 |
* This file is based on the original D1X arch/sdl/digi.c |
| 13 |
* |
| 14 |
* -- MD2211 (2006-10-12) |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <bitset> |
| 18 |
#include <span> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <SDL.h> |
| 24 |
#include <SDL_mixer.h> |
| 25 |
|
| 26 |
#include "pstypes.h" |
| 27 |
#include "dxxerror.h" |
| 28 |
#include "sounds.h" |
| 29 |
#include "digi.h" |
| 30 |
#include "digi_mixer.h" |
| 31 |
#include "digi_mixer_music.h" |
| 32 |
#include "console.h" |
| 33 |
#include "config.h" |
| 34 |
#include "args.h" |
| 35 |
|
| 36 |
#include "maths.h" |
| 37 |
#include "piggy.h" |
| 38 |
#include "u_mem.h" |
| 39 |
#include <memory> |
| 40 |
#include "compiler-cf_assert.h" |
| 41 |
#include "d_bitset.h" |
| 42 |
#include "d_range.h" |
| 43 |
#include "d_underlying_value.h" |
| 44 |
#include "d_uspan.h" |
| 45 |
#include "d_zip.h" |
| 46 |
|
| 47 |
#define MIX_DIGI_DEBUG 0 |
| 48 |
|
| 49 |
#define DXX_FEATURE_INTERNAL_RESAMPLER (DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SDL1 || DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SOUNDBLASTER16) |
| 50 |
|
| 51 |
namespace dcx { |
| 52 |
|
| 53 |
namespace { |
| 54 |
|
| 55 |
#if !((defined(__APPLE__) && defined(__MACH__)) || defined(macintosh)) |
| 56 |
constexpr std::size_t SOUND_BUFFER_SIZE{2048}; |
| 57 |
#else |
| 58 |
constexpr std::size_t SOUND_BUFFER_SIZE{1024}; |
| 59 |
#endif |
| 60 |
|
| 61 |
constexpr uint16_t MIX_OUTPUT_FORMAT{AUDIO_S16}; |
| 62 |
constexpr int MIX_OUTPUT_CHANNELS{2}; |
| 63 |
|
| 64 |
/* In mixer mode, always request 44Khz. This guarantees a need to upsample, |
| 65 |
* but allows passing into the mixing subsystem sounds that are natively higher |
| 66 |
* quality, like a user's personal music collection. |
| 67 |
* |
| 68 |
* 44Khz is an integer multiple of both the Descent 1 sounds (11Khz) and the |
| 69 |
* Descent 2 sounds (variously, 11Khz or 22Khz), so the upsample should be |
| 70 |
* straightforward. |
| 71 |
*/ |
| 72 |
constexpr auto digi_sample_rate{underlying_value(sound_sample_rate::_44k)}; |
| 73 |
enumerated_bitset<64, sound_channel> channels; |
| 74 |
|
| 75 |
/* channel management */ |
| 76 |
static sound_channel digi_mixer_find_channel(const enumerated_bitset<64, sound_channel> &channels, const unsigned max_channels) |
| 77 |
{ |
| 78 |
uint8_t i{}; |
| 79 |
for (; i < max_channels; ++i) |
| 80 |
if (!channels[(sound_channel{i})]) |
| 81 |
break; |
| 82 |
return sound_channel{i}; |
| 83 |
} |
| 84 |
|
| 85 |
struct RAIIMix_Chunk : public Mix_Chunk |
| 86 |
{ |
| 87 |
RAIIMix_Chunk() = default; |
| 88 |
~RAIIMix_Chunk() |
| 89 |
{ |
| 90 |
delete [] abuf; |
| 91 |
} |
| 92 |
RAIIMix_Chunk(const RAIIMix_Chunk &) = delete; |
| 93 |
RAIIMix_Chunk &operator=(const RAIIMix_Chunk &) = delete; |
| 94 |
}; |
| 95 |
|
| 96 |
static uint8_t fix2byte(const fix f) |
| 97 |
{ |
| 98 |
if (f >= UINT8_MAX << 8) |
| 99 |
/* Values greater than this would produce incorrect results if |
| 100 |
* shifted and truncated. As a special case, coerce such values |
| 101 |
* to the largest representable return value. |
| 102 |
*/ |
| 103 |
return UINT8_MAX; |
| 104 |
return f >> 8; |
| 105 |
} |
| 106 |
|
| 107 |
uint8_t digi_initialised; |
| 108 |
unsigned digi_mixer_max_channels = std::size(channels); |
| 109 |
|
| 110 |
void digi_mixer_free_channel(const int channel_num) |
| 111 |
{ |
| 112 |
if (const auto o = channels.valid_index(channel_num)) |
| 113 |
channels.reset(*o); |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/* Initialise audio */ |
| 119 |
int digi_mixer_init() |
| 120 |
{ |
| 121 |
#if MIX_DIGI_DEBUG |
| 122 |
con_printf(CON_DEBUG, "digi_init %u (SDL_Mixer)", MAX_SOUNDS.value); |
| 123 |
#endif |
| 124 |
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) Error("SDL audio initialisation failed: %s.", SDL_GetError()); |
| 125 |
|
| 126 |
#if SDL_MAJOR_VERSION == 1 |
| 127 |
if (Mix_OpenAudio(digi_sample_rate, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, SOUND_BUFFER_SIZE)) |
| 128 |
#else |
| 129 |
if (Mix_OpenAudioDevice(digi_sample_rate, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, SOUND_BUFFER_SIZE, NULL, 0)) |
| 130 |
#endif |
| 131 |
{ |
| 132 |
//edited on 10/05/98 by Matt Mueller - should keep running, just with no sound. |
| 133 |
con_printf(CON_URGENT,"\nError: Couldn't open audio: %s", SDL_GetError()); |
| 134 |
CGameArg.SndNoSound = 1; |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
digi_mixer_max_channels = Mix_AllocateChannels(digi_mixer_max_channels); |
| 139 |
channels.reset(); |
| 140 |
Mix_Pause(0); |
| 141 |
Mix_ChannelFinished(digi_mixer_free_channel); |
| 142 |
|
| 143 |
digi_initialised = 1; |
| 144 |
|
| 145 |
digi_mixer_set_digi_volume((CGameCfg.DigiVolume * 32768) / 8); |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
/* Shut down audio */ |
| 151 |
void digi_mixer_close() { |
| 152 |
#if MIX_DIGI_DEBUG |
| 153 |
con_printf(CON_DEBUG, "digi_close (SDL_Mixer)"); |
| 154 |
#endif |
| 155 |
if (!digi_initialised) return; |
| 156 |
digi_initialised = 0; |
| 157 |
Mix_CloseAudio(); |
| 158 |
} |
| 159 |
|
| 160 |
namespace { |
| 161 |
|
| 162 |
#if DXX_FEATURE_INTERNAL_RESAMPLER |
| 163 |
|
| 164 |
enum class upscale_factor : uint8_t |
| 165 |
{ |
| 166 |
/* The enum values must be the ratio of the destination divided by the |
| 167 |
* source, because the numeric value is used to compute how much buffer |
| 168 |
* space to allocate. Do not renumber these constants. |
| 169 |
*/ |
| 170 |
#if DXX_BUILD_DESCENT == 2 |
| 171 |
from_22khz_to_44khz = 2, |
| 172 |
#endif |
| 173 |
from_11khz_to_44khz = 4, |
| 174 |
}; |
| 175 |
|
| 176 |
#if DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SDL1 |
| 177 |
namespace emulate_sdl1 { |
| 178 |
|
| 179 |
static unique_span<uint8_t> convert_audio(const std::span<const uint8_t> input, const std::size_t output_per_input) |
| 180 |
{ |
| 181 |
using output_type = int16_t; |
| 182 |
unique_span<uint8_t> result{input.size() * output_per_input * sizeof(output_type)}; |
| 183 |
const auto result_span{result.span()}; |
| 184 |
const std::span<output_type> output{reinterpret_cast<output_type *>(result_span.data()), result_span.size_bytes() / sizeof(output_type)}; |
| 185 |
auto output_iter = output.begin(); |
| 186 |
for (const auto input_value : input) |
| 187 |
{ |
| 188 |
/* Assert that the minimum and maximum possible values in the input can |
| 189 |
* be represented in the output without truncation. */ |
| 190 |
using input_type = decltype(input)::value_type; |
| 191 |
constexpr int8_t convert_u8_to_s8{INT8_MIN}; |
| 192 |
static_assert(std::in_range<output_type>(output_type{input_type{0}} + convert_u8_to_s8)); |
| 193 |
static_assert(std::in_range<output_type>(output_type{input_type{UINT8_MAX}} + convert_u8_to_s8)); |
| 194 |
const auto output_value = (output_type{input_value} + convert_u8_to_s8) << 8; |
| 195 |
const auto output_next_iter = std::next(output_iter, output_per_input); |
| 196 |
assert(output_iter != output.end()); |
| 197 |
std::fill(std::exchange(output_iter, output_next_iter), output_next_iter, output_value); |
| 198 |
} |
| 199 |
assert(output_iter == output.end()); |
| 200 |
return result; |
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
#endif |
| 205 |
|
| 206 |
#if DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SOUNDBLASTER16 |
| 207 |
namespace emulate_soundblaster16 { |
| 208 |
|
| 209 |
/* |
| 210 |
* Blackman windowed-sinc filter coefficients at 1/4 bandwidth of upsampled |
| 211 |
* frequency. Chosen for linear phase and approximates ~10th order IIR |
| 212 |
* |
| 213 |
* MATLAB/Octave code: |
| 214 |
|
| 215 |
N = 51; % Num coeffs (odd) |
| 216 |
B = 0.25; % 1/4 band |
| 217 |
half = (N-1)/2; |
| 218 |
n = (-half:half); % the sample index |
| 219 |
% Windowed sinc |
| 220 |
h_ideal = 2 * B .* sinc(B*n); |
| 221 |
h_win = blackman(N); |
| 222 |
b = h_win .* h_ideal.'; |
| 223 |
% Convert to fix point to ultimately apply to signed 16-bit data |
| 224 |
b_s16 = int32(round(b * (2^16 -1))); % coeffs! |
| 225 |
|
| 226 |
*/ |
| 227 |
constexpr std::size_t FILTER_LEN = 51; |
| 228 |
static constexpr std::array<int32_t, FILTER_LEN> coeffs_quarterband{{ |
| 229 |
0, 0, -7, -25, -35, 0, 94, 200, 205, 0, -395, -751, -702, 0, 1178, 2127, |
| 230 |
1907, 0, -3050, -5490, -5011, 0, 9275, 20326, 29311, 32767, 29311, |
| 231 |
20326, 9275, 0, -5011, -5490, -3050, 0, 1907, 2127, 1178, 0, -702, |
| 232 |
-751, -395, 0, 205, 200, 94, 0, -35, -25, -7, 0, 0 |
| 233 |
}} |
| 234 |
#if DXX_BUILD_DESCENT == 2 |
| 235 |
, |
| 236 |
// Coefficient set for half-band (e.g. 22050 -> 44100) |
| 237 |
coeffs_halfband{{ |
| 238 |
0, 0, -11, 0, 49, 0, -133, 0, 290, 0, -558, 0, 992, 0, -1666, 0, 2697, 0, |
| 239 |
-4313, 0, 7086, 0, -13117, 0, 41452, 65535, 41452, 0, -13117, 0, 7086, 0, |
| 240 |
-4313, 0, 2697, 0, -1666, 0, 992, 0, -558, 0, 290, 0, -133, 0, 49, 0, -11, |
| 241 |
0, 0 |
| 242 |
}} |
| 243 |
#endif |
| 244 |
; |
| 245 |
|
| 246 |
// Fixed-point FIR filtering |
| 247 |
// Not optimal: consider optimization with 1/4, 1/2 band filters, and symmetric kernels |
| 248 |
static auto filter_fir(const unique_span<int8_t> signal_storage, const std::span<const int32_t, FILTER_LEN> coeffs) |
| 249 |
{ |
| 250 |
const auto signal{signal_storage.span()}; |
| 251 |
const std::size_t outsize = signal.size(); |
| 252 |
unique_span<int16_t> result(outsize); |
| 253 |
const auto output{result.span()}; |
| 254 |
// A FIR filter is just a 1D convolution |
| 255 |
// Keep only signalLen samples |
| 256 |
for (const auto nn : xrange(outsize)) |
| 257 |
{ |
| 258 |
// Determine start/stop indices for convolved chunk |
| 259 |
constexpr std::size_t coeffsLen = FILTER_LEN; |
| 260 |
/* Avoid use of `std::max` here, since `nn + 1 < coeffsLen` would cause |
| 261 |
* unsigned subtraction to underflow. |
| 262 |
*/ |
| 263 |
const std::size_t min_idx = (nn + 1 > coeffsLen ? nn + 1 - coeffsLen : 0u); |
| 264 |
const std::size_t max_idx = nn; |
| 265 |
if (min_idx > max_idx) |
| 266 |
continue; |
| 267 |
|
| 268 |
int32_t cur_output{0}; // Increase bit size for fixed point expansion |
| 269 |
// Sum over each sample * coefficient in this column |
| 270 |
for (const auto kk : xrange(min_idx, max_idx + 1)) |
| 271 |
{ |
| 272 |
const auto product = int32_t{signal[kk]} * coeffs[nn - kk]; |
| 273 |
cur_output = cur_output + product; |
| 274 |
} |
| 275 |
|
| 276 |
// Save and fit back into int16 |
| 277 |
output[nn] = static_cast<int16_t>(cur_output >> 8); // Arithmetic shift |
| 278 |
} |
| 279 |
return result; |
| 280 |
} |
| 281 |
|
| 282 |
static auto upsample(const std::span<const uint8_t> input, const upscale_factor upFactor) |
| 283 |
{ |
| 284 |
const std::size_t factor = underlying_value(upFactor); |
| 285 |
switch (upFactor) |
| 286 |
{ |
| 287 |
case upscale_factor::from_11khz_to_44khz: |
| 288 |
#if DXX_BUILD_DESCENT == 2 |
| 289 |
case upscale_factor::from_22khz_to_44khz: |
| 290 |
#endif |
| 291 |
break; |
| 292 |
default: |
| 293 |
cf_assert(false); |
| 294 |
} |
| 295 |
const auto upsampledLen = input.size() * factor; |
| 296 |
/* Caution: `output` is sparsely initialized, so the value-initialization |
| 297 |
* from `make_unique` is necessary. This site cannot be converted to |
| 298 |
* `make_unique_for_overwrite`. |
| 299 |
*/ |
| 300 |
unique_span<int8_t> result(upsampledLen); |
| 301 |
const auto output{result.span()}; |
| 302 |
using output_type = decltype(output)::value_type; |
| 303 |
for (const auto ii : xrange(input.size())) |
| 304 |
{ |
| 305 |
/* Assert that the minimum and maximum possible values in the input can |
| 306 |
* be represented in the output without truncation. */ |
| 307 |
using conversion_type = int16_t; |
| 308 |
using input_type = decltype(input)::value_type; |
| 309 |
constexpr int8_t convert_u8_to_s8{INT8_MIN}; |
| 310 |
static_assert(std::in_range<output_type>(conversion_type{input_type{0}} + convert_u8_to_s8)); |
| 311 |
static_assert(std::in_range<output_type>(conversion_type{input_type{UINT8_MAX}} + convert_u8_to_s8)); |
| 312 |
// Save input sample, convert to signed |
| 313 |
output[ii*factor] = conversion_type{input[ii]} + convert_u8_to_s8; |
| 314 |
} |
| 315 |
return result; |
| 316 |
} |
| 317 |
|
| 318 |
static auto replicateChannel(const unique_span<int16_t> input_storage, const std::size_t output_per_input) |
| 319 |
{ |
| 320 |
const std::size_t chFactor = MIX_OUTPUT_CHANNELS; |
| 321 |
const auto input{input_storage.span()}; |
| 322 |
using output_type = int16_t; |
| 323 |
unique_span<uint8_t> result{output_per_input * sizeof(output_type)}; |
| 324 |
const auto output = reinterpret_cast<output_type *>(result.get()); |
| 325 |
for (const auto ii : xrange(input.size())) |
| 326 |
{ |
| 327 |
// Duplicate and interleave as many channels as needed |
| 328 |
std::fill_n(std::next(output, ii * chFactor), chFactor, input[ii]); |
| 329 |
} |
| 330 |
return result; |
| 331 |
} |
| 332 |
|
| 333 |
static auto convert_audio(const std::span<const uint8_t> input, const std::size_t output_per_input, const upscale_factor upFactor) |
| 334 |
{ |
| 335 |
// We expect a 4x upscaling 11025 -> 44100 |
| 336 |
// But maybe 2x for d2x in some cases |
| 337 |
auto &coeffs = |
| 338 |
#if DXX_BUILD_DESCENT == 2 |
| 339 |
(upFactor == upscale_factor::from_22khz_to_44khz) |
| 340 |
? coeffs_halfband |
| 341 |
/* Otherwise, assume upscale_factor::from_11khz_to_44khz */ |
| 342 |
: |
| 343 |
#endif |
| 344 |
coeffs_quarterband; |
| 345 |
|
| 346 |
return replicateChannel( |
| 347 |
// First upsample |
| 348 |
// Apply LPF filter to smooth out upscaled points |
| 349 |
// There will be some uniform amplitude loss here, but less than -3dB |
| 350 |
filter_fir(upsample(input, upFactor), coeffs), |
| 351 |
output_per_input); |
| 352 |
} |
| 353 |
|
| 354 |
} |
| 355 |
#endif |
| 356 |
#endif |
| 357 |
|
| 358 |
#if DXX_FEATURE_EXTERNAL_RESAMPLER_SDL_NATIVE |
| 359 |
namespace sdl_native { |
| 360 |
|
| 361 |
static unique_span<uint8_t> convert_audio(const sound_effect sound_idx, const std::span<const uint8_t> data, const int freq) |
| 362 |
{ |
| 363 |
SDL_AudioCVT cvt; |
| 364 |
if (SDL_BuildAudioCVT(&cvt, AUDIO_U8, 1, freq, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, digi_sample_rate) == -1) |
| 365 |
{ |
| 366 |
con_printf(CON_URGENT, "%s:%u: SDL_BuildAudioCVT failed: sound=%u dlen=%" DXX_PRI_size_type " freq=%i out_format=%i out_channels=%i out_freq=%i", __FILE__, __LINE__, sound_idx, data.size(), freq, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, digi_sample_rate); |
| 367 |
return {}; |
| 368 |
} |
| 369 |
if (cvt.len_mult < 1) |
| 370 |
{ |
| 371 |
con_printf(CON_URGENT, "%s:%u: SDL_BuildAudioCVT requested invalid length multiplier: sound=%u dlen=%" DXX_PRI_size_type " freq=%i out_format=%i out_channels=%i out_freq=%i len_mult=%i", __FILE__, __LINE__, sound_idx, data.size(), freq, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, digi_sample_rate, cvt.len_mult); |
| 372 |
return {}; |
| 373 |
} |
| 374 |
const std::size_t workingSize = data.size() * cvt.len_mult; |
| 375 |
auto cvtbuf = std::make_unique<uint8_t[]>(workingSize); |
| 376 |
cvt.buf = cvtbuf.get(); |
| 377 |
cvt.len = data.size(); |
| 378 |
memcpy(cvt.buf, data.data(), data.size()); |
| 379 |
if (SDL_ConvertAudio(&cvt)) |
| 380 |
{ |
| 381 |
con_printf(CON_URGENT, "%s:%u: SDL_ConvertAudio failed: sound=%u dlen=%" DXX_PRI_size_type " freq=%i out_format=%i out_channels=%i out_freq=%i", __FILE__, __LINE__, sound_idx, data.size(), freq, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, digi_sample_rate); |
| 382 |
return {}; |
| 383 |
} |
| 384 |
if (const std::size_t convertedSize = cvt.len_cvt; convertedSize < workingSize) |
| 385 |
{ |
| 386 |
/* The final sound required less space to store than |
| 387 |
* SDL_BuildAudioCVT requested for an intermediate buffer. |
| 388 |
* Allocate a new buffer just large enough for the final sound, |
| 389 |
* copy the staging buffer into it, and use that new buffer as the |
| 390 |
* long term storage. |
| 391 |
*/ |
| 392 |
auto outbuf = std::make_unique<uint8_t[]>(convertedSize); |
| 393 |
memcpy(outbuf.get(), cvt.buf, convertedSize); |
| 394 |
return {std::move(outbuf), convertedSize}; |
| 395 |
} |
| 396 |
else |
| 397 |
{ |
| 398 |
/* The final sound required as much (or more) space than was |
| 399 |
* requested. If it required more, there was likely memory |
| 400 |
* corruption, and that would be a bug in SDL audio conversion. |
| 401 |
* Therefore, assume that this path is for when the requested space |
| 402 |
* was exactly correct. No memory can be recovered with an extra |
| 403 |
* copy, so transfer the staging buffer to the output structure. |
| 404 |
*/ |
| 405 |
return {std::move(cvtbuf), convertedSize}; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
} |
| 410 |
#endif |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
} |
| 415 |
|
| 416 |
namespace dsx { |
| 417 |
|
| 418 |
namespace { |
| 419 |
|
| 420 |
static std::array<RAIIMix_Chunk, MAX_SOUNDS> SoundChunks; |
| 421 |
|
| 422 |
/* |
| 423 |
* Play-time conversion. Performs output conversion only once per sound effect used. |
| 424 |
* Once the sound sample has been converted, it is cached in SoundChunks[] |
| 425 |
*/ |
| 426 |
static void mixdigi_convert_sound(const sound_effect sound_idx, RAIIMix_Chunk &sci, const digi_sound &gs, const uint16_t freq) |
| 427 |
{ |
| 428 |
const auto data = gs.span(); |
| 429 |
if (data.empty()) |
| 430 |
return; |
| 431 |
|
| 432 |
digi_mixer_method method = CGameArg.SndMixerMethod; |
| 433 |
unique_span<uint8_t> cvtbuf; |
| 434 |
#if !DXX_FEATURE_EXTERNAL_RESAMPLER_SDL_NATIVE |
| 435 |
(void)sound_idx; |
| 436 |
#endif |
| 437 |
switch (method) |
| 438 |
{ |
| 439 |
#if DXX_FEATURE_INTERNAL_RESAMPLER |
| 440 |
#if DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SDL1 |
| 441 |
case digi_mixer_method::emulate_sdl1: |
| 442 |
#endif |
| 443 |
#if DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SOUNDBLASTER16 |
| 444 |
case digi_mixer_method::emulate_soundblaster16: |
| 445 |
#endif |
| 446 |
{ |
| 447 |
/* Only a small set of conversions are supported. List them out |
| 448 |
* explicitly instead of using division. This also allows the |
| 449 |
* conversion factor to be an `enum class`, which emphasizes its |
| 450 |
* limited legal values. |
| 451 |
*/ |
| 452 |
const auto upFactor = ({ |
| 453 |
upscale_factor r; |
| 454 |
if (freq == underlying_value(sound_sample_rate::_11k)) |
| 455 |
r = upscale_factor::from_11khz_to_44khz; |
| 456 |
#if DXX_BUILD_DESCENT == 2 |
| 457 |
else if (freq == underlying_value(sound_sample_rate::_22k)) |
| 458 |
r = upscale_factor::from_22khz_to_44khz; |
| 459 |
#endif |
| 460 |
else |
| 461 |
return; |
| 462 |
r; |
| 463 |
}); |
| 464 |
const std::size_t output_per_input = underlying_value(upFactor) * MIX_OUTPUT_CHANNELS; |
| 465 |
switch (method) |
| 466 |
{ |
| 467 |
#if DXX_FEATURE_EXTERNAL_RESAMPLER_SDL_NATIVE |
| 468 |
case digi_mixer_method::sdl_native: |
| 469 |
/* This is unreachable, since the case labels of the |
| 470 |
* outer switch only match emulation paths. |
| 471 |
*/ |
| 472 |
return; |
| 473 |
#endif |
| 474 |
#if DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SDL1 |
| 475 |
case digi_mixer_method::emulate_sdl1: |
| 476 |
cvtbuf = emulate_sdl1::convert_audio(data, output_per_input); |
| 477 |
break; |
| 478 |
#endif |
| 479 |
#if DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SOUNDBLASTER16 |
| 480 |
case digi_mixer_method::emulate_soundblaster16: |
| 481 |
cvtbuf = emulate_soundblaster16::convert_audio(data, data.size() * output_per_input, upFactor); |
| 482 |
break; |
| 483 |
#endif |
| 484 |
} |
| 485 |
} |
| 486 |
break; |
| 487 |
#endif |
| 488 |
#if DXX_FEATURE_EXTERNAL_RESAMPLER_SDL_NATIVE |
| 489 |
case digi_mixer_method::sdl_native: |
| 490 |
cvtbuf = sdl_native::convert_audio(sound_idx, data, freq); |
| 491 |
break; |
| 492 |
#endif |
| 493 |
} |
| 494 |
sci.alen = cvtbuf.size(); |
| 495 |
sci.abuf = cvtbuf.release(); |
| 496 |
sci.allocated = 1; |
| 497 |
sci.volume = 128; // Max volume = 128 |
| 498 |
} |
| 499 |
|
| 500 |
static Mix_Chunk &mixdigi_convert_sound(const sound_effect i) |
| 501 |
{ |
| 502 |
auto &sci = SoundChunks[i]; |
| 503 |
if (!sci.abuf) |
| 504 |
{ |
| 505 |
auto &gs = GameSounds[i]; |
| 506 |
#if DXX_BUILD_DESCENT == 1 |
| 507 |
const auto freq{gs.freq}; |
| 508 |
#elif DXX_BUILD_DESCENT == 2 |
| 509 |
const auto freq = underlying_value(GameArg.SndDigiSampleRate); |
| 510 |
#endif |
| 511 |
//proceed only if not converted yet |
| 512 |
mixdigi_convert_sound(i, sci, gs, freq); |
| 513 |
} |
| 514 |
return sci; |
| 515 |
} |
| 516 |
|
| 517 |
} |
| 518 |
|
| 519 |
// Volume 0-F1_0 |
| 520 |
sound_channel digi_mixer_start_sound(sound_effect soundnum, const fix volume, const sound_pan pan, const int looping, const int loop_start, const int loop_end, sound_object *) |
| 521 |
{ |
| 522 |
if (!digi_initialised) |
| 523 |
return sound_channel::None; |
| 524 |
|
| 525 |
if (soundnum == sound_effect::None) |
| 526 |
return sound_channel::None; |
| 527 |
|
| 528 |
const unsigned max_channels = digi_mixer_max_channels; |
| 529 |
if (max_channels > channels.size()) |
| 530 |
return sound_channel::None; |
| 531 |
const auto c = digi_mixer_find_channel(channels, max_channels); |
| 532 |
const auto channel = underlying_value(c); |
| 533 |
if (channel >= max_channels) |
| 534 |
return sound_channel::None; |
| 535 |
|
| 536 |
mixdigi_convert_sound(soundnum); |
| 537 |
|
| 538 |
const int mix_pan = fix2byte(static_cast<fix>(pan)); |
| 539 |
#if MIX_DIGI_DEBUG |
| 540 |
con_printf(CON_DEBUG, "digi_start_sound %d, volume=%d, pan %d (start=%d, end=%d)", soundnum, volume, mix_pan, loop_start, loop_end); |
| 541 |
#else |
| 542 |
(void)loop_start; |
| 543 |
(void)loop_end; |
| 544 |
#endif |
| 545 |
|
| 546 |
const int mix_loop = looping * -1; |
| 547 |
Mix_PlayChannel(channel, &(SoundChunks[soundnum]), mix_loop); |
| 548 |
Mix_SetPanning(channel, 255-mix_pan, mix_pan); |
| 549 |
Mix_SetDistance(channel, UINT8_MAX - fix2byte(volume)); |
| 550 |
channels.set(c); |
| 551 |
return c; |
| 552 |
} |
| 553 |
|
| 554 |
} |
| 555 |
|
| 556 |
namespace dcx { |
| 557 |
|
| 558 |
void digi_mixer_set_channel_volume(const sound_channel channel, const int volume) |
| 559 |
{ |
| 560 |
if (!digi_initialised) return; |
| 561 |
Mix_SetDistance(underlying_value(channel), UINT8_MAX - fix2byte(volume)); |
| 562 |
} |
| 563 |
|
| 564 |
void digi_mixer_set_channel_pan(const sound_channel channel, const sound_pan pan) |
| 565 |
{ |
| 566 |
int mix_pan = fix2byte(static_cast<fix>(pan)); |
| 567 |
Mix_SetPanning(underlying_value(channel), 255 - mix_pan, mix_pan); |
| 568 |
} |
| 569 |
|
| 570 |
void digi_mixer_stop_sound(const sound_channel channel) |
| 571 |
{ |
| 572 |
if (!digi_initialised) return; |
| 573 |
const auto c = underlying_value(channel); |
| 574 |
#if MIX_DIGI_DEBUG |
| 575 |
con_printf(CON_DEBUG, "%s:%u: %d", __FUNCTION__, __LINE__, c); |
| 576 |
#endif |
| 577 |
Mix_HaltChannel(c); |
| 578 |
channels.reset(channel); |
| 579 |
} |
| 580 |
|
| 581 |
void digi_mixer_end_sound(const sound_channel channel) |
| 582 |
{ |
| 583 |
digi_mixer_stop_sound(channel); |
| 584 |
channels.reset(channel); |
| 585 |
} |
| 586 |
|
| 587 |
void digi_mixer_set_digi_volume( int dvolume ) |
| 588 |
{ |
| 589 |
/* |
| 590 |
* Double-scale Mix_Volume to more closely match old pre-20a0166c Rebirth builds |
| 591 |
* This also preserves the (unintentionally?) more accurate percieved volume scaling of those builds |
| 592 |
* (e.g. perceived sound volume will match perceived midi volume more closely) |
| 593 |
*/ |
| 594 |
digi_volume = dvolume; |
| 595 |
if (!digi_initialised) return; |
| 596 |
Mix_Volume(-1, fix2byte(fixmul(dvolume, dvolume))); |
| 597 |
} |
| 598 |
|
| 599 |
int digi_mixer_is_channel_playing(const sound_channel c) |
| 600 |
{ |
| 601 |
return channels[c]; |
| 602 |
} |
| 603 |
|
| 604 |
void digi_mixer_stop_all_channels() |
| 605 |
{ |
| 606 |
channels = {}; |
| 607 |
Mix_HaltChannel(-1); |
| 608 |
} |
| 609 |
|
| 610 |
} |