1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef BLADERUNNER_AUDIO_MIXER_H
24 #define BLADERUNNER_AUDIO_MIXER_H
25 
26 #include "audio/audiostream.h"
27 #include "audio/mixer.h"
28 
29 #include "common/mutex.h"
30 
31 namespace BladeRunner {
32 
33 class BladeRunnerEngine;
34 
35 #if !BLADERUNNER_ORIGINAL_BUG
36 enum audioMixerAppTimers {
37 	kAudioMixerAppTimerMusicNext    =  0,
38 	kAudioMixerAppTimerMusicFadeOut =  1
39 };
40 #endif
41 
42 class AudioMixer {
43 #if BLADERUNNER_ORIGINAL_BUGS
44 	static const int kChannels         = 9;
45 	static const int kUsableChannels   = 8;
46 	static const int kMusicChannel     = 8;
47 #else
48 	static const int kChannels         = 15;
49 	static const int kUsableChannels   = 14;
50 	static const int kMusicChannel     = 14;
51 
52 	static const int kAudioMixerAppTimersNum = 2;
53 #endif // BLADERUNNER_ORIGINAL_BUGS
54 	static const int kUpdatesPerSecond = 40;
55 
56 	struct Channel {
57 		bool                isPresent;
58 		int                 priority;
59 		bool                loop;
60 		Audio::SoundHandle  handle;
61 		Audio::AudioStream *stream;
62 		float               volume;
63 		float               volumeDelta;
64 		float               volumeTarget;
65 		float               pan;
66 		float               panDelta;
67 		float               panTarget;
68 		void              (*endCallback)(int channel, void *data);
69 		void               *callbackData;
70 		uint32              timeStarted;
71 		uint32              trackDurationMs;
72 		bool                sentToMixer;
73 	};
74 
75 	BladeRunnerEngine *_vm;
76 
77 	Channel       _channels[kChannels];
78 	Common::Mutex _mutex;
79 
80 #if !BLADERUNNER_ORIGINAL_BUGS
81 	struct audioMixerAppTimer {
82 		bool                started;
83 		uint32              intervalMillis; // expiration interval in milliseconds
84 		uint32              lastFired;      // time of last time the timer expired in milliseconds
85 	};
86 
87 	audioMixerAppTimer      _audioMixerAppTimers[kAudioMixerAppTimersNum];
88 #endif // !BLADERUNNER_ORIGINAL_BUGS
89 
90 public:
91 	AudioMixer(BladeRunnerEngine *vm);
92 	~AudioMixer();
93 
94 	int play(Audio::Mixer::SoundType type, Audio::RewindableAudioStream *stream, int priority, bool loop, int volume, int pan, void(*endCallback)(int, void *), void *callbackData, uint32 trackDurationMs);
95 	int playMusic(Audio::RewindableAudioStream *stream, int volume, void(*endCallback)(int, void *), void *callbackData, uint32 trackDurationMs);
96 	void stop(int channel, uint32 time);
97 
98 	void adjustVolume(int channel, int newVolume, uint32 time);
99 	void adjustPan(int channel, int newPan, uint32 time);
100 
101 #if !BLADERUNNER_ORIGINAL_BUGS
102 	void startAppTimerProc(int audioMixAppTimerId, uint32 intervalMillis);
103 	void stopAppTimerProc(int audioMixAppTimerId);
104 #endif // !BLADERUNNER_ORIGINAL_BUGS
105 	// TODO Are these completely unused?
106 //	void resume(int channel, uint32 delay);
107 //	void pause(int channel, uint32 delay);
108 
109 private:
110 	int playInChannel(int channel, Audio::Mixer::SoundType type, Audio::RewindableAudioStream *stream, int priority, bool loop, int volume, int pan, void(*endCallback)(int, void *), void *callbackData, uint32 trackDurationMs);
111 
112 	bool isActive(int channel) const;
113 	void tick();
114 	static void timerCallback(void *refCon);
115 };
116 
117 } // End of namespace BladeRunner
118 
119 #endif
120