aboutsummaryrefslogtreecommitdiff
path: root/engines/adl/sound.cpp
blob: 63eea45953c6ab087c8c00808b02c3c3b4f789c6 (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
/* 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.
 *
 */

#include "common/system.h"

#include "audio/mixer.h"

#include "adl/sound.h"

namespace Adl {

// Generic PC-speaker synth
// This produces more accurate frequencies than Audio::PCSpeaker, but only
// does square waves.
class Speaker {
public:
	Speaker(int sampleRate);

	void startTone(double freq);
	void stopTone();
	void generateSamples(int16 *buffer, int numSamples);

private:
	int _rate;
	frac_t _halfWaveLen, _halfWaveRem;
	int16 _curSample;
};

Speaker::Speaker(int sampleRate) :
		_rate(sampleRate),
		_curSample(32767) {

	stopTone();
}

void Speaker::startTone(double freq) {
	_halfWaveLen = _halfWaveRem = doubleToFrac(_rate / freq / 2);

	if (_halfWaveLen < (frac_t)FRAC_ONE) {
		// Tone out of range at this sample rate
		stopTone();
	}
}

void Speaker::stopTone() {
	_halfWaveLen = 0;
	_halfWaveRem = intToFrac(32767);
}

void Speaker::generateSamples(int16 *buffer, int numSamples) {
	int offset = 0;

	while (offset < numSamples) {
		if (_halfWaveRem >= 0 && _halfWaveRem < (frac_t)FRAC_ONE) {
			// Rising/falling edge
			// Switch level
			_curSample = ~_curSample;
			// Use transition point fraction for current sample value
			buffer[offset++] = _halfWaveRem ^ _curSample;
			// Compute next transition point
			_halfWaveRem += _halfWaveLen - FRAC_ONE;
		} else {
			// Low/high level (incl. silence)
			// Generate as many samples as we can
			const int samples = MIN(numSamples - offset, (int)fracToInt(_halfWaveRem));
			Common::fill(buffer + offset, buffer + offset + samples, _curSample);
			offset += samples;

			// Count down to level transition point, unless we're playing silence
			if (_halfWaveLen > 0)
				_halfWaveRem -= intToFrac(samples);
		}
	}
}

Sound::Sound(const Tones &tones) :
		_tones(tones),
		_toneIndex(0),
		_samplesRem(0) {

	_rate = g_system->getMixer()->getOutputRate();
	_speaker = new Speaker(_rate);
}

Sound::~Sound() {
	delete _speaker;
}

bool Sound::endOfData() const {
	return _samplesRem == 0 && _toneIndex == _tones.size();
}

int Sound::readBuffer(int16 *buffer, const int numSamples) {
	int offset = 0;

	while (offset < numSamples) {
		if (_samplesRem == 0) {
			// Set up next tone

			if (_toneIndex == _tones.size()) {
				// No more tones
				return offset;
			}

			if (_tones[_toneIndex].freq == 0.0)
				_speaker->stopTone();
			else
				_speaker->startTone(_tones[_toneIndex].freq);

			// Compute length of tone
			_samplesRem = _rate * _tones[_toneIndex++].len / 1000;
		}

		// Generate as many samples as we can
		const int samples = MIN(numSamples - offset, _samplesRem);
		_speaker->generateSamples(buffer + offset, samples);

		_samplesRem -= samples;
		offset += samples;
	}

	return numSamples;
}

} // End of namespace Adl