~ubuntu-branches/debian/jessie/scummvm/jessie

« back to all changes in this revision

Viewing changes to audio/midiplayer.h

  • Committer: Bazaar Package Importer
  • Author(s): Moritz Muehlenhoff
  • Date: 2011-05-25 19:02:23 UTC
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20110525190223-fiqm0oaec714xk31
Tags: upstream-1.3.0
ImportĀ upstreamĀ versionĀ 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * $URL$
 
22
 * $Id$
 
23
 *
 
24
 */
 
25
 
 
26
#ifndef SOUND_MIDIPLAYER_H
 
27
#define SOUND_MIDIPLAYER_H
 
28
 
 
29
#include "common/scummsys.h"
 
30
#include "common/mutex.h"
 
31
#include "audio/mididrv.h"
 
32
 
 
33
class MidiParser;
 
34
 
 
35
namespace Audio {
 
36
 
 
37
/**
 
38
 * Simple MIDI playback class.
 
39
 *
 
40
 * @note Currently incomplete, as it lacks play() methods. This is just a
 
41
 * start of the real thing, which tries to include code replicated between
 
42
 * several of our engines.
 
43
 *
 
44
 * Eventually, this should offer ways to start playback of SMF and XMIDI
 
45
 * data (and possibly make it easy to add further playback methods),
 
46
 * should be able to automatically instantiate _driver as needed,
 
47
 * should perform memory management on the MidiParser object(s) being
 
48
 * used, and possibly more.
 
49
 *
 
50
 * Also, pause/resume handling should be unified (we provide an implementation,
 
51
 * but several subclasses use different ones).
 
52
 *
 
53
 * Also, care should be taken to ensure that mutex locking is done right.
 
54
 *
 
55
 * @todo Document origin of this class. It is based on code shared by
 
56
 * several engines (e.g. DRACI says it copied it from MADE, which took
 
57
 * it from SAGE).
 
58
 */
 
59
class MidiPlayer : public MidiDriver_BASE {
 
60
public:
 
61
        MidiPlayer();
 
62
        ~MidiPlayer();
 
63
 
 
64
        // TODO: Implement ways to actually play stuff
 
65
        //virtual void play(TODO);
 
66
 
 
67
        // TODO: Document these
 
68
        virtual void stop();
 
69
        virtual void pause();
 
70
        virtual void resume();
 
71
 
 
72
        /**
 
73
         * Return whether there is currently any MIDI music playing.
 
74
         *
 
75
         * @todo There is a subtle difference between the semantics of this in
 
76
         *       various subclasses, related to paused music: Namely, should this
 
77
         *       function return true or false if a MIDI song is currently loaded,
 
78
         *       but paused? In the base implementation of pause/resume, "false"
 
79
         *       will be returned (that is, it is not possible to distinguish between
 
80
         *       nothing being played, and an active but paused MIDI tune).
 
81
         *       But in several subclasses (e.g. in HUGO), there is a separate _paused
 
82
         *       variable, which is used to pause playback, and for these, "true"
 
83
         *       will be returned.
 
84
         *       And in SAGA, isPlaying is overwritten to return the value
 
85
         *       of _parser->isPlaying() (which should amount to "true" in the
 
86
         *       described situation).
 
87
         *       We really should unify this and clearly define the desired
 
88
         *       semantics of this method.
 
89
         */
 
90
        bool isPlaying() const { return _isPlaying; }
 
91
 
 
92
        /**
 
93
         * Return the currently active master volume, in the range 0-255.
 
94
         */
 
95
        int getVolume() const { return _masterVolume; }
 
96
 
 
97
        /**
 
98
         * Set the master volume to the specified value in the range 0-255.
 
99
         * (values outside this range are automatically clipped).
 
100
         * This may cause suitable MIDI events to be sent to active channels.
 
101
         *
 
102
         * @todo This method currently does not do anything if the new volume
 
103
         *       matches the old volume. But some engines always update the
 
104
         *       volume (Parallaction, Tinsel, Touche, ...). This is why
 
105
         *       this method is currently virtual. We really should figure
 
106
         *       which way to do it, and then make this the default, and make
 
107
         *       this method non-virtual again.
 
108
         */
 
109
        virtual void setVolume(int volume);
 
110
 
 
111
        /**
 
112
         * Update the volume according to the ConfMan's 'music_volume'
 
113
         * setting. also respects the 'mute' setting.
 
114
         */
 
115
        void syncVolume();
 
116
 
 
117
        // TODO: Document this
 
118
        bool hasNativeMT32() const { return _nativeMT32; }
 
119
 
 
120
        // MidiDriver_BASE implementation
 
121
        virtual void send(uint32 b);
 
122
        virtual void metaEvent(byte type, byte *data, uint16 length);
 
123
 
 
124
protected:
 
125
        /**
 
126
         * This method is invoked by the default send() implementation,
 
127
         * after suitably filtering the message b.
 
128
         */
 
129
        virtual void sendToChannel(byte ch, uint32 b);
 
130
 
 
131
        /**
 
132
         * This method is invoked by metaEvent when an end-of-track
 
133
         * event arrives. By default, this tells the parser
 
134
         * to jump to the start (if looping is enabled) resp.
 
135
         * invokes stope():
 
136
         * Overload this to customize behavior.
 
137
         */
 
138
        virtual void endOfTrack();
 
139
 
 
140
        // TODO: Document this
 
141
        virtual void onTimer();
 
142
 
 
143
        static void timerCallback(void *data);
 
144
 
 
145
        void createDriver(int flags = MDT_MIDI | MDT_ADLIB | MDT_PREFER_GM);
 
146
 
 
147
protected:
 
148
        enum {
 
149
                /**
 
150
                 * The number of MIDI channels supported.
 
151
                 */
 
152
                kNumChannels = 16
 
153
        };
 
154
 
 
155
        Common::Mutex _mutex;
 
156
        MidiDriver *_driver;
 
157
 
 
158
        /**
 
159
         * A MidiParser instances, to be created by methods of a MidiPlayer
 
160
         * subclass.
 
161
         * @note The stop() method (and hence the destructor) invoke the
 
162
         * unloadMusic() method of _parser and then delete it.
 
163
         */
 
164
        MidiParser *_parser;
 
165
 
 
166
        /**
 
167
         * This is an (optional) pointer to a malloc'ed buffer containing
 
168
         * MIDI data used by _parser. The stop() method (and hence the
 
169
         * destructor) will free this if set.
 
170
         * Subclasses of this class may use _midiData, but don't have to
 
171
         */
 
172
        byte *_midiData;
 
173
 
 
174
        MidiChannel *_channelsTable[kNumChannels];
 
175
        uint8 _channelsVolume[kNumChannels];
 
176
 
 
177
        bool _isLooping;
 
178
        bool _isPlaying;
 
179
 
 
180
        /**
 
181
         * The master volume, in the range 0-255.
 
182
         */
 
183
        int _masterVolume;      // FIXME: byte or int ?
 
184
 
 
185
        bool _nativeMT32;
 
186
};
 
187
 
 
188
 
 
189
} // End of namespace Audio
 
190
 
 
191
#endif