scummvm/engines/cge/sound.cpp

227 lines
3.9 KiB
C++
Raw Normal View History

/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This code is based on original Soltys source code
* Copyright (c) 1994-1995 Janus B. Wisniewski and L.K. Avalon
*/
2011-06-13 09:57:24 +00:00
#include "cge/general.h"
#include "cge/startup.h"
#include "cge/sound.h"
#include "cge/text.h"
#include "cge/cfile.h"
#include "cge/vol.h"
2011-06-10 20:57:09 +00:00
namespace CGE {
Fx _fx = 16; // must precede SOUND!!
Sound _sound;
Sound::Sound() {
2011-07-03 09:28:22 +00:00
if (Startup::_soundOk)
open();
}
Sound::~Sound() {
close();
}
void Sound::close() {
killMidi();
2011-07-03 09:28:22 +00:00
sndDone();
}
void Sound::open() {
2011-07-03 09:28:22 +00:00
sndInit();
play(_fx[30000], 8);
}
void Sound::play(DataCk *wav, int pan, int cnt) {
2011-06-13 09:57:24 +00:00
if (wav) {
stop();
2011-07-25 15:50:58 +00:00
_smpinf._saddr = (uint8 *) &*(wav->addr());
_smpinf._slen = (uint16)wav->size();
_smpinf._span = pan;
_smpinf._sflag = cnt;
sndDigiStart(&_smpinf);
2011-06-13 09:57:24 +00:00
}
}
void Sound::stop() {
sndDigiStop(&_smpinf);
}
2011-07-25 15:50:58 +00:00
Fx::Fx(int size) : _current(NULL) {
_cache = new Han[size];
for (_size = 0; _size < size; _size++) {
_cache[_size]._ref = 0;
_cache[_size]._wav = NULL;
2011-06-13 09:57:24 +00:00
}
}
Fx::~Fx() {
clear();
delete[] _cache;
}
void Fx::clear() {
Han *p, * q;
for (p = _cache, q = p + _size; p < q; p++) {
if (p->_ref) {
p->_ref = 0;
delete p->_wav;
p->_wav = NULL;
2011-06-13 09:57:24 +00:00
}
}
_current = NULL;
}
int Fx::find(int ref) {
Han *p, * q;
2011-06-13 09:57:24 +00:00
int i = 0;
for (p = _cache, q = p + _size; p < q; p++) {
if (p->_ref == ref)
2011-06-13 09:57:24 +00:00
break;
2011-06-13 11:07:45 +00:00
else
2011-06-13 09:57:24 +00:00
++i;
}
return i;
}
void Fx::preload(int ref0) {
Han *cacheLim = _cache + _size;
2011-06-13 09:57:24 +00:00
int ref;
for (ref = ref0; ref < ref0 + 10; ref++) {
2011-06-13 09:57:24 +00:00
static char fname[] = "FX00000.WAV";
wtom(ref, fname + 2, 10, 5);
INI_FILE file = INI_FILE(fname);
2011-07-25 15:50:58 +00:00
DataCk *wav = loadWave(&file);
2011-06-13 09:57:24 +00:00
if (wav) {
Han *p = &_cache[find(0)];
if (p >= cacheLim)
2011-06-13 09:57:24 +00:00
break;
p->_wav = wav;
p->_ref = ref;
2011-06-13 09:57:24 +00:00
}
}
}
DataCk *Fx::load(int idx, int ref) {
2011-06-13 09:57:24 +00:00
static char fname[] = "FX00000.WAV";
wtom(ref, fname + 2, 10, 5);
2011-06-13 09:57:24 +00:00
INI_FILE file = INI_FILE(fname);
2011-07-25 15:50:58 +00:00
DataCk *wav = loadWave(&file);
2011-06-13 09:57:24 +00:00
if (wav) {
Han *p = &_cache[idx];
p->_wav = wav;
p->_ref = ref;
2011-06-13 09:57:24 +00:00
}
return wav;
}
DataCk *Fx::operator [](int ref) {
2011-06-13 09:57:24 +00:00
int i;
if ((i = find(ref)) < _size)
_current = _cache[i]._wav;
2011-06-13 09:57:24 +00:00
else {
if ((i = find(0)) >= _size) {
clear();
2011-06-13 09:57:24 +00:00
i = 0;
}
_current = load(i, ref);
}
return _current;
}
static uint8 *midi = NULL;
void killMidi() {
2011-07-03 09:28:22 +00:00
sndMidiStop();
2011-06-13 09:57:24 +00:00
if (midi) {
delete[] midi;
midi = NULL;
}
}
void loadMidi(int ref) {
2011-06-13 09:57:24 +00:00
static char fn[] = "00.MID";
wtom(ref, fn, 10, 2);
2011-06-30 06:30:23 +00:00
if (INI_FILE::exist(fn)) {
killMidi();
2011-06-13 09:57:24 +00:00
INI_FILE mid = fn;
2011-07-01 06:37:40 +00:00
if (mid._error == 0) {
2011-06-30 06:30:23 +00:00
uint16 siz = (uint16) mid.size();
2011-06-13 09:57:24 +00:00
midi = new uint8[siz];
if (midi) {
2011-06-30 06:30:23 +00:00
mid.read(midi, siz);
2011-07-01 06:37:40 +00:00
if (mid._error)
killMidi();
2011-06-13 09:57:24 +00:00
else
2011-07-03 09:28:22 +00:00
sndMidiStart(midi);
2011-06-13 09:57:24 +00:00
}
}
}
}
2011-07-01 23:02:14 +00:00
void *Patch(int pat) {
2011-06-13 09:57:24 +00:00
void *p = NULL;
static char fn[] = "PATCH000.SND";
2011-06-13 09:57:24 +00:00
wtom(pat, fn + 5, 10, 3);
INI_FILE snd = fn;
2011-07-01 06:37:40 +00:00
if (!snd._error) {
2011-06-30 06:30:23 +00:00
uint16 siz = (uint16) snd.size();
2011-06-13 09:57:24 +00:00
p = (uint8 *) malloc(siz);
if (p) {
2011-06-30 06:30:23 +00:00
snd.read(p, siz);
2011-07-01 06:37:40 +00:00
if (snd._error) {
2011-06-13 09:57:24 +00:00
free(p);
p = NULL;
}
}
}
2011-06-13 09:57:24 +00:00
return p;
}
2011-06-10 20:57:09 +00:00
} // End of namespace CGE