Files
DoubleTroubleRecomp/runtime/audio.c
T
linkatplugandClaude Opus 5.5 90c398d531 Son de l'intro (voix 16 bits) et menu de réglages F12
- audio.c : flux 16 bits non signés (voix de l'intro, qui saturaient),
  pause du mixeur pendant le menu sauf les sons en boucle.
- ail.c : retrait de la trace de débogage.
- menu.c/menu.h : menu en surimpression indépendant de SDL (volumes,
  plein écran, taille de fenêtre, lissage, format 4:3) et fichier de réglages.
- menu_font.h : police Roboto (Apache 2.0) générée par tools/gen_font.py.
- host_sdl.c : F12 ouvre le menu ; le temps du jeu est figé pendant le menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 02:55:52 +01:00

360 lines
13 KiB
C

/* Mixeur audio : remplace Miles AIL. Indépendant de l'hôte : l'hôte appelle audio_mix() depuis
* son fil audio (SDL) ou à chaque image (sans fenêtre), et fournit host_audio_lock/unlock.
*
* Voix 0..36 : module son de TUCKER.EXE (overrides.c)
* bruitages : 32 voix, « en cours » en [0x46644 + 4n] (n < 7)
* musiques : 4 voix, « en cours » en [0x4663c + 4n] (n < 2)
* voix : 1 voix, speech\samNNNN.wav lu sur le CD, « en cours » en [0x46638]
* Voix 40..63 : canaux AIL émulés (ail.c), utilisés par INTRO.EXE ; double tampon possible. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpu.h"
#include "rt.h"
typedef struct {
int16_t *pcm; /* échantillons 16 bits, entrelacés si stéréo */
uint32_t frames;
} Chunk;
typedef struct {
Chunk main; /* son entier (fichier) */
Chunk buf[2]; /* double tampon (flux) */
int streaming, play_buf, buf_full[2], next_load, stream_end;
int chans; /* 1 ou 2 */
int bits16, sign8; /* format des données brutes chargées en flux */
uint32_t rate;
uint64_t pos, step; /* position et pas en 32.32 */
int vol; /* 0..127, volume demandé par le jeu */
int loops; /* 0 = infini, sinon nombre de passages restants */
int group;
int active, paused, ended;
uint32_t flag_addr; /* indicateur du jeu remis à 0 en fin de lecture (0 = aucun) */
} Voice;
static Voice voices[AUDIO_VOICES];
int audio_volume[AUDIO_GROUPS] = {100, 100, 100}; /* réglages utilisateur, en % */
int audio_master = 100;
int audio_hold; /* menu ouvert : seules les boucles (ambiances, musique) continuent */
static int fixed_voice(int group, int ch) {
return group == AUDIO_FX ? ch : group == AUDIO_MUSIC ? AUDIO_FX_CH + ch : AUDIO_FX_CH + AUDIO_MUS_CH;
}
/* ------------------------------------------------------------------ WAV → PCM 16 bits */
/* signed = 0 : données non signées (8 ou 16 bits), comme AIL sans DIG_PCM_SIGN */
static int16_t *to_pcm16(const uint8_t *d, uint32_t len, int bits16, int is_signed, uint32_t *samples) {
uint32_t i, n = bits16 ? len / 2 : len;
uint16_t flip = is_signed ? 0 : 0x8000;
int16_t *p = (int16_t *)malloc((n ? n : 1) * sizeof(int16_t));
if (!p) return NULL;
for (i = 0; i < n; i++)
p[i] = bits16 ? (int16_t)((uint16_t)(d[2 * i] | d[2 * i + 1] << 8) ^ flip)
: (int16_t)(is_signed ? (int8_t)d[i] << 8 : (d[i] - 128) << 8);
*samples = n;
return p;
}
static int wav_decode(const uint8_t *d, uint32_t len, Chunk *c, int *chans, uint32_t *rate) {
uint32_t p = 12, fmt_ch = 0, bits = 0, data_off = 0, data_len = 0, n;
uint16_t tag = 1;
if (len < 12 || memcmp(d, "RIFF", 4) || memcmp(d + 8, "WAVE", 4)) return 0;
*rate = 0;
while (p + 8 <= len) {
uint32_t sz = d[p + 4] | d[p + 5] << 8 | d[p + 6] << 16 | (uint32_t)d[p + 7] << 24;
if (!memcmp(d + p, "fmt ", 4) && p + 24 <= len) {
tag = (uint16_t)(d[p + 8] | d[p + 9] << 8);
fmt_ch = d[p + 10] | d[p + 11] << 8;
*rate = d[p + 12] | d[p + 13] << 8 | d[p + 14] << 16 | (uint32_t)d[p + 15] << 24;
bits = d[p + 22] | d[p + 23] << 8;
} else if (!memcmp(d + p, "data", 4)) {
data_off = p + 8;
data_len = sz;
if (data_off + data_len > len) data_len = len - data_off;
break;
}
p += 8 + sz + (sz & 1);
}
if (tag != 1 || !data_off || !*rate || (bits != 8 && bits != 16) || fmt_ch < 1 || fmt_ch > 2) return 0;
c->pcm = to_pcm16(d + data_off, data_len, bits == 16, bits == 16, &n); /* WAV : 8 bits non signé, 16 bits signé */
if (!c->pcm) return 0;
*chans = (int)fmt_ch;
c->frames = n / fmt_ch;
return 1;
}
static void chunk_free(Chunk *c) { free(c->pcm); c->pcm = NULL; c->frames = 0; }
static void voice_clear(Voice *v) {
chunk_free(&v->main); chunk_free(&v->buf[0]); chunk_free(&v->buf[1]);
memset(v, 0, sizeof *v);
}
static void set_step(Voice *v) { v->step = v->rate ? ((uint64_t)v->rate << 32) / AUDIO_RATE : 0; }
/* ------------------------------------------------------------------ mixage (fil audio) */
static const Chunk *cur_chunk(Voice *v) {
return v->streaming ? &v->buf[v->play_buf] : &v->main;
}
void audio_mix(int16_t *out, int frames) {
int i, k;
static int32_t acc[2 * 4096];
while (frames > 0) {
int n = frames > 4096 ? 4096 : frames;
memset(acc, 0, (size_t)n * 2 * sizeof(int32_t));
for (k = 0; k < AUDIO_VOICES; k++) {
Voice *v = &voices[k];
int32_t gain;
if (!v->active || v->paused) continue;
if (audio_hold && (v->streaming || v->loops != 0)) continue; /* reprend exactement au même endroit */
gain = v->vol * audio_volume[v->group] * audio_master / 100; /* 0..12700 */
for (i = 0; i < n; i++) {
const Chunk *c = cur_chunk(v);
uint32_t f = (uint32_t)(v->pos >> 32), f2;
int32_t frac = (int32_t)((v->pos >> 16) & 0xffff), l, r;
if (f >= c->frames) {
if (v->streaming) { /* tampon fini : on passe à l'autre */
if (c->frames) { v->buf_full[v->play_buf] = 0; v->play_buf ^= 1; v->pos = 0; }
if (!v->buf_full[v->play_buf]) {
if (v->stream_end) { v->active = 0; v->ended = 1; }
break; /* famine : silence en attendant */
}
continue;
}
if (v->loops != 1) {
if (v->loops > 1) v->loops--;
v->pos -= (uint64_t)c->frames << 32;
if ((uint32_t)(v->pos >> 32) >= c->frames) v->pos = 0;
f = (uint32_t)(v->pos >> 32);
} else { v->active = 0; v->ended = 1; break; }
}
f2 = f + 1 < c->frames ? f + 1 : f;
if (v->chans == 1) {
int32_t a = c->pcm[f], b = c->pcm[f2];
l = r = a + ((b - a) * frac >> 16);
} else {
int32_t a = c->pcm[2 * f], b = c->pcm[2 * f2], cc = c->pcm[2 * f + 1], d = c->pcm[2 * f2 + 1];
l = a + ((b - a) * frac >> 16);
r = cc + ((d - cc) * frac >> 16);
}
acc[2 * i] += l * gain / 12700;
acc[2 * i + 1] += r * gain / 12700;
v->pos += v->step;
}
}
for (i = 0; i < 2 * n; i++) out[i] = (int16_t)(acc[i] > 32767 ? 32767 : acc[i] < -32768 ? -32768 : acc[i]);
out += 2 * n;
frames -= n;
}
}
/* fil principal : fin de lecture → remise à zéro des indicateurs du jeu */
void audio_update(void) {
int k;
host_audio_lock();
for (k = 0; k < AUDIO_VOICES; k++) {
Voice *v = &voices[k];
if (v->ended && v->flag_addr) { WR32(v->flag_addr, 0); v->flag_addr = 0; }
}
host_audio_unlock();
}
void audio_reset(void) {
int k;
host_audio_lock();
for (k = 0; k < AUDIO_VOICES; k++) voice_clear(&voices[k]);
host_audio_unlock();
}
/* ------------------------------------------------------------------ voix génériques (ail.c) */
void audio_v_init(int k, int group) {
host_audio_lock();
voice_clear(&voices[k]);
voices[k].group = group;
voices[k].vol = 127;
voices[k].loops = 1;
voices[k].chans = 1;
voices[k].rate = 11025;
set_step(&voices[k]);
host_audio_unlock();
}
int audio_v_load_wav(int k, const uint8_t *wav, uint32_t len) {
Voice *v = &voices[k];
Chunk c = {0};
int chans;
uint32_t rate;
if (!wav_decode(wav, len, &c, &chans, &rate)) return 0;
host_audio_lock();
chunk_free(&v->main); chunk_free(&v->buf[0]); chunk_free(&v->buf[1]);
v->main = c; v->chans = chans; v->rate = rate; set_step(v);
v->streaming = 0; v->active = 0; v->ended = 0; v->pos = 0;
host_audio_unlock();
return 1;
}
void audio_v_format(int k, int bits16, int stereo, int sign8) {
Voice *v = &voices[k];
host_audio_lock();
v->bits16 = bits16; v->chans = stereo ? 2 : 1; v->sign8 = sign8;
host_audio_unlock();
}
void audio_v_rate(int k, uint32_t rate) {
host_audio_lock();
voices[k].rate = rate; set_step(&voices[k]);
host_audio_unlock();
}
void audio_v_volume(int k, int vol) {
host_audio_lock();
voices[k].vol = vol < 0 ? 0 : vol > 127 ? 127 : vol;
host_audio_unlock();
}
void audio_v_loops(int k, int loops) {
host_audio_lock();
voices[k].loops = loops < 0 ? 1 : loops;
host_audio_unlock();
}
void audio_v_group(int k, int group) { voices[k].group = group; }
void audio_v_start(int k) {
Voice *v = &voices[k];
host_audio_lock();
if (!v->streaming) { v->pos = 0; v->active = v->main.frames != 0; }
else v->active = 1;
v->paused = 0; v->ended = 0;
host_audio_unlock();
}
void audio_v_pause(int k, int paused) {
host_audio_lock();
voices[k].paused = paused;
host_audio_unlock();
}
void audio_v_end(int k) {
Voice *v = &voices[k];
host_audio_lock();
v->active = 0; v->ended = 1; v->paused = 0;
host_audio_unlock();
}
void audio_v_free(int k) {
host_audio_lock();
voice_clear(&voices[k]);
host_audio_unlock();
}
/* AIL : SMP_FREE 1, SMP_DONE 2, SMP_PLAYING 4, SMP_STOPPED 8 */
int audio_v_status(int k) {
Voice *v = &voices[k];
if (v->active) return v->paused ? 8 : 4;
return 2;
}
/* double tampon : indice du tampon à remplir, ou -1 */
int audio_v_ready(int k) {
Voice *v = &voices[k];
int r;
host_audio_lock();
if (!v->streaming) { v->streaming = 1; v->play_buf = 0; v->next_load = 0; v->buf_full[0] = v->buf_full[1] = 0; v->stream_end = 0; }
r = v->buf_full[v->next_load] ? -1 : v->next_load;
host_audio_unlock();
return r;
}
void audio_v_queue(int k, int slot, const uint8_t *data, uint32_t len) {
Voice *v = &voices[k];
uint32_t n = 0;
int16_t *p = len ? to_pcm16(data, len, v->bits16, v->sign8, &n) : NULL;
host_audio_lock();
slot &= 1;
if (!v->streaming) { v->streaming = 1; v->play_buf = slot; v->buf_full[0] = v->buf_full[1] = 0; v->stream_end = 0; v->pos = 0; }
chunk_free(&v->buf[slot]);
if (p && n >= (uint32_t)v->chans) {
v->buf[slot].pcm = p; v->buf[slot].frames = n / (uint32_t)v->chans;
v->buf_full[slot] = 1;
if (!v->active) { v->active = 1; v->ended = 0; if (!v->buf_full[v->play_buf]) v->play_buf = slot; }
} else {
free(p);
v->stream_end = 1; /* tampon vide : fin du flux */
}
v->next_load = slot ^ 1;
host_audio_unlock();
}
/* ------------------------------------------------------------------ API du module son de TUCKER.EXE */
static uint32_t wav_size(uint32_t a) { /* taille d'une image RIFF en mémoire du jeu */
if (RD32(a) != 0x46464952u) return 0; /* "RIFF" */
return RD32(a + 4) + 8;
}
static int start_fixed(int group, int ch, const uint8_t *wav, uint32_t len, int vol, int loop, uint32_t flag) {
int k = fixed_voice(group, ch);
audio_v_init(k, group);
if (!audio_v_load_wav(k, wav, len)) {
if (rt_cfg.verbose) fprintf(stderr, "[son] WAV illisible (groupe %d, canal %d)\n", group, ch);
if (flag) WR32(flag, 0);
return 0;
}
if (rt_cfg.verbose)
fprintf(stderr, "[son] groupe %d canal %d : %u Hz, %d voie(s), %.2f s, volume %d%s\n", group, ch,
voices[k].rate, voices[k].chans, voices[k].main.frames / (double)voices[k].rate, vol,
loop ? ", en boucle" : "");
audio_v_volume(k, vol);
audio_v_loops(k, loop ? 0 : 1);
host_audio_lock();
voices[k].flag_addr = flag;
host_audio_unlock();
audio_v_start(k);
if (flag) WR32(flag, 1);
return 1;
}
void audio_play_mem(int group, int ch, uint32_t addr, int vol, int loop, uint32_t flag) {
uint32_t len = wav_size(addr);
if (!len || len > 64u << 20) {
if (rt_cfg.verbose) fprintf(stderr, "[son] pas de WAV en %08x\n", addr);
if (flag) WR32(flag, 0);
return;
}
start_fixed(group, ch, M + (addr & MEM_MASK), len, vol, loop, flag);
}
int audio_play_file(int group, int ch, const char *dos_name, int vol, uint32_t flag) {
char path[600];
FILE *f;
long len;
uint8_t *buf;
int ok = 0;
if (!dos_map_path(dos_name, path, sizeof path, 0) || !(f = fopen(path, "rb"))) {
if (rt_cfg.verbose) fprintf(stderr, "[son] introuvable : %s\n", dos_name);
return 0;
}
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
buf = (uint8_t *)malloc(len > 0 ? (size_t)len : 1);
if (buf && fread(buf, 1, (size_t)len, f) == (size_t)len)
ok = start_fixed(group, ch, buf, (uint32_t)len, vol, 0, flag);
free(buf);
fclose(f);
return ok;
}
void audio_stop(int group, int ch) {
int k = fixed_voice(group, ch);
host_audio_lock();
if (voices[k].flag_addr) WR32(voices[k].flag_addr, 0);
voice_clear(&voices[k]);
host_audio_unlock();
}
void audio_set_volume(int group, int ch, int vol) { audio_v_volume(fixed_voice(group, ch), vol); }
int audio_playing(int group, int ch) { return voices[fixed_voice(group, ch)].active; }