source: mainline/uspace/lib/trackmod/xm.c@ d648e83

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d648e83 was 16bfcd3, checked in by jzr <zarevucky.jiri@…>, 8 years ago

Fix up headers.

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Copyright (c) 2014 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup trackmod
30 * @{
31 */
32/**
33 * @file Extended Module (.xm).
34 */
35
36#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <mem.h>
40#include <types/common.h>
41
42#include "byteorder.h"
43#include "xm.h"
44#include "trackmod.h"
45#include "types/xm.h"
46
47static char xm_id_text[] = "Extended Module: ";
48
49/** Load XM order list
50 *
51 * @param xm_hdr XM header
52 * @param module Module
53 * @return EOK on success, EIO on format error, ENOMEM if out of memory.
54 */
55static int trackmod_xm_load_order_list(xm_hdr_t *xm_hdr, trackmod_module_t *module)
56{
57 int rc;
58 size_t i;
59
60 /* Order list */
61 module->ord_list_len = uint16_t_le2host(xm_hdr->song_len);
62 if (module->ord_list_len > xm_pat_ord_table_size) {
63 /* Invalid song length */
64 rc = EIO;
65 goto error;
66 }
67
68 module->ord_list = calloc(sizeof(size_t), module->ord_list_len);
69 if (module->ord_list == NULL) {
70 printf("Out of memory.\n");
71 rc = ENOMEM;
72 goto error;
73 }
74
75 for (i = 0; i < module->ord_list_len; i++) {
76 module->ord_list[i] = xm_hdr->pat_ord_table[i];
77 }
78
79 module->restart_pos = uint16_t_le2host(xm_hdr->restart_pos);
80 if (module->restart_pos >= module->ord_list_len) {
81 rc = EIO;
82 goto error;
83 }
84
85 return EOK;
86error:
87 return rc;
88}
89
90/** Decode XM pattern.
91 *
92 * @param data Packed pattern data
93 * @param pattern Pattern to load to
94 * @return EOK on success, EINVAL if there is error in the coded data.
95 */
96static int trackmod_xm_decode_pattern(uint8_t *data, size_t dsize,
97 trackmod_pattern_t *pattern)
98{
99 size_t cells;
100 size_t i;
101 size_t si;
102 uint8_t mask;
103
104 cells = pattern->rows * pattern->channels;
105 si = 0;
106
107 for (i = 0; i < cells; i++) {
108 if (si >= dsize)
109 return EINVAL;
110
111 if ((data[si] & 0x80) != 0) {
112 mask = data[si++] & 0x1f;
113 } else {
114 mask = 0x1f;
115 }
116
117 /* Note */
118 if ((mask & 0x1) != 0) {
119 if (si >= dsize)
120 return EINVAL;
121 pattern->data[i].note = data[si++] & 0x7f;
122 }
123
124 /* Instrument */
125 if ((mask & 0x2) != 0) {
126 if (si >= dsize)
127 return EINVAL;
128 pattern->data[i].instr = data[si++];
129 }
130
131 /* Volume */
132 if ((mask & 0x4) != 0) {
133 if (si >= dsize)
134 return EINVAL;
135 pattern->data[i].volume = data[si++];
136 }
137
138 /* Effect type */
139 if ((mask & 0x8) != 0) {
140 if (si >= dsize)
141 return EINVAL;
142 pattern->data[i].effect = (unsigned)data[si++] << 8;
143 }
144
145 /* Effect parameter */
146 if ((mask & 0x10) != 0) {
147 if (si >= dsize)
148 return EINVAL;
149 pattern->data[i].effect |= data[si++];
150 }
151 }
152
153 /* Note: Ignoring any extra data */
154
155 return EOK;
156}
157
158/** Load XM patterns
159 *
160 * @param file File
161 * @param module Module
162 * @return EOK on success, EIO on format error, ENOMEM if out of memory.
163 */
164static int trackmod_xm_load_patterns(FILE *f, trackmod_module_t *module)
165{
166 size_t i;
167 size_t hdr_size;
168 uint8_t pack_type;
169 size_t rows;
170 size_t data_size;
171 ssize_t nread;
172 xm_pattern_t pattern;
173 uint8_t *buf = NULL;
174 long seek_amount;
175 int rc;
176
177 module->pattern = calloc(sizeof(trackmod_pattern_t), module->patterns);
178 if (module->pattern == NULL) {
179 rc = ENOMEM;
180 goto error;
181 }
182
183 for (i = 0; i < module->patterns; i++) {
184 rc = fread(&pattern, 1, sizeof(xm_pattern_t), f);
185 if (rc != sizeof(xm_pattern_t)) {
186 rc = EIO;
187 goto error;
188 }
189
190 hdr_size = (size_t)uint32_t_le2host(pattern.hdr_size);
191 pack_type = pattern.pack_type;
192 rows = uint16_t_le2host(pattern.rows);
193 data_size = uint16_t_le2host(pattern.data_size);
194
195 if (pack_type != 0) {
196 rc = EIO;
197 goto error;
198 }
199
200 /* Jump to end of pattern header */
201 seek_amount = (long)hdr_size - (long)sizeof(xm_pattern_t);
202 if (fseek(f, seek_amount, SEEK_CUR) < 0) {
203 rc = EIO;
204 goto error;
205 }
206
207 module->pattern[i].rows = rows;
208 module->pattern[i].channels = module->channels;
209 module->pattern[i].data = calloc(sizeof(trackmod_cell_t),
210 rows * module->channels);
211
212 if (module->pattern[i].data == NULL) {
213 rc = ENOMEM;
214 goto error;
215 }
216
217 buf = calloc(1, data_size);
218 if (buf == NULL) {
219 rc = ENOMEM;
220 goto error;
221 }
222
223 nread = fread(buf, 1, data_size, f);
224 if (nread != (ssize_t)data_size) {
225 rc = EIO;
226 goto error;
227 }
228
229 rc = trackmod_xm_decode_pattern(buf, data_size,
230 &module->pattern[i]);
231 if (rc != EOK)
232 goto error;
233
234 free(buf);
235 buf = NULL;
236 }
237
238 return EOK;
239error:
240 free(buf);
241 return rc;
242}
243
244/** Decode XM sample data.
245 *
246 * XM sample data is delta-encoded. Undo the delta encoding and convert
247 * byte order.
248 */
249static void trackmod_xm_decode_sample_data(trackmod_sample_t *sample)
250{
251 size_t i;
252 int8_t *i8p;
253 int16_t *i16p;
254 int8_t cur8;
255 int16_t cur16;
256
257 if (sample->bytes_smp == 1) {
258 cur8 = 0;
259 i8p = (int8_t *)sample->data;
260
261 for (i = 0; i < sample->length; i++) {
262 cur8 = cur8 + i8p[i];
263 i8p[i] = cur8;
264 }
265 } else {
266 cur16 = 0;
267 i16p = (int16_t *)sample->data;
268
269 for (i = 0; i < sample->length; i++) {
270 cur16 = cur16 + (int16_t)uint16_t_le2host(i16p[i]);
271 i16p[i] = cur16;
272 }
273 }
274}
275
276/** Load XM instruments
277 *
278 * @param file File
279 * @param module Module
280 * @return EOK on success, EIO on format error, ENOMEM if out of memory.
281 */
282static int trackmod_xm_load_instruments(xm_hdr_t *xm_hdr, FILE *f,
283 trackmod_module_t *module)
284{
285 size_t i, j;
286 xm_instr_t instr;
287 xm_instr_ext_t instrx;
288 xm_smp_t smp;
289 size_t samples;
290 size_t instr_size;
291 size_t smp_size;
292 size_t smp_hdr_size = 0; /* GCC false alarm on uninitialized */
293 ssize_t nread;
294 uint8_t ltype;
295 trackmod_sample_t *sample;
296 void *smp_data;
297 long pos;
298 int rc;
299
300 module->instrs = uint16_t_le2host(xm_hdr->instruments);
301 module->instr = calloc(module->instrs, sizeof(trackmod_instr_t));
302 if (module->instr == NULL)
303 return ENOMEM;
304
305 for (i = 0; i < module->instrs; i++) {
306 pos = ftell(f);
307 rc = fread(&instr, 1, sizeof(xm_instr_t), f);
308 if (rc != sizeof(xm_instr_t)) {
309 rc = EIO;
310 goto error;
311 }
312
313 samples = uint16_t_le2host(instr.samples);
314 instr_size = (size_t)uint32_t_le2host(instr.size);
315
316 if (samples > 0) {
317 rc = fread(&instrx, 1, sizeof(xm_instr_ext_t), f);
318 if (rc != sizeof(xm_instr_ext_t)) {
319 rc = EIO;
320 goto error;
321 }
322
323 smp_hdr_size = uint32_t_le2host(instrx.smp_hdr_size);
324
325 for (j = 0; j < xm_smp_note_size; j++) {
326 module->instr[i].key_smp[j] =
327 instrx.smp_note[j];
328 }
329
330 module->instr[i].samples = samples;
331 module->instr[i].sample = calloc(samples,
332 sizeof(trackmod_sample_t));
333 if (module->instr[i].sample == NULL) {
334 rc = ENOMEM;
335 goto error;
336 }
337 }
338
339 if (fseek(f, pos + instr_size, SEEK_SET) < 0) {
340 rc = EIO;
341 goto error;
342 }
343
344 for (j = 0; j < samples; j++) {
345 sample = &module->instr[i].sample[j];
346 pos = ftell(f);
347
348 rc = fread(&smp, 1, sizeof(xm_smp_t), f);
349 if (rc != sizeof(xm_smp_t)) {
350 rc = EIO;
351 goto error;
352 }
353
354 smp_size = (size_t)uint32_t_le2host(smp.length);
355
356 smp_data = calloc(smp_size, 1);
357 if (smp_data == NULL) {
358 rc = ENOMEM;
359 goto error;
360 }
361
362 if (fseek(f, pos + smp_hdr_size, SEEK_SET) < 0) {
363 rc = EIO;
364 goto error;
365 }
366
367 nread = fread(smp_data, 1, smp_size, f);
368 if (nread != (ssize_t)smp_size) {
369 rc = EIO;
370 goto error;
371 }
372
373 if (smp.smp_type & (1 << xmst_16_bit)) {
374 sample->bytes_smp = 2;
375 } else {
376 sample->bytes_smp = 1;
377 }
378
379 sample->data = smp_data;
380 sample->length = smp_size / sample->bytes_smp;
381
382 ltype = smp.smp_type & 0x3;
383 switch (ltype) {
384 case xmsl_no_loop:
385 sample->loop_type = tl_no_loop;
386 break;
387 case xmsl_forward_loop:
388 sample->loop_type = tl_forward_loop;
389 break;
390 case xmsl_pingpong_loop:
391 sample->loop_type = tl_pingpong_loop;
392 break;
393 default:
394 rc = EIO;
395 goto error;
396 }
397
398 sample->loop_start =
399 uint32_t_le2host(smp.loop_start) / sample->bytes_smp;
400 sample->loop_len =
401 uint32_t_le2host(smp.loop_len) / sample->bytes_smp;
402 sample->def_vol = 0x40;
403 sample->rel_note = smp.rel_note;
404 sample->finetune = smp.finetune / 2;
405
406 trackmod_xm_decode_sample_data(sample);
407 }
408 }
409
410 return EOK;
411error:
412 return rc;
413}
414
415/** Load extended module.
416 *
417 * @param fname File name
418 * @param rmodule Place to store pointer to newly loaded module.
419 * @return EOK on success, ENONEM if out of memory, EIO on I/O error
420 * or if any error is found in the format of the file.
421 */
422int trackmod_xm_load(char *fname, trackmod_module_t **rmodule)
423{
424 FILE *f = NULL;
425 trackmod_module_t *module = NULL;
426 xm_hdr_t xm_hdr;
427 size_t nread;
428 size_t hdr_size;
429 int rc;
430
431 f = fopen(fname, "rb");
432 if (f == NULL) {
433 printf("Error opening file.\n");
434 rc = EIO;
435 goto error;
436 }
437
438 nread = fread(&xm_hdr, 1, sizeof(xm_hdr_t), f);
439 if (nread < sizeof(xm_hdr_t)) {
440 printf("File too small.\n");
441 rc = EIO;
442 goto error;
443 }
444
445 if (memcmp(xm_hdr.id_text, xm_id_text, xm_id_text_size) != 0) {
446 rc = EIO;
447 goto error;
448 }
449
450 module = trackmod_module_new();
451 if (module == NULL) {
452 printf("Out of memory.\n");
453 rc = ENOMEM;
454 goto error;
455 }
456
457 module->channels = uint16_t_le2host(xm_hdr.channels);
458 module->patterns = uint16_t_le2host(xm_hdr.patterns);
459 module->ord_list_len = uint16_t_le2host(xm_hdr.song_len);
460
461 hdr_size = (size_t)uint32_t_le2host(xm_hdr.hdr_size)
462 + offsetof(xm_hdr_t, hdr_size);
463
464 module->def_bpm = uint16_t_le2host(xm_hdr.def_bpm);
465 module->def_tpr = uint16_t_le2host(xm_hdr.def_tempo);
466
467 /* Jump to end of file header */
468 if (fseek(f, hdr_size, SEEK_SET) < 0) {
469 rc = EIO;
470 goto error;
471 }
472
473 rc = trackmod_xm_load_order_list(&xm_hdr, module);
474 if (rc != EOK)
475 goto error;
476
477 rc = trackmod_xm_load_patterns(f, module);
478 if (rc != EOK)
479 goto error;
480
481 rc = trackmod_xm_load_instruments(&xm_hdr, f, module);
482 if (rc != EOK)
483 goto error;
484
485 (void) fclose(f);
486
487 *rmodule = module;
488 return EOK;
489error:
490 if (module != NULL)
491 trackmod_module_destroy(module);
492 if (f != NULL)
493 (void) fclose(f);
494 return rc;
495}
496
497/** @}
498 */
Note: See TracBrowser for help on using the repository browser.