aboutsummaryrefslogtreecommitdiff
path: root/engines/cge/sound.cpp
blob: 75adf3868a275e67e59a80e76a0c2c22c905adbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* 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
 */

#include "cge/general.h"
#include "cge/startup.h"
#include "cge/sound.h"
#include "cge/text.h"
#include "cge/cfile.h"
#include "cge/vol.h"


namespace CGE {

bool  Music       = true;
FX    Fx          = 16;   // must precede SOUND!!
SOUND Sound;


SOUND::SOUND(void) {
	if (STARTUP::SoundOk)
		Open();
}


SOUND::~SOUND(void) {
	Close();
}


void SOUND::Close(void) {
	KillMIDI();
	SNDDone();
}


void SOUND::Open(void) {
	SNDInit();
	Play(Fx[30000], 8);
}


void SOUND::Play(DATACK *wav, int pan, int cnt) {
	if (wav) {
		Stop();
		smpinf.saddr = (uint8 *) &*(wav->EAddr());
		smpinf.slen = (uint16)wav->Size();
		smpinf.span = pan;
		smpinf.sflag = cnt;
		SNDDigiStart(&smpinf);
	}
}


void SOUND::Stop(void) {
	SNDDigiStop(&smpinf);
}


FX::FX(int size) : Emm(0L), Current(NULL) {
	Cache = new HAN[size];
	for (Size = 0; Size < size; Size++) {
		Cache[Size].Ref = 0;
		Cache[Size].Wav = NULL;
	}
}


FX::~FX(void) {
	Clear();
	delete[] Cache;
}


void FX::Clear(void) {
	HAN *p, * q;
	for (p = Cache, q = p + Size; p < q; p++) {
		if (p->Ref) {
			p->Ref = 0;
			delete p->Wav;
			p->Wav = NULL;
		}
	}
	Emm.release();
	Current = NULL;
}


int FX::Find(int ref) {
	HAN *p, * q;
	int i = 0;
	for (p = Cache, q = p + Size; p < q; p++) {
		if (p->Ref == ref)
			break;
		else
			++i;
	}
	return i;
}


void FX::Preload(int ref0) {
	HAN *CacheLim = Cache + Size;
	int ref;

	for (ref = ref0; ref < ref0 + 10; ref++) {
		static char fname[] = "FX00000.WAV";
		wtom(ref, fname + 2, 10, 5);
		INI_FILE file = INI_FILE(fname);
		DATACK *wav = LoadWave(&file, &Emm);
		if (wav) {
			HAN *p = &Cache[Find(0)];
			if (p >= CacheLim)
				break;
			p->Wav = wav;
			p->Ref = ref;
		}
	}
}


DATACK *FX::Load(int idx, int ref) {
	static char fname[] = "FX00000.WAV";
	wtom(ref, fname + 2, 10, 5);

	INI_FILE file = INI_FILE(fname);
	DATACK *wav = LoadWave(&file, &Emm);
	if (wav) {
		HAN *p = &Cache[idx];
		p->Wav = wav;
		p->Ref = ref;
	}
	return wav;
}


DATACK *FX::operator [](int ref) {
	int i;
	if ((i = Find(ref)) < Size)
		Current = Cache[i].Wav;
	else {
		if ((i = Find(0)) >= Size) {
			Clear();
			i = 0;
		}
		Current = Load(i, ref);
	}
	return Current;
}


static  uint8  *midi    = NULL;


void KillMIDI(void) {
	SNDMIDIStop();
	if (midi) {
		delete[] midi;
		midi = NULL;
	}
}


void LoadMIDI(int ref) {
	static char fn[] = "00.MID";
	wtom(ref, fn, 10, 2);
	if (INI_FILE::exist(fn)) {
		KillMIDI();
		INI_FILE mid = fn;
		if (mid._error == 0) {
			uint16 siz = (uint16) mid.size();
			midi = new uint8[siz];
			if (midi) {
				mid.read(midi, siz);
				if (mid._error)
					KillMIDI();
				else
					SNDMIDIStart(midi);
			}
		}
	}
}


void *Patch(int pat) {
	void *p = NULL;
	static char fn[] = "PATCH000.SND";

	wtom(pat, fn + 5, 10, 3);
	INI_FILE snd = fn;
	if (!snd._error) {
		uint16 siz = (uint16) snd.size();
		p = (uint8 *) malloc(siz);
		if (p) {
			snd.read(p, siz);
			if (snd._error) {
				free(p);
				p = NULL;
			}
		}
	}
	return p;
}

} // End of namespace CGE