Files
Dejvino 2ac8706606 Rename Lotus3 to MiataDrive; original-graphics title screen
- examples/Lotus3 -> examples/MiataDrive, sketch MiataDrive.ino,
  emulator target APP=MiataDrive, platformio src_dir updated
- drop the borrowed photo title background (title_bg.h): the title
  screen now renders the game's own start line under the arch with the
  selected car parked on the road; color picker labels sit over the
  horizon, showcase car doubles as the live preview
- add dedicated README (controls, features, sprite pipeline, gallery)
- commit in-progress work the game now depends on: sound engine events
  (engine hum, crash noise) plus the 4-line SDL audio shim in the
  emulator host
2026-11-12 22:29:37 +01:00

58 lines
1.7 KiB
C++

/**
* @file sound_engine.h
* @brief Tiny non-blocking I2S sound engine for the T-Deck speaker.
*
* A FreeRTOS task owns the I2S peripheral (speaker, I2S_NUM_0) and renders
* queued sound events one after another. The game only ever pushes events,
* so it never blocks. Event types:
*
* SND_TONE fixed square-wave beep
* SND_SWEEP square wave gliding from freq down/up to freq2 (sirens, booms)
* SND_NOISE white-noise burst with linear decay (explosions)
*/
#pragma once
#include <Arduino.h>
enum SndType : uint8_t {
SND_TONE = 0, // fixed square wave
SND_SWEEP, // square wave gliding freq -> freq2
SND_NOISE, // white noise burst
};
struct SndEvent {
SndType type;
uint16_t freq; // Hz (tone, or sweep start)
uint16_t freq2; // Hz (sweep end; ignored for tone/noise)
uint16_t dur; // duration in ms
uint16_t amp; // peak amplitude 0..32767
};
class SoundEngine {
public:
static const uint8_t VOL_MAX = 8;
bool begin();
void play(const SndEvent &e);
uint8_t pending() const;
void clear();
// Master volume, applied to every rendered sound
void volumeUp();
void volumeDown();
void toggleMute();
void setVolume(uint8_t v); // restore a saved level (0 = muted)
uint8_t volume() const { return vol_; }
private:
static void trampoline(void *arg);
void task();
void renderTone(uint16_t freq, uint16_t durMs, uint16_t amp);
void renderSweep(uint16_t freq0, uint16_t freq1, uint16_t durMs, uint16_t amp);
void renderNoise(uint16_t durMs, uint16_t amp);
QueueHandle_t q_ = nullptr;
uint8_t vol_ = 5; // default: moderate
uint8_t lastVol_ = 5; // remembered for mute toggle
};