From 443adbe950dd0b4829affa5b012b3ff9315a99c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Thu, 16 Apr 2020 11:33:17 +0200 Subject: [PATCH] Start music --- assets/sfx_noise.raw | 1 + platform_sdl.h | 21 +++++++----- sounds.h | 78 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 assets/sfx_noise.raw diff --git a/assets/sfx_noise.raw b/assets/sfx_noise.raw new file mode 100644 index 0000000..d49be9e --- /dev/null +++ b/assets/sfx_noise.raw @@ -0,0 +1 @@ +yBkpfwgdkqnpsqzf|m^{A{qdr}kidncsRhD@p^kuqgye_7vwlpnkljVvYqyo?XUssbP{oaGi`}NuzhOf;p{yxb6tzxsziqzgyX=knyZmus+jvcSheysD}|sjTnQSiixɘ]bUuttqk`sByxf|Ttmzuaklg}}n_&LƧrtNraRryj2oyQZsgpx[VE\TBddj_}oogTg@}YNqwofTYmm|uY}nQL~qtkawojk_jxoax}zƌbm`{pshpkX~TKkc`YRdN}|l?W|fWnjRWw|Zxhe~ylljqmalVyg^~^`gEffclLSywqn}j}Yyhxn^Z|^b_VZnqwa^ʧVRfCr~hcrArzlqg_pcjm6VW[Yq|ww}]e 0) ? ((mixed < 255) ? mixed : 255) : 0; + return mixed; +} + void audioFillCallback(void *userdata, uint8_t *s, int l) { for (int i = 0; i < l; ++i) { - s[i] = audioBuff[audioPos]; - + s[i] = addSamples(audioBuff[audioPos],SFG_getNextMusicSample()); audioBuff[audioPos] = 127; - audioPos = (audioPos < SFG_SFX_SAMPLE_COUNT - 1) ? (audioPos + 1) : 0; } } @@ -199,12 +206,8 @@ void SFG_playSound(uint8_t soundIndex, uint8_t volume) for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) { - int16_t mixedValue = - audioBuff[pos] - 127 + SFG_GET_SFX_SAMPLE(soundIndex,i) * volumeStep; - - mixedValue = (mixedValue > 0) ? ((mixedValue < 255) ? mixedValue : 255) : 0; - - audioBuff[pos] = mixedValue; + audioBuff[pos] = + addSamples(audioBuff[pos],SFG_GET_SFX_SAMPLE(soundIndex,i) * volumeStep); pos = (pos < SFG_SFX_SAMPLE_COUNT - 1) ? (pos + 1) : 0; } diff --git a/sounds.h b/sounds.h index 46cbc0e..de14750 100644 --- a/sounds.h +++ b/sounds.h @@ -30,6 +30,84 @@ (SFG_sounds[soundIndex][sampleIndex / 2] >> 4) : \ (SFG_sounds[soundIndex][sampleIndex / 2] & 0x0f)) +#define SFG_TRACK_SAMPLES (512 * 1024) + +struct +{ // all should be initialized to 0 + uint8_t track; + uint32_t t; + uint32_t t2; + uint32_t n3t; + uint32_t n5t; + uint32_t n7t; +} SFG_MusicState; + +uint8_t SFG_getNextMusicSample() +{ + if (SFG_MusicState.t >= SFG_TRACK_SAMPLES) + { + SFG_MusicState.track++; + + if (SFG_MusicState.track >= 3) + SFG_MusicState.track = 0; + + SFG_MusicState.t = 0; + SFG_MusicState.t2 = 0; + SFG_MusicState.n3t = 0; + SFG_MusicState.n5t = 0; + SFG_MusicState.n7t = 0; + } + + uint8_t result; + + #define t SFG_MusicState.t + #define t2 SFG_MusicState.t2 + #define n3t SFG_MusicState.n3t + + switch (SFG_MusicState.track) + { + case 0: + { + uint32_t a = ((t >> 7) | (t >> 9) | (~t << 1) | t); + result = ((t) & 65536 ? (a & (((t * t) >> 16) & 0x09)) : ~a); + + break; + } + + case 1: + { + result = (t & (3 << (((t >> 10) ^ ((t >> 10) << (t >> 6)))))); + + break; + } + + case 2: + { + result = + ((((t >> (t >> 2)) + (t >> (t >> 7)))) & 0x3f | (t >> 5) | (t >> 11)) + & (t & (32768 | 8192) ? 0xf0 : 0x30); + + break; + } + + default: + result = 127; + break; + } + + #undef t + #undef t2 + #undef n3t + + SFG_MusicState.t += 1; + SFG_MusicState.t2 += SFG_MusicState.t; + SFG_MusicState.n3t += 3; + SFG_MusicState.n5t += 5; + SFG_MusicState.n7t += 7; + + return result; +} + SFG_PROGRAM_MEMORY uint8_t SFG_sounds[][SFG_SFX_SIZE] = { { // 0, bullet shot