aboutsummaryrefslogtreecommitdiff
path: root/sword1/music.cpp
blob: 4f76dc21dbdafb217f55de3143449f0198748bd3 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

// todo: add fadeout, crossfading.
// this code always loops. make it depend on _loopFlag

#include "stdafx.h"
#include "music.h"
#include "sound/mixer.h"
#include "common/util.h"
#include "common/file.h"

SwordMusic::SwordMusic(OSystem *system, SoundMixer *pMixer) {
	_system = system;
	_mixer = pMixer;
	_mixer->setupPremix(passMixerFunc, this);
	_fading = false;
	_playing = false;
	_loop = false;
	_mutex = _system->create_mutex();
	_fadeSmpInBuf = _fadeBufPos = _waveSize = _wavePos = _bufPos = _smpInBuf = 0;
	assert(_mixer->getOutputRate() == 22050);
	_fadeBuf = NULL;
	_musicBuf = NULL;
}

SwordMusic::~SwordMusic() {
	_mixer->setupPremix(0, 0);
}

void SwordMusic::passMixerFunc(void *param, int16 *buf, uint len) {
	((SwordMusic*)param)->mixer(buf, len);
}

void SwordMusic::mixTo(int16 *src, int16 *dst, uint32 len) {
	if (!_playing)
		memset(dst, 0, len * 8);
	if (!_fading) { // no fading, simply copy it over
		for (uint32 cnt = 0; cnt < len; cnt++)
			dst[(cnt << 2) | 0] = dst[(cnt << 2) | 1] =
			dst[(cnt << 2) | 2] = dst[(cnt << 2) | 3] = (int16)READ_LE_UINT16(src + cnt);
	} else {
		if (_fadeBuf) { // do a cross fade
			for (uint32 cnt = 0; cnt < len; cnt++) {
				int16 resVal = ((int16)READ_LE_UINT16(_fadeBuf + _fadeBufPos) * _fadeVal) >> 15;
				resVal += ((int16)READ_LE_UINT16(src + cnt) * (32768 - _fadeVal)) >> 15;
				dst[(cnt << 2) | 0] = dst[(cnt << 2) | 1] =
				dst[(cnt << 2) | 2] = dst[(cnt << 2) | 3] = resVal;
				_fadeVal--;
				_fadeBufPos++;
				_fadeSmpInBuf--;
			}
			if ((!_fadeVal) || (!_fadeSmpInBuf)) {
				free(_fadeBuf);
				_fadeBuf = NULL;
				_fading = false;
			}
			if (_fadeBufPos == BUFSIZE)
				_fadeBufPos = 0;
		} else { // simple fadeout
			for (uint32 cnt = 0; cnt < len; cnt++) {
				dst[(cnt << 2) | 0] = dst[(cnt << 2) | 1] =
				dst[(cnt << 2) | 2] = dst[(cnt << 2) | 3] = 
				((int16)READ_LE_UINT16(src + cnt) * _fadeVal) >> 15;
				_fadeVal--;
			}
			if ((!_fadeVal) || (!_smpInBuf)) {
				_fading = _playing = false;
				free(_musicBuf);
				_musicBuf = NULL;
			}
		}
	}
}

void SwordMusic::mixer(int16 *buf, uint32 len) {
	if (!_playing) {
		memset(buf, 0, 2 * len * sizeof(int16));
		return;
	}
	uint32 remain = 0;	
	if (_smpInBuf < (len >> 1)) {
		if (_loop)
			return;
		remain = (len >> 1) - _smpInBuf;
		len = _smpInBuf << 1;
	}
	_system->lock_mutex(_mutex);
	len >>= 1;
	while (len) {
		uint32 length = len;
		length = MIN(length, BUFSIZE - _bufPos);
		if (_fading && _fadeBuf) {
			length = MIN(length, (uint32)_fadeVal);
			length = MIN(length, _fadeSmpInBuf);
			length = MIN(length, BUFSIZE - _fadeBufPos);
		}
		mixTo(_musicBuf + _bufPos, buf, length);
		len -= length;
		buf += 4 * length;
		_bufPos += length;
		_smpInBuf -= length;
		if (_bufPos == BUFSIZE)
			_bufPos = 0;
	}
	if (remain) {
		memset(buf, 0, remain * 8);
		_playing = false;
	}
	_system->unlock_mutex(_mutex);
}

void SwordMusic::stream(void) {
	// make sure we've got enough samples in buffer.
	if ((_smpInBuf < 4 * SAMPLERATE) && _playing && _musicFile.isOpen()) {
		_system->lock_mutex(_mutex);
		uint32 loadTotal = BUFSIZE - _smpInBuf;
		while (uint32 doLoad = loadTotal) {
			if (BUFSIZE - ((_bufPos + _smpInBuf) % BUFSIZE) < loadTotal)
				doLoad = BUFSIZE - (_bufPos + _smpInBuf) % BUFSIZE;
			doLoad = MIN(doLoad, _waveSize - _wavePos);
			
			int16 *dest = _musicBuf + ((_bufPos + _smpInBuf) % BUFSIZE);
			_musicFile.read(dest, doLoad * 2);
			_wavePos += doLoad;
			if (_wavePos == _waveSize) {
				if (_loop) {
					_wavePos = 0;
					_musicFile.seek(WAVEHEADERSIZE);
				} else
					loadTotal = doLoad;
			}
			loadTotal -= doLoad;
			_smpInBuf += doLoad;
		}
		_system->unlock_mutex(_mutex);
	}
}

void SwordMusic::startMusic(int32 tuneId, int32 loopFlag) {
	_system->lock_mutex(_mutex);
	_loop = (loopFlag > 0);
	if (tuneId) {		
		if (_musicFile.isOpen())
			_musicFile.close();
		char fName[20];
		sprintf(fName, "music/%s.wav", _tuneList[tuneId]);
		_musicFile.open(fName);
		if (_musicFile.isOpen()) {
			if (_playing) { // do a cross fade
				_fadeBuf = _musicBuf;
				_fadeBufPos = _bufPos;
				_fadeSmpInBuf = _smpInBuf;
				_fading = true;
				_fadeVal = 32768;
			} else
				_fading = false;
			_musicBuf = (int16*)malloc(BUFSIZE * 2);

			_musicFile.seek(0x28);
			_waveSize = _musicFile.readUint32LE() / 2;
			_wavePos = 0;
			_smpInBuf = 0;
			_bufPos = 0;
			_playing = true;
		} else {
			_fading = true;
			_fadeVal = 32768;
			if (_fadeBuf) {
				free(_fadeBuf);
				_fadeBuf = NULL;
			}
		}
	} else {
		if (_playing)
			fadeDown();
		if (_musicFile.isOpen())
			_musicFile.close();
	}
	_system->unlock_mutex(_mutex);
	stream();
}

void SwordMusic::fadeDown(void) {
	_fadeVal = 32768;
	_fading = true;
	if (_fadeBuf) {
		free(_fadeBuf);
		_fadeBuf = NULL;
	}
}