aboutsummaryrefslogtreecommitdiff
path: root/sword1/music.h
blob: 7ce91aa430254abf547cb6bebe59881b2d094d67 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $Header$
 *
 */

#ifndef BSMUSIC_H
#define BSMUSIC_H

#include "common/scummsys.h"
#include "common/mutex.h"
#include "common/file.h"
#include "sound/audiostream.h"
#include "sound/rate.h"

namespace Audio {
	class Mixer;
}

namespace Sword1 {

#define TOTAL_TUNES 270

enum MusicMode {
	MusicNone = 0,
	MusicWave,
	MusicMp3,
	MusicVorbis
};

class WaveAudioStream : public AudioStream {
public:
	WaveAudioStream(Common::File *source, uint32 pSize);
	virtual ~WaveAudioStream();
	virtual int readBuffer(int16 *buffer, const int numSamples);
	virtual bool isStereo(void) const { return _isStereo; };
	virtual bool endOfData(void) const;
	virtual int getRate(void) const { return _rate; };
private:
	Common::File	*_sourceFile;
	uint8	*_sampleBuf;
	uint32	 _rate;
	bool	 _isStereo;
	uint32   _samplesLeft;
	uint16	 _bitsPerSample;
};

class MusicHandle : public AudioStream {
private:
	Common::File _file;
	bool _looping;
	int32 _fading;
	int32 _fadeSamples;
	MusicMode _musicMode;
	AudioStream *_audioSource;
	AudioStream *createAudioSource(void);
public:
	MusicHandle() : _looping(false), _fading(0), _audioSource(NULL) {}
	virtual int readBuffer(int16 *buffer, const int numSamples);
	bool play(const char *filename, bool loop);
	void stop();
	void fadeUp();
	void fadeDown();
	bool streaming() const;
	int32 fading() { return _fading; }
	bool endOfData() const;
	bool endOfStream() const { return false; }
	bool isStereo() const;
	int getRate() const;
};

class Music : public AudioStream {
public:
	Music(Audio::Mixer *pMixer);
	~Music();
	void startMusic(int32 tuneId, int32 loopFlag);
	void fadeDown();
	void setVolume(uint8 volL, uint8 volR);
	void giveVolume(uint8 *volL, uint8 *volR);

	// AudioStream API
	int readBuffer(int16 *buffer, const int numSamples) {
		mixer(buffer, numSamples / 2);
		return numSamples;
	}
	bool isStereo() const { return true; }
	bool endOfData() const { return false; }
	int getRate() const { return _sampleRate; }

private:
	Audio::st_volume_t _volumeL, _volumeR;
	MusicHandle _handles[2];
	Audio::RateConverter *_converter[2];
	Audio::Mixer *_mixer;
	uint32 _sampleRate;
	Common::Mutex _mutex;

	static void passMixerFunc(void *param, int16 *buf, uint len);
	void mixer(int16 *buf, uint32 len);

	static const char _tuneList[TOTAL_TUNES][8]; // in staticres.cpp
};

} // End of namespace Sword1

#endif // BSMUSIC_H