xref: /btstack/3rd-party/hxcmod-player/hxcmod.h (revision 5d4d8cc7b1d35a90bbd6d5ffd2d3050b2bfc861c)
1 ///////////////////////////////////////////////////////////////////////////////////
2 //-------------------------------------------------------------------------------//
3 //-------------------------------------------------------------------------------//
4 //-----------H----H--X----X-----CCCCC----22222----0000-----0000------11----------//
5 //----------H----H----X-X-----C--------------2---0----0---0----0--1--1-----------//
6 //---------HHHHHH-----X------C----------22222---0----0---0----0-----1------------//
7 //--------H----H----X--X----C----------2-------0----0---0----0-----1-------------//
8 //-------H----H---X-----X---CCCCC-----222222----0000-----0000----1111------------//
9 //-------------------------------------------------------------------------------//
10 //----------------------------------------------------- http://hxc2001.free.fr --//
11 ///////////////////////////////////////////////////////////////////////////////////
12 // File : hxcmod.h
13 // Contains: a tiny mod player
14 //
15 // Written by: Jean Francois DEL NERO
16 //
17 // Change History (most recent first):
18 ///////////////////////////////////////////////////////////////////////////////////
19 #ifndef MODPLAY_DEF
20 #define MODPLAY_DEF
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #ifndef HXCMOD_SLOW_TARGET
27 	#define HXCMOD_STATE_REPORT_SUPPORT 1
28 	#define HXCMOD_OUTPUT_FILTER 1
29 	#define HXCMOD_OUTPUT_STEREO_MIX 1
30 	#define HXCMOD_CLIPPING_CHECK 1
31 #endif
32 
33 // Warning : The following option
34 // required 32KB of additional RAM :
35 // #define HXCMOD_USE_PRECALC_VOLUME_TABLE 1
36 
37 // Basic type
38 typedef unsigned char   muchar;
39 typedef signed   char   mchar;
40 typedef unsigned short  muint;
41 typedef          short  mint;
42 typedef unsigned long   mulong;
43 
44 #ifdef HXCMOD_16BITS_TARGET
45 	typedef unsigned short  mssize;
46 #else
47 	typedef unsigned long   mssize;
48 #endif
49 
50 #ifdef HXCMOD_8BITS_OUTPUT
51 	#ifdef HXCMOD_UNSIGNED_OUTPUT
52 	typedef unsigned char  msample;
53 	#else
54 	typedef signed char    msample;
55 	#endif
56 #else
57 	#ifdef HXCMOD_UNSIGNED_OUTPUT
58 	typedef unsigned short msample;
59 	#else
60 	typedef signed short   msample;
61 	#endif
62 #endif
63 
64 #ifdef HXCMOD_MAXCHANNELS
65 	#define NUMMAXCHANNELS HXCMOD_MAXCHANNELS
66 #else
67 	#define NUMMAXCHANNELS 32
68 #endif
69 
70 #define MAXNOTES 12*12
71 
72 //
73 // MOD file structures
74 //
75 
76 #pragma pack(1)
77 
78 typedef struct {
79 	muchar  name[22];
80 	muint   length;
81 	muchar  finetune;
82 	muchar  volume;
83 	muint   reppnt;
84 	muint   replen;
85 } sample;
86 
87 typedef struct {
88 	muchar  sampperiod;
89 	muchar  period;
90 	muchar  sampeffect;
91 	muchar  effect;
92 } note;
93 
94 typedef struct {
95 	muchar  title[20];
96 	sample  samples[31];
97 	muchar  length;
98 	muchar  protracker;
99 	muchar  patterntable[128];
100 	muchar  signature[4];
101 	muchar  speed;
102 } module;
103 
104 #pragma pack()
105 
106 //
107 // HxCMod Internal structures
108 //
109 typedef struct {
110 	mchar * sampdata;
111 	muint   length;
112 	muint   reppnt;
113 	muint   replen;
114 	muchar  sampnum;
115 
116 	mchar * nxt_sampdata;
117 	muint   nxt_length;
118 	muint   nxt_reppnt;
119 	muint   nxt_replen;
120 	muchar  update_nxt_repeat;
121 
122 	mchar * dly_sampdata;
123 	muint   dly_length;
124 	muint   dly_reppnt;
125 	muint   dly_replen;
126 	muchar  note_delay;
127 
128 	mchar * lst_sampdata;
129 	muint   lst_length;
130 	muint   lst_reppnt;
131 	muint   lst_replen;
132 	muchar  retrig_cnt;
133 	muchar  retrig_param;
134 
135 	muint   funkoffset;
136 	mint    funkspeed;
137 
138 	mint    glissando;
139 
140 	mulong  samppos;
141 	mulong  sampinc;
142 	muint   period;
143 	muchar  volume;
144 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
145 	mint  * volume_table;
146 #endif
147 	muchar  effect;
148 	muchar  parameffect;
149 	muint   effect_code;
150 
151 	mulong  last_set_offset;
152 
153 	mint    decalperiod;
154 	mint    portaspeed;
155 	mint    portaperiod;
156 	mint    vibraperiod;
157 	mint    Arpperiods[3];
158 	muchar  ArpIndex;
159 
160 	muchar  volumeslide;
161 
162 	muchar  vibraparam;
163 	muchar  vibrapointeur;
164 
165 	muchar  finetune;
166 
167 	muchar  cut_param;
168 
169 	muint   patternloopcnt;
170 	muint   patternloopstartpoint;
171 } hxcmod_channel_t;
172 
173 typedef struct {
174 	module  song;
175 	mchar * sampledata[31];
176 	note *  patterndata[128];
177 
178 #ifdef HXCMOD_16BITS_TARGET
179 	muint   playrate;
180 #else
181 	mulong  playrate;
182 #endif
183 
184 	muint   tablepos;
185 	muint   patternpos;
186 	muint   patterndelay;
187 	muchar  jump_loop_effect;
188 	muchar  bpm;
189 
190 #ifdef HXCMOD_16BITS_TARGET
191 	muint   patternticks;
192 	muint   patterntickse;
193 	muint   patternticksaim;
194 	muint   patternticksem;
195 	muint   tick_cnt;
196 #else
197 	mulong  patternticks;
198 	mulong  patterntickse;
199 	mulong  patternticksaim;
200 	mulong  patternticksem;
201 	mulong  tick_cnt;
202 #endif
203 
204 	mulong  sampleticksconst;
205 
206 	hxcmod_channel_t channels[NUMMAXCHANNELS];
207 
208 	muint   number_of_channels;
209 
210 	muint   mod_loaded;
211 
212 	mint    last_r_sample;
213 	mint    last_l_sample;
214 
215 	mint    stereo;
216 	mint    stereo_separation;
217 	mint    bits;
218 	mint    filter;
219 
220 #ifdef EFFECTS_USAGE_STATE
221 	int effects_event_counts[32];
222 #endif
223 
224 #ifdef HXCMOD_USE_PRECALC_VOLUME_TABLE
225 	mint    precalc_volume_array[65*256];
226 	mint  * volume_selection_table[65];
227 #endif
228 
229 } modcontext;
230 
231 //
232 // Player states structures
233 //
234 typedef struct track_state_
235 {
236 	unsigned char instrument_number;
237 	unsigned short cur_period;
238 	unsigned char  cur_volume;
239 	unsigned short cur_effect;
240 	unsigned short cur_parameffect;
241 }track_state;
242 
243 typedef struct tracker_state_
244 {
245 	int number_of_tracks;
246 	int bpm;
247 	int speed;
248 	int cur_pattern;
249 	int cur_pattern_pos;
250 	int cur_pattern_table_pos;
251 	unsigned int buf_index;
252 	track_state tracks[NUMMAXCHANNELS];
253 }tracker_state;
254 
255 typedef struct tracker_state_instrument_
256 {
257 	char name[22];
258 	int active;
259 }tracker_state_instrument;
260 
261 typedef struct tracker_buffer_state_
262 {
263 	int nb_max_of_state;
264 	int nb_of_state;
265 	int cur_rd_index;
266 	int sample_step;
267 	char name[64];
268 	tracker_state_instrument instruments[31];
269 	tracker_state * track_state_buf;
270 }tracker_buffer_state;
271 
272 ///////////////////////////////////////////////////////////////////////////////////
273 // HxCMOD Core API:
274 // -------------------------------------------
275 // int  hxcmod_init(modcontext * modctx)
276 //
277 // - Initialize the modcontext buffer. Must be called before doing anything else.
278 //   Return 1 if success. 0 in case of error.
279 // -------------------------------------------
280 // int  hxcmod_load( modcontext * modctx, void * mod_data, int mod_data_size )
281 //
282 // - "Load" a MOD from memory (from "mod_data" with size "mod_data_size").
283 //   Return 1 if success. 0 in case of error.
284 // -------------------------------------------
285 // void hxcmod_fillbuffer( modcontext * modctx, unsigned short * outbuffer, mssize nbsample, tracker_buffer_state * trkbuf )
286 //
287 // - Generate and return the next samples chunk to outbuffer.
288 //   nbsample specify the number of stereo 16bits samples you want.
289 //   The output format is signed 44100Hz 16-bit Stereo PCM samples.
290 //   The output buffer size in byte must be equal to ( nbsample * 2 * 2 ).
291 //   The optional trkbuf parameter can be used to get detailed status of the player. Put NULL/0 is unused.
292 // -------------------------------------------
293 // void hxcmod_unload( modcontext * modctx )
294 //
295 // - "Unload" / clear the player status.
296 // -------------------------------------------
297 ///////////////////////////////////////////////////////////////////////////////////
298 
299 int  hxcmod_init( modcontext * modctx );
300 int  hxcmod_setcfg( modcontext * modctx, int samplerate, int stereo_separation, int filter );
301 int  hxcmod_load( modcontext * modctx, void * mod_data, int mod_data_size );
302 void hxcmod_fillbuffer( modcontext * modctx, msample * outbuffer, mssize nbsample, tracker_buffer_state * trkbuf );
303 void hxcmod_unload( modcontext * modctx );
304 
305 #ifdef __cplusplus
306 }
307 #endif
308 
309 #endif /* MODPLAY_DEF */
310