aboutsummaryrefslogtreecommitdiff
path: root/sound/mods/paula.cpp
blob: 9f2eb4290aa5b5abd590bae3d87ea7ed9fe662a6 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "sound/mods/paula.h"

namespace Audio {

Paula::Paula(bool stereo, int rate, uint interruptFreq) :
		_stereo(stereo), _rate(rate), _periodScale((kPalSystemClock / 2.0) / rate), _intFreq(interruptFreq) {

	clearVoices();
	_voice[0].panning = 63;
	_voice[1].panning = 191;
	_voice[2].panning = 191;
	_voice[3].panning = 63;

	if (_intFreq == 0)
		_intFreq = _rate;

	_curInt = 0;
	_timerBase = 1;
	_playing = false;
	_end = true;
}

Paula::~Paula() {
}

void Paula::clearVoice(byte voice) {
	assert(voice < NUM_VOICES);

	_voice[voice].data = 0;
	_voice[voice].dataRepeat = 0;
	_voice[voice].length = 0;
	_voice[voice].lengthRepeat = 0;
	_voice[voice].period = 0;
	_voice[voice].periodRepeat = 0;
	_voice[voice].volume = 0;
	_voice[voice].offset = 0;
	_voice[voice].dmaCount = 0;
}

int Paula::readBuffer(int16 *buffer, const int numSamples) {
	Common::StackLock lock(_mutex);

	memset(buffer, 0, numSamples * 2);
	if (!_playing) {
		return numSamples;
	}

	if (_stereo)
		return readBufferIntern<true>(buffer, numSamples);
	else
		return readBufferIntern<false>(buffer, numSamples);
}


template<bool stereo>
inline void mixBuffer(int16 *&buf, const int8 *data, frac_t &offset, frac_t rate, int end, byte volume, byte panning) {
	for (int i = 0; i < end; i++) {
		const int32 tmp = ((int32) data[fracToInt(offset)]) * volume;
		if (stereo) {
			*buf++ += (tmp * (255 - panning)) >> 7;
			*buf++ += (tmp * (panning)) >> 7;
		} else
			*buf++ += tmp;

		offset += rate;
	}
}

template<bool stereo>
int Paula::readBufferIntern(int16 *buffer, const int numSamples) {
	int samples = _stereo ? numSamples / 2 : numSamples;
	while (samples > 0) {

		// Handle 'interrupts'. This gives subclasses the chance to adjust the channel data
		// (e.g. insert new samples, do pitch bending, whatever).
		if (_curInt == 0) {
			_curInt = _intFreq;
			interrupt();
		}

		// Compute how many samples to generate: at most the requested number of samples,
		// of course, but we may stop earlier when an 'interrupt' is expected.
		const uint nSamples = MIN((uint)samples, _curInt);

		// Loop over the four channels of the emulated Paula chip
		for (int voice = 0; voice < NUM_VOICES; voice++) {
			// No data, or paused -> skip channel
			if (!_voice[voice].data || (_voice[voice].period <= 0))
				continue;

			// The Paula chip apparently run at 7.0937892 MHz. We combine this with
			// the requested output sampling rate (typicall 44.1 kHz or 22.05 kHz)
			// as well as the "period" of the channel we are processing right now,
			// to compute the correct output 'rate'.
			frac_t rate = doubleToFrac(_periodScale / _voice[voice].period);

			// Cap the volume
			_voice[voice].volume = MIN((byte) 0x40, _voice[voice].volume);

			// Cache some data (helps the compiler to optimize the code, by
			// indirectly telling it that no data aliasing can occur).
			frac_t offset = _voice[voice].offset;
			frac_t sLen = intToFrac(_voice[voice].length);
			const int8 *data = _voice[voice].data;
			int dmaCount = _voice[voice].dmaCount;
			int16 *p = buffer;
			int end = 0;
			int neededSamples = nSamples;

			// Compute the number of samples to generate; that is, either generate
			// just as many as were requested, or until the buffer is used up.
			// Note that dividing two frac_t yields an integer (as the denominators
			// cancel out each other).
			// Note that 'end' could be 0 here. No harm in that :-).
			end = MIN(neededSamples, (int)((sLen - offset + rate - 1) / rate));
			mixBuffer<stereo>(p, data, offset, rate, end, _voice[voice].volume, _voice[voice].panning);
			neededSamples -= end;

			// If we have not yet generated enough samples, and looping is active: loop!
			if (neededSamples > 0 && _voice[voice].lengthRepeat > 2) {
				// At this point we know that we have used up all samples in the buffer, so reset it.
				_voice[voice].data = data = _voice[voice].dataRepeat;
				_voice[voice].length = _voice[voice].lengthRepeat;
				sLen = intToFrac(_voice[voice].length);
				// TODO: the value in offset shouldnt be dropped but scaled to new rate

				if (_voice[voice].period != _voice[voice].periodRepeat) {
					_voice[voice].period = _voice[voice].periodRepeat;
					rate = doubleToFrac(_periodScale / _voice[voice].period);
				}

				// If the "rate" exceeds the sample rate, we would have to perform constant
				// wrap arounds. So, apply the first step of the euclidean algorithm to
				// achieve the same more efficiently: Take rate modulo sLen
				// TODO: This messes up dmaCount
				if (sLen < rate)
					rate %= sLen;

				// Repeat as long as necessary.
				while (neededSamples > 0) {
					// TODO offset -= sLen, but only if same rate otherwise need to scale first
					offset &= FRAC_LO_MASK;
					dmaCount++;

					// Compute the number of samples to generate (see above) and mix 'em.
					end = MIN(neededSamples, (int)((sLen - offset + rate - 1) / rate));
					mixBuffer<stereo>(p, data, offset, rate, end, _voice[voice].volume, _voice[voice].panning);
					neededSamples -= end;
				}
			}

			// Write back the cached data
			_voice[voice].offset = offset;
			_voice[voice].dmaCount = dmaCount;

		}
		buffer += _stereo ? nSamples * 2 : nSamples;
		_curInt -= nSamples;
		samples -= nSamples;
	}
	return numSamples;
}

} // End of namespace Audio