Mercurial > hg > dmlib
annotate tools/mod2wav.c @ 2256:fe974f670d1d
Rename a variable.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 15 Jun 2019 22:53:25 +0300 |
parents | e3f0eaf23f4f |
children | c146033f1f6a |
rev | line source |
---|---|
0 | 1 /* |
2 * mod2wav - Render XM/JSSMOD module to WAV waveform file | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2007 Tecnic Software productions (TNSP) | |
5 * | |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
1432
a9516570cc26
Improve build, so that we can build the tools and tests with minimal
Matti Hamalainen <ccr@tnsp.org>
parents:
1235
diff
changeset
|
8 #include "dmtool.h" |
0 | 9 #include <stdio.h> |
10 #include <stdlib.h> | |
11 #include "jss.h" | |
12 #include "jssmod.h" | |
13 #include "jssmix.h" | |
14 #include "jssplr.h" | |
15 #include "dmlib.h" | |
16 #include "dmargs.h" | |
1953
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
17 #include "dmfile.h" |
285
245b15cd1919
Don't link libSDL uselessly to utilities that do not actually use it.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
18 #include "dmmutex.h" |
0 | 19 |
20 | |
1953
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
21 #define DM_WAVE_FORMAT_PCM (1) |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
22 #define DM_WAVE_RIFF_ID "RIFF" |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
23 #define DM_WAVE_WAVE_ID "WAVE" |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
24 #define DM_WAVE_FMT_ID "fmt " |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
25 #define DM_WAVE_DATA_ID "data" |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
26 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
27 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
28 typedef struct |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
29 { |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
30 Uint8 chunkID[4]; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
31 Uint32 chunkSize; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
32 } DMWaveChunk; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
33 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
34 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
35 typedef struct |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
36 { |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
37 Uint8 riffID[4]; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
38 Uint32 fileSize; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
39 Uint8 riffType[4]; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
40 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
41 DMWaveChunk chFormat; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
42 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
43 Uint16 wFormatTag; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
44 Uint16 nChannels; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
45 Uint32 nSamplesPerSec; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
46 Uint32 nAvgBytesPerSec; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
47 Uint16 nBlockAlign; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
48 Uint16 wBitsPerSample; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
49 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
50 DMWaveChunk chData; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
51 // Data follows here |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
52 } DMWaveFile; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
53 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
54 |
300 | 55 char *optInFilename = NULL, *optOutFilename = NULL; |
0 | 56 int optOutFormat = JSS_AUDIO_S16, |
57 optOutChannels = 2, | |
58 optOutFreq = 44100, | |
59 optMuteOChannels = -1, | |
60 optStartOrder = -1; | |
61 BOOL optUsePlayTime = FALSE; | |
62 size_t optPlayTime; | |
63 | |
64 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
65 static const DMOptArg optList[] = |
37 | 66 { |
67 { 0, '?', "help", "Show this help", OPT_NONE }, | |
68 { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
69 { 3, '1', "16bit", "16-bit output", OPT_NONE }, | |
70 { 4, '8', "8bit", "8-bit output", OPT_NONE }, | |
71 { 5, 'm', "mono", "Mono output", OPT_NONE }, | |
72 { 6, 's', "stereo", "Stereo output", OPT_NONE }, | |
73 { 7, 'f', "freq", "Output frequency", OPT_ARGREQ }, | |
74 { 8, 'M', "mute", "Mute other channels than #", OPT_ARGREQ }, | |
75 { 9, 'o', "order", "Start from order #", OPT_ARGREQ }, | |
76 { 10, 't', "time", "Play for # seconds", OPT_ARGREQ }, | |
0 | 77 // {10, 'l', "loop", "Loop for # times", OPT_ARGREQ }, |
78 }; | |
79 | |
80 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
81 | |
82 | |
83 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
84 { | |
85 (void) optArg; | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
86 |
37 | 87 switch (optN) |
88 { | |
89 case 0: | |
90 dmPrintBanner(stdout, dmProgName, | |
91 "[options] [sourcefile] [destfile]"); | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
92 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
93 dmArgsPrintHelp(stdout, optList, optListN, 0); |
37 | 94 exit(0); |
95 break; | |
0 | 96 |
37 | 97 case 2: |
98 dmVerbosity++; | |
99 break; | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
100 |
37 | 101 case 3: |
102 optOutFormat = JSS_AUDIO_S16; | |
103 break; | |
0 | 104 |
37 | 105 case 4: |
106 optOutFormat = JSS_AUDIO_U8; | |
107 break; | |
0 | 108 |
37 | 109 case 5: |
110 optOutChannels = JSS_AUDIO_MONO; | |
111 break; | |
0 | 112 |
37 | 113 case 6: |
38
8b04b0b51edc
Oops, fix a stupid bug in -s option .. it was setting the wrong variable. :|
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
114 optOutChannels = JSS_AUDIO_STEREO; |
37 | 115 break; |
0 | 116 |
37 | 117 case 7: |
118 optOutFreq = atoi(optArg); | |
119 break; | |
0 | 120 |
37 | 121 case 8: |
122 optMuteOChannels = atoi(optArg); | |
123 break; | |
124 | |
125 case 9: | |
126 optStartOrder = atoi(optArg); | |
127 break; | |
0 | 128 |
37 | 129 case 10: |
130 optPlayTime = atoi(optArg); | |
131 optUsePlayTime = TRUE; | |
132 break; | |
0 | 133 |
37 | 134 default: |
2183
e3f0eaf23f4f
Change the error message for unimplemented option argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1953
diff
changeset
|
135 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg); |
37 | 136 return FALSE; |
0 | 137 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
138 |
0 | 139 return TRUE; |
140 } | |
141 | |
142 | |
143 BOOL argHandleFile(char *currArg) | |
144 { | |
300 | 145 if (!optInFilename) |
146 optInFilename = currArg; | |
37 | 147 else |
300 | 148 if (!optOutFilename) |
149 optOutFilename = currArg; | |
37 | 150 else |
151 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
152 dmErrorMsg("Too many filename arguments (only source and dest needed) '%s'\n", currArg); |
0 | 153 return FALSE; |
154 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
155 |
0 | 156 return TRUE; |
157 } | |
158 | |
159 | |
1953
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
160 BOOL dmWriteWAVChunk(FILE * f, DMWaveChunk *ch) |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
161 { |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
162 return dm_fwrite_str(f, ch->chunkID, 4) && dm_fwrite_le32(f, ch->chunkSize); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
163 } |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
164 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
165 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
166 void dmMakeWAVChunk(DMWaveChunk *ch, const char *chunkID, const Uint32 chunkSize) |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
167 { |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
168 memcpy(&(ch->chunkID), (const void *) chunkID, 4); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
169 ch->chunkSize = chunkSize; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
170 } |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
171 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
172 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
173 void dmWriteWAVHeader(FILE *outFile, int sampBits, int sampFreq, int sampChn, size_t sampLen) |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
174 { |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
175 DMWaveFile wav; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
176 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
177 // PCM WAVE chunk |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
178 dmMakeWAVChunk(&wav.chFormat, DM_WAVE_FMT_ID, (2 + 2 + 4 + 4 + 2 + 2)); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
179 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
180 wav.wFormatTag = DM_WAVE_FORMAT_PCM; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
181 wav.nChannels = sampChn; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
182 wav.nSamplesPerSec = sampFreq; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
183 wav.nAvgBytesPerSec = (sampBits * sampChn * sampFreq) / 8; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
184 wav.nBlockAlign = (sampBits * sampChn) / 8; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
185 wav.wBitsPerSample = sampBits; |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
186 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
187 // Data chunk |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
188 dmMakeWAVChunk(&wav.chData, DM_WAVE_DATA_ID, (sampLen * wav.nBlockAlign)); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
189 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
190 // RIFF header |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
191 memcpy(&wav.riffID, (const void *) DM_WAVE_RIFF_ID, 4); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
192 memcpy(&wav.riffType, (const void *) DM_WAVE_WAVE_ID, 4); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
193 wav.fileSize = ((4 + 4 + 4) + wav.chFormat.chunkSize + wav.chData.chunkSize); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
194 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
195 // Write header |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
196 dm_fwrite_str(outFile, wav.riffID, sizeof(wav.riffID)); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
197 dm_fwrite_le32(outFile, wav.fileSize); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
198 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
199 dm_fwrite_str(outFile, wav.riffType, sizeof(wav.riffType)); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
200 dmWriteWAVChunk(outFile, &wav.chFormat); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
201 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
202 dm_fwrite_le16(outFile, wav.wFormatTag); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
203 dm_fwrite_le16(outFile, wav.nChannels); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
204 dm_fwrite_le32(outFile, wav.nSamplesPerSec); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
205 dm_fwrite_le32(outFile, wav.nAvgBytesPerSec); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
206 dm_fwrite_le16(outFile, wav.nBlockAlign); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
207 dm_fwrite_le16(outFile, wav.wBitsPerSample); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
208 |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
209 dmWriteWAVChunk(outFile, &wav.chData); |
c3e88d9343ca
Move WAV stuff back into mod2wav.c, made no sense to have it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
210 } |
0 | 211 int main(int argc, char *argv[]) |
212 { | |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
213 DMResource *inFile = NULL; |
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
214 FILE *outFile = NULL; |
68 | 215 JSSModule *mod = NULL; |
216 JSSMixer *dev = NULL; | |
217 JSSPlayer *plr = NULL; | |
0 | 218 int result = -1; |
219 size_t bufLen = 1024*4, dataTotal, dataWritten, sampSize; | |
220 Uint8 *mb = NULL; | |
221 | |
222 dmInitProg("mod2wav", "XM/JSSMOD to WAV renderer", "0.2", NULL, NULL); | |
223 dmVerbosity = 1; | |
224 | |
225 // Parse arguments | |
226 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:
797
diff
changeset
|
227 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
0 | 228 exit(1); |
229 | |
230 // Check arguments | |
300 | 231 if (optInFilename == NULL || optOutFilename == NULL) |
37 | 232 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
233 dmErrorMsg("Input or output file not specified. Try --help.\n"); |
0 | 234 return 1; |
235 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
236 |
0 | 237 // Initialize miniJSS |
238 jssInit(); | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
239 |
0 | 240 // Open the source file |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1434
diff
changeset
|
241 if ((result = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
242 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
243 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
|
244 optInFilename, result, dmErrorStr(result)); |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
245 return 1; |
0 | 246 } |
247 | |
248 // Read module file | |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
249 dmMsg(1, "Reading file: %s\n", optInFilename); |
0 | 250 #ifdef JSS_SUP_XM |
797
f066e9dccf29
Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
251 result = jssLoadXM(inFile, &mod, TRUE); |
0 | 252 #endif |
253 #ifdef JSS_SUP_JSSMOD | |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
254 dmfreset(inFile); |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
255 if (result != DMERR_OK) |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
256 { |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
257 dmMsg(1, "* Trying JSSMOD ...\n"); |
797
f066e9dccf29
Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
258 result = jssLoadJSSMOD(inFile, &mod, TRUE); |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
259 dmfreset(inFile); |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
260 if (result == DMERR_OK) |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
261 result = jssLoadJSSMOD(inFile, &mod, FALSE); |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
262 } |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
263 else |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
264 { |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
265 dmMsg(2, "* Trying XM...\n"); |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
266 result = jssLoadXM(inFile, &mod, FALSE); |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
267 } |
0 | 268 #endif |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
269 dmf_close(inFile); |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
270 |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
271 // Check for errors, we still might have some data tho |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
272 if (result != DMERR_OK) |
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
273 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
274 dmErrorMsg("Error loading module file, %d: %s\n", |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
275 result, dmErrorStr(result)); |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
276 } |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
277 |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
278 // Check if we have anything |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
279 if (mod == NULL) |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
280 return 3; |
0 | 281 |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
282 // Try to convert it |
68 | 283 if ((result = jssConvertModuleForPlaying(mod)) != DMERR_OK) |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
284 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
285 dmErrorMsg("Could not convert module for playing, %d: %s\n", |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
286 result, dmErrorStr(result)); |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
287 return 3; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
288 } |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
289 |
0 | 290 // Open mixer |
68 | 291 dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO); |
292 if (dev == NULL) | |
39 | 293 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
294 dmErrorMsg("jvmInit() returned NULL\n"); |
0 | 295 return 4; |
296 } | |
297 | |
68 | 298 sampSize = jvmGetSampleSize(dev); |
83
6b42aed2745b
Cleanups and correct the dmf_create_stdio() issue here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
299 if ((mb = dmMalloc(bufLen * sampSize)) == NULL) |
39 | 300 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
301 dmErrorMsg("Could not allocate mixing buffer\n"); |
0 | 302 return 5; |
303 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
304 |
0 | 305 dmMsg(1, "Using fmt=%d, bits=%d, channels=%d, freq=%d [%d / sample]\n", |
68 | 306 optOutFormat, jvmGetSampleRes(dev), optOutChannels, optOutFreq, |
0 | 307 sampSize); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
308 |
0 | 309 // Initialize player |
68 | 310 if ((plr = jmpInit(dev)) == NULL) |
311 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
312 dmErrorMsg("jmpInit() returned NULL.\n"); |
0 | 313 return 6; |
314 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
315 |
0 | 316 // Set callback |
68 | 317 jvmSetCallback(dev, jmpExec, plr); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
318 |
0 | 319 // Initialize playing |
68 | 320 jmpSetModule(plr, mod); |
37 | 321 if (optStartOrder >= 0) |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
322 { |
0 | 323 dmMsg(1, "Starting from song order #%d\n", optStartOrder); |
324 } else | |
325 optStartOrder = 0; | |
37 | 326 |
68 | 327 jmpPlayOrder(plr, optStartOrder); |
185
1f8f4d7cb33b
Increase default global volume.
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
328 jvmSetGlobalVol(dev, 150); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
329 |
68 | 330 if (optMuteOChannels > 0 && optMuteOChannels <= mod->nchannels) |
37 | 331 { |
0 | 332 int i; |
68 | 333 for (i = 0; i < mod->nchannels; i++) |
334 jvmMute(dev, i, TRUE); | |
335 jvmMute(dev, optMuteOChannels - 1, FALSE); | |
0 | 336 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
337 |
0 | 338 // Open output file |
300 | 339 if ((outFile = fopen(optOutFilename, "wb")) == NULL) |
37 | 340 { |
1434
ad3ad5d9681b
Get rid of another direct 'errno' usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
341 int err = dmGetErrno(); |
ad3ad5d9681b
Get rid of another direct 'errno' usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
342 dmErrorMsg("Error opening output file '%s' #%d: %s.\n", |
ad3ad5d9681b
Get rid of another direct 'errno' usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
343 optInFilename, err, dmErrorStr(err)); |
0 | 344 return 7; |
345 } | |
346 | |
347 // Write initial header | |
206
9b6c0ed66960
Use the routines factored into dmwav module.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
348 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, 1024); |
0 | 349 |
350 // Render audio data and output to file | |
351 if (optUsePlayTime) | |
352 dmMsg(1, "Rendering module (%d seconds) ...\n", optPlayTime); | |
353 else | |
354 dmMsg(1, "Rendering module ...\n"); | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
355 |
0 | 356 optPlayTime *= optOutFreq; |
357 dataTotal = 0; | |
358 dataWritten = 1; | |
68 | 359 while (plr->isPlaying && dataWritten > 0) |
0 | 360 { |
361 size_t writeLen = bufLen; | |
362 if (optUsePlayTime && (writeLen + dataTotal) > optPlayTime) | |
363 writeLen = optPlayTime - dataTotal; | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
364 |
0 | 365 if (writeLen > 0) |
366 { | |
68 | 367 jvmRenderAudio(dev, mb, writeLen); |
0 | 368 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) |
369 jssEncodeSample16((Uint16 *)mb, writeLen * optOutChannels, jsampSwapEndianess); | |
370 #endif | |
371 dataWritten = fwrite(mb, sampSize, writeLen, outFile); | |
372 if (dataWritten < writeLen) | |
373 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
374 dmErrorMsg("Error writing data!\n"); |
0 | 375 fclose(outFile); |
376 return 8; | |
377 } | |
378 dataTotal += dataWritten; | |
379 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
380 |
0 | 381 if (optUsePlayTime && dataTotal >= optPlayTime) |
382 break; | |
383 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
384 |
0 | 385 // Write the correct header |
386 if (fseek(outFile, 0L, SEEK_SET) != 0) | |
387 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
388 dmErrorMsg("Error rewinding to header position!\n"); |
0 | 389 return 9; |
390 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
391 |
206
9b6c0ed66960
Use the routines factored into dmwav module.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
392 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, dataTotal); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1199
diff
changeset
|
393 |
0 | 394 // Done! |
395 fclose(outFile); | |
68 | 396 |
397 jmpClose(plr); | |
398 jvmClose(dev); | |
399 jssFreeModule(mod); | |
400 jssClose(); | |
401 | |
0 | 402 dmMsg(1, "OK.\n"); |
403 return 0; | |
404 } |