1 #ifndef XMP_H
2 #define XMP_H
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #define XMP_VERSION "4.5.0"
9 #define XMP_VERCODE 0x040500
10 #define XMP_VER_MAJOR 4
11 #define XMP_VER_MINOR 5
12 #define XMP_VER_RELEASE 0
13 
14 #if defined(_WIN32) && !defined(__CYGWIN__)
15 # if defined(BUILDING_STATIC)
16 #  define LIBXMP_EXPORT
17 # elif defined(BUILDING_DLL)
18 #  define LIBXMP_EXPORT __declspec(dllexport)
19 # else
20 #  define LIBXMP_EXPORT __declspec(dllimport)
21 # endif
22 #elif defined(__OS2__) && defined(__WATCOMC__) && defined(__SW_BD)
23 #  define LIBXMP_EXPORT __declspec(dllexport)
24 #elif (defined(__GNUC__) || defined(__clang__) || defined(__HP_cc)) && defined(XMP_SYM_VISIBILITY)
25 # define LIBXMP_EXPORT __attribute__((visibility ("default")))
26 #elif defined(__SUNPRO_C) && defined(XMP_LDSCOPE_GLOBAL)
27 # define LIBXMP_EXPORT __global
28 #elif defined(EMSCRIPTEN)
29 # define LIBXMP_EXPORT EMSCRIPTEN_KEEPALIVE
30 #else
31 # define LIBXMP_EXPORT
32 #endif
33 
34 #define XMP_NAME_SIZE		64	/* Size of module name and type */
35 
36 #define XMP_KEY_OFF		0x81	/* Note number for key off event */
37 #define XMP_KEY_CUT		0x82	/* Note number for key cut event */
38 #define XMP_KEY_FADE		0x83	/* Note number for fade event */
39 
40 /* mixer parameter macros */
41 
42 /* sample format flags */
43 #define XMP_FORMAT_8BIT		(1 << 0) /* Mix to 8-bit instead of 16 */
44 #define XMP_FORMAT_UNSIGNED	(1 << 1) /* Mix to unsigned samples */
45 #define XMP_FORMAT_MONO		(1 << 2) /* Mix to mono instead of stereo */
46 
47 /* player parameters */
48 #define XMP_PLAYER_AMP		0	/* Amplification factor */
49 #define XMP_PLAYER_MIX		1	/* Stereo mixing */
50 #define XMP_PLAYER_INTERP	2	/* Interpolation type */
51 #define XMP_PLAYER_DSP		3	/* DSP effect flags */
52 #define XMP_PLAYER_FLAGS	4	/* Player flags */
53 #define XMP_PLAYER_CFLAGS	5	/* Player flags for current module */
54 #define XMP_PLAYER_SMPCTL	6	/* Sample control flags */
55 #define XMP_PLAYER_VOLUME	7	/* Player module volume */
56 #define XMP_PLAYER_STATE	8	/* Internal player state (read only) */
57 #define XMP_PLAYER_SMIX_VOLUME	9	/* SMIX volume */
58 #define XMP_PLAYER_DEFPAN	10	/* Default pan setting */
59 #define XMP_PLAYER_MODE 	11	/* Player personality */
60 #define XMP_PLAYER_MIXER_TYPE	12	/* Current mixer (read only) */
61 #define XMP_PLAYER_VOICES	13	/* Maximum number of mixer voices */
62 
63 /* interpolation types */
64 #define XMP_INTERP_NEAREST	0	/* Nearest neighbor */
65 #define XMP_INTERP_LINEAR	1	/* Linear (default) */
66 #define XMP_INTERP_SPLINE	2	/* Cubic spline */
67 
68 /* dsp effect types */
69 #define XMP_DSP_LOWPASS		(1 << 0) /* Lowpass filter effect */
70 #define XMP_DSP_ALL		(XMP_DSP_LOWPASS)
71 
72 /* player state */
73 #define XMP_STATE_UNLOADED	0	/* Context created */
74 #define XMP_STATE_LOADED	1	/* Module loaded */
75 #define XMP_STATE_PLAYING	2	/* Module playing */
76 
77 /* player flags */
78 #define XMP_FLAGS_VBLANK	(1 << 0) /* Use vblank timing */
79 #define XMP_FLAGS_FX9BUG	(1 << 1) /* Emulate FX9 bug */
80 #define XMP_FLAGS_FIXLOOP	(1 << 2) /* Emulate sample loop bug */
81 #define XMP_FLAGS_A500		(1 << 3) /* Use Paula mixer in Amiga modules */
82 
83 /* player modes */
84 #define XMP_MODE_AUTO		0	/* Autodetect mode (default) */
85 #define XMP_MODE_MOD		1	/* Play as a generic MOD player */
86 #define XMP_MODE_NOISETRACKER	2	/* Play using Noisetracker quirks */
87 #define XMP_MODE_PROTRACKER	3	/* Play using Protracker quirks */
88 #define XMP_MODE_S3M		4	/* Play as a generic S3M player */
89 #define XMP_MODE_ST3		5	/* Play using ST3 bug emulation */
90 #define XMP_MODE_ST3GUS		6	/* Play using ST3+GUS quirks */
91 #define XMP_MODE_XM		7	/* Play as a generic XM player */
92 #define XMP_MODE_FT2		8	/* Play using FT2 bug emulation */
93 #define XMP_MODE_IT		9	/* Play using IT quirks */
94 #define XMP_MODE_ITSMP		10	/* Play using IT sample mode quirks */
95 
96 /* mixer types */
97 #define XMP_MIXER_STANDARD	0	/* Standard mixer */
98 #define XMP_MIXER_A500		1	/* Amiga 500 */
99 #define XMP_MIXER_A500F		2	/* Amiga 500 with led filter */
100 
101 /* sample flags */
102 #define XMP_SMPCTL_SKIP		(1 << 0) /* Don't load samples */
103 
104 /* limits */
105 #define XMP_MAX_KEYS		121	/* Number of valid keys */
106 #define XMP_MAX_ENV_POINTS	32	/* Max number of envelope points */
107 #define XMP_MAX_MOD_LENGTH	256	/* Max number of patterns in module */
108 #define XMP_MAX_CHANNELS	64	/* Max number of channels in module */
109 #define XMP_MAX_SRATE		49170	/* max sampling rate (Hz) */
110 #define XMP_MIN_SRATE		4000	/* min sampling rate (Hz) */
111 #define XMP_MIN_BPM		20	/* min BPM */
112 /* frame rate = (50 * bpm / 125) Hz */
113 /* frame size = (sampling rate * channels * size) / frame rate */
114 #define XMP_MAX_FRAMESIZE	(5 * XMP_MAX_SRATE * 2 / XMP_MIN_BPM)
115 
116 /* error codes */
117 #define XMP_END			1
118 #define XMP_ERROR_INTERNAL	2	/* Internal error */
119 #define XMP_ERROR_FORMAT	3	/* Unsupported module format */
120 #define XMP_ERROR_LOAD		4	/* Error loading file */
121 #define XMP_ERROR_DEPACK	5	/* Error depacking file */
122 #define XMP_ERROR_SYSTEM	6	/* System error */
123 #define XMP_ERROR_INVALID	7	/* Invalid parameter */
124 #define XMP_ERROR_STATE		8	/* Invalid player state */
125 
126 struct xmp_channel {
127 	int pan;			/* Channel pan (0x80 is center) */
128 	int vol;			/* Channel volume */
129 #define XMP_CHANNEL_SYNTH	(1 << 0)  /* Channel is synthesized */
130 #define XMP_CHANNEL_MUTE  	(1 << 1)  /* Channel is muted */
131 #define XMP_CHANNEL_SPLIT	(1 << 2)  /* Split Amiga channel in bits 5-4 */
132 #define XMP_CHANNEL_SURROUND	(1 << 4)  /* Surround channel */
133 	int flg;			/* Channel flags */
134 };
135 
136 struct xmp_pattern {
137 	int rows;			/* Number of rows */
138 	int index[1];			/* Track index */
139 };
140 
141 struct xmp_event {
142 	unsigned char note;		/* Note number (0 means no note) */
143 	unsigned char ins;		/* Patch number */
144 	unsigned char vol;		/* Volume (0 to basevol) */
145 	unsigned char fxt;		/* Effect type */
146 	unsigned char fxp;		/* Effect parameter */
147 	unsigned char f2t;		/* Secondary effect type */
148 	unsigned char f2p;		/* Secondary effect parameter */
149 	unsigned char _flag;		/* Internal (reserved) flags */
150 };
151 
152 struct xmp_track {
153 	int rows;			/* Number of rows */
154 	struct xmp_event event[1];	/* Event data */
155 };
156 
157 struct xmp_envelope {
158 #define XMP_ENVELOPE_ON		(1 << 0)  /* Envelope is enabled */
159 #define XMP_ENVELOPE_SUS	(1 << 1)  /* Envelope has sustain point */
160 #define XMP_ENVELOPE_LOOP	(1 << 2)  /* Envelope has loop */
161 #define XMP_ENVELOPE_FLT	(1 << 3)  /* Envelope is used for filter */
162 #define XMP_ENVELOPE_SLOOP	(1 << 4)  /* Envelope has sustain loop */
163 #define XMP_ENVELOPE_CARRY	(1 << 5)  /* Don't reset envelope position */
164 	int flg;			/* Flags */
165 	int npt;			/* Number of envelope points */
166 	int scl;			/* Envelope scaling */
167 	int sus;			/* Sustain start point */
168 	int sue;			/* Sustain end point */
169 	int lps;			/* Loop start point */
170 	int lpe;			/* Loop end point */
171 	short data[XMP_MAX_ENV_POINTS * 2];
172 };
173 
174 struct xmp_subinstrument {
175 	int vol;		/* Default volume */
176 	int gvl;		/* Global volume */
177 	int pan;		/* Pan */
178 	int xpo;		/* Transpose */
179 	int fin;		/* Finetune */
180 	int vwf;		/* Vibrato waveform */
181 	int vde;		/* Vibrato depth */
182 	int vra;		/* Vibrato rate */
183 	int vsw;		/* Vibrato sweep */
184 	int rvv;		/* Random volume/pan variation (IT) */
185 	int sid;		/* Sample number */
186 #define XMP_INST_NNA_CUT	0x00
187 #define XMP_INST_NNA_CONT	0x01
188 #define XMP_INST_NNA_OFF	0x02
189 #define XMP_INST_NNA_FADE	0x03
190 	int nna;		/* New note action */
191 #define XMP_INST_DCT_OFF	0x00
192 #define XMP_INST_DCT_NOTE	0x01
193 #define XMP_INST_DCT_SMP	0x02
194 #define XMP_INST_DCT_INST	0x03
195 	int dct;		/* Duplicate check type */
196 #define XMP_INST_DCA_CUT	XMP_INST_NNA_CUT
197 #define XMP_INST_DCA_OFF	XMP_INST_NNA_OFF
198 #define XMP_INST_DCA_FADE	XMP_INST_NNA_FADE
199 	int dca;		/* Duplicate check action */
200 	int ifc;		/* Initial filter cutoff */
201 	int ifr;		/* Initial filter resonance */
202 };
203 
204 
205 struct xmp_instrument {
206 	char name[32];			/* Instrument name */
207 	int vol;			/* Instrument volume */
208 	int nsm;			/* Number of samples */
209 	int rls;			/* Release (fadeout) */
210 	struct xmp_envelope aei;	/* Amplitude envelope info */
211 	struct xmp_envelope pei;	/* Pan envelope info */
212 	struct xmp_envelope fei;	/* Frequency envelope info */
213 
214 	struct {
215 		unsigned char ins;	/* Instrument number for each key */
216 		signed char xpo;	/* Instrument transpose for each key */
217 	} map[XMP_MAX_KEYS];
218 
219 	struct xmp_subinstrument *sub;
220 
221 	void *extra;			/* Extra fields */
222 };
223 
224 struct xmp_sample {
225 	char name[32];			/* Sample name */
226 	int len;			/* Sample length */
227 	int lps;			/* Loop start */
228 	int lpe;			/* Loop end */
229 #define XMP_SAMPLE_16BIT	(1 << 0)  /* 16bit sample */
230 #define XMP_SAMPLE_LOOP		(1 << 1)  /* Sample is looped */
231 #define XMP_SAMPLE_LOOP_BIDIR	(1 << 2)  /* Bidirectional sample loop */
232 #define XMP_SAMPLE_LOOP_REVERSE	(1 << 3)  /* Backwards sample loop */
233 #define XMP_SAMPLE_LOOP_FULL	(1 << 4)  /* Play full sample before looping */
234 #define XMP_SAMPLE_SLOOP	(1 << 5)  /* Sample has sustain loop */
235 #define XMP_SAMPLE_SLOOP_BIDIR	(1 << 6)  /* Bidirectional sustain loop */
236 #define XMP_SAMPLE_SYNTH	(1 << 15) /* Data contains synth patch */
237 	int flg;			/* Flags */
238 	unsigned char *data;		/* Sample data */
239 };
240 
241 struct xmp_sequence {
242 	int entry_point;
243 	int duration;
244 };
245 
246 struct xmp_module {
247 	char name[XMP_NAME_SIZE];	/* Module title */
248 	char type[XMP_NAME_SIZE];	/* Module format */
249 	int pat;			/* Number of patterns */
250 	int trk;			/* Number of tracks */
251 	int chn;			/* Tracks per pattern */
252 	int ins;			/* Number of instruments */
253 	int smp;			/* Number of samples */
254 	int spd;			/* Initial speed */
255 	int bpm;			/* Initial BPM */
256 	int len;			/* Module length in patterns */
257 	int rst;			/* Restart position */
258 	int gvl;			/* Global volume */
259 
260 	struct xmp_pattern **xxp;	/* Patterns */
261 	struct xmp_track **xxt;		/* Tracks */
262 	struct xmp_instrument *xxi;	/* Instruments */
263 	struct xmp_sample *xxs;		/* Samples */
264 	struct xmp_channel xxc[XMP_MAX_CHANNELS]; /* Channel info */
265 	unsigned char xxo[XMP_MAX_MOD_LENGTH];	/* Orders */
266 };
267 
268 struct xmp_test_info {
269 	char name[XMP_NAME_SIZE];	/* Module title */
270 	char type[XMP_NAME_SIZE];	/* Module format */
271 };
272 
273 struct xmp_module_info {
274 	unsigned char md5[16];		/* MD5 message digest */
275 	int vol_base;			/* Volume scale */
276 	struct xmp_module *mod;		/* Pointer to module data */
277 	char *comment;			/* Comment text, if any */
278 	int num_sequences;		/* Number of valid sequences */
279 	struct xmp_sequence *seq_data;	/* Pointer to sequence data */
280 };
281 
282 struct xmp_channel_info {	/* Current channel information */
283 	unsigned int period;	/* Sample period (* 4096) */
284 	unsigned int position;	/* Sample position */
285 	short pitchbend;	/* Linear bend from base note*/
286 	unsigned char note;	/* Current base note number */
287 	unsigned char instrument; /* Current instrument number */
288 	unsigned char sample;	/* Current sample number */
289 	unsigned char volume;	/* Current volume */
290 	unsigned char pan;	/* Current stereo pan */
291 	unsigned char reserved;	/* Reserved */
292 	struct xmp_event event;	/* Current track event */
293 };
294 
295 
296 struct xmp_frame_info {			/* Current frame information */
297 	int pos;			/* Current position */
298 	int pattern;			/* Current pattern */
299 	int row;			/* Current row in pattern */
300 	int num_rows;			/* Number of rows in current pattern */
301 	int frame;			/* Current frame */
302 	int speed;			/* Current replay speed */
303 	int bpm;			/* Current bpm */
304 	int time;			/* Current module time in ms */
305 	int total_time;			/* Estimated replay time in ms*/
306 	int frame_time;			/* Frame replay time in us */
307 	void *buffer;			/* Pointer to sound buffer */
308 	int buffer_size;		/* Used buffer size */
309 	int total_size;			/* Total buffer size */
310 	int volume;			/* Current master volume */
311 	int loop_count;			/* Loop counter */
312 	int virt_channels;		/* Number of virtual channels */
313 	int virt_used;			/* Used virtual channels */
314 	int sequence;			/* Current sequence */
315 
316 	struct xmp_channel_info channel_info[XMP_MAX_CHANNELS];
317 };
318 
319 
320 typedef char *xmp_context;
321 
322 LIBXMP_EXPORT extern const char *xmp_version;
323 LIBXMP_EXPORT extern const unsigned int xmp_vercode;
324 
325 LIBXMP_EXPORT xmp_context xmp_create_context  (void);
326 LIBXMP_EXPORT void        xmp_free_context    (xmp_context);
327 #ifdef EDUKE32_DISABLED
328 LIBXMP_EXPORT int         xmp_test_module     (char *, struct xmp_test_info *);
329 LIBXMP_EXPORT int         xmp_load_module     (xmp_context, char *);
330 #endif
331 LIBXMP_EXPORT void        xmp_scan_module     (xmp_context);
332 LIBXMP_EXPORT void        xmp_release_module  (xmp_context);
333 LIBXMP_EXPORT int         xmp_start_player    (xmp_context, int, int);
334 LIBXMP_EXPORT int         xmp_play_frame      (xmp_context);
335 LIBXMP_EXPORT int         xmp_play_buffer     (xmp_context, void *, int, int);
336 LIBXMP_EXPORT void        xmp_get_frame_info  (xmp_context, struct xmp_frame_info *);
337 LIBXMP_EXPORT void        xmp_end_player      (xmp_context);
338 LIBXMP_EXPORT void        xmp_inject_event    (xmp_context, int, struct xmp_event *);
339 LIBXMP_EXPORT void        xmp_get_module_info (xmp_context, struct xmp_module_info *);
340 LIBXMP_EXPORT const char  **xmp_get_format_list (void);
341 LIBXMP_EXPORT int         xmp_next_position   (xmp_context);
342 LIBXMP_EXPORT int         xmp_prev_position   (xmp_context);
343 LIBXMP_EXPORT int         xmp_set_position    (xmp_context, int);
344 LIBXMP_EXPORT int         xmp_set_row         (xmp_context, int);
345 LIBXMP_EXPORT int         xmp_set_tempo_factor(xmp_context, double);
346 LIBXMP_EXPORT void        xmp_stop_module     (xmp_context);
347 LIBXMP_EXPORT void        xmp_restart_module  (xmp_context);
348 LIBXMP_EXPORT int         xmp_seek_time       (xmp_context, int);
349 LIBXMP_EXPORT int         xmp_channel_mute    (xmp_context, int, int);
350 LIBXMP_EXPORT int         xmp_channel_vol     (xmp_context, int, int);
351 LIBXMP_EXPORT int         xmp_set_player      (xmp_context, int, int);
352 LIBXMP_EXPORT int         xmp_get_player      (xmp_context, int);
353 LIBXMP_EXPORT int         xmp_set_instrument_path (xmp_context, char *);
354 LIBXMP_EXPORT int         xmp_load_module_from_memory (xmp_context, void *, long);
355 #ifdef EDUKE32_DISABLED
356 LIBXMP_EXPORT int         xmp_load_module_from_file (xmp_context, void *, long);
357 #endif
358 /* External sample mixer API */
359 LIBXMP_EXPORT int         xmp_start_smix       (xmp_context, int, int);
360 LIBXMP_EXPORT void        xmp_end_smix         (xmp_context);
361 LIBXMP_EXPORT int         xmp_smix_play_instrument(xmp_context, int, int, int, int);
362 LIBXMP_EXPORT int         xmp_smix_play_sample (xmp_context, int, int, int, int);
363 LIBXMP_EXPORT int         xmp_smix_channel_pan (xmp_context, int, int);
364 #ifdef EDUKE32_DISABLED
365 LIBXMP_EXPORT int         xmp_smix_load_sample (xmp_context, int, char *);
366 #endif
367 LIBXMP_EXPORT int         xmp_smix_release_sample (xmp_context, int);
368 
369 #ifdef __cplusplus
370 }
371 #endif
372 
373 #endif	/* XMP_H */
374