annotate tools/xm2jss.c @ 1246:7bd50496c9ec

Work on improved module conversion.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 07 Mar 2015 18:19:43 +0200
parents 2a0488078b78
children c44fcdc1c2e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * xm2jss - Convert XM module to JSSMOD
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2006-2009 Tecnic Software productions (TNSP)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include <stdio.h>
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <errno.h>
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "jss.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "jssmod.h"
184
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
12 #include "jssplr.h"
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include "dmlib.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include "dmargs.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #include "dmres.h"
1241
5e2b90e3e4bc Fix hasData handling and include dmfile.h header.
Matti Hamalainen <ccr@tnsp.org>
parents: 1237
diff changeset
16 #include "dmfile.h"
285
245b15cd1919 Don't link libSDL uselessly to utilities that do not actually use it.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
17 #include "dmmutex.h"
245b15cd1919 Don't link libSDL uselessly to utilities that do not actually use it.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
18
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
184
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
20 #define jmpNMODEffectTable (36)
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
21 static const char jmpMODEffectTable[jmpNMODEffectTable] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1099
f309a382d6e7 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1097
diff changeset
22 #define JM_SAMPLE_MODE_MASK (jsampFlipSign | jsampSwapEndianess | jsampSplit | jsampDelta)
184
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
23
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
300
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
25 char *optInFilename = NULL, *optOutFilename = NULL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 BOOL optIgnoreErrors = FALSE,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 optStripExtInstr = FALSE,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 optStripInstr = FALSE,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 optStripSamples = FALSE,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 optOptimize = FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 int optPatternMode = PATMODE_COMP_HORIZ,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 optSampMode16 = jsampDelta,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 optSampMode8 = jsampFlipSign | jsampDelta;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 static const char* patModeTable[PATMODE_LAST] =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 "Raw horizontal",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 "Compressed horizontal (similar to XM modules)",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 "Raw vertical",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 "Compressed vertical",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 "Raw vertical for each element",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
860
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 856
diff changeset
47 static const DMOptArg optList[] =
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 856
diff changeset
48 {
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 { 0, '?', "help", "Show this help", OPT_NONE },
337
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
50 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
51 { 2, 'i', "ignore", "Ignore errors", OPT_NONE },
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
52 { 3, 'p', "patterns", "Pattern storage mode", OPT_ARGREQ },
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
53 { 4, 'E', "strip-ext-instr","Strip ext. instruments (implies -I -S)", OPT_NONE },
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
54 { 5, 'I', "strip-instr", "Strip instruments (implies -S)", OPT_NONE },
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
55 { 6, 'S', "strip-samples", "Strip instr. sampledata", OPT_NONE },
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 { 7, '8', "smode8", "8-bit sample conversion flags", OPT_ARGREQ },
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 { 8, '1', "smode16", "16-bit sample conversion flags", OPT_ARGREQ },
337
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
58 { 9, 'O', "optimize", "Optimize module", OPT_NONE },
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 const int optListN = sizeof(optList) / sizeof(optList[0]);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 void argShowHelp()
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 int i;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67
337
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
68 dmPrintBanner(stdout, dmProgName, "[options] <input.xm> <output.jmod>");
860
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 856
diff changeset
69 dmArgsPrintHelp(stdout, optList, optListN, 0);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 printf("\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 "Pattern storage modes:\n");
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
73
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 for (i = 1; i < PATMODE_LAST; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 printf(" %d = %s\n", i, patModeTable[i-1]);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
76
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 printf(
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 "\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 "Sample data conversion flags (summative):\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 " 1 = Delta encoding (DEF 8 & 16)\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 " 2 = Flip signedness (DEF 8)\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 " 4 = Swap endianess (affects 16-bit only)\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 " 8 = Split and de-interleave hi/lo bytes (affects 16-bit only)\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 "\n"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 );
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 (void) optArg;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
91
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 switch (optN)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 case 0:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 argShowHelp();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 exit(0);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 case 1:
337
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
100 dmVerbosity++;
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
101 break;
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
102
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
103 case 2:
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 optIgnoreErrors = TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
337
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
107 case 3:
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 optPatternMode = atoi(optArg);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 if (optPatternMode <= 0 || optPatternMode >= PATMODE_LAST)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
111 dmErrorMsg("Unknown pattern conversion mode %d\n", optPatternMode);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
337
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
116 case 4: optStripExtInstr = TRUE; break;
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
117 case 5: optStripInstr = TRUE; break;
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
118 case 6: optStripSamples = TRUE; break;
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
119
1099
f309a382d6e7 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1097
diff changeset
120 case 7: optSampMode8 = atoi(optArg) & JM_SAMPLE_MODE_MASK; break;
f309a382d6e7 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1097
diff changeset
121 case 8: optSampMode16 = atoi(optArg) & JM_SAMPLE_MODE_MASK; break;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
337
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
123 case 9: optOptimize = TRUE; break;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 default:
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
126 dmErrorMsg("Unknown argument '%s'.\n", currArg);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
129
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 return TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 BOOL argHandleFile(char *currArg)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 // Was not option argument
300
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
137 if (!optInFilename)
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
138 optInFilename = currArg;
337
b420025e5cd2 Change argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
139 else
300
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
140 if (!optOutFilename)
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
141 optOutFilename = currArg;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
144 dmErrorMsg("Too many filename arguments specified, '%s'.\n", currArg);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
147
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 return TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 /* These functions and the macro mess are meant to make the
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 * conversion routines themselves clearer and simpler.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 BOOL jsPutByte(Uint8 *patBuf, size_t patBufSize, size_t *npatBuf, Uint8 val)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 if (*npatBuf >= patBufSize)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 patBuf[*npatBuf] = val;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 (*npatBuf)++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 return TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 #define JSPUTBYTE(x) do { if (!jsPutByte(patBuf, patBufSize, patSize, x)) return DMERR_BOUNDS; } while (0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 #define JSCOMP(x,z) do { if ((x) != jsetNotSet) { qflags |= (z); qcomp++; } } while (0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 #define JSCOMPPUT(xf,xv,qv) do { \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 if (qflags & (xf)) { \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 if ((xv) < 0 || (xv) > 255) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 "%s value out of bounds %d.\n", qv, (xv)); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 JSPUTBYTE(xv); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 } \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 } while (0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 #define JSCONVPUT(xv,qv) do { \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 if ((xv) != jsetNotSet) { \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 if ((xv) < 0 || (xv) > 254) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 "%s value out of bounds %d.\n", qv, (xv)); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 JSPUTBYTE((xv) + 1); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 } else { \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 JSPUTBYTE(0); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 } \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 } while (0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 /* Convert a note
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 */
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
194 static int jssConvertNote(
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
195 Uint8 *patBuf, const size_t patBufSize,
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
196 size_t *patSize, const JSSNote *pnote)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 Uint8 tmp;
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
199 if (pnote->note == jsetNotSet)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 tmp = 0;
1194
1f2cfa0119aa Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
201 else
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
202 if (pnote->note == jsetNoteOff)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 tmp = 127;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 else
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
205 tmp = pnote->note + 1;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 if (tmp > 0x7f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Note value out of bounds %d > 0x7f.\n", tmp);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 JSPUTBYTE(tmp & 0x7f);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
211
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
212 JSCONVPUT(pnote->instrument, "Instrument");
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
213 JSCONVPUT(pnote->volume, "Volume");
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
214 JSCONVPUT(pnote->effect, "Effect");
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
215
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
216 tmp = (pnote->param != jsetNotSet) ? pnote->param : 0;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 JSPUTBYTE(tmp);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
218
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 /* Compress a note
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 */
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
225 static int jssCompressNote(
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
226 Uint8 *patBuf, const size_t patBufSize,
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
227 size_t *patSize, const JSSNote *pnote)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 Uint8 qflags = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 int qcomp = 0;
1201
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
231
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
232 // Determine what would get stored,
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
233 // aka actually how much space we use
1210
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
234 JSCOMP(pnote->note, JM_COMP_NOTE);
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
235 JSCOMP(pnote->instrument, JM_COMP_INSTRUMENT);
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
236 JSCOMP(pnote->volume, JM_COMP_VOLUME);
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
237 JSCOMP(pnote->effect, JM_COMP_EFFECT);
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
238 if (pnote->param != jsetNotSet && pnote->param != 0)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 {
1210
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
240 qflags |= JM_COMP_PARAM;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 qcomp++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
243
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 if (qcomp < 4)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 {
1201
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
246 // Okay, it's less than 4 bytes, so use compressed
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 JSPUTBYTE(qflags | 0x80);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
248
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
249 if (pnote->note != jsetNotSet)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 {
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
251 Uint8 tmp = (pnote->note != jsetNoteOff) ? pnote->note : 127;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 if (tmp > 0x7f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Note value out of bounds %d > 0x7f.\n", tmp);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 JSPUTBYTE(tmp);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
256
1210
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
257 JSCOMPPUT(JM_COMP_INSTRUMENT, pnote->instrument, "Instrument");
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
258 JSCOMPPUT(JM_COMP_VOLUME, pnote->volume, "Volume");
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
259 JSCOMPPUT(JM_COMP_EFFECT, pnote->effect, "Effect");
6b48bad13399 Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents: 1209
diff changeset
260 JSCOMPPUT(JM_COMP_PARAM, pnote->param, "Param");
1201
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
261 }
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
262 else
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
263 {
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
264 // Was 4 bytes or more, just dump it all in ..
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
265 return jssConvertNote(patBuf, patBufSize, patSize, pnote);
1201
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
266 }
83693e2797dd Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1195
diff changeset
267
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 /* Compress pattern
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 */
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
274 static int jssConvertPatternCompHoriz(
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
275 Uint8 *patBuf, const size_t patBufSize,
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
276 size_t *patSize, const JSSPattern *pattern)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 int row, channel;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 *patSize = 0;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
280
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 for (row = 0; row < pattern->nrows; row++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 for (channel = 0; channel < pattern->nchannels; channel++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 {
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
284 const JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel];
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
285 const int res = jssCompressNote(patBuf, patBufSize, patSize, pnote);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 if (res != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 JSSERROR(res, res, "Note compression failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 patBuf, patBufSize, *patSize, row, channel);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 return res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
293
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
298 static int jssConvertPatternCompVert(
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
299 Uint8 *patBuf, const size_t patBufSize,
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
300 size_t *patSize, const JSSPattern *pattern)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 int row, channel;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 *patSize = 0;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
304
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 for (channel = 0; channel < pattern->nchannels; channel++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 for (row = 0; row < pattern->nrows; row++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 {
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
308 const JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel];
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
309 const int res = jssCompressNote(patBuf, patBufSize, patSize, pnote);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 if (res != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 JSSERROR(res, res, "Note compression failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 patBuf, patBufSize, *patSize, row, channel);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 return res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
317
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 /* Convert a pattern
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 */
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
324 static int jssConvertPatternRawHoriz(
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
325 Uint8 *patBuf, const size_t patBufSize,
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
326 size_t *patSize, const JSSPattern *pattern)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 int row, channel;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 *patSize = 0;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
330
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 for (row = 0; row < pattern->nrows; row++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 for (channel = 0; channel < pattern->nchannels; channel++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 {
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
334 const JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel];
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
335 const int res = jssConvertNote(patBuf, patBufSize, patSize, pnote);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 if (res != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 JSSERROR(res, res, "Note conversion failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 patBuf, patBufSize, *patSize, row, channel);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 return res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
343
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
348 static int jssConvertPatternRawVert(
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
349 Uint8 *patBuf, const size_t patBufSize,
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
350 size_t *patSize, const JSSPattern *pattern)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 int row, channel;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 *patSize = 0;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
354
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 for (channel = 0; channel < pattern->nchannels; channel++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 for (row = 0; row < pattern->nrows; row++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 {
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
358 const JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel];
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
359 const int res = jssConvertNote(patBuf, patBufSize, patSize, pnote);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 if (res != DMERR_OK)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 JSSERROR(res, res, "Note conversion failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 patBuf, patBufSize, *patSize, row, channel);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 return res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
367
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 #define JSFOREACHNOTE1 \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 for (channel = 0; channel < pattern->nchannels; channel++) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 for (row = 0; row < pattern->nrows; row++) { \
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
375 const JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 #define JSFOREACHNOTE2 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
379 static int jssConvertPatternRawElem(
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
380 Uint8 *patBuf, const size_t patBufSize,
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
381 size_t *patSize, const JSSPattern *pattern)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 Uint8 tmp;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 int row, channel;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 *patSize = 0;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
386
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 JSFOREACHNOTE1;
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
388 if (pnote->note == jsetNotSet)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 tmp = 0;
1194
1f2cfa0119aa Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
390 else
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
391 if (pnote->note == jsetNoteOff)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 tmp = 127;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 else
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
394 tmp = pnote->note + 1;
1194
1f2cfa0119aa Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
395
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 if (tmp > 0x7f)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Note value out of bounds %d > 0x7f.\n", tmp);
1194
1f2cfa0119aa Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
398
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 JSPUTBYTE(tmp);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 JSFOREACHNOTE2;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
401
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 JSFOREACHNOTE1;
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
403 JSCONVPUT(pnote->instrument, "Instrument");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 JSFOREACHNOTE2;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
405
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 JSFOREACHNOTE1;
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
407 JSCONVPUT(pnote->volume, "Volume");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 JSFOREACHNOTE2;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
409
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 JSFOREACHNOTE1;
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
411 JSCONVPUT(pnote->effect, "Effect");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412 JSFOREACHNOTE2;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
413
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 JSFOREACHNOTE1;
1207
59b5821b5cf9 Rename function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1201
diff changeset
415 tmp = (pnote->param != jsetNotSet) ? pnote->param : 0;
338
cd57ba1130eb Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
416 JSPUTBYTE(tmp);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 JSFOREACHNOTE2;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
418
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 #undef JSFOREACHNOTE1
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 #undef JSFOREACHNOTE2
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
426 static BOOL jssMODWriteEnvelope(FILE *outFile, JSSEnvelope *env, const char *name, const int ninst)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 int i;
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
429 BOOL ok =
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
430 dm_fwrite_byte(outFile, env->flags) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
431 dm_fwrite_byte(outFile, env->npoints) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
432 dm_fwrite_byte(outFile, env->sustain) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
433 dm_fwrite_byte(outFile, env->loopS) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
434 dm_fwrite_byte(outFile, env->loopE);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
435
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
436 for (i = 0; ok && i < env->npoints; i++)
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
437 {
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
438 ok =
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
439 dm_fwrite_le16(outFile, env->points[i].frame) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
440 dm_fwrite_le16(outFile, env->points[i].value);
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
441 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
442
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
443 if (!ok)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 {
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
445 JSSERROR(DMERR_FWRITE, ok,
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
446 "Failed to write JSSMOD %s-envelope for instrument #%d.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
447 name, ninst);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 }
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
449
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
450 return ok;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 /* Save a JSSMOD file
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 */
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
456 int jssSaveJSSMOD(FILE *outFile, JSSModule *module, int patMode, int flags8, int flags16)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 JSSMODHeader jssH;
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
459 const size_t patBufSize = 256*1024; // 256kB pattern buffer
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 Uint8 *patBuf;
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
461 size_t totalSize;
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
462 int index;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 // Check the module
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
465 if (module == NULL)
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
466 JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR,
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
467 "Module pointer was NULL\n");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
469 if (module->nchannels < 1 || module->npatterns < 1 || module->norders < 1 ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
470 module->nchannels > jsetMaxChannels ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
471 module->npatterns > jsetMaxPatterns ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
472 module->norders > jsetMaxOrders)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS,
1195
4d27c4ec8d4d Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents: 1194
diff changeset
474 "Module had invalid values (nchannels=%d, npatterns=%d, norders=%d)\n",
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
475 module->nchannels, module->npatterns, module->norders);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 // Create the JSSMOD header
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 jssH.idMagic[0] = 'J';
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 jssH.idMagic[1] = 'M';
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 jssH.idVersion = JSSMOD_VERSION;
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
481 jssH.defFlags = module->defFlags;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
482 jssH.intVersion = module->intVersion;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
483 jssH.norders = module->norders;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
484 jssH.npatterns = module->npatterns;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
485 jssH.nextInstruments = module->nextInstruments;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
486 jssH.ninstruments = module->ninstruments;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
487 jssH.defRestartPos = module->defRestartPos;
1231
a275b7382ffa Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1211
diff changeset
488
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
489 jssH.nchannels = module->nchannels;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
490 jssH.defSpeed = module->defSpeed;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
491 jssH.defTempo = module->defTempo;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 jssH.patMode = patMode;
1209
cb31e57982f2 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1207
diff changeset
493
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 // Write header
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
495 if (!dm_fwrite_str(outFile, jssH.idMagic, sizeof(jssH.idMagic)) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
496 !dm_fwrite_byte(outFile, jssH.idVersion) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
497
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
498 !dm_fwrite_le16(outFile, jssH.defFlags) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
499 !dm_fwrite_le16(outFile, jssH.intVersion) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
500 !dm_fwrite_le16(outFile, jssH.norders) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
501 !dm_fwrite_le16(outFile, jssH.npatterns) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
502 !dm_fwrite_le16(outFile, jssH.nextInstruments) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
503 !dm_fwrite_le16(outFile, jssH.ninstruments) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
504 !dm_fwrite_le16(outFile, jssH.defRestartPos) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
505
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
506 !dm_fwrite_byte(outFile, jssH.nchannels) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
507 !dm_fwrite_byte(outFile, jssH.defSpeed) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
508 !dm_fwrite_byte(outFile, jssH.defTempo) ||
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
509 !dm_fwrite_byte(outFile, jssH.patMode))
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
510 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
1245
2a0488078b78 Cleanups, more consistent error messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 1244
diff changeset
511 "Error writing JSSMOD header!\n");
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
512
9
c42ee907de9c Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
513 totalSize = sizeof(jssH);
c42ee907de9c Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
514 dmMsg(1," * JSSMOD-header 0x%04x, %d bytes.\n", JSSMOD_VERSION, totalSize);
c42ee907de9c Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
515
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 // Write orders list
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
517 for (totalSize = index = 0; index < module->norders; index++)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 {
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
519 int tmp = module->orderList[index];
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
520 if (tmp != jsetNotSet && tmp > module->npatterns)
1237
8b6ed580e800 Add sanity check to order list writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
521 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
8b6ed580e800 Add sanity check to order list writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
522 "Orderlist entry #%d has invalid value %d.\n",
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
523 index, tmp);
1237
8b6ed580e800 Add sanity check to order list writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
524
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
525 if (tmp == jsetNotSet)
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
526 tmp = 0xffff;
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
527
1237
8b6ed580e800 Add sanity check to order list writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
528 if (!dm_fwrite_le16(outFile, tmp))
8b6ed580e800 Add sanity check to order list writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
529 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
8b6ed580e800 Add sanity check to order list writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
530 "Could not write JSSMOD orders list entry #%d (%d).\n",
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
531 index, tmp);
1237
8b6ed580e800 Add sanity check to order list writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
532
8b6ed580e800 Add sanity check to order list writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
533 totalSize += sizeof(Uint16);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535
9
c42ee907de9c Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
536 dmMsg(1," * %d item orders list, %d bytes.\n",
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
537 module->norders, totalSize);
9
c42ee907de9c Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
538
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 // Allocate pattern compression buffer
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 if ((patBuf = dmMalloc(patBufSize)) == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 "Error allocating memory for pattern compression buffer.\n");
9
c42ee907de9c Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
543
1232
10cb3c400451 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1231
diff changeset
544 // Convert and write patterns
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
545 for (totalSize = index = 0; index < module->npatterns; index++)
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
546 if (module->patterns[index] != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 {
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
548 JSSPattern *pattern = module->patterns[index];
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
549 size_t dataSize = 0;
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
550 int ret;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
551
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
552 if (pattern->nrows > jsetMaxRows)
1232
10cb3c400451 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1231
diff changeset
553 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
10cb3c400451 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1231
diff changeset
554 "Pattern #%d has %d rows > %d max.\n",
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
555 index, pattern->nrows, jsetMaxRows);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
556
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 switch (patMode)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 case PATMODE_RAW_HORIZ:
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
560 ret = jssConvertPatternRawHoriz(patBuf, patBufSize, &dataSize, pattern);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 case PATMODE_COMP_HORIZ:
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
563 ret = jssConvertPatternCompHoriz(patBuf, patBufSize, &dataSize, pattern);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 case PATMODE_RAW_VERT:
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
566 ret = jssConvertPatternRawVert(patBuf, patBufSize, &dataSize, pattern);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 case PATMODE_COMP_VERT:
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
569 ret = jssConvertPatternCompVert(patBuf, patBufSize, &dataSize, pattern);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 case PATMODE_RAW_ELEM:
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
572 ret = jssConvertPatternRawElem(patBuf, patBufSize, &dataSize, pattern);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 default:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
1245
2a0488078b78 Cleanups, more consistent error messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 1244
diff changeset
576 "Unsupported pattern conversion mode %d for pattern #%d.\n",
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
577 patMode, index);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
580 if (ret != DMERR_OK)
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
581 JSSERROR(ret, ret, "Error converting pattern data #%d.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
582 pattern);
1211
83ed44e9630b Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1210
diff changeset
583
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
584 dmMsg(3, " - Pattern %d size %d bytes\n", index, dataSize);
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
585 totalSize += dataSize + sizeof(JSSMODPattern);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
587 if (!dm_fwrite_le32(outFile, dataSize) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
588 !dm_fwrite_le16(outFile, pattern->nrows) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
589 !dm_fwrite_str(outFile, patBuf, dataSize))
1211
83ed44e9630b Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1210
diff changeset
590 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
591 "Error writing JSSMOD pattern #%d.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
592 index);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 }
1242
e44a2b7abe07 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1241
diff changeset
594 else
e44a2b7abe07 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1241
diff changeset
595 JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR,
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
596 "Pattern #%d was NULL.\n", index);
1242
e44a2b7abe07 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1241
diff changeset
597
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 dmFree(patBuf);
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
599 dmMsg(1," * %d patterns, %d bytes.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
600 module->npatterns, totalSize);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 // Write extended instruments
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
603 for (totalSize = index = 0; index < module->nextInstruments; index++)
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
604 if (module->extInstruments[index] != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 {
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
606 JSSExtInstrument *einst = module->extInstruments[index];
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
607 int i;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
608
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
609 BOOL ok =
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
610 dm_fwrite_byte(outFile, einst->nsamples) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
611 dm_fwrite_byte(outFile, einst->vibratoType) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
612 dm_fwrite_le16(outFile, einst->vibratoSweep) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
613 dm_fwrite_le16(outFile, einst->vibratoDepth) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
614 dm_fwrite_le16(outFile, einst->vibratoRate) &&
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
615 dm_fwrite_le16(outFile, einst->fadeOut);
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
616
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
617 for (i = 0; ok && i < jsetNNotes; i++)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618 {
1242
e44a2b7abe07 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1241
diff changeset
619 int snum = einst->sNumForNotes[i];
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
620 Uint32 tmp = (snum != jsetNotSet) ? snum + 1 : 0;
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
621 ok = dm_fwrite_le32(outFile, tmp);
1242
e44a2b7abe07 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1241
diff changeset
622 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
623
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
624 if (!ok ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
625 !jssMODWriteEnvelope(outFile, &einst->volumeEnv, "volume", index) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
626 !jssMODWriteEnvelope(outFile, &einst->panningEnv, "panning", index))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
628 "Error writing JSSMOD extended instrument #%d.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
629 index);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
631 totalSize += sizeof(JSSMODExtInstrument);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 }
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
633 else
1245
2a0488078b78 Cleanups, more consistent error messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 1244
diff changeset
634 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR,
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
635 "Extended instrument #%d NULL!\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
636 index);
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
637
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
638 dmMsg(1," * %d Extended Instruments, %d bytes.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
639 module->nextInstruments, totalSize);
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
640
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
641 // Write sample instrument headers
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
642 for (totalSize = index = 0; index < module->ninstruments; index++)
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
643 if (module->instruments[index] != NULL)
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
644 {
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
645 JSSInstrument *inst = module->instruments[index];
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
646 Uint8 convFlags = (inst->flags & jsf16bit) ? flags16 : flags8;
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
647 if (inst->data != NULL)
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
648 convFlags |= jsampHasData;
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
649
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
650 // Write instrument header to file
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
651 if (!dm_fwrite_le32(outFile, inst->size) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
652 !dm_fwrite_le32(outFile, inst->loopS) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
653 !dm_fwrite_le32(outFile, inst->loopE) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
654 !dm_fwrite_le16(outFile, inst->flags) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
655 !dm_fwrite_le16(outFile, inst->C4BaseSpeed) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
656 !dm_fwrite_le16(outFile, inst->ERelNote) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
657 !dm_fwrite_le16(outFile, inst->EFineTune) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
658 !dm_fwrite_le16(outFile, inst->EPanning) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
659 !dm_fwrite_byte(outFile, inst->volume) ||
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
660 !dm_fwrite_byte(outFile, inst->convFlags))
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
661 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
662 "Error writing JSSMOD instrument #%d.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
663 index);
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
664
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
665 totalSize += sizeof(JSSMODInstrument);
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
666 }
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
667 else
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
668 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR,
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
669 "Instrument #%d NULL!\n", index);
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
670
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
671 dmMsg(1," * %d Instrument headers, %d bytes.\n",
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
672 module->ninstruments, totalSize);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
673
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674 // Write sample data
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
675 for (totalSize = index = 0; index < module->ninstruments; index++)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676 {
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
677 JSSInstrument *inst = module->instruments[index];
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
678 if (inst != NULL && inst->data != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679 {
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
680 size_t bsize = inst->size;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681 if (inst->flags & jsf16bit)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682 {
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
683 bsize *= sizeof(Uint16);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 jssEncodeSample16(inst->data, inst->size, flags16);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687 {
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
688 bsize *= sizeof(Uint8);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689 jssEncodeSample8(inst->data, inst->size, flags8);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
690 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
691
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
692 if (!dm_fwrite_str(outFile, inst->data, bsize))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
693 JSSERROR(DMERR_FWRITE, DMERR_FWRITE,
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
694 "Error writing JSSMOD sample data for instrument #%d.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
695 index);
1243
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
696
377c8a603d21 Cleanups in JSSMOD writing.
Matti Hamalainen <ccr@tnsp.org>
parents: 1242
diff changeset
697 totalSize += bsize;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
699 }
1246
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
700
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
701 dmMsg(1," * %d samples, %d bytes.\n",
7bd50496c9ec Work on improved module conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 1245
diff changeset
702 module->ninstruments, totalSize);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
703
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 return DMERR_OK;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
708 /* Scan given pattern for used instruments and channels.
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
709 * Also checks if the pattern is emty.
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
710 */
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
711 void scanPattern(JSSModule *module, JSSPattern *pattern, int npattern, BOOL *usedExtInstruments, BOOL *usedChannels, BOOL *empty)
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
712 {
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
713 int row, channel;
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
714 JSSNote *n = pattern->data;
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
715 *empty = FALSE;
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
716
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
717 // Check all notes in this pattern to see what instruments are used
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
718 for (row = 0; row < pattern->nrows; row++)
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
719 for (channel = 0; channel < pattern->nchannels; channel++, n++)
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
720 {
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
721 if (n->instrument != jsetNotSet)
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
722 {
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
723 if (n->instrument >= 0 && n->instrument < module->nextInstruments)
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
724 usedExtInstruments[n->instrument] = TRUE;
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
725 else
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
726 {
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
727 dmMsg(2, "Pattern 0x%x, row=0x%x, chn=%d has invalid instrument 0x%x\n",
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
728 npattern + 1, row, channel, n->instrument + 1);
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
729 }
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
730 }
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
731
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
732 if (n->note != jsetNotSet ||
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
733 n->instrument != jsetNotSet ||
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
734 n->volume != jsetNotSet ||
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
735 n->effect != jsetNotSet ||
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
736 n->param != jsetNotSet)
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
737 {
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
738 usedChannels[channel] = TRUE;
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
739 *empty = FALSE;
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
740 }
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
741 }
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
742 }
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
743
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
744
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
745 /* Optimize a given module
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
746 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
747 JSSModule *optimizeModule(JSSModule *m)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
748 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
749 BOOL usedPatterns[jsetMaxPatterns + 1],
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
750 usedInstruments[jsetMaxInstruments + 1],
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
751 usedExtInstruments[jsetMaxInstruments + 1],
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
752 usedChannels[jsetMaxChannels];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
753 int mapExtInstruments[jsetMaxInstruments + 1],
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
754 mapInstruments[jsetMaxInstruments + 1],
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755 mapPatterns[jsetMaxPatterns + 1];
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
756 JSSModule *r = NULL;
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
757 int i, n8, n16, unused;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759 // Allocate a new module
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760 if ((r = jssAllocateModule()) == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
763 // Copy things
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 r->moduleType = m->moduleType;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765 r->moduleName = dm_strdup(m->moduleName);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
766 r->trackerName = dm_strdup(m->trackerName);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
767 r->defSpeed = m->defSpeed;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
768 r->defTempo = m->defTempo;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
769 r->defFlags = m->defFlags;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
770 r->defRestartPos = m->defRestartPos;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 r->intVersion = m->intVersion;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772 r->nchannels = m->nchannels;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
773 r->norders = m->norders;
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
774
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775 for (i = 0; i < jsetNChannels; i++)
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
776 {
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777 r->defPanning[i] = m->defPanning[i];
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
778 usedChannels[i] = FALSE;
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
779 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
780
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
781 // Initialize values
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
782 for (i = 0; i <= jsetMaxInstruments; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
783 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
784 usedExtInstruments[i] = FALSE;
798
422f85db78fd Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 797
diff changeset
785 usedInstruments[i] = FALSE;
422f85db78fd Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 797
diff changeset
786 mapExtInstruments[i] = jsetNotSet;
422f85db78fd Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 797
diff changeset
787 mapInstruments[i] = jsetNotSet;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
788 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
789
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
790 for (i = 0; i <= jsetMaxPatterns; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
792 usedPatterns[i] = FALSE;
798
422f85db78fd Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 797
diff changeset
793 mapPatterns[i] = jsetNotSet;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
794 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
795
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
796 //
798
422f85db78fd Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 797
diff changeset
797 // Find out all actually used patterns and ext.instruments
1100
654602d3d5a5 Improve comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1099
diff changeset
798 // by going through all patterns specified in the order list
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
799 //
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
800 dmMsg(1, "Scanning patterns for used instruments and channels...\n");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
801 for (i = 0; i < m->norders; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
802 {
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
803 int npat = m->orderList[i];
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
804 if (npat >= 0 && npat < m->npatterns)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
805 {
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
806 JSSPattern *pattern = m->patterns[npat];
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
807 if (pattern != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
808 {
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
809 BOOL empty;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
810
1100
654602d3d5a5 Improve comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1099
diff changeset
811 // Mark this pattern as used
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
812 usedPatterns[npat] = TRUE;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
813
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
814 // Scan for used instruments and channels
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
815 scanPattern(m, pattern, npat, usedExtInstruments, usedChannels, &empty);
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
816
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
817 // Empty patterns with 64 rows are "removed"
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
818 if (empty && pattern->nrows == 64)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
819 {
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
820 m->orderList[i] = jsetNotSet;
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
821 usedPatterns[npat] = FALSE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
822 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
823 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
824 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
825 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
826 dmErrorMsg("Pattern 0x%x is used on order 0x%x, but has no data!\n",
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
827 npat, i);
807
d83dc7e47076 Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents: 798
diff changeset
828
d83dc7e47076 Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents: 798
diff changeset
829 // Fix it.
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
830 m->orderList[i] = jsetNotSet;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
831 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
832 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
833 else
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
834 if (npat != jsetNotSet)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
835 {
1101
0577f10dc3de Improve messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 1100
diff changeset
836 dmErrorMsg("Order 0x%x has invalid pattern number 0x%x, changing to empty!\n",
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
837 i, npat);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
838
807
d83dc7e47076 Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents: 798
diff changeset
839 // Fix it.
1244
072851dcec5c Improve module optimization.
Matti Hamalainen <ccr@tnsp.org>
parents: 1243
diff changeset
840 m->orderList[i] = jsetNotSet;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
841 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
842 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
843
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
844 //
1100
654602d3d5a5 Improve comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 1099
diff changeset
845 // Find used sample instruments
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
846 //
1101
0577f10dc3de Improve messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 1100
diff changeset
847 dmMsg(1, "Checking ext.instruments for used sample instruments...\n");
856
415cc781e127 Fix some brain farts in instrument remapping.
Matti Hamalainen <ccr@tnsp.org>
parents: 807
diff changeset
848 for (i = 0; i < jsetMaxInstruments; i++)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
849 if (usedExtInstruments[i] && m->extInstruments[i] != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
850 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
851 int note;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
852 JSSExtInstrument *e = m->extInstruments[i];
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
853
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
854 for (note = 0; note < jsetNNotes; note++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
855 if (e->sNumForNotes[note] != jsetNotSet)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
856 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
857 int q = e->sNumForNotes[note];
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
858 if (q >= 0 && q < m->ninstruments)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
859 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
860 usedInstruments[q] = TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
861 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
862 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
863 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
864 dmErrorMsg("Ext.instrument #%d sNumForNotes[%d] value out range (%d < %d).\n",
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
865 i + 1, m->ninstruments, q + 1);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
867 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
868 }
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
869
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
870 //
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
871 // Create pattern mappings
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
872 //
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
873 dmMsg(1, "Creating pattern remaps...\n");
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
874 for (unused = i = 0; i <= jsetMaxPatterns; i++)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
875 if (usedPatterns[i])
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
876 {
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
877 JSSPattern *pat;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
878
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
879 if (i >= m->npatterns)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
880 {
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
881 dmErrorMsg("Pattern 0x%x >= 0x%x, but used!\n", i, m->npatterns);
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
882 continue;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
883 }
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
884
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
885 if ((pat = m->patterns[i]) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
886 {
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
887 dmErrorMsg("Pattern 0x%x used but is NULL.\n", i);
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
888 continue;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
889 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
890
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
891 mapPatterns[i] = r->npatterns;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
892 r->patterns[r->npatterns] = m->patterns[i];
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
893 (r->npatterns)++;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
894 }
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
895 else
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
896 if (m->patterns[i] != NULL)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
897 unused++;
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
898
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
899 dmMsg(1, "%d used patterns (%d unused).\n",
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
900 r->npatterns, unused);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
901
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
902
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
903 //
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
904 // Re-map instruments
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
905 //
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
906 dmMsg(1, "Creating sample instrument remaps...\n");
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
907 for (unused = n8 = n16 = i = 0; i < jsetMaxInstruments; i++)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
908 if (usedInstruments[i])
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
909 {
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
910 JSSInstrument *ip;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
911 if (optStripInstr)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
912 continue;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
913
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
914 if (i >= m->ninstruments)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
915 {
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
916 dmErrorMsg("Instrument 0x%x >= 0x%x, but used!\n",
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
917 i + 1, m->ninstruments);
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
918 continue;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
919 }
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
920
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
921 if ((ip = m->instruments[i]) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
922 {
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
923 dmErrorMsg("Instrument 0x%x used but is NULL.\n", i + 1);
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
924 continue;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
925 }
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
926
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
927 dmPrint(2, "%02x -> %02x : ", i + 1, r->ninstruments + 1);
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
928
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
929 mapInstruments[i] = r->ninstruments;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
930 r->instruments[r->ninstruments] = ip;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
931 (r->ninstruments)++;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
932
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
933 if (ip->flags & jsf16bit)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
934 n16++;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
935 else
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
936 n8++;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
937 }
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
938 else
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
939 if (m->instruments[i] != NULL)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
940 unused++;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
941
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
942 dmPrint(2, "\n");
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
943 dmMsg(1, "Total of %d [16-bit] + %d [8-bit] samples = %d instruments (%d unused).\n",
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
944 n16, n8, r->ninstruments, unused);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
945
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
946 //
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
947 // Re-map ext.instruments
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
948 //
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
949 dmMsg(1, "Creating ext.instrument remaps...\n");
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
950 for (unused = i = 0; i < jsetMaxInstruments; i++)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
951 if (usedExtInstruments[i])
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
952 {
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
953 JSSExtInstrument *eip;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
954 int note;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
955 if (optStripExtInstr)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
956 continue;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
957
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
958 if (i >= m->nextInstruments)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
959 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
960 dmErrorMsg("Ext.instrument 0x%x >= 0x%x, but used!\n",
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
961 i + 1, m->nextInstruments);
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
962 continue;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
963 }
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
964
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
965 if ((eip = m->extInstruments[i]) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
966 {
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
967 dmErrorMsg("Extended instrument 0x%x used but is NULL.\n", i + 1);
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
968 continue;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
969 }
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
970
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
971 dmPrint(2, "%02x -> %02x : ", i + 1, r->nextInstruments + 1);
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
972
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
973 mapExtInstruments[i] = r->nextInstruments;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
974 r->extInstruments[r->nextInstruments] = eip;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
975 (r->nextInstruments)++;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
976
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
977 // Re-map sNumForNotes table for this ext.instrument
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
978 for (note = 0; note < jsetNNotes; note++)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
979 {
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
980 int q = eip->sNumForNotes[note];
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
981 if (q != jsetNotSet)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
982 {
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
983 int map;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
984 if (q >= 0 && q < jsetMaxInstruments)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
985 {
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
986 map = mapInstruments[q];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
987 }
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
988 else
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
989 {
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
990 map = jsetNotSet;
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
991 dmErrorMsg("Einst=%d, note=%d, sNumForNote=%d (%d max)\n",
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
992 i + 1, note, q + 1, r->ninstruments);
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
993 }
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
994
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
995 dmPrint(3, "%02x.%02x ", q + 1, map + 1);
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
996 eip->sNumForNotes[note] = map;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
997 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
998 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
999 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1000 else
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
1001 if (m->extInstruments[i] != NULL)
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
1002 unused++;
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
1003
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1004 dmPrint(2, "\n");
1097
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
1005 dmMsg(1, "%d extended instruments (%d unused).\n",
0e3cb9e61170 Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1094
diff changeset
1006 r->nextInstruments, unused);
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
1007
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
1008 //
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
1009 // Remap pattern data with remapped instrument data
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
1010 //
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1011 for (i = 0; i < r->npatterns; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1012 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1013 int row, channel;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1014 JSSPattern *p = r->patterns[i];
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1015 JSSNote *n = p->data;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
1016
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1017 for (row = 0; row < p->nrows; row++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1018 for (channel = 0; channel < p->nchannels; channel++, n++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1019 {
184
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1020 char effect;
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1021
1109
c8bcdddf233c Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 1108
diff changeset
1022 // If not stripping extended instruments, check for
c8bcdddf233c Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 1108
diff changeset
1023 // the validity of the used instrument and remap
338
cd57ba1130eb Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
1024 if (!optStripExtInstr)
cd57ba1130eb Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
1025 {
856
415cc781e127 Fix some brain farts in instrument remapping.
Matti Hamalainen <ccr@tnsp.org>
parents: 807
diff changeset
1026 if (n->instrument >= 0 && n->instrument < jsetMaxInstruments)
338
cd57ba1130eb Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
1027 n->instrument = mapExtInstruments[n->instrument];
202
85614db5f577 Warn about invalid instruments.
Matti Hamalainen <ccr@tnsp.org>
parents: 184
diff changeset
1028
1116
9d78c880c6e1 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1115
diff changeset
1029 if (n->instrument != jsetNotSet &&
9d78c880c6e1 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1115
diff changeset
1030 r->extInstruments[n->instrument] == NULL)
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
1031 {
1116
9d78c880c6e1 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1115
diff changeset
1032 dmErrorMsg("Non-existing instrument used #%d, INTERNAL ERROR.\n",
1111
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
1033 n->instrument + 1);
f9a96fc2a932 Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents: 1109
diff changeset
1034 }
338
cd57ba1130eb Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
1035 }
cd57ba1130eb Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
1036
184
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1037 JMPGETEFFECT(effect, n->effect);
1094
8fc475bbfa42 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1093
diff changeset
1038
184
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1039 switch (effect)
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1040 {
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1041 case 'C': // Cxx = Set volume
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1042 if (n->volume == jsetNotSet)
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1043 {
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1044 n->volume = n->param;
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1045 n->effect = jsetNotSet;
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1046 n->param = jsetNotSet;
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1047 }
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1048 break;
50f55def91e5 Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
1049 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1050 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1051 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
1052
1113
28dcf10fb8a9 Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 1111
diff changeset
1053 //
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1054 // Remap orders list
1113
28dcf10fb8a9 Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 1111
diff changeset
1055 //
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1056 for (i = 0; i < m->norders; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1057 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1058 r->orderList[i] = mapPatterns[m->orderList[i]];
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1059 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1060
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1061 return r;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1062 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1063
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1064
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1065 int main(int argc, char *argv[])
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1066 {
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
1067 DMResource *inFile = NULL;
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
1068 FILE *outFile = NULL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1069 JSSModule *sm, *dm;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1070 int result;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1071
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1072 dmInitProg("xm2jss", "XM to JSSMOD converter", "0.6", NULL, NULL);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1073 dmVerbosity = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1074
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1075 // Parse arguments
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1076 if (!dmArgsProcess(argc, argv, optList, optListN,
860
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 856
diff changeset
1077 argHandleOpt, argHandleFile, OPTH_BAILOUT))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1078 exit(1);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1079
300
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1080 // Check arguments
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1081 if (optInFilename == NULL || optOutFilename == NULL)
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1082 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
1083 dmErrorMsg("Input or output file not specified. Try --help.\n");
300
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1084 return 1;
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1085 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1086
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1087 // Read the source file
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
1088 if ((result = dmf_create_stdio(optInFilename, "rb", &inFile)) != DMERR_OK)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1089 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
1090 dmErrorMsg("Error opening input file '%s', %d: %s\n",
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
1091 optInFilename, result, dmErrorStr(result));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1092 return 1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1093 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1094
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1095 // Initialize miniJSS
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1096 jssInit();
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1097
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1098 // Read file
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1099 dmMsg(1, "Reading XM-format file ...\n");
797
f066e9dccf29 Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents: 796
diff changeset
1100 result = jssLoadXM(inFile, &sm, FALSE);
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
1101 dmf_close(inFile);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1102 if (result != 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1103 {
1195
4d27c4ec8d4d Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents: 1194
diff changeset
1104 dmErrorMsg("Error while loading XM file (%d), ", result);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1105 if (optIgnoreErrors)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1106 fprintf(stderr, "ignoring. This may cause problems.\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1107 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1108 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1109 fprintf(stderr, "giving up. Use --ignore if you want to try to convert anyway.\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1110 return 2;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1111 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1112 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1113
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1114 // Check stripping settings
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1115 if (optStripExtInstr) optStripInstr = TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1116 if (optStripInstr) optStripSamples = TRUE;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
1117
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1118 // Remove samples
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1119 if (optStripSamples)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1120 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1121 int i;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
1122
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1123 dmMsg(1, "Stripping samples...\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1124 for (i = 0; i < sm->ninstruments; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1125 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1126 dmFree(sm->instruments[i]->data);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1127 sm->instruments[i]->data = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1128 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1129 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1130
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1131 // Remove instruments
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1132 if (optStripInstr)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1133 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1134 int i;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
1135
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1136 dmMsg(1, "Stripping instruments...\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1137 for (i = 0; i < sm->ninstruments; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1138 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1139 dmFree(sm->instruments[i]);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1140 sm->instruments[i] = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1141 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1142 sm->ninstruments = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1143 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1144
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1145 // Remove ext.instruments
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1146 if (optStripExtInstr)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1147 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1148 int i;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
1149
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1150 dmMsg(1, "Stripping ext.instruments...\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1151 for (i = 0; i < sm->nextInstruments; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1152 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1153 dmFree(sm->extInstruments[i]);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1154 sm->extInstruments[i] = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1155 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1156 sm->nextInstruments = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1157 }
1109
c8bcdddf233c Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 1108
diff changeset
1158
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1159 // Run the optimization procedure
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1160 if (optOptimize)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1161 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1162 dmMsg(1, "Optimizing module data...\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1163 dm = optimizeModule(sm);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1164 } else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1165 dm = sm;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1166
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1167 // Write output file
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
1168 if ((outFile = fopen(optOutFilename, "wb")) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1169 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
1170 dmErrorMsg("Error creating output file '%s', %d: %s\n",
300
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1171 optOutFilename, errno, strerror(errno));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1172 return 1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1173 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1174
9
c42ee907de9c Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1175 dmMsg(1, "Writing JSSMOD-format file [patMode=0x%04x, samp8=0x%02x, samp16=0x%02x]\n",
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1176 optPatternMode, optSampMode8, optSampMode16);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
1177
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
1178 result = jssSaveJSSMOD(outFile, dm, optPatternMode, optSampMode8, optSampMode16);
1113
28dcf10fb8a9 Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 1111
diff changeset
1179
1114
7aaab1533c90 Free the source module data at end.
Matti Hamalainen <ccr@tnsp.org>
parents: 1113
diff changeset
1180 dmFree(sm);
7aaab1533c90 Free the source module data at end.
Matti Hamalainen <ccr@tnsp.org>
parents: 1113
diff changeset
1181
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
1182 fclose(outFile);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1233
diff changeset
1183
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1184 if (result != 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1185 {
1113
28dcf10fb8a9 Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 1111
diff changeset
1186 dmErrorMsg(
28dcf10fb8a9 Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 1111
diff changeset
1187 "Error while saving JSSMOD file, %d: %s\n"
28dcf10fb8a9 Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 1111
diff changeset
1188 "WARNING: The resulting file may be broken!\n",
300
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1189 result, dmErrorStr(result));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1190 }
300
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1191 else
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1192 {
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1193 dmMsg(1, "Conversion complete.\n");
4972ca91d062 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
1194 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1195 return 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1196 }