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