Licar/audio.h

123 lines
2.3 KiB
C
Raw Normal View History

2024-12-23 22:50:07 +01:00
/**
audio: this file implements the audio system.
*/
#ifndef _LCR_AUDIO
#define _LCR_AUDIO
#define LCR_SOUND_NONE 0
#define LCR_SOUND_CLICK 1
2024-12-25 22:28:46 +01:00
#define LCR_SOUND_CRASH_SMALL 2
#define LCR_SOUND_CRASH_BIG 3
#define LCR_SOUND_ACCELERATOR 4
#define LCR_AUDIO_CRASH_LEN 2048
2024-12-23 22:50:07 +01:00
struct
{
uint32_t frame;
uint8_t soundPlayed;
uint16_t soundPlayedSample;
uint32_t noise;
int engineIntensity;
int engineOsc;
2024-12-24 01:41:45 +01:00
int engineInc;
2024-12-23 22:50:07 +01:00
} 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.noise = 0;
LCR_audio.engineOsc = 0;
2024-12-24 01:41:45 +01:00
LCR_audio.engineInc = 1;
2024-12-23 22:50:07 +01:00
LCR_audio.engineIntensity = 0;
2024-12-24 01:41:45 +01:00
2024-12-23 22:50:07 +01:00
}
void LCR_audioSetEngineIntensity(uint8_t value)
{
LCR_audio.engineIntensity = value;
}
void LCR_audioPlaySound(uint8_t sound)
{
2024-12-25 22:28:46 +01:00
LCR_LOG2("playing sound");
2024-12-23 22:50:07 +01:00
LCR_audio.soundPlayed = sound;
LCR_audio.soundPlayedSample = 0;
}
uint8_t _LCR_audioNoise(void)
{
LCR_audio.noise = LCR_audio.noise * 32310901 + 37;
return LCR_audio.noise >> 16;
}
2024-12-24 01:41:45 +01:00
2024-12-23 22:50:07 +01:00
uint8_t LCR_audioGetNextSample(void)
{
unsigned char result = 128;
2024-12-25 22:28:46 +01:00
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);
if (LCR_audio.soundPlayedSample >= 2 * LCR_AUDIO_CRASH_LEN)
LCR_audio.soundPlayed = LCR_SOUND_NONE;
break;
default:
break;
}
if (LCR_audio.soundPlayed != LCR_SOUND_NONE)
LCR_audio.soundPlayedSample++;
else
{
LCR_audio.engineOsc +=
LCR_audio.engineInc ?
(((_LCR_audioNoise() % 256) <
(10 + LCR_audio.engineIntensity))) : -31;
if (LCR_audio.engineInc && LCR_audio.engineOsc >
(90 + (LCR_audio.engineIntensity / 8)))
LCR_audio.engineInc = 0;
else if ((!LCR_audio.engineInc) && LCR_audio.engineOsc < 10)
LCR_audio.engineInc = 1;
result +=
LCR_audio.engineIntensity < 20 ?
LCR_audio.engineOsc / 2 : LCR_audio.engineOsc;
}
2024-12-23 22:50:07 +01:00
LCR_audio.frame++;
return result;
}
#endif // guard