mirror of https://github.com/OpenTTD/OpenTTD
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
parent
70be35f319
commit
f04780e66d
1
Makefile
1
Makefile
|
@ -600,6 +600,7 @@ C_SOURCES += minilzo.c
|
||||||
C_SOURCES += misc.c
|
C_SOURCES += misc.c
|
||||||
C_SOURCES += misc_cmd.c
|
C_SOURCES += misc_cmd.c
|
||||||
C_SOURCES += misc_gui.c
|
C_SOURCES += misc_gui.c
|
||||||
|
C_SOURCES += mixer.c
|
||||||
C_SOURCES += music_gui.c
|
C_SOURCES += music_gui.c
|
||||||
C_SOURCES += namegen.c
|
C_SOURCES += namegen.c
|
||||||
C_SOURCES += network.c
|
C_SOURCES += network.c
|
||||||
|
|
|
@ -0,0 +1,154 @@
|
||||||
|
#include "stdafx.h"
|
||||||
|
#include "ttd.h"
|
||||||
|
#include "mixer.h"
|
||||||
|
|
||||||
|
struct MixerChannel {
|
||||||
|
// Mixer
|
||||||
|
Mixer *mx;
|
||||||
|
bool active;
|
||||||
|
|
||||||
|
// pointer to allocated buffer memory
|
||||||
|
int8 *memory;
|
||||||
|
|
||||||
|
// current position in memory
|
||||||
|
uint32 pos;
|
||||||
|
uint32 frac_pos;
|
||||||
|
uint32 frac_speed;
|
||||||
|
uint32 samples_left;
|
||||||
|
|
||||||
|
// Mixing volume
|
||||||
|
uint volume_left;
|
||||||
|
uint volume_right;
|
||||||
|
|
||||||
|
uint flags;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Mixer {
|
||||||
|
uint32 play_rate;
|
||||||
|
MixerChannel channels[8];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples)
|
||||||
|
{
|
||||||
|
int8 *b;
|
||||||
|
uint32 frac_pos;
|
||||||
|
uint32 frac_speed;
|
||||||
|
uint volume_left;
|
||||||
|
uint volume_right;
|
||||||
|
|
||||||
|
if (samples > sc->samples_left) samples = sc->samples_left;
|
||||||
|
sc->samples_left -= samples;
|
||||||
|
assert(samples > 0);
|
||||||
|
|
||||||
|
b = sc->memory + sc->pos;
|
||||||
|
frac_pos = sc->frac_pos;
|
||||||
|
frac_speed = sc->frac_speed;
|
||||||
|
volume_left = sc->volume_left;
|
||||||
|
volume_right = sc->volume_right;
|
||||||
|
|
||||||
|
if (frac_speed == 0x10000) {
|
||||||
|
// Special case when frac_speed is 0x10000
|
||||||
|
do {
|
||||||
|
buffer[0] += *b * volume_left >> 8;
|
||||||
|
buffer[1] += *b * volume_right >> 8;
|
||||||
|
b++;
|
||||||
|
buffer += 2;
|
||||||
|
} while (--samples > 0);
|
||||||
|
} else {
|
||||||
|
do {
|
||||||
|
buffer[0] += *b * volume_left >> 8;
|
||||||
|
buffer[1] += *b * volume_right >> 8;
|
||||||
|
buffer += 2;
|
||||||
|
frac_pos += frac_speed;
|
||||||
|
b += frac_pos >> 16;
|
||||||
|
frac_pos &= 0xffff;
|
||||||
|
} while (--samples > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
sc->frac_pos = frac_pos;
|
||||||
|
sc->pos = b - sc->memory;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void MxCloseChannel(MixerChannel *mc)
|
||||||
|
{
|
||||||
|
if (mc->flags & MX_AUTOFREE) free(mc->memory);
|
||||||
|
mc->memory = NULL;
|
||||||
|
mc->active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MxMixSamples(Mixer *mx, void *buffer, uint samples)
|
||||||
|
{
|
||||||
|
MixerChannel *mc;
|
||||||
|
|
||||||
|
// Clear the buffer
|
||||||
|
memset(buffer, 0, sizeof(int16) * 2 * samples);
|
||||||
|
|
||||||
|
// Mix each channel
|
||||||
|
for (mc = mx->channels; mc != endof(mx->channels); mc++) {
|
||||||
|
if (mc->active) {
|
||||||
|
mix_int8_to_int16(mc, buffer, samples);
|
||||||
|
if (mc->samples_left == 0) MxCloseChannel(mc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
{
|
||||||
|
static FILE *out = NULL;
|
||||||
|
if (out == NULL)
|
||||||
|
out = fopen("d:\\dump.raw", "wb");
|
||||||
|
fwrite(buffer, samples * 4, 1, out);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
MixerChannel *MxAllocateChannel(Mixer *mx)
|
||||||
|
{
|
||||||
|
MixerChannel *mc;
|
||||||
|
for (mc = mx->channels; mc != endof(mx->channels); mc++)
|
||||||
|
if (mc->memory == NULL) {
|
||||||
|
mc->active = false;
|
||||||
|
mc->mx = mx;
|
||||||
|
return mc;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint flags)
|
||||||
|
{
|
||||||
|
mc->memory = mem;
|
||||||
|
mc->flags = flags;
|
||||||
|
mc->frac_pos = 0;
|
||||||
|
mc->pos = 0;
|
||||||
|
|
||||||
|
mc->frac_speed = (rate << 16) / mc->mx->play_rate;
|
||||||
|
|
||||||
|
// adjust the magnitude to prevent overflow
|
||||||
|
while (size & 0xFFFF0000) {
|
||||||
|
size >>= 1;
|
||||||
|
rate = (rate >> 1) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
mc->samples_left = size * mc->mx->play_rate / rate;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MxSetChannelVolume(MixerChannel *mc, uint left, uint right)
|
||||||
|
{
|
||||||
|
mc->volume_left = left;
|
||||||
|
mc->volume_right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MxActivate(MixerChannel* mc)
|
||||||
|
{
|
||||||
|
mc->active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool MxInitialize(uint rate)
|
||||||
|
{
|
||||||
|
static Mixer mx;
|
||||||
|
_mixer = &mx;
|
||||||
|
mx.play_rate = rate;
|
||||||
|
return true;
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef MIXER_H
|
||||||
|
#define MIXER_H
|
||||||
|
|
||||||
|
typedef struct Mixer Mixer;
|
||||||
|
typedef struct MixerChannel MixerChannel;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
MX_AUTOFREE = 1,
|
||||||
|
// MX_8BIT = 2,
|
||||||
|
// MX_STEREO = 4,
|
||||||
|
// MX_UNSIGNED = 8,
|
||||||
|
};
|
||||||
|
|
||||||
|
VARDEF Mixer *_mixer;
|
||||||
|
|
||||||
|
bool MxInitialize(uint rate);
|
||||||
|
void MxMixSamples(Mixer *mx, void *buffer, uint samples);
|
||||||
|
|
||||||
|
MixerChannel *MxAllocateChannel(Mixer *mx);
|
||||||
|
void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint flags);
|
||||||
|
void MxSetChannelVolume(MixerChannel *mc, uint left, uint right);
|
||||||
|
void MxActivate(MixerChannel*);
|
||||||
|
|
||||||
|
#endif
|
2
sdl.c
2
sdl.c
|
@ -4,7 +4,7 @@
|
||||||
#include "ttd.h"
|
#include "ttd.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "gfx.h"
|
#include "gfx.h"
|
||||||
#include "sound.h"
|
#include "mixer.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
176
sound.c
176
sound.c
|
@ -1,33 +1,13 @@
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "ttd.h"
|
#include "ttd.h"
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
|
#include "mixer.h"
|
||||||
#include "sound.h"
|
#include "sound.h"
|
||||||
#include "vehicle.h"
|
#include "vehicle.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include "viewport.h"
|
#include "viewport.h"
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
|
|
||||||
typedef struct MixerChannel {
|
|
||||||
// Mixer
|
|
||||||
Mixer *mx;
|
|
||||||
bool active;
|
|
||||||
|
|
||||||
// pointer to allocated buffer memory
|
|
||||||
int8 *memory;
|
|
||||||
|
|
||||||
// current position in memory
|
|
||||||
uint32 pos;
|
|
||||||
uint32 frac_pos;
|
|
||||||
uint32 frac_speed;
|
|
||||||
uint32 samples_left;
|
|
||||||
|
|
||||||
// Mixing volume
|
|
||||||
uint volume_left;
|
|
||||||
uint volume_right;
|
|
||||||
|
|
||||||
uint flags;
|
|
||||||
} MixerChannel;
|
|
||||||
|
|
||||||
typedef struct FileEntry {
|
typedef struct FileEntry {
|
||||||
uint32 file_offset;
|
uint32 file_offset;
|
||||||
uint32 file_size;
|
uint32 file_size;
|
||||||
|
@ -36,150 +16,29 @@ typedef struct FileEntry {
|
||||||
uint8 channels;
|
uint8 channels;
|
||||||
} FileEntry;
|
} FileEntry;
|
||||||
|
|
||||||
struct Mixer {
|
static uint _file_count;
|
||||||
uint32 play_rate;
|
static FileEntry* _files;
|
||||||
FileEntry *files;
|
|
||||||
uint32 file_count;
|
|
||||||
MixerChannel channels[8];
|
|
||||||
};
|
|
||||||
|
|
||||||
enum {
|
|
||||||
MX_AUTOFREE = 1,
|
|
||||||
// MX_8BIT = 2,
|
|
||||||
// MX_STEREO = 4,
|
|
||||||
MX_UNSIGNED = 8,
|
|
||||||
};
|
|
||||||
|
|
||||||
#define SOUND_SLOT 31
|
#define SOUND_SLOT 31
|
||||||
|
|
||||||
|
|
||||||
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples)
|
static void OpenBankFile(const char *filename)
|
||||||
{
|
|
||||||
int8 *b;
|
|
||||||
uint32 frac_pos;
|
|
||||||
uint32 frac_speed;
|
|
||||||
uint volume_left;
|
|
||||||
uint volume_right;
|
|
||||||
|
|
||||||
if (samples > sc->samples_left) samples = sc->samples_left;
|
|
||||||
sc->samples_left -= samples;
|
|
||||||
assert(samples > 0);
|
|
||||||
|
|
||||||
b = sc->memory + sc->pos;
|
|
||||||
frac_pos = sc->frac_pos;
|
|
||||||
frac_speed = sc->frac_speed;
|
|
||||||
volume_left = sc->volume_left;
|
|
||||||
volume_right = sc->volume_right;
|
|
||||||
|
|
||||||
if (frac_speed == 0x10000) {
|
|
||||||
// Special case when frac_speed is 0x10000
|
|
||||||
do {
|
|
||||||
buffer[0] += *b * volume_left >> 8;
|
|
||||||
buffer[1] += *b * volume_right >> 8;
|
|
||||||
b++;
|
|
||||||
buffer += 2;
|
|
||||||
} while (--samples > 0);
|
|
||||||
} else {
|
|
||||||
do {
|
|
||||||
buffer[0] += *b * volume_left >> 8;
|
|
||||||
buffer[1] += *b * volume_right >> 8;
|
|
||||||
buffer += 2;
|
|
||||||
frac_pos += frac_speed;
|
|
||||||
b += frac_pos >> 16;
|
|
||||||
frac_pos &= 0xffff;
|
|
||||||
} while (--samples > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
sc->frac_pos = frac_pos;
|
|
||||||
sc->pos = b - sc->memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MxCloseChannel(MixerChannel *mc)
|
|
||||||
{
|
|
||||||
if (mc->flags & MX_AUTOFREE) free(mc->memory);
|
|
||||||
mc->memory = NULL;
|
|
||||||
mc->active = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MxMixSamples(Mixer *mx, void *buffer, uint samples)
|
|
||||||
{
|
|
||||||
MixerChannel *mc;
|
|
||||||
|
|
||||||
// Clear the buffer
|
|
||||||
memset(buffer, 0, sizeof(int16) * 2 * samples);
|
|
||||||
|
|
||||||
// Mix each channel
|
|
||||||
for (mc = mx->channels; mc != endof(mx->channels); mc++) {
|
|
||||||
if (mc->active) {
|
|
||||||
mix_int8_to_int16(mc, buffer, samples);
|
|
||||||
if (mc->samples_left == 0) MxCloseChannel(mc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
{
|
|
||||||
static FILE *out = NULL;
|
|
||||||
if (out == NULL)
|
|
||||||
out = fopen("d:\\dump.raw", "wb");
|
|
||||||
fwrite(buffer, samples * 4, 1, out);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static MixerChannel *MxAllocateChannel(Mixer *mx)
|
|
||||||
{
|
|
||||||
MixerChannel *mc;
|
|
||||||
for (mc = mx->channels; mc != endof(mx->channels); mc++)
|
|
||||||
if (mc->memory == NULL) {
|
|
||||||
mc->active = false;
|
|
||||||
mc->mx = mx;
|
|
||||||
return mc;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint flags)
|
|
||||||
{
|
|
||||||
mc->memory = mem;
|
|
||||||
mc->flags = flags;
|
|
||||||
mc->frac_pos = 0;
|
|
||||||
mc->pos = 0;
|
|
||||||
|
|
||||||
mc->frac_speed = (rate << 16) / mc->mx->play_rate;
|
|
||||||
|
|
||||||
// adjust the magnitude to prevent overflow
|
|
||||||
while (size & 0xFFFF0000) {
|
|
||||||
size >>= 1;
|
|
||||||
rate = (rate >> 1) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
mc->samples_left = size * mc->mx->play_rate / rate;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MxSetChannelVolume(MixerChannel *mc, uint left, uint right)
|
|
||||||
{
|
|
||||||
mc->volume_left = left;
|
|
||||||
mc->volume_right = right;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MxOpenBankFile(Mixer *mx, const char *filename)
|
|
||||||
{
|
{
|
||||||
uint count, i;
|
uint count, i;
|
||||||
uint32 size, tag;
|
uint32 size, tag;
|
||||||
FileEntry *fe;
|
FileEntry *fe;
|
||||||
|
|
||||||
FioOpenFile(SOUND_SLOT, filename);
|
FioOpenFile(SOUND_SLOT, filename);
|
||||||
mx->file_count = count = FioReadDword() >> 3;
|
_file_count = count = FioReadDword() >> 3;
|
||||||
fe = mx->files = calloc(sizeof(FileEntry), count);
|
fe = _files = calloc(sizeof(FileEntry), count);
|
||||||
|
|
||||||
FioSeekTo(0, SEEK_SET);
|
FioSeekTo(0, SEEK_SET);
|
||||||
|
|
||||||
for (i = 0; i != count; i++, fe++) {
|
for (i = 0; i != count; i++) {
|
||||||
fe->file_offset = FioReadDword();
|
fe[i].file_offset = FioReadDword();
|
||||||
fe->file_size = FioReadDword();
|
fe[i].file_size = FioReadDword();
|
||||||
}
|
}
|
||||||
|
|
||||||
fe = mx->files;
|
|
||||||
for (i = 0; i != count; i++, fe++) {
|
for (i = 0; i != count; i++, fe++) {
|
||||||
char name[255];
|
char name[255];
|
||||||
|
|
||||||
|
@ -227,9 +86,9 @@ static void MxOpenBankFile(Mixer *mx, const char *filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool MxSetBankSource(MixerChannel *mc, uint bank)
|
static bool SetBankSource(MixerChannel *mc, uint bank)
|
||||||
{
|
{
|
||||||
FileEntry *fe = &mc->mx->files[bank];
|
FileEntry *fe = &_files[bank];
|
||||||
int8 *mem;
|
int8 *mem;
|
||||||
uint i;
|
uint i;
|
||||||
|
|
||||||
|
@ -245,17 +104,14 @@ static bool MxSetBankSource(MixerChannel *mc, uint bank)
|
||||||
|
|
||||||
assert(fe->bits_per_sample == 8 && fe->channels == 1 && fe->file_size != 0 && fe->rate != 0);
|
assert(fe->bits_per_sample == 8 && fe->channels == 1 && fe->file_size != 0 && fe->rate != 0);
|
||||||
|
|
||||||
MxSetChannelRawSrc(mc, mem, fe->file_size, fe->rate, MX_AUTOFREE | MX_UNSIGNED);
|
MxSetChannelRawSrc(mc, mem, fe->file_size, fe->rate, MX_AUTOFREE);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MxInitialize(uint rate, const char *filename)
|
bool SoundInitialize(const char *filename)
|
||||||
{
|
{
|
||||||
static Mixer mx;
|
OpenBankFile(filename);
|
||||||
_mixer = &mx;
|
|
||||||
mx.play_rate = rate;
|
|
||||||
MxOpenBankFile(&mx, filename);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,9 +122,9 @@ static void StartSound(uint sound, uint panning, uint volume)
|
||||||
MixerChannel *mc = MxAllocateChannel(_mixer);
|
MixerChannel *mc = MxAllocateChannel(_mixer);
|
||||||
if (mc == NULL)
|
if (mc == NULL)
|
||||||
return;
|
return;
|
||||||
if (MxSetBankSource(mc, sound)) {
|
if (SetBankSource(mc, sound)) {
|
||||||
MxSetChannelVolume(mc, volume << 8, volume << 8);
|
MxSetChannelVolume(mc, volume << 8, volume << 8);
|
||||||
mc->active = true;
|
MxActivate(mc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
sound.h
6
sound.h
|
@ -1,8 +1,6 @@
|
||||||
#ifndef SOUND_H
|
#ifndef SOUND_H
|
||||||
#define SOUND_H
|
#define SOUND_H
|
||||||
|
|
||||||
typedef struct Mixer Mixer;
|
|
||||||
|
|
||||||
typedef struct MusicFileSettings {
|
typedef struct MusicFileSettings {
|
||||||
byte playlist;
|
byte playlist;
|
||||||
byte music_vol;
|
byte music_vol;
|
||||||
|
@ -17,10 +15,8 @@ VARDEF byte _music_wnd_cursong;
|
||||||
VARDEF bool _song_is_active;
|
VARDEF bool _song_is_active;
|
||||||
VARDEF byte _cur_playlist[33];
|
VARDEF byte _cur_playlist[33];
|
||||||
VARDEF MusicFileSettings msf;
|
VARDEF MusicFileSettings msf;
|
||||||
VARDEF Mixer *_mixer;
|
|
||||||
|
|
||||||
bool MxInitialize(uint rate, const char *filename);
|
bool SoundInitialize(const char *filename);
|
||||||
void MxMixSamples(Mixer *mx, void *buffer, uint samples);
|
|
||||||
|
|
||||||
typedef enum SoundFx {
|
typedef enum SoundFx {
|
||||||
SND_02_SPLAT, // 0 == 0x00 !
|
SND_02_SPLAT, // 0 == 0x00 !
|
||||||
|
|
4
ttd.c
4
ttd.c
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#define VARDEF
|
#define VARDEF
|
||||||
#include "ttd.h"
|
#include "ttd.h"
|
||||||
|
#include "mixer.h"
|
||||||
#include "spritecache.h"
|
#include "spritecache.h"
|
||||||
#include "gfx.h"
|
#include "gfx.h"
|
||||||
#include "gui.h"
|
#include "gui.h"
|
||||||
|
@ -626,7 +627,8 @@ int ttd_main(int argc, char* argv[])
|
||||||
// Sample catalogue
|
// Sample catalogue
|
||||||
DEBUG(misc, 1) ("Loading sound effects...");
|
DEBUG(misc, 1) ("Loading sound effects...");
|
||||||
_os_version = GetOSVersion();
|
_os_version = GetOSVersion();
|
||||||
MxInitialize(11025, "sample.cat");
|
MxInitialize(11025);
|
||||||
|
SoundInitialize("sample.cat");
|
||||||
|
|
||||||
// This must be done early, since functions use the InvalidateWindow* calls
|
// This must be done early, since functions use the InvalidateWindow* calls
|
||||||
InitWindowSystem();
|
InitWindowSystem();
|
||||||
|
|
2
win32.c
2
win32.c
|
@ -4,7 +4,7 @@
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
#include "gfx.h"
|
#include "gfx.h"
|
||||||
#include "sound.h"
|
#include "mixer.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include "gui.h"
|
#include "gui.h"
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
Loading…
Reference in New Issue