1 /*(LGPL)
2 ------------------------------------------------------------
3 	a_midifile.c - MIDI file loader and player
4 ------------------------------------------------------------
5  * Stolen from the AdPlug player, stripped down,
6  * translated into C, and adapted for the Audio Engine
7  * by David Olofson, 2002.
8  *
9  * 20020219:	Separated data from player.
10  *
11  * 20020613:	Removed code for all special formats
12  *		and AdLib hardware specific stuff. (Can't
13  *		use the data. Can't maintain the code.)
14  *
15  * 20020615:	* Removed all 'long' types. If this engine
16  *		  is supposed to work in 16 bit environments,
17  *		  there's a *lot* more than a few longs to it.
18  *		* Cleaned up type namespace.
19  *		* Fixed various stuff that Splint whines about.
20  *		* Added musical time printout function.
21  *
22  * Below is the original copyright. Of course, the LGPL
23  * license still applies.
24 ------------------------------------------------------------
25  * Adplug - Replayer for many OPL2/OPL3 audio file formats.
26  * Copyright (C) 1999, 2000, 2001 Simon Peter, <dn.tlp@gmx.net>, et al.
27  *
28  * This library is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU Lesser General Public
30  * License as published by the Free Software Foundation; either
31  * version 2.1 of the License, or (at your option) any later version.
32  *
33  * This library is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36  * Lesser General Public License for more details.
37  *
38  * You should have received a copy of the GNU Lesser General Public
39  * License along with this library; if not, write to the Free Software
40  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
41  *
42  *
43  * mid.h - LAA & MID & CMF Player by Philip Hassey (philhassey@hotmail.com)
44  */
45 
46 #ifndef _A_MIDIFILE_H_
47 #define _A_MIDIFILE_H_
48 
49 #include "a_midisock.h"
50 
51 /*----------------------------------------------------------
52 	midi_file_t
53 ----------------------------------------------------------*/
54 
55 typedef struct midi_file_t
56 {
57 	const char	*author;
58 	const char	*title;
59 	const char	*remarks;
60 	unsigned	flen;
61 	unsigned	subsongs;
62 	unsigned	format;
63 	unsigned	tracks;
64 	unsigned char	*data;
65 } midi_file_t;
66 
67 midi_file_t *mf_open(const char *name);
68 void mf_close(midi_file_t *mf);
69 
70 
71 /*----------------------------------------------------------
72 	midi_player_t
73 ----------------------------------------------------------*/
74 
75 typedef struct mp_track_context_t
76 {
77 	int		on;
78 	unsigned	pos;
79 	unsigned	iwait;
80 	unsigned char	pv;
81 } mp_track_context_t;
82 
83 
84 typedef struct mp_track_t
85 {
86 	int			enabled;
87 	unsigned		tend;
88 	unsigned		spos;
89 	mp_track_context_t	c;
90 	mp_track_context_t	loop_start;
91 } mp_track_t;
92 
93 
94 typedef struct mp_timesig_t
95 {
96 	unsigned char	beats;
97 	unsigned char	value;
98 	unsigned char	clock;
99 	unsigned char	qn;
100 } mp_timesig_t;
101 
102 typedef struct mp_context_t
103 {
104 	unsigned	ppqn;		/* Pulses Per Quarter Note */
105 	unsigned	usqtr;		/* microseconds per quarter note */
106 	float		spulse;		/* seconds per pulse */
107 	mp_timesig_t	timesig;
108 	unsigned	time;		/* Current position in pulses */
109 	unsigned	iwait;		/* Pulses to next event */
110 	float		fwait;		/* Seconds to next event */
111 } mp_context_t;
112 
113 
114 typedef struct midi_player_t
115 {
116 	midisock_t	*sock;
117 	midi_file_t	*mf;
118 	unsigned	pos;
119 	mp_context_t	c;
120 	mp_context_t	loop_start;
121 	mp_track_t	track[16];
122 	int		doing;
123 	int		pitch;
124 } midi_player_t;
125 
126 
127 midi_player_t *mp_open(midisock_t *ms);
128 void mp_close(midi_player_t *mp);
129 
130 /* Select a previously loaded MIDI file for playback */
131 int mp_select(midi_player_t *mp, midi_file_t *midifile);
132 
133 /* Play all events until the next delay */
134 int mp_update(midi_player_t *mp);
135 
136 /* Play all events until current_position + dt seconds */
137 int mp_play(midi_player_t *mp, float dt);
138 
139 /* Rewind to start of sub song 'subsong' */
140 void mp_rewind(midi_player_t *mp, unsigned subsong);
141 
142 void mp_stop(midi_player_t *mp);
143 
144 #endif /*_A_MIDIFILE_H_*/
145