ScummVM API documentation
music.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 // Music class
23 
24 #ifndef TINSEL_MUSIC_H
25 #define TINSEL_MUSIC_H
26 
27 #include "audio/midiplayer.h"
28 #include "audio/audiostream.h"
29 #include "audio/mixer.h"
30 #include "common/mutex.h"
31 
32 class MidiParser;
33 
34 namespace Tinsel {
35 
36 class Music {
37 public:
38  Music() : _currentMidi(0), _currentLoop(false) {
39  _midiBuffer.pDat = nullptr;
40  _midiBuffer.size = 0;
41  }
42 
43  ~Music() {
44  DeleteMidiBuffer();
45  }
46 
47  bool PlayMidiSequence( // Plays the specified MIDI sequence through the sound driver
48  uint32 dwFileOffset, // handle of MIDI sequence data
49  bool bLoop); // Whether to loop the sequence
50 
51  bool MidiPlaying(); // Returns TRUE if a Midi tune is currently playing
52 
53  bool StopMidi(); // Stops any currently playing midi
54 
55  void SetMidiVolume( // Sets the volume of the MIDI music. Returns the old volume
56  int vol); // new volume - 0..MAXMIDIVOL
57 
58  int GetMidiVolume();
59 
60  void OpenMidiFiles();
61  void DeleteMidiBuffer();
62 
63  void CurrentMidiFacts(SCNHANDLE *pMidi, bool *pLoop);
64  void RestoreMidiFacts(SCNHANDLE Midi, bool Loop);
65 
66  int GetTrackNumber(SCNHANDLE hMidi);
67  SCNHANDLE GetTrackOffset(int trackNumber);
68 
69  uint8 *GetMidiBuffer() { return _midiBuffer.pDat; }
70 
71  uint8* ResizeMidiBuffer(uint32 newSize) {
72  if (_midiBuffer.size < newSize) {
73  _midiBuffer.pDat = (byte*)realloc(_midiBuffer.pDat, newSize);
74  assert(_midiBuffer.pDat);
75  }
76 
77  return _midiBuffer.pDat;
78  }
79 
80  void dumpMusic();
81 
82 private:
83  // sound buffer structure used for MIDI data and samples
84  struct SOUND_BUFFER {
85  uint8 *pDat; // pointer to actual buffer
86  uint32 size; // size of the buffer
87  };
88 
89  // MIDI buffer
90  SOUND_BUFFER _midiBuffer;
91 
92  SCNHANDLE _currentMidi;
93  bool _currentLoop;
94 
95  // We allocate 155 entries because that's the maximum, used in the SCN version
96  SCNHANDLE _midiOffsets[155];
97 };
98 
100 public:
102 
103  void setVolume(int volume) override;
104 
105  void playMIDI(uint32 size, bool loop);
106 
107 // void stop();
108  void pause() override;
109  void resume() override;
110 
111  // MidiDriver_BASE interface implementation
112  void send(uint32 b) override;
113 
114  // The original sets the "sequence timing" to 109 Hz, whatever that
115  // means. The default is 120.
116  uint32 getBaseTempo() { return _driver ? (109 * _driver->getBaseTempo()) / 120 : 0; }
117 
118  bool _milesAudioMode;
119 
120 private:
121  void playXMIDI(uint32 size, bool loop);
122  void playSEQ(uint32 size, bool loop);
123 };
124 
126 public:
127  PCMMusicPlayer();
128  ~PCMMusicPlayer() override;
129 
130  bool isPlaying() const;
131 
132  bool isDimmed() const;
133 
134  void getTunePlaying(void *voidPtr, int length);
135  void restoreThatTune(void *voidPtr);
136 
137  void setMusicSceneDetails(SCNHANDLE hScript, SCNHANDLE hSegment, const char *fileName);
138 
139  void setVolume(int volume);
140 
141  void startPlay(int id);
142  void stopPlay();
143 
144  bool getMusicTinselDimmed() const;
145  void dim(bool bTinselDim);
146  void unDim(bool bTinselUnDim);
147  void dimIteration();
148 
149  void startFadeOut(int ticks);
150  void fadeOutIteration();
151 
152  int readBuffer(int16 *buffer, const int numSamples) override;
153  bool isStereo() const override;
154  bool endOfData() const override { return _end; }
155  bool endOfStream() const override { return false; }
156  int getRate() const override;
157 
158 protected:
159  enum State {
160  S_IDLE,
161  S_NEW,
162  S_MID,
163  S_END1,
164  S_END2,
165  S_END3,
166  S_NEXT,
167  S_STOP
168  };
169 
170  Audio::SoundHandle _handle;
171  Audio::AudioStream *_curChunk;
172  Common::Mutex _mutex;
173 
174  bool _end;
175 
176  int _silenceSamples;
177 
178  State _state, _mState;
179  bool _forcePlay;
180  int32 _scriptNum;
181  int32 _scriptIndex;
182  SCNHANDLE _hScript;
183  SCNHANDLE _hSegment;
184  Common::Path _filename;
185  Common::File _file;
186 
187  uint8 _volume;
188 
189  bool _dimmed;
190  bool _dimmedTinsel;
191  uint8 _dimmedVolume;
192  int _dimIteration;
193  int _dimPosition;
194 
195  uint8 _fadeOutVolume;
196  int _fadeOutIteration;
197 
198  void play();
199  void stop();
200  void setVol(uint8 volume);
201 
202  bool getNextChunk();
203 
204  void loadMusicFromSegment(int segmentNum);
205  void loadADPCMMusicFromSegment(int segmentNum);
206  void loadMP3MusicFromSegment(int segmentNum);
207 };
208 
209 } // End of namespace Tinsel
210 
211 #endif
Definition: midiplayer.h:63
uint32 SCNHANDLE
Definition: dw.h:31
bool endOfStream() const override
Definition: music.h:155
Definition: path.h:52
Definition: music.h:125
Definition: music.h:99
Definition: tinsel.h:146
Definition: mixer.h:49
Definition: file.h:47
Definition: mutex.h:67
Definition: actors.h:36
Definition: audiostream.h:50
bool endOfData() const override
Definition: music.h:154
Definition: music.h:36
Definition: common.h:36
Definition: midiparser.h:354