Continue crash sound

This commit is contained in:
Miloslav Ciz 2024-12-27 07:45:27 +01:00
parent b90f5bbfc8
commit 85b8b1352d
3 changed files with 65 additions and 64 deletions

58
audio.h
View file

@ -18,24 +18,32 @@ struct
{
uint32_t frame;
uint8_t soundPlayed;
uint16_t soundPlayedSample;
uint16_t soundPlayedFrame;
uint32_t noise;
uint8_t crashSample;
int engineIntensity;
int engineOsc;
int engineInc;
} LCR_audio;
void LCR_audioInit(void)
{
LCR_LOG0("initializing audio");
LCR_audio.frame = 0;
LCR_audio.soundPlayed = LCR_SOUND_NONE;
LCR_audio.soundPlayedSample = 0;
LCR_audio.soundPlayedFrame = 0;
LCR_audio.noise = 0;
LCR_audio.crashSample = 0;
LCR_audio.engineOsc = 0;
LCR_audio.engineInc = 1;
@ -54,7 +62,7 @@ void LCR_audioPlaySound(uint8_t sound)
{
LCR_LOG2("playing sound");
LCR_audio.soundPlayed = sound;
LCR_audio.soundPlayedSample = 0;
LCR_audio.soundPlayedFrame = 0;
}
uint8_t _LCR_audioNoise(void)
@ -63,39 +71,41 @@ uint8_t _LCR_audioNoise(void)
return LCR_audio.noise >> 16;
}
uint8_t LCR_audioGetNextSample(void)
{
unsigned char result = 128;
switch (LCR_audio.soundPlayed)
{
switch (LCR_audio.soundPlayed)
{
case LCR_SOUND_CRASH_SMALL:
result += (LCR_AUDIO_CRASH_LEN - LCR_audio.soundPlayedSample) *
(_LCR_audioNoise() / 16) / LCR_AUDIO_CRASH_LEN;
if (LCR_audio.soundPlayedSample >= LCR_AUDIO_CRASH_LEN)
LCR_audio.soundPlayed = LCR_SOUND_NONE;
break;
case LCR_SOUND_CRASH_BIG:
result += ((LCR_AUDIO_CRASH_LEN * 2) - LCR_audio.soundPlayedSample) *
(_LCR_audioNoise() / 8) / (2 * LCR_AUDIO_CRASH_LEN);
{
int limit = (LCR_audio.soundPlayed == LCR_SOUND_CRASH_BIG ? 256 : 128) -
(LCR_audio.soundPlayedFrame * 256) / LCR_AUDIO_CRASH_LEN;
if (LCR_audio.soundPlayedSample >= 2 * LCR_AUDIO_CRASH_LEN)
if (LCR_audio.frame % 2 || // lower frequency
LCR_audio.soundPlayedFrame < LCR_AUDIO_CRASH_LEN / 4)
LCR_audio.crashSample = _LCR_audioNoise();
if (LCR_audio.crashSample > limit)
LCR_audio.crashSample = (_LCR_audioNoise() % 2) ? 0 : limit;
result += LCR_audio.crashSample - limit / 2;
if (limit == 0)
LCR_audio.soundPlayed = LCR_SOUND_NONE;
break;
}
default:
break;
}
}
if (LCR_audio.soundPlayed != LCR_SOUND_NONE)
LCR_audio.soundPlayedSample++;
else
{
if (LCR_audio.soundPlayed != LCR_SOUND_NONE)
LCR_audio.soundPlayedFrame++;
else
{
LCR_audio.engineOsc +=
LCR_audio.engineInc ?
(((_LCR_audioNoise() % 256) <
@ -110,13 +120,11 @@ else
result +=
LCR_audio.engineIntensity < 20 ?
LCR_audio.engineOsc / 2 : LCR_audio.engineOsc;
}
}
LCR_audio.frame++;
return result;
}
#endif // guard

View file

@ -16,23 +16,14 @@ FILE *musicFile = 0;
void audioFillCallback(void *userdata, uint8_t *s, int l)
{
for (int i = 0; i < l; ++i)
{
unsigned char byte = 128;
if (musicFile)
{
if (!fread(&byte,1,1,musicFile))
if (!fread(s,1,l,musicFile))
rewind(musicFile);
}
// s[i] = byte;
s[i] = byte / 2 + LCR_gameGetNextAudioSample() / 2;
//s[i] = LCR_gameGetNextAudioSample();
}
for (int i = 0; i < l; ++i)
s[i] = s[i] / 2 + LCR_gameGetNextAudioSample() / 2;
}
const uint8_t *keyboardState;
@ -97,7 +88,7 @@ int main(int argc, char *argv[])
audioSpec.freq = 8000;
audioSpec.format = AUDIO_U8;
audioSpec.channels = 1;
audioSpec.samples = 256;
audioSpec.samples = 64;
if (SDL_OpenAudio(&audioSpec,NULL) < 0)
fputs("could not initialize audio",stderr);

View file

@ -45,7 +45,10 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_DRIFT_THRESHOLD_1 (LCR_GAME_UNIT / 4)
#define LCR_CAR_DRIFT_THRESHOLD_0 (LCR_GAME_UNIT / 200)
#define LCR_CAR_CRASH_SPEED_THRESHOLD 25
#define LCR_CAR_CRASH_SPEED_DIFF 25
#define LCR_CAR_CRASH_SPEED_THRESHOLD 70
// multipliers (in 8ths) of friction and acceleration on concrete:
#define LCR_CAR_GRASS_FACTOR 5
@ -1077,7 +1080,9 @@ uint32_t LCR_racingStep(unsigned int input)
TPE_worldStep(&(LCR_racing.physicsWorld));
LCR_LOG2("stepping physics engine done");
int speedDiff = LCR_racing.carSpeeds[0] - LCR_racing.carSpeeds[1];
int speedDiff =
TPE_abs(LCR_racing.carSpeeds[0]) -
TPE_abs(LCR_racing.carSpeeds[1]);
LCR_racing.carSpeeds[1] = LCR_racing.carSpeeds[0];
@ -1087,14 +1092,11 @@ uint32_t LCR_racingStep(unsigned int input)
if (TPE_vec3Dot(carVel,carForw) < 0)
LCR_racing.carSpeeds[0] *= -1;
else if (speedDiff < -1 * LCR_CAR_CRASH_SPEED_THRESHOLD)
{
result |= (speedDiff < -2 * LCR_CAR_CRASH_SPEED_THRESHOLD) ?
if (speedDiff < -1 * LCR_CAR_CRASH_SPEED_DIFF &&
TPE_abs(LCR_racing.carSpeeds[0]) <= LCR_CAR_CRASH_SPEED_THRESHOLD)
result |= (speedDiff < -2 * LCR_CAR_CRASH_SPEED_DIFF) ?
LCR_RACING_EVENT_CRASH_BIG : LCR_RACING_EVENT_CRASH_SMALL;
LCR_racing.carSpeeds[1] = 0; // prevent several crash events in a row
}
_LCR_racingUpdateCarPosRot();
TPE_Unit angle = TPE_vec3Dot(carUp,TPE_vec3Normalized(TPE_vec3Minus(