tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2163: #include "functions.h" tron@679: #include "map.h" tron@1496: #include "mixer.h" truelight@0: #include "sound.h" truelight@0: #include "vehicle.h" truelight@0: #include "window.h" truelight@0: #include "viewport.h" truelight@0: #include "fileio.h" truelight@0: tron@337: typedef struct FileEntry { truelight@0: uint32 file_offset; truelight@0: uint32 file_size; truelight@0: uint16 rate; truelight@0: uint8 bits_per_sample; truelight@0: uint8 channels; tron@337: } FileEntry; truelight@0: tron@1496: static uint _file_count; tron@1496: static FileEntry* _files; truelight@0: truelight@0: #define SOUND_SLOT 31 peter1138@2594: // Number of levels of panning per side peter1138@2594: #define PANNING_LEVELS 16 truelight@0: truelight@0: tron@1496: static void OpenBankFile(const char *filename) truelight@0: { tron@2371: FileEntry* fe; tron@2371: uint count; tron@2371: uint i; truelight@0: truelight@0: FioOpenFile(SOUND_SLOT, filename); tron@2371: count = FioReadDword() / 8; tron@2371: fe = calloc(sizeof(*fe), count); tron@2371: tron@2371: if (fe == NULL) { tron@2371: _file_count = 0; tron@2371: _files = NULL; tron@2372: return; tron@2371: } tron@2371: tron@2371: _file_count = count; tron@2371: _files = fe; truelight@0: truelight@0: FioSeekTo(0, SEEK_SET); truelight@0: tron@1496: for (i = 0; i != count; i++) { tron@1496: fe[i].file_offset = FioReadDword(); tron@1496: fe[i].file_size = FioReadDword(); truelight@0: } truelight@193: tron@337: for (i = 0; i != count; i++, fe++) { tron@337: char name[255]; truelight@0: tron@337: FioSeekTo(fe->file_offset, SEEK_SET); tron@337: tron@337: // Check for special case, see else case tron@337: FioReadBlock(name, FioReadByte()); // Read the name of the sound tron@337: if (strcmp(name, "Corrupt sound") != 0) { tron@337: FioSeekTo(12, SEEK_CUR); // Skip past RIFF header tron@337: tron@337: // Read riff tags tron@337: for (;;) { tron@2371: uint32 tag = FioReadDword(); tron@2371: uint32 size = FioReadDword(); tron@337: tron@337: if (tag == ' tmf') { tron@337: FioReadWord(); // wFormatTag tron@337: fe->channels = FioReadWord(); // wChannels tron@337: FioReadDword(); // samples per second tron@337: fe->rate = 11025; // seems like all samples should be played at this rate. tron@337: FioReadDword(); // avg bytes per second tron@337: FioReadWord(); // alignment tron@337: fe->bits_per_sample = FioReadByte(); // bits per sample tron@337: FioSeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR); tron@337: } else if (tag == 'atad') { tron@337: fe->file_size = size; tron@337: fe->file_offset = FioGetPos() | (SOUND_SLOT << 24); tron@337: break; tron@337: } else { tron@337: fe->file_size = 0; tron@337: break; tron@337: } truelight@0: } tron@337: } else { tron@337: /* tron@337: * Special case for the jackhammer sound tron@337: * (name in sample.cat is "Corrupt sound") tron@337: * It's no RIFF file, but raw PCM data tron@337: */ tron@337: fe->channels = 1; tron@337: fe->rate = 11025; tron@337: fe->bits_per_sample = 8; tron@337: fe->file_offset = FioGetPos() | (SOUND_SLOT << 24); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1496: static bool SetBankSource(MixerChannel *mc, uint bank) truelight@0: { tron@2371: FileEntry* fe; tron@2371: int8* mem; truelight@0: uint i; truelight@0: tron@2371: if (bank >= _file_count) return false; tron@2371: fe = &_files[bank]; truelight@193: tron@2371: if (fe->file_size == 0) return false; tron@2371: tron@2371: mem = malloc(fe->file_size); tron@2371: if (mem == NULL) return false; tron@2371: truelight@0: FioSeekToFile(fe->file_offset); truelight@0: FioReadBlock(mem, fe->file_size); truelight@0: tron@337: for (i = 0; i != fe->file_size; i++) darkvater@1136: mem[i] += -128; // Convert unsigned sound data to signed truelight@193: truelight@0: assert(fe->bits_per_sample == 8 && fe->channels == 1 && fe->file_size != 0 && fe->rate != 0); truelight@0: tron@1496: MxSetChannelRawSrc(mc, mem, fe->file_size, fe->rate, MX_AUTOFREE); truelight@0: truelight@0: return true; truelight@0: } truelight@0: tron@1496: bool SoundInitialize(const char *filename) truelight@0: { tron@1496: OpenBankFile(filename); truelight@0: return true; truelight@0: } truelight@0: truelight@0: // Low level sound player peter1138@2594: static void StartSound(uint sound, int panning, uint volume) truelight@0: { tron@2371: MixerChannel* mc; peter1138@2594: uint left_vol, right_vol; tron@2371: tron@2371: if (volume == 0) return; tron@2371: mc = MxAllocateChannel(_mixer); tron@2371: if (mc == NULL) return; tron@2371: if (!SetBankSource(mc, sound)) return; peter1138@2594: peter1138@2594: panning = clamp(panning, -PANNING_LEVELS, PANNING_LEVELS); peter1138@2594: left_vol = (volume * PANNING_LEVELS) - (volume * panning); peter1138@2594: right_vol = (volume * PANNING_LEVELS) + (volume * panning); peter1138@2594: MxSetChannelVolume(mc, left_vol * 128 / PANNING_LEVELS, right_vol * 128 / PANNING_LEVELS); tron@2371: MxActivateChannel(mc); truelight@0: } truelight@0: truelight@0: truelight@0: static const byte _vol_factor_by_zoom[] = {255, 190, 134}; truelight@0: truelight@0: static const byte _sound_base_vol[] = { truelight@0: 128, 90, 128, 128, 128, 128, 128, 128, truelight@0: 128, 90, 90, 128, 128, 128, 128, 128, truelight@0: 128, 128, 128, 80, 128, 128, 128, 128, truelight@0: 128, 128, 128, 128, 128, 128, 128, 128, truelight@0: 128, 128, 90, 90, 90, 128, 90, 128, truelight@0: 128, 90, 128, 128, 128, 90, 128, 128, truelight@0: 128, 128, 128, 128, 90, 128, 128, 128, truelight@0: 128, 90, 128, 128, 128, 128, 128, 128, truelight@0: 128, 128, 90, 90, 90, 128, 128, 128, truelight@0: 90, truelight@0: }; truelight@0: truelight@0: static const byte _sound_idx[] = { tron@2371: 2, 3, 4, 5, 6, 7, 8, 9, truelight@0: 10, 11, 12, 13, 14, 15, 16, 17, truelight@0: 18, 19, 20, 21, 22, 23, 24, 25, truelight@0: 26, 27, 28, 29, 30, 31, 32, 33, tron@2371: 34, 35, 36, 37, 38, 39, 40, 0, tron@2371: 1, 41, 42, 43, 44, 45, 46, 47, truelight@0: 48, 49, 50, 51, 52, 53, 54, 55, truelight@0: 56, 57, 58, 59, 60, 61, 62, 63, truelight@0: 64, 65, 66, 67, 68, 69, 70, 71, truelight@0: 72, truelight@0: }; truelight@0: tron@337: static void SndPlayScreenCoordFx(SoundFx sound, int x, int y) truelight@0: { tron@2371: const Window* w; truelight@0: tron@2371: if (msf.effect_vol == 0) return; truelight@0: tron@337: for (w = _windows; w != _last_window; w++) { tron@2371: const ViewPort* vp = w->viewport; tron@2371: tron@2371: if (vp != NULL && truelight@0: IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) && truelight@0: IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) { peter1138@2594: int left = (x - vp->virtual_left); truelight@193: truelight@0: StartSound( truelight@193: _sound_idx[sound], peter1138@2594: left / (vp->virtual_width / ((PANNING_LEVELS << 1) + 1)) - PANNING_LEVELS, truelight@0: (_sound_base_vol[sound] * msf.effect_vol * _vol_factor_by_zoom[vp->zoom]) >> 15 truelight@0: ); truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: } truelight@0: tron@337: void SndPlayTileFx(SoundFx sound, TileIndex tile) truelight@0: { tron@337: /* emits sound from center (+ 8) of the tile */ tron@926: int x = TileX(tile) * 16 + 8; tron@926: int y = TileY(tile) * 16 + 8; tron@337: Point pt = RemapCoords(x, y, GetSlopeZ(x, y)); truelight@0: SndPlayScreenCoordFx(sound, pt.x, pt.y); truelight@0: } truelight@0: tron@337: void SndPlayVehicleFx(SoundFx sound, const Vehicle *v) truelight@0: { truelight@0: SndPlayScreenCoordFx(sound, tron@337: (v->left_coord + v->right_coord) / 2, tron@337: (v->top_coord + v->bottom_coord) / 2 truelight@0: ); truelight@0: } truelight@0: tron@337: void SndPlayFx(SoundFx sound) truelight@0: { truelight@0: StartSound( truelight@193: _sound_idx[sound], peter1138@2594: 0, truelight@0: (_sound_base_vol[sound] * msf.effect_vol) >> 7 truelight@0: ); truelight@0: }