| 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"cxx . . $ `<file>/src/nortsong.h`/ |
| 20 | |
| 21 | #include "file.h"cxx . . $ `<file>/src/file.h`/ |
| 22 | #include "joystick.h"cxx . . $ `<file>/src/joystick.h`/ |
| 23 | #include "keyboard.h"cxx . . $ `<file>/src/keyboard.h`/ |
| 24 | #include "loudness.h"cxx . . $ `<file>/src/loudness.h`/ |
| 25 | #include "musmast.h"cxx . . $ `<file>/src/musmast.h`/ |
| 26 | #include "opentyr.h"cxx . . $ `<file>/src/opentyr.h`/ |
| 27 | #include "params.h"cxx . . $ `<file>/src/params.h`/ |
| 28 | #include "sndmast.h"cxx . . $ `<file>/src/sndmast.h`/ |
| 29 | #include "vga256d.h"cxx . . $ `<file>/src/vga256d.h`/ |
| 30 | |
| 31 | #include "SDL.h" |
| 32 | |
| 33 | JE_wordcxx . . $ JE_word# frameCountMaxcxx . . $ frameCountMax.; |
| 34 | |
| 35 | Sint16cxx . . $ Sint16# *soundSamplescxx . . $ soundSamples.[SOUND_COUNTcxx . . $ `src/sndmast.h:26:9`!] = { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! }; /* [1..soundnum + 9] */ // FKA digiFx |
| 36 | size_t soundSampleCountcxx . . $ soundSampleCount.[SOUND_COUNTcxx . . $ `src/sndmast.h:26:9`!] = { 0 }; /* [1..soundnum + 9] */ // FKA fxSize |
| 37 | |
| 38 | JE_wordcxx . . $ JE_word# tyrMusicVolumecxx . . $ tyrMusicVolume., fxVolumecxx . . $ fxVolume.; |
| 39 | const JE_wordcxx . . $ JE_word# fxPlayVolcxx . . $ fxPlayVol. = 4; |
| 40 | JE_wordcxx . . $ JE_word# tempVolumecxx . . $ tempVolume.; |
| 41 | |
| 42 | // The period of the x86 programmable interval timer in milliseconds. |
| 43 | static const float pitPeriodcxx . . $ pitPeriod. = (12.0f / 14318180.0f) * 1000.0f; |
| 44 | |
| 45 | static Uint16cxx . . $ Uint16# delaySpeedcxx . . $ delaySpeed. = 0x4300; |
| 46 | static float delayPeriodcxx . . $ delayPeriod. = 0x4300 * ((12.0f / 14318180.0f) * 1000.0f); |
| 47 | |
| 48 | static Uint32cxx . . $ Uint32# targetcxx . . $ target. = 0; |
| 49 | static Uint32cxx . . $ Uint32# target2cxx . . $ target2. = 0; |
| 50 | |
| 51 | void setDelaycxx . . $ setDelay(d4f767463ce0a6b3).(int delay) // FKA NortSong.frameCount |
| 52 | { |
| 53 | targetcxx . . $ target. = SDL_GetTickscxx . . $ SDL_GetTicks(db52760e79bf9054).() + delay * delayPeriodcxx . . $ delayPeriod.; |
| 54 | } |
| 55 | |
| 56 | void setDelay2cxx . . $ setDelay2(d4f767463ce0a6b3).(int delay) // FKA NortSong.frameCount2 |
| 57 | { |
| 58 | target2cxx . . $ target2. = SDL_GetTickscxx . . $ SDL_GetTicks(db52760e79bf9054).() + delay * delayPeriodcxx . . $ delayPeriod.; |
| 59 | } |
| 60 | |
| 61 | Uint32cxx . . $ Uint32# getDelayTickscxx . . $ getDelayTicks(db52760e79bf9054).(void) // FKA NortSong.frameCount |
| 62 | { |
| 63 | Sint32cxx . . $ Sint32# delay = targetcxx . . $ target. - SDL_GetTickscxx . . $ SDL_GetTicks(db52760e79bf9054).(); |
| 64 | return MAXcxx . . $ `src/opentyr.h:29:9`!(0, delay); |
| 65 | } |
| 66 | |
| 67 | Uint32cxx . . $ Uint32# getDelayTicks2cxx . . $ getDelayTicks2(db52760e79bf9054).(void) // FKA NortSong.frameCount2 |
| 68 | { |
| 69 | Sint32cxx . . $ Sint32# delay = target2cxx . . $ target2. - SDL_GetTickscxx . . $ SDL_GetTicks(db52760e79bf9054).(); |
| 70 | return MAXcxx . . $ `src/opentyr.h:29:9`!(0, delay); |
| 71 | } |
| 72 | |
| 73 | void wait_delaycxx . . $ wait_delay(49f6e7a06ebc5aa8).(void) |
| 74 | { |
| 75 | Sint32cxx . . $ Sint32# delay = targetcxx . . $ target. - SDL_GetTickscxx . . $ SDL_GetTicks(db52760e79bf9054).(); |
| 76 | if (delay > 0) |
| 77 | SDL_Delaycxx . . $ SDL_Delay(cb2f890c2fdad230).(delay); |
| 78 | } |
| 79 | |
| 80 | void service_wait_delaycxx . . $ service_wait_delay(49f6e7a06ebc5aa8).(void) |
| 81 | { |
| 82 | for (; ; ) |
| 83 | { |
| 84 | service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); |
| 85 | |
| 86 | Sint32cxx . . $ Sint32# delay = targetcxx . . $ target. - SDL_GetTickscxx . . $ SDL_GetTicks(db52760e79bf9054).(); |
| 87 | if (delay <= 0) |
| 88 | return; |
| 89 | |
| 90 | SDL_Delaycxx . . $ SDL_Delay(cb2f890c2fdad230).(MINcxx . . $ `src/opentyr.h:28:9`!(delay, SDL_POLL_INTERVALcxx . . $ `src/keyboard.h:28:9`!)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void wait_delayorinputcxx . . $ wait_delayorinput(49f6e7a06ebc5aa8).(void) |
| 95 | { |
| 96 | for (; ; ) |
| 97 | { |
| 98 | service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); |
| 99 | poll_joystickscxx . . $ poll_joysticks(49f6e7a06ebc5aa8).(); |
| 100 | |
| 101 | if (newkeycxx . . $ newkey. || mousedowncxx . . $ mousedown. || joydowncxx . . $ joydown.) |
| 102 | { |
| 103 | newkeycxx . . $ newkey. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | Sint32cxx . . $ Sint32# delay = targetcxx . . $ target. - SDL_GetTickscxx . . $ SDL_GetTicks(db52760e79bf9054).(); |
| 108 | if (delay <= 0) |
| 109 | return; |
| 110 | |
| 111 | SDL_Delaycxx . . $ SDL_Delay(cb2f890c2fdad230).(MINcxx . . $ `src/opentyr.h:28:9`!(delay, SDL_POLL_INTERVALcxx . . $ `src/keyboard.h:28:9`!)); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void loadSndFilecxx . . $ loadSndFile(b45ddcbc8423892c).(boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! xmas) |
| 116 | { |
| 117 | FILEcxx . . $ FILE# *f; |
| 118 | |
| 119 | f = dir_fopen_diecxx . . $ dir_fopen_die(2493aa730c89ee72).(data_dircxx . . $ data_dir(33a1f659e94feb42).(), "tyrian.snd", "rb"); |
| 120 | |
| 121 | Uint16cxx . . $ Uint16# sfxCount; |
| 122 | Uint32cxx . . $ Uint32# sfxPositions[SFX_COUNTcxx . . $ `src/sndmast.h:24:9`! + 1]; |
| 123 | |
| 124 | // Read number of sounds. |
| 125 | fread_u16_diecxx . . $ fread_u16_die(c6228d4965a46ad0).(&sfxCount, 1, f); |
| 126 | if (sfxCount != SFX_COUNTcxx . . $ `src/sndmast.h:24:9`!) |
| 127 | goto die; |
| 128 | |
| 129 | // Read positions of sounds. |
| 130 | fread_u32_diecxx . . $ fread_u32_die(503b8e976a020473).(sfxPositions, sfxCount, f); |
| 131 | |
| 132 | // Determine end of last sound. |
| 133 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!); |
| 134 | sfxPositions[sfxCount] = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 135 | |
| 136 | // Read samples. |
| 137 | for (size_t i = 0; i < sfxCount; ++i) |
| 138 | { |
| 139 | soundSampleCountcxx . . $ soundSampleCount.[i] = sfxPositions[i + 1] - sfxPositions[i]; |
| 140 | |
| 141 | // Sound size cannot exceed 64 KiB. |
| 142 | if (soundSampleCountcxx . . $ soundSampleCount.[i] > UINT16_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:117:10`!) |
| 143 | goto die; |
| 144 | |
| 145 | freecxx . . $ free(5d22bdacc48458e8).(soundSamplescxx . . $ soundSamples.[i]); |
| 146 | soundSamplescxx . . $ soundSamples.[i] = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(soundSampleCountcxx . . $ soundSampleCount.[i]); |
| 147 | |
| 148 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, sfxPositions[i], SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 149 | fread_u8_diecxx . . $ fread_u8_die(77ebc59a7bf9f340).((Uint8cxx . . $ Uint8# *)soundSamplescxx . . $ soundSamples.[i], soundSampleCountcxx . . $ soundSampleCount.[i], f); |
| 150 | } |
| 151 | |
| 152 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 153 | |
| 154 | f = dir_fopen_diecxx . . $ dir_fopen_die(2493aa730c89ee72).(data_dircxx . . $ data_dir(33a1f659e94feb42).(), xmas ? "voicesc.snd" : "voices.snd", "rb"); |
| 155 | |
| 156 | Uint16cxx . . $ Uint16# voiceCount; |
| 157 | Uint32cxx . . $ Uint32# voicePositions[VOICE_COUNTcxx . . $ `src/sndmast.h:25:9`! + 1]; |
| 158 | |
| 159 | // Read number of sounds. |
| 160 | fread_u16_diecxx . . $ fread_u16_die(c6228d4965a46ad0).(&voiceCount, 1, f); |
| 161 | if (voiceCount != VOICE_COUNTcxx . . $ `src/sndmast.h:25:9`!) |
| 162 | goto die; |
| 163 | |
| 164 | // Read positions of sounds. |
| 165 | fread_u32_diecxx . . $ fread_u32_die(503b8e976a020473).(voicePositions, voiceCount, f); |
| 166 | |
| 167 | // Determine end of last sound. |
| 168 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!); |
| 169 | voicePositions[voiceCount] = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 170 | |
| 171 | for (size_t vi = 0; vi < voiceCount; ++vi) |
| 172 | { |
| 173 | size_t i = SFX_COUNTcxx . . $ `src/sndmast.h:24:9`! + vi; |
| 174 | |
| 175 | soundSampleCountcxx . . $ soundSampleCount.[i] = voicePositions[vi + 1] - voicePositions[vi]; |
| 176 | |
| 177 | // Voice sounds have some bad data at the end. |
| 178 | soundSampleCountcxx . . $ soundSampleCount.[i] = soundSampleCountcxx . . $ soundSampleCount.[i] >= 100 |
| 179 | ? soundSampleCountcxx . . $ soundSampleCount.[i] - 100 |
| 180 | : 0; |
| 181 | |
| 182 | // Sound size cannot exceed 64 KiB. |
| 183 | if (soundSampleCountcxx . . $ soundSampleCount.[i] > UINT16_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:117:10`!) |
| 184 | goto die; |
| 185 | |
| 186 | freecxx . . $ free(5d22bdacc48458e8).(soundSamplescxx . . $ soundSamples.[i]); |
| 187 | soundSamplescxx . . $ soundSamples.[i] = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(soundSampleCountcxx . . $ soundSampleCount.[i]); |
| 188 | |
| 189 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, voicePositions[vi], SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 190 | fread_u8_diecxx . . $ fread_u8_die(77ebc59a7bf9f340).((Uint8cxx . . $ Uint8# *)soundSamplescxx . . $ soundSamples.[i], soundSampleCountcxx . . $ soundSampleCount.[i], f); |
| 191 | } |
| 192 | |
| 193 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 194 | |
| 195 | // Convert samples to output sample format and rate. |
| 196 | |
| 197 | SDL_AudioCVTcxx . . $ SDL_AudioCVT# cvt; |
| 198 | if (SDL_BuildAudioCVTcxx . . $ SDL_BuildAudioCVT(a0b837eb321a7e30).(&cvt, AUDIO_S8cxx . . $ `<external>/178947386371380387/SDL_audio.h:92:9`!, 1, 11025, AUDIO_S16SYScxx . . $ `<external>/178947386371380387/SDL_audio.h:125:9`!, 1, audioSampleRatecxx . . $ audioSampleRate.) < 0) |
| 199 | { |
| 200 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: Failed to build audio converter: %s\n", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).()); |
| 201 | |
| 202 | for (int i = 0; i < SOUND_COUNTcxx . . $ `src/sndmast.h:26:9`!; ++i) |
| 203 | soundSampleCountcxx . . $ soundSampleCount.[i] = 0; |
| 204 | |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | size_t maxSampleSize = 0; |
| 209 | for (size_t i = 0; i < SOUND_COUNTcxx . . $ `src/sndmast.h:26:9`!; ++i) |
| 210 | maxSampleSize = MAXcxx . . $ `src/opentyr.h:29:9`!(maxSampleSize, soundSampleCount[i]); |
| 211 | |
| 212 | cvt.bufcxx . . $ SDL_AudioCVT#buf. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(maxSampleSize * cvt.len_multcxx . . $ SDL_AudioCVT#len_mult.); |
| 213 | |
| 214 | for (size_t i = 0; i < SOUND_COUNTcxx . . $ `src/sndmast.h:26:9`!; ++i) |
| 215 | { |
| 216 | cvt.lencxx . . $ SDL_AudioCVT#len. = soundSampleCountcxx . . $ soundSampleCount.[i]; |
| 217 | memcpycxx . . $ memcpy(cc817cb175f282d).(cvt.bufcxx . . $ SDL_AudioCVT#buf., soundSamplescxx . . $ soundSamples.[i], cvt.lencxx . . $ SDL_AudioCVT#len.); |
| 218 | |
| 219 | if (SDL_ConvertAudiocxx . . $ SDL_ConvertAudio(4dc3c6f0b33ca5fe).(&cvt)) |
| 220 | { |
| 221 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: Failed to convert audio: %s\n", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).()); |
| 222 | |
| 223 | soundSampleCountcxx . . $ soundSampleCount.[i] = 0; |
| 224 | |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | freecxx . . $ free(5d22bdacc48458e8).(soundSamplescxx . . $ soundSamples.[i]); |
| 229 | soundSamplescxx . . $ soundSamples.[i] = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(cvt.len_cvtcxx . . $ SDL_AudioCVT#len_cvt.); |
| 230 | |
| 231 | memcpycxx . . $ memcpy(cc817cb175f282d).(soundSamplescxx . . $ soundSamples.[i], cvt.bufcxx . . $ SDL_AudioCVT#buf., cvt.len_cvtcxx . . $ SDL_AudioCVT#len_cvt.); |
| 232 | soundSampleCountcxx . . $ soundSampleCount.[i] = cvt.len_cvtcxx . . $ SDL_AudioCVT#len_cvt. / sizeof (Sint16cxx . . $ Sint16#); |
| 233 | } |
| 234 | |
| 235 | freecxx . . $ free(5d22bdacc48458e8).(cvt.bufcxx . . $ SDL_AudioCVT#buf.); |
| 236 | |
| 237 | return; |
| 238 | |
| 239 | die: |
| 240 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: Unexpected data was read from a file.\n"); |
| 241 | SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).(); |
| 242 | exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!); |
| 243 | } |
| 244 | |
| 245 | void JE_playSampleNumcxx . . $ JE_playSampleNum(ac50b47256ae7414).(JE_bytecxx . . $ JE_byte# samplenum) |
| 246 | { |
| 247 | multiSamplePlaycxx . . $ multiSamplePlay(c50bd6ef3d54aac8).(soundSamplescxx . . $ soundSamples.[samplenum-1], soundSampleCountcxx . . $ soundSampleCount.[samplenum-1], 0, fxPlayVolcxx . . $ fxPlayVol.); |
| 248 | } |
| 249 | |
| 250 | void setDelaySpeedcxx . . $ setDelaySpeed(ed53e3bbd3fa2f69).(Uint16cxx . . $ Uint16# speed) // FKA NortSong.speed and NortSong.setTimerInt |
| 251 | { |
| 252 | delaySpeedcxx . . $ delaySpeed. = speed; |
| 253 | delayPeriodcxx . . $ delayPeriod. = speed * pitPeriodcxx . . $ pitPeriod.; |
| 254 | } |
| 255 | |
| 256 | void JE_changeVolumecxx . . $ JE_changeVolume(b690d98fa3ee2e89).(JE_wordcxx . . $ JE_word# *music, int music_delta, JE_wordcxx . . $ 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_playSampleNumcxx . . $ JE_playSampleNum(ac50b47256ae7414).(S_CLINKcxx . . $ S_CLINK.); |
| 267 | } |
| 268 | else if (music_temp < 0) |
| 269 | { |
| 270 | music_temp = 0; |
| 271 | JE_playSampleNumcxx . . $ JE_playSampleNum(ac50b47256ae7414).(S_CLINKcxx . . $ S_CLINK.); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (sample_delta) |
| 276 | { |
| 277 | if (sample_temp > 255) |
| 278 | { |
| 279 | sample_temp = 255; |
| 280 | JE_playSampleNumcxx . . $ JE_playSampleNum(ac50b47256ae7414).(S_CLINKcxx . . $ S_CLINK.); |
| 281 | } |
| 282 | else if (sample_temp < 0) |
| 283 | { |
| 284 | sample_temp = 0; |
| 285 | JE_playSampleNumcxx . . $ JE_playSampleNum(ac50b47256ae7414).(S_CLINKcxx . . $ S_CLINK.); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | *music = music_temp; |
| 290 | *sample = sample_temp; |
| 291 | |
| 292 | set_volumecxx . . $ set_volume(8fbe6856f5410d23).(*music, *sample); |
| 293 | } |