| 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 "nortsong.h" |
| 20 | |
| 21 | #include "file.h" |
| 22 | #include "joystick.h" |
| 23 | #include "keyboard.h" |
| 24 | #include "loudness.h" |
| 25 | #include "musmast.h" |
| 26 | #include "opentyr.h" |
| 27 | #include "params.h" |
| 28 | #include "sndmast.h" |
| 29 | #include "vga256d.h" |
| 30 | |
| 31 | #include "SDL.h" |
| 32 | |
| 33 | JE_word frameCountMax; |
| 34 | |
| 35 | Sint16 *soundSamples[SOUND_COUNT] = { NULL }; /* [1..soundnum + 9] */ // FKA digiFx |
| 36 | size_t soundSampleCount[SOUND_COUNT] = { 0 }; /* [1..soundnum + 9] */ // FKA fxSize |
| 37 | |
| 38 | JE_word tyrMusicVolume, fxVolume; |
| 39 | const JE_word fxPlayVol = 4; |
| 40 | JE_word tempVolume; |
| 41 | |
| 42 | // The period of the x86 programmable interval timer in milliseconds. |
| 43 | static const float pitPeriod = (12.0f / 14318180.0f) * 1000.0f; |
| 44 | |
| 45 | static Uint16 delaySpeed = 0x4300; |
| 46 | static float delayPeriod = 0x4300 * ((12.0f / 14318180.0f) * 1000.0f); |
| 47 | |
| 48 | static Uint32 target = 0; |
| 49 | static Uint32 target2 = 0; |
| 50 | |
| 51 | void setDelay(int delay) // FKA NortSong.frameCount |
| 52 | { |
| 53 | target = SDL_GetTicks() + delay * delayPeriod; |
| 54 | } |
| 55 | |
| 56 | void setDelay2(int delay) // FKA NortSong.frameCount2 |
| 57 | { |
| 58 | target2 = SDL_GetTicks() + delay * delayPeriod; |
| 59 | } |
| 60 | |
| 61 | Uint32 getDelayTicks(void) // FKA NortSong.frameCount |
| 62 | { |
| 63 | Sint32 delay = target - SDL_GetTicks(); |
| 64 | return MAX(0, delay); |
| 65 | } |
| 66 | |
| 67 | Uint32 getDelayTicks2(void) // FKA NortSong.frameCount2 |
| 68 | { |
| 69 | Sint32 delay = target2 - SDL_GetTicks(); |
| 70 | return MAX(0, delay); |
| 71 | } |
| 72 | |
| 73 | void wait_delay(void) |
| 74 | { |
| 75 | Sint32 delay = target - SDL_GetTicks(); |
| 76 | if (delay > 0) |
| 77 | SDL_Delay(delay); |
| 78 | } |
| 79 | |
| 80 | void service_wait_delay(void) |
| 81 | { |
| 82 | for (; ; ) |
| 83 | { |
| 84 | service_SDL_events(false); |
| 85 | |
| 86 | Sint32 delay = target - SDL_GetTicks(); |
| 87 | if (delay <= 0) |
| 88 | return; |
| 89 | |
| 90 | SDL_Delay(MIN(delay, SDL_POLL_INTERVAL)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void wait_delayorinput(void) |
| 95 | { |
| 96 | for (; ; ) |
| 97 | { |
| 98 | service_SDL_events(false); |
| 99 | poll_joysticks(); |
| 100 | |
| 101 | if (newkey || mousedown || joydown) |
| 102 | { |
| 103 | newkey = false; |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | Sint32 delay = target - SDL_GetTicks(); |
| 108 | if (delay <= 0) |
| 109 | return; |
| 110 | |
| 111 | SDL_Delay(MIN(delay, SDL_POLL_INTERVAL)); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void loadSndFile(bool xmas) |
| 116 | { |
| 117 | FILE *f; |
| 118 | |
| 119 | f = dir_fopen_die(data_dir(), "tyrian.snd", "rb"); |
| 120 | |
| 121 | Uint16 sfxCount; |
| 122 | Uint32 sfxPositions[SFX_COUNT + 1]; |
| 123 | |
| 124 | // Read number of sounds. |
| 125 | fread_u16_die(&sfxCount, 1, f); |
| 126 | if (sfxCount != SFX_COUNT) |
| 127 | goto die; |
| 128 | |
| 129 | // Read positions of sounds. |
| 130 | fread_u32_die(sfxPositions, sfxCount, f); |
| 131 | |
| 132 | // Determine end of last sound. |
| 133 | fseek(f, 0, SEEK_END); |
| 134 | sfxPositions[sfxCount] = ftell(f); |
| 135 | |
| 136 | // Read samples. |
| 137 | for (size_t i = 0; i < sfxCount; ++i) |
| 138 | { |
| 139 | soundSampleCount[i] = sfxPositions[i + 1] - sfxPositions[i]; |
| 140 | |
| 141 | // Sound size cannot exceed 64 KiB. |
| 142 | if (soundSampleCount[i] > UINT16_MAX) |
| 143 | goto die; |
| 144 | |
| 145 | free(soundSamples[i]); |
| 146 | soundSamples[i] = malloc(soundSampleCount[i]); |
| 147 | |
| 148 | fseek(f, sfxPositions[i], SEEK_SET); |
| 149 | fread_u8_die((Uint8 *)soundSamples[i], soundSampleCount[i], f); |
| 150 | } |
| 151 | |
| 152 | fclose(f); |
| 153 | |
| 154 | f = dir_fopen_die(data_dir(), xmas ? "voicesc.snd" : "voices.snd", "rb"); |
| 155 | |
| 156 | Uint16 voiceCount; |
| 157 | Uint32 voicePositions[VOICE_COUNT + 1]; |
| 158 | |
| 159 | // Read number of sounds. |
| 160 | fread_u16_die(&voiceCount, 1, f); |
| 161 | if (voiceCount != VOICE_COUNT) |
| 162 | goto die; |
| 163 | |
| 164 | // Read positions of sounds. |
| 165 | fread_u32_die(voicePositions, voiceCount, f); |
| 166 | |
| 167 | // Determine end of last sound. |
| 168 | fseek(f, 0, SEEK_END); |
| 169 | voicePositions[voiceCount] = ftell(f); |
| 170 | |
| 171 | for (size_t vi = 0; vi < voiceCount; ++vi) |
| 172 | { |
| 173 | size_t i = SFX_COUNT + vi; |
| 174 | |
| 175 | soundSampleCount[i] = voicePositions[vi + 1] - voicePositions[vi]; |
| 176 | |
| 177 | // Voice sounds have some bad data at the end. |
| 178 | soundSampleCount[i] = soundSampleCount[i] >= 100 |
| 179 | ? soundSampleCount[i] - 100 |
| 180 | : 0; |
| 181 | |
| 182 | // Sound size cannot exceed 64 KiB. |
| 183 | if (soundSampleCount[i] > UINT16_MAX) |
| 184 | goto die; |
| 185 | |
| 186 | free(soundSamples[i]); |
| 187 | soundSamples[i] = malloc(soundSampleCount[i]); |
| 188 | |
| 189 | fseek(f, voicePositions[vi], SEEK_SET); |
| 190 | fread_u8_die((Uint8 *)soundSamples[i], soundSampleCount[i], f); |
| 191 | } |
| 192 | |
| 193 | fclose(f); |
| 194 | |
| 195 | // Convert samples to output sample format and rate. |
| 196 | |
| 197 | SDL_AudioCVT cvt; |
| 198 | if (SDL_BuildAudioCVT(&cvt, AUDIO_S8, 1, 11025, AUDIO_S16SYS, 1, audioSampleRate) < 0) |
| 199 | { |
| 200 | fprintf(stderr, "error: Failed to build audio converter: %s\n", SDL_GetError()); |
| 201 | |
| 202 | for (int i = 0; i < SOUND_COUNT; ++i) |
| 203 | soundSampleCount[i] = 0; |
| 204 | |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | size_t maxSampleSize = 0; |
| 209 | for (size_t i = 0; i < SOUND_COUNT; ++i) |
| 210 | maxSampleSize = MAX(maxSampleSize, soundSampleCount[i]); |
| 211 | |
| 212 | cvt.buf = malloc(maxSampleSize * cvt.len_mult); |
| 213 | |
| 214 | for (size_t i = 0; i < SOUND_COUNT; ++i) |
| 215 | { |
| 216 | cvt.len = soundSampleCount[i]; |
| 217 | memcpy(cvt.buf, soundSamples[i], cvt.len); |
| 218 | |
| 219 | if (SDL_ConvertAudio(&cvt)) |
| 220 | { |
| 221 | fprintf(stderr, "error: Failed to convert audio: %s\n", SDL_GetError()); |
| 222 | |
| 223 | soundSampleCount[i] = 0; |
| 224 | |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | free(soundSamples[i]); |
| 229 | soundSamples[i] = malloc(cvt.len_cvt); |
| 230 | |
| 231 | memcpy(soundSamples[i], cvt.buf, cvt.len_cvt); |
| 232 | soundSampleCount[i] = cvt.len_cvt / sizeof (Sint16); |
| 233 | } |
| 234 | |
| 235 | free(cvt.buf); |
| 236 | |
| 237 | return; |
| 238 | |
| 239 | die: |
| 240 | fprintf(stderr, "error: Unexpected data was read from a file.\n"); |
| 241 | SDL_Quit(); |
| 242 | exit(EXIT_FAILURE); |
| 243 | } |
| 244 | |
| 245 | void JE_playSampleNum(JE_byte samplenum) |
| 246 | { |
| 247 | multiSamplePlay(soundSamples[samplenum-1], soundSampleCount[samplenum-1], 0, fxPlayVol); |
| 248 | } |
| 249 | |
| 250 | void setDelaySpeed(Uint16 speed) // FKA NortSong.speed and NortSong.setTimerInt |
| 251 | { |
| 252 | delaySpeed = speed; |
| 253 | delayPeriod = speed * pitPeriod; |
| 254 | } |
| 255 | |
| 256 | void JE_changeVolume(JE_word *music, int music_delta, JE_word *sample, int sample_delta) |
| 257 | { |
| 258 | int music_temp = *music + music_delta, |
| 259 | sample_temp = *sample + sample_delta; |
| 260 | |
| 261 | if (music_delta) |
| 262 | { |
| 263 | if (music_temp > 255) |
| 264 | { |
| 265 | music_temp = 255; |
| 266 | JE_playSampleNum(S_CLINK); |
| 267 | } |
| 268 | else if (music_temp < 0) |
| 269 | { |
| 270 | music_temp = 0; |
| 271 | JE_playSampleNum(S_CLINK); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (sample_delta) |
| 276 | { |
| 277 | if (sample_temp > 255) |
| 278 | { |
| 279 | sample_temp = 255; |
| 280 | JE_playSampleNum(S_CLINK); |
| 281 | } |
| 282 | else if (sample_temp < 0) |
| 283 | { |
| 284 | sample_temp = 0; |
| 285 | JE_playSampleNum(S_CLINK); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | *music = music_temp; |
| 290 | *sample = sample_temp; |
| 291 | |
| 292 | set_volume(*music, *sample); |
| 293 | } |