| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "loudness.h" |
| 20 | |
| 21 | #include "file.h" |
| 22 | #include "lds_play.h" |
| 23 | #include "nortsong.h" |
| 24 | #include "opentyr.h" |
| 25 | #include "params.h" |
| 26 | |
| 27 | #include <assert.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #define OUTPUT_QUALITY 4 // 44.1 kHz |
| 32 | |
| 33 | int audioSampleRate = 0; |
| 34 | |
| 35 | bool music_stopped = true; |
| 36 | unsigned int song_playing = 0; |
| 37 | |
| 38 | bool audio_disabled = false, music_disabled = false, samples_disabled = false; |
| 39 | |
| 40 | static SDL_AudioDeviceID audioDevice = 0; |
| 41 | |
| 42 | static Uint8 musicVolume = 255; |
| 43 | static Uint8 sampleVolume = 255; |
| 44 | |
| 45 | static const float volumeRange = 30.0f; // dB |
| 46 | |
| 47 | // Fixed point Q20.12; needs to be able to store (10 * INT16_MIN/MAX) |
| 48 | static Sint32 volumeFactorTable[256]; |
| 49 | #define TO_FIXED(x) ((Sint32)((x) * (1 << 12))) |
| 50 | #define FIXED_TO_INT(x) ((Sint32)((x) >> 12)) |
| 51 | |
| 52 | // Twice the Loudness update rate (in updates/second). In Tyrian, Loudness |
| 53 | // updates were performed at the same rate as the game timer, which varied |
| 54 | // depending on the game speed (~69.57 Hz at most game speeds). We don't have |
| 55 | // the same limitations, so we'll keep the update rate constant, but we do want |
| 56 | // to stick to integer math, so we'll update at 69.5 Hz. |
| 57 | static const int ldsUpdate2Rate = 139; // 69.5 * 2 |
| 58 | |
| 59 | static int samplesPerLdsUpdate; |
| 60 | static int samplesPerLdsUpdateFrac; |
| 61 | |
| 62 | static int samplesUntilLdsUpdate = 0; |
| 63 | static int samplesUntilLdsUpdateFrac = 0; |
| 64 | |
| 65 | static FILE *music_file = NULL; |
| 66 | static Uint32 *song_offset; |
| 67 | static Uint16 song_count = 0; |
| 68 | |
| 69 | #define CHANNEL_COUNT 8 |
| 70 | static const Sint16 *channelSamples[CHANNEL_COUNT]; |
| 71 | static size_t channelSampleCount[CHANNEL_COUNT] = { 0 }; |
| 72 | static Uint8 channelVolume[CHANNEL_COUNT]; |
| 73 | #define CHANNEL_VOLUME_LEVELS 8 |
| 74 | |
| 75 | static void audioCallback(void *userdata, Uint8 *stream, int size); |
| 76 | |
| 77 | static void load_song(unsigned int song_num); |
| 78 | |
| 79 | bool init_audio(void) |
| 80 | { |
| 81 | if (audio_disabled) |
| 82 | return false; |
| 83 | |
| 84 | SDL_AudioSpec ask, got; |
| 85 | |
| 86 | ask.freq = 11025 * OUTPUT_QUALITY; |
| 87 | ask.format = AUDIO_S16SYS; |
| 88 | ask.channels = 1; |
| 89 | ask.samples = 256 * OUTPUT_QUALITY; // ~23 ms |
| 90 | ask.callback = audioCallback; |
| 91 | |
| 92 | if (SDL_InitSubSystem(SDL_INIT_AUDIO)) |
| 93 | { |
| 94 | fprintf(stderr, "error: failed to initialize SDL audio: %s\n", SDL_GetError()); |
| 95 | audio_disabled = true; |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | int allowedChanges = SDL_AUDIO_ALLOW_FREQUENCY_CHANGE; |
| 100 | #if SDL_VERSION_ATLEAST(2, 0, 9) |
| 101 | allowedChanges |= SDL_AUDIO_ALLOW_SAMPLES_CHANGE; |
| 102 | #endif |
| 103 | audioDevice = SDL_OpenAudioDevice(/*device*/ NULL, /*iscapture*/ 0, &ask, &got, allowedChanges); |
| 104 | |
| 105 | if (audioDevice == 0) |
| 106 | { |
| 107 | fprintf(stderr, "error: SDL failed to open audio device: %s\n", SDL_GetError()); |
| 108 | audio_disabled = true; |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | audioSampleRate = got.freq; |
| 113 | |
| 114 | samplesPerLdsUpdate = 2 * (audioSampleRate / ldsUpdate2Rate); |
| 115 | samplesPerLdsUpdateFrac = 2 * (audioSampleRate % ldsUpdate2Rate); |
| 116 | |
| 117 | volumeFactorTable[0] = 0; |
| 118 | for (size_t i = 1; i < 256; ++i) |
| 119 | volumeFactorTable[i] = TO_FIXED(powf(10, (255 - i) * (-volumeRange / (20.0f * 255)))); |
| 120 | |
| 121 | opl_init(); |
| 122 | |
| 123 | SDL_PauseAudioDevice(audioDevice, 0); // unpause |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | static void audioCallback(void *userdata, Uint8 *stream, int size) |
| 129 | { |
| 130 | (void)userdata; |
| 131 | |
| 132 | Sint16 *const samples = (Sint16 *)stream; |
| 133 | const int samplesCount = size / sizeof (Sint16); |
| 134 | |
| 135 | if (!music_disabled && !music_stopped) |
| 136 | { |
| 137 | Sint16 *remaining = samples; |
| 138 | int remainingCount = samplesCount; |
| 139 | while (remainingCount > 0) |
| 140 | { |
| 141 | if (samplesUntilLdsUpdate == 0) |
| 142 | { |
| 143 | lds_update(); |
| 144 | |
| 145 | // The number of samples that should be produced per Loudness |
| 146 | // update is not an integer, but we can only produce an integer |
| 147 | // number of samples, so we accumulate the fractional samples |
| 148 | // until it amounts to a whole sample. |
| 149 | samplesUntilLdsUpdate += samplesPerLdsUpdate; |
| 150 | samplesUntilLdsUpdateFrac += samplesPerLdsUpdateFrac; |
| 151 | if (samplesUntilLdsUpdateFrac >= ldsUpdate2Rate) |
| 152 | { |
| 153 | samplesUntilLdsUpdate += 1; |
| 154 | samplesUntilLdsUpdateFrac -= ldsUpdate2Rate; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | int count = MIN(samplesUntilLdsUpdate, remainingCount); |
| 159 | |
| 160 | opl_update(remaining, count); |
| 161 | |
| 162 | remaining += count; |
| 163 | remainingCount -= count; |
| 164 | |
| 165 | samplesUntilLdsUpdate -= count; |
| 166 | } |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | for (int i = 0; i < samplesCount; ++i) |
| 171 | samples[i] = 0; |
| 172 | } |
| 173 | |
| 174 | Sint32 musicVolumeFactor = volumeFactorTable[musicVolume]; |
| 175 | musicVolumeFactor *= 2; // OPL emulator is too quiet |
| 176 | |
| 177 | if (samples_disabled && !music_disabled) |
| 178 | { |
| 179 | // Mix music |
| 180 | Sint16 *remaining = samples; |
| 181 | int remainingCount = samplesCount; |
| 182 | while (remainingCount > 0) |
| 183 | { |
| 184 | Sint32 sample = *remaining * musicVolumeFactor; |
| 185 | |
| 186 | sample = FIXED_TO_INT(sample); |
| 187 | *remaining = MIN(MAX(INT16_MIN, sample), INT16_MAX); |
| 188 | |
| 189 | remaining += 1; |
| 190 | remainingCount -= 1; |
| 191 | } |
| 192 | } |
| 193 | else if (!samples_disabled) |
| 194 | { |
| 195 | Sint32 sampleVolumeFactor = volumeFactorTable[sampleVolume]; |
| 196 | Sint32 sampleVolumeFactors[CHANNEL_VOLUME_LEVELS]; |
| 197 | for (int i = 0; i < CHANNEL_VOLUME_LEVELS; ++i) |
| 198 | sampleVolumeFactors[i] = sampleVolumeFactor * (i + 1) / CHANNEL_VOLUME_LEVELS; |
| 199 | |
| 200 | // Mix music and channels |
| 201 | Sint16 *remaining = samples; |
| 202 | int remainingCount = samplesCount; |
| 203 | while (remainingCount > 0) |
| 204 | { |
| 205 | Sint32 sample = *remaining * musicVolumeFactor; |
| 206 | |
| 207 | for (size_t i = 0; i < CHANNEL_COUNT; ++i) |
| 208 | { |
| 209 | if (channelSampleCount[i] > 0) |
| 210 | { |
| 211 | sample += *channelSamples[i] * sampleVolumeFactors[channelVolume[i]]; |
| 212 | |
| 213 | channelSamples[i] += 1; |
| 214 | channelSampleCount[i] -= 1; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | sample = FIXED_TO_INT(sample); |
| 219 | *remaining = MIN(MAX(INT16_MIN, sample), INT16_MAX); |
| 220 | |
| 221 | remaining += 1; |
| 222 | remainingCount -= 1; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void deinit_audio(void) |
| 228 | { |
| 229 | if (audio_disabled) |
| 230 | return; |
| 231 | |
| 232 | if (audioDevice != 0) |
| 233 | { |
| 234 | SDL_PauseAudioDevice(audioDevice, 1); // pause |
| 235 | SDL_CloseAudioDevice(audioDevice); |
| 236 | audioDevice = 0; |
| 237 | } |
| 238 | |
| 239 | SDL_QuitSubSystem(SDL_INIT_AUDIO); |
| 240 | |
| 241 | memset(channelSampleCount, 0, sizeof channelSampleCount); |
| 242 | |
| 243 | lds_free(); |
| 244 | } |
| 245 | |
| 246 | void load_music(void) // FKA NortSong.loadSong |
| 247 | { |
| 248 | if (music_file == NULL) |
| 249 | { |
| 250 | music_file = dir_fopen_die(data_dir(), "music.mus", "rb"); |
| 251 | |
| 252 | fread_u16_die(&song_count, 1, music_file); |
| 253 | |
| 254 | song_offset = malloc((song_count + 1) * sizeof(*song_offset)); |
| 255 | |
| 256 | fread_u32_die(song_offset, song_count, music_file); |
| 257 | |
| 258 | song_offset[song_count] = ftell_eof(music_file); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void load_song(unsigned int song_num) // FKA NortSong.loadSong |
| 263 | { |
| 264 | if (song_num < song_count) |
| 265 | { |
| 266 | unsigned int song_size = song_offset[song_num + 1] - song_offset[song_num]; |
| 267 | lds_load(music_file, song_offset[song_num], song_size); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | fprintf(stderr, "warning: failed to load song %d\n", song_num + 1); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void play_song(unsigned int song_num) // FKA NortSong.playSong |
| 276 | { |
| 277 | if (audio_disabled) |
| 278 | return; |
| 279 | |
| 280 | if (song_num != song_playing) |
| 281 | { |
| 282 | SDL_LockAudioDevice(audioDevice); |
| 283 | |
| 284 | music_stopped = true; |
| 285 | |
| 286 | SDL_UnlockAudioDevice(audioDevice); |
| 287 | |
| 288 | load_song(song_num); |
| 289 | |
| 290 | song_playing = song_num; |
| 291 | } |
| 292 | |
| 293 | SDL_LockAudioDevice(audioDevice); |
| 294 | |
| 295 | music_stopped = false; |
| 296 | |
| 297 | SDL_UnlockAudioDevice(audioDevice); |
| 298 | } |
| 299 | |
| 300 | void restart_song(void) // FKA Player.selectSong(1) |
| 301 | { |
| 302 | if (audio_disabled) |
| 303 | return; |
| 304 | |
| 305 | SDL_LockAudioDevice(audioDevice); |
| 306 | |
| 307 | lds_rewind(); |
| 308 | |
| 309 | music_stopped = false; |
| 310 | |
| 311 | SDL_UnlockAudioDevice(audioDevice); |
| 312 | } |
| 313 | |
| 314 | void stop_song(void) // FKA Player.selectSong(0) |
| 315 | { |
| 316 | if (audio_disabled) |
| 317 | return; |
| 318 | |
| 319 | SDL_LockAudioDevice(audioDevice); |
| 320 | |
| 321 | music_stopped = true; |
| 322 | |
| 323 | SDL_UnlockAudioDevice(audioDevice); |
| 324 | } |
| 325 | |
| 326 | void fade_song(void) // FKA Player.selectSong($C001) |
| 327 | { |
| 328 | if (audio_disabled) |
| 329 | return; |
| 330 | |
| 331 | SDL_LockAudioDevice(audioDevice); |
| 332 | |
| 333 | lds_fade(1); |
| 334 | |
| 335 | SDL_UnlockAudioDevice(audioDevice); |
| 336 | } |
| 337 | |
| 338 | void set_volume(Uint8 musicVolume_, Uint8 sampleVolume_) // FKA NortSong.setVol and Player.setVol |
| 339 | { |
| 340 | if (audio_disabled) |
| 341 | return; |
| 342 | |
| 343 | SDL_LockAudioDevice(audioDevice); |
| 344 | |
| 345 | musicVolume = musicVolume_; |
| 346 | sampleVolume = sampleVolume_; |
| 347 | |
| 348 | SDL_UnlockAudioDevice(audioDevice); |
| 349 | } |
| 350 | |
| 351 | void multiSamplePlay(const Sint16 *samples, size_t sampleCount, Uint8 chan, Uint8 vol) // FKA Player.multiSamplePlay |
| 352 | { |
| 353 | assert(chan < CHANNEL_COUNT); |
| 354 | assert(vol < CHANNEL_VOLUME_LEVELS); |
| 355 | |
| 356 | if (audio_disabled || samples_disabled) |
| 357 | return; |
| 358 | |
| 359 | SDL_LockAudioDevice(audioDevice); |
| 360 | |
| 361 | channelSamples[chan] = samples; |
| 362 | channelSampleCount[chan] = sampleCount; |
| 363 | channelVolume[chan] = vol; |
| 364 | |
| 365 | SDL_UnlockAudioDevice(audioDevice); |
| 366 | } |