aboutsummaryrefslogtreecommitdiff
path: root/sound/mididrv.h
blob: 2783b6c6518a12068c32710e72e18a9c09ce2e11 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001/2002 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#ifndef SOUND_MIDIDRV_H
#define SOUND_MIDIDRV_H

#include "scummsys.h"

class MidiChannel;

struct MidiEvent {
	uint32 delta;
	uint32 event;
};

// Abstract MIDI Driver Class
class MidiDriver {

public:
	/* called whenever the midi driver is in streaming mode,
	 * and more midi commands need to be generated
	 * return 0 to tell the mididriver that the end of stream was reached
	 */
	typedef int StreamCallback (void *param, MidiEvent * ev, int num);


	/* open modes, pass one of those to open() */
	enum {
		MO_SIMPLE = 1,
		MO_STREAMING = 2,
	};

	/* Special events that can be inserted in a MidiEvent.
	 * event = (ME_xxx<<24) | <24-bit data associated with event>
	 */
	enum {
		ME_NONE = 0,
		ME_TEMPO = 1,
	};

	/* error codes returned by open.
	 * can be converted to a string with get_error_name()
	 */
	enum {
		MERR_CANNOT_CONNECT = 1,
		MERR_STREAMING_NOT_AVAILABLE = 2,
		MERR_DEVICE_NOT_AVAILABLE = 3,
		MERR_ALREADY_OPEN = 4,
	};

	enum {
		PROP_TIMEDIV = 1,
		PROP_SMALLHEADER = 2,
	};


	/* open the midi driver.
	 * returns 0 if successful.
	 * otherwise an error code. */
	virtual int open(int mode) = 0;

	/* close the midi driver */
	virtual void close() = 0;

	/* output a packed midi command to the midi stream
	 * valid only if mode is MO_SIMPLE
	 */
	virtual void send(uint32 b) = 0;

	/* set callback when more streams need to be generated.
	 * valid only when mode==MO_STREAMING
	 */
	virtual void set_stream_callback(void *param, StreamCallback *sc) = 0;

	/* Pause or resume streaming MIDI */
	virtual void pause(bool pause) = 0;


	/* Get or set a property */
	virtual uint32 property(int prop, uint32 param);

	/* retrieve a string representation of an error code */
	static const char *get_error_name(int error_code);

	// HIGH-LEVEL SEMANTIC METHODS
	virtual void setPitchBendRange (byte channel, uint range)
	{
		send((  0   << 16) | (101 << 8) | (0xB0 | channel));
		send((  0   << 16) | (100 << 8) | (0xB0 | channel));
		send((range << 16) | (  6 << 8) | (0xB0 | channel));
		send((  0   << 16) | ( 38 << 8) | (0xB0 | channel));
	}

	virtual void sysEx_customInstrument (byte channel, uint32 type, byte *instr) { }

	// Timing functions - MidiDriver now operates timers
	virtual void setTimerCallback (void *timer_param, void (*timer_proc) (void *)) = 0;
	virtual uint32 getBaseTempo (void) = 0;

	// Channel allocation functions
	virtual MidiChannel *allocateChannel() = 0;
	virtual MidiChannel *getPercussionChannel() = 0;
};



class MidiChannel {
public:
	virtual void release() = 0;

	// Regular messages
	virtual void noteOff (byte note) = 0;
	virtual void noteOn (byte note, byte velocity) = 0;
	virtual void programChange (byte program) = 0;
	virtual void pitchBend (int16 bend) = 0; // -0x2000 to +0x1FFF

	// Control Change messages
	virtual void controlChange (byte control, byte value) = 0;
	virtual void modulationWheel (byte value) = 0;
	virtual void volume (byte value) = 0;
	virtual void panPosition (byte value) = 0;
	virtual void pitchBendFactor (byte value) = 0;
	virtual void detune (byte value) = 0;
	virtual void priority (byte value) = 0;
	virtual void sustain (bool value) = 0;
	virtual void effectLevel (byte value) = 0;
	virtual void chorusLevel (byte value) = 0;
	virtual void allNotesOff() = 0;

	// SysEx messages
	virtual void sysEx_customInstrument (uint32 type, byte *instr) = 0;
};



// MIDI Driver Types
enum {
	MD_AUTO = 0,
	MD_NULL = 1,
	MD_WINDOWS = 2,
	MD_TIMIDITY = 3,
	MD_SEQ = 4,
	MD_QTMUSIC = 5,
	MD_ETUDE = 6,
	MD_COREAUDIO = 7,
	MD_MIDIEMU = 8,
	MD_ALSA = 9,
	MD_ADLIB = 10
};


/* Factory functions => no need to include the specific classes
 * in this header => faster compile */
extern MidiDriver *MidiDriver_NULL_create();
extern MidiDriver *MidiDriver_ADLIB_create();
extern MidiDriver *MidiDriver_WIN_create();
extern MidiDriver *MidiDriver_SEQ_create();
extern MidiDriver *MidiDriver_QT_create();
extern MidiDriver *MidiDriver_CORE_create();
extern MidiDriver *MidiDriver_ETUDE_create();
extern MidiDriver *MidiDriver_ALSA_create();

#endif