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

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

Fix gcc warnings when building with -O1 or -Og flags.
(Thanks Ondřej Hlavatý!)

  • 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
41#include "byteorder.h"
42#include "xm.h"
43#include "trackmod.h"
44#include "types/xm.h"
45
46static char xm_id_text[] = "Extended Module: ";
47
48/** Load XM order list
49 *
50 * @param xm_hdr XM header
51 * @param module Module
52 * @return EOK on success, EIO on format error, ENOMEM if out of memory.
53 */
54static int trackmod_xm_load_order_list(xm_hdr_t *xm_hdr, trackmod_module_t *module)
55{
56 int rc;
57 size_t i;
58
59 /* Order list */
60 module->ord_list_len = uint16_t_le2host(xm_hdr->song_len);
61 if (module->ord_list_len > xm_pat_ord_table_size) {
62 /* Invalid song length */
63 rc = EIO;
64 goto error;
65 }
66
67 module->ord_list = calloc(sizeof(size_t), module->ord_list_len);
68 if (module->ord_list == NULL) {
69 printf("Out of memory.\n");
70 rc = ENOMEM;
71 goto error;
72 }
73
74 for (i = 0; i < module->ord_list_len; i++) {
75 module->ord_list[i] = xm_hdr->pat_ord_table[i];
76 }
77
78 module->restart_pos = uint16_t_le2host(xm_hdr->restart_pos);
79 if (module->restart_pos >= module->ord_list_len) {
80 rc = EIO;
81 goto error;
82 }
83
84 return EOK;
85error:
86 return rc;
87}
88
89/** Decode XM pattern.
90 *
91 * @param data Packed pattern data
92 * @param pattern Pattern to load to
93 * @return EOK on success, EINVAL if there is error in the coded data.
94 */
95static int trackmod_xm_decode_pattern(uint8_t *data, size_t dsize,
96 trackmod_pattern_t *pattern)
97{
98 size_t cells;
99 size_t i;
100 size_t si;
101 uint8_t mask;
102
103 cells = pattern->rows * pattern->channels;
104 si = 0;
105
106 for (i = 0; i < cells; i++) {
107 if (si >= dsize)
108 return EINVAL;
109
110 if ((data[si] & 0x80) != 0) {
111 mask = data[si++] & 0x1f;
112 } else {
113 mask = 0x1f;
114 }
115
116 /* Note */
117 if ((mask & 0x1) != 0) {
118 if (si >= dsize)
119 return EINVAL;
120 pattern->data[i].note = data[si++] & 0x7f;
121 }
122
123 /* Instrument */
124 if ((mask & 0x2) != 0) {
125 if (si >= dsize)
126 return EINVAL;
127 pattern->data[i].instr = data[si++];
128 }
129
130 /* Volume */
131 if ((mask & 0x4) != 0) {
132 if (si >= dsize)
133 return EINVAL;
134 pattern->data[i].volume = data[si++];
135 }
136
137 /* Effect type */
138 if ((mask & 0x8) != 0) {
139 if (si >= dsize)
140 return EINVAL;
141 pattern->data[i].effect = (unsigned)data[si++] << 8;
142 }
143
144 /* Effect parameter */
145 if ((mask & 0x10) != 0) {
146 if (si >= dsize)
147 return EINVAL;
148 pattern->data[i].effect |= data[si++];
149 }
150 }
151
152 /* Note: Ignoring any extra data */
153
154 return EOK;
155}
156
157/** Load XM patterns
158 *
159 * @param file File
160 * @param module Module
161 * @return EOK on success, EIO on format error, ENOMEM if out of memory.
162 */
163static int trackmod_xm_load_patterns(FILE *f, trackmod_module_t *module)
164{
165 size_t i;
166 size_t hdr_size;
167 uint8_t pack_type;
168 size_t rows;
169 size_t data_size;
170 ssize_t nread;
171 xm_pattern_t pattern;
172 uint8_t *buf = NULL;
173 long seek_amount;
174 int rc;
175
176 module->pattern = calloc(sizeof(trackmod_pattern_t), module->patterns);
177 if (module->pattern == NULL) {
178 rc = ENOMEM;
179 goto error;
180 }
181
182 for (i = 0; i < module->patterns; i++) {
183 rc = fread(&pattern, 1, sizeof(xm_pattern_t), f);
184 if (rc != sizeof(xm_pattern_t)) {
185 rc = EIO;
186 goto error;
187 }
188
189 hdr_size = (size_t)uint32_t_le2host(pattern.hdr_size);
190 pack_type = pattern.pack_type;
191 rows = uint16_t_le2host(pattern.rows);
192 data_size = uint16_t_le2host(pattern.data_size);
193
194 if (pack_type != 0) {
195 rc = EIO;
196 goto error;
197 }
198
199 /* Jump to end of pattern header */
200 seek_amount = (long)hdr_size - (long)sizeof(xm_pattern_t);
201 if (fseek(f, seek_amount, SEEK_CUR) < 0) {
202 rc = EIO;
203 goto error;
204 }
205
206 module->pattern[i].rows = rows;
207 module->pattern[i].channels = module->channels;
208 module->pattern[i].data = calloc(sizeof(trackmod_cell_t),
209 rows * module->channels);
210
211 if (module->pattern[i].data == NULL) {
212 rc = ENOMEM;
213 goto error;
214 }
215
216 buf = calloc(1, data_size);
217 if (buf == NULL) {
218 rc = ENOMEM;
219 goto error;
220 }
221
222 nread = fread(buf, 1, data_size, f);
223 if (nread != (ssize_t)data_size) {
224 rc = EIO;
225 goto error;
226 }
227
228 rc = trackmod_xm_decode_pattern(buf, data_size,
229 &module->pattern[i]);
230 if (rc != EOK)
231 goto error;
232
233 free(buf);
234 buf = NULL;
235 }
236
237 return EOK;
238error:
239 free(buf);
240 return rc;
241}
242
243/** Decode XM sample data.
244 *
245 * XM sample data is delta-encoded. Undo the delta encoding and convert
246 * byte order.
247 */
248static void trackmod_xm_decode_sample_data(trackmod_sample_t *sample)
249{
250 size_t i;
251 int8_t *i8p;
252 int16_t *i16p;
253 int8_t cur8;
254 int16_t cur16;
255
256 if (sample->bytes_smp == 1) {
257 cur8 = 0;
258 i8p = (int8_t *)sample->data;
259
260 for (i = 0; i < sample->length; i++) {
261 cur8 = cur8 + i8p[i];
262 i8p[i] = cur8;
263 }
264 } else {
265 cur16 = 0;
266 i16p = (int16_t *)sample->data;
267
268 for (i = 0; i < sample->length; i++) {
269 cur16 = cur16 + (int16_t)uint16_t_le2host(i16p[i]);
270 i16p[i] = cur16;
271 }
272 }
273}
274
275/** Load XM instruments
276 *
277 * @param file File
278 * @param module Module
279 * @return EOK on success, EIO on format error, ENOMEM if out of memory.
280 */
281static int trackmod_xm_load_instruments(xm_hdr_t *xm_hdr, FILE *f,
282 trackmod_module_t *module)
283{
284 size_t i, j;
285 xm_instr_t instr;
286 xm_instr_ext_t instrx;
287 xm_smp_t smp;
288 size_t samples;
289 size_t instr_size;
290 size_t smp_size;
291 size_t smp_hdr_size = 0; /* GCC false alarm on uninitialized */
292 ssize_t nread;
293 uint8_t ltype;
294 trackmod_sample_t *sample;
295 void *smp_data;
296 long pos;
297 int rc;
298
299 module->instrs = uint16_t_le2host(xm_hdr->instruments);
300 module->instr = calloc(module->instrs, sizeof(trackmod_instr_t));
301 if (module->instr == NULL)
302 return ENOMEM;
303
304 for (i = 0; i < module->instrs; i++) {
305 pos = ftell(f);
306 rc = fread(&instr, 1, sizeof(xm_instr_t), f);
307 if (rc != sizeof(xm_instr_t)) {
308 rc = EIO;
309 goto error;
310 }
311
312 samples = uint16_t_le2host(instr.samples);
313 instr_size = (size_t)uint32_t_le2host(instr.size);
314
315 if (samples > 0) {
316 rc = fread(&instrx, 1, sizeof(xm_instr_ext_t), f);
317 if (rc != sizeof(xm_instr_ext_t)) {
318 rc = EIO;
319 goto error;
320 }
321
322 smp_hdr_size = uint32_t_le2host(instrx.smp_hdr_size);
323
324 for (j = 0; j < xm_smp_note_size; j++) {
325 module->instr[i].key_smp[j] =
326 instrx.smp_note[j];
327 }
328
329 module->instr[i].samples = samples;
330 module->instr[i].sample = calloc(samples,
331 sizeof(trackmod_sample_t));
332 if (module->instr[i].sample == NULL) {
333 rc = ENOMEM;
334 goto error;
335 }
336 }
337
338 if (fseek(f, pos + instr_size, SEEK_SET) < 0) {
339 rc = EIO;
340 goto error;
341 }
342
343 for (j = 0; j < samples; j++) {
344 sample = &module->instr[i].sample[j];
345 pos = ftell(f);
346
347 rc = fread(&smp, 1, sizeof(xm_smp_t), f);
348 if (rc != sizeof(xm_smp_t)) {
349 rc = EIO;
350 goto error;
351 }
352
353 smp_size = (size_t)uint32_t_le2host(smp.length);
354
355 smp_data = calloc(smp_size, 1);
356 if (smp_data == NULL) {
357 rc = ENOMEM;
358 goto error;
359 }
360
361 if (fseek(f, pos + smp_hdr_size, SEEK_SET) < 0) {
362 rc = EIO;
363 goto error;
364 }
365
366 nread = fread(smp_data, 1, smp_size, f);
367 if (nread != (ssize_t)smp_size) {
368 rc = EIO;
369 goto error;
370 }
371
372 if (smp.smp_type & (1 << xmst_16_bit)) {
373 sample->bytes_smp = 2;
374 } else {
375 sample->bytes_smp = 1;
376 }
377
378 sample->data = smp_data;
379 sample->length = smp_size / sample->bytes_smp;
380
381 ltype = smp.smp_type & 0x3;
382 switch (ltype) {
383 case xmsl_no_loop:
384 sample->loop_type = tl_no_loop;
385 break;
386 case xmsl_forward_loop:
387 sample->loop_type = tl_forward_loop;
388 break;
389 case xmsl_pingpong_loop:
390 sample->loop_type = tl_pingpong_loop;
391 break;
392 default:
393 rc = EIO;
394 goto error;
395 }
396
397 sample->loop_start =
398 uint32_t_le2host(smp.loop_start) / sample->bytes_smp;
399 sample->loop_len =
400 uint32_t_le2host(smp.loop_len) / sample->bytes_smp;
401 sample->def_vol = 0x40;
402 sample->rel_note = smp.rel_note;
403 sample->finetune = smp.finetune / 2;
404
405 trackmod_xm_decode_sample_data(sample);
406 }
407 }
408
409 return EOK;
410error:
411 return rc;
412}
413
414/** Load extended module.
415 *
416 * @param fname File name
417 * @param rmodule Place to store pointer to newly loaded module.
418 * @return EOK on success, ENONEM if out of memory, EIO on I/O error
419 * or if any error is found in the format of the file.
420 */
421int trackmod_xm_load(char *fname, trackmod_module_t **rmodule)
422{
423 FILE *f = NULL;
424 trackmod_module_t *module = NULL;
425 xm_hdr_t xm_hdr;
426 size_t nread;
427 size_t hdr_size;
428 int rc;
429
430 f = fopen(fname, "rb");
431 if (f == NULL) {
432 printf("Error opening file.\n");
433 rc = EIO;
434 goto error;
435 }
436
437 nread = fread(&xm_hdr, 1, sizeof(xm_hdr_t), f);
438 if (nread < sizeof(xm_hdr_t)) {
439 printf("File too small.\n");
440 rc = EIO;
441 goto error;
442 }
443
444 if (memcmp(xm_hdr.id_text, xm_id_text, xm_id_text_size) != 0) {
445 rc = EIO;
446 goto error;
447 }
448
449 module = trackmod_module_new();
450 if (module == NULL) {
451 printf("Out of memory.\n");
452 rc = ENOMEM;
453 goto error;
454 }
455
456 module->channels = uint16_t_le2host(xm_hdr.channels);
457 module->patterns = uint16_t_le2host(xm_hdr.patterns);
458 module->ord_list_len = uint16_t_le2host(xm_hdr.song_len);
459
460 hdr_size = (size_t)uint32_t_le2host(xm_hdr.hdr_size)
461 + offsetof(xm_hdr_t, hdr_size);
462
463 module->def_bpm = uint16_t_le2host(xm_hdr.def_bpm);
464 module->def_tpr = uint16_t_le2host(xm_hdr.def_tempo);
465
466 /* Jump to end of file header */
467 if (fseek(f, hdr_size, SEEK_SET) < 0) {
468 rc = EIO;
469 goto error;
470 }
471
472 rc = trackmod_xm_load_order_list(&xm_hdr, module);
473 if (rc != EOK)
474 goto error;
475
476 rc = trackmod_xm_load_patterns(f, module);
477 if (rc != EOK)
478 goto error;
479
480 rc = trackmod_xm_load_instruments(&xm_hdr, f, module);
481 if (rc != EOK)
482 goto error;
483
484 (void) fclose(f);
485
486 *rmodule = module;
487 return EOK;
488error:
489 if (module != NULL)
490 trackmod_module_destroy(module);
491 if (f != NULL)
492 (void) fclose(f);
493 return rc;
494}
495
496/** @}
497 */
Note: See TracBrowser for help on using the repository browser.