Mercurial > hg > dmlib
annotate tests/plrtest.c @ 2298:b5abfff07ca9
Add new DMGrowBuf helper functions dmGrowBufCopyOffsSize() and
dmGrowBufConstCopyOffsSize().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 04 Jul 2019 10:54:16 +0300 |
parents | 186cf6a7d634 |
children | aacf3bd1cceb |
rev | line source |
---|---|
0 | 1 #include "jss.h" |
2 #include "jssmod.h" | |
3 #include "jssmix.h" | |
4 #include "jssplr.h" | |
5 #include <string.h> | |
6 #include <unistd.h> | |
7 #include <SDL.h> | |
8 | |
9 | |
10 static const char patNoteTable[12][3] = | |
11 { | |
12 "C-", "C#", "D-", | |
13 "D#", "E-", "F-", | |
14 "F#", "G-", "G#", | |
15 "A-", "A#", "B-" | |
16 }; | |
17 | |
18 | |
19 #define jmpNMODEffectTable (36) | |
20 static const char jmpMODEffectTable[jmpNMODEffectTable] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
21 | |
22 | |
1603 | 23 void printRow(FILE * fh, const JSSPattern * pat, const int row) |
0 | 24 { |
1603 | 25 const JSSNote *note = &(pat->data[pat->nchannels * row]); |
26 int width = pat->nchannels < 5 ? pat->nchannels : 5; | |
0 | 27 |
1603 | 28 fprintf(fh, "%.2x: ", row); |
0 | 29 |
1603 | 30 for (int channel = 0; channel < width; channel++) |
0 | 31 { |
1603 | 32 switch (note->note) |
0 | 33 { |
34 case jsetNotSet: | |
1603 | 35 fprintf(fh, "... "); |
0 | 36 break; |
37 case jsetNoteOff: | |
1603 | 38 fprintf(fh, "=== "); |
0 | 39 break; |
40 default: | |
1603 | 41 fprintf(fh, "%s%d ", |
42 patNoteTable[note->note % 12], | |
43 note->note / 12); | |
0 | 44 break; |
45 } | |
46 | |
1603 | 47 if (note->instrument != jsetNotSet) |
48 fprintf(fh, "%.2x ", note->instrument + 1); | |
0 | 49 else |
1603 | 50 fprintf(fh, ".. "); |
0 | 51 |
1603 | 52 if (note->volume == jsetNotSet) |
53 fprintf(fh, ".. "); | |
54 else | |
55 if (note->volume >= 0x00 && note->volume <= 0x40) | |
56 fprintf(fh, "%.2x ", note->volume); | |
0 | 57 else |
58 { | |
1603 | 59 char ch; |
60 switch (note->volume & 0xf0) | |
0 | 61 { |
1603 | 62 case 0x50: ch = '-'; break; |
63 case 0x60: ch = '+'; break; | |
64 case 0x70: ch = '/'; break; | |
65 case 0x80: ch = '\\'; break; | |
66 case 0x90: ch = 'S'; break; | |
67 case 0xa0: ch = 'V'; break; | |
68 case 0xb0: ch = 'P'; break; | |
69 case 0xc0: ch = '<'; break; | |
70 case 0xd0: ch = '>'; break; | |
71 case 0xe0: ch = 'M'; break; | |
72 default: ch = '?'; break; | |
0 | 73 } |
1603 | 74 fprintf(fh, "%c%x ", |
75 ch, note->volume & 0x0f); | |
0 | 76 } |
77 | |
1603 | 78 if (note->effect >= 0 && note->effect < jmpNMODEffectTable) |
79 fprintf(fh, "%c", jmpMODEffectTable[note->effect]); | |
0 | 80 else |
1603 | 81 fprintf(fh, "%c", note->effect == jsetNotSet ? '.' : '?'); |
0 | 82 |
1603 | 83 if (note->param != jsetNotSet) |
84 fprintf(fh, "%.2x|", note->param); | |
0 | 85 else |
1603 | 86 fprintf(fh, "..|"); |
0 | 87 |
1603 | 88 note++; |
0 | 89 } |
90 } | |
91 | |
92 | |
93 void audioCallback(void *userdata, Uint8 *stream, int len) | |
94 { | |
95 JSSMixer *d = (JSSMixer *) userdata; | |
96 | |
97 if (d != NULL) | |
98 { | |
99 jvmRenderAudio(d, stream, len / jvmGetSampleSize(d)); | |
100 } | |
101 } | |
102 | |
103 | |
104 int main(int argc, char *argv[]) | |
105 { | |
67 | 106 SDL_AudioSpec afmt; |
107 DMResource *file = NULL; | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
667
diff
changeset
|
108 char *inFilename = NULL; |
0 | 109 int result = -1; |
67 | 110 JSSModule *mod = NULL; |
111 JSSMixer *dev = NULL; | |
112 JSSPlayer *plr = NULL; | |
0 | 113 |
114 if (argc > 1) | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
667
diff
changeset
|
115 inFilename = argv[1]; |
0 | 116 |
117 // Open the files | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
667
diff
changeset
|
118 if (inFilename == NULL) |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1603
diff
changeset
|
119 result = dmf_open_stdio_stream(stdin, &file); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
667
diff
changeset
|
120 else |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1603
diff
changeset
|
121 result = dmf_open_stdio(inFilename, "rb", &file); |
2046 | 122 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
667
diff
changeset
|
123 if (result != DMERR_OK) |
0 | 124 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
667
diff
changeset
|
125 fprintf(stderr, "Error opening input file '%s', #%d: %s\n", |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
667
diff
changeset
|
126 inFilename, result, dmErrorStr(result)); |
0 | 127 return 1; |
128 } | |
2046 | 129 |
0 | 130 // Initialize miniJSS |
131 fprintf(stderr, "Initializing miniJSS\n"); | |
132 jssInit(); | |
133 | |
2046 | 134 |
0 | 135 // Read module file |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
136 dmMsg(1, "Reading file: %s\n", inFilename); |
0 | 137 #ifdef JSS_SUP_XM |
797
f066e9dccf29
Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
138 result = jssLoadXM(file, &mod, TRUE); |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
139 #endif |
0 | 140 #ifdef JSS_SUP_JSSMOD |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
141 dmfreset(file); |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
142 if (result != DMERR_OK) |
0 | 143 { |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
144 dmMsg(1, "* Trying JSSMOD ...\n"); |
797
f066e9dccf29
Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
145 result = jssLoadJSSMOD(file, &mod, TRUE); |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
146 dmfreset(file); |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
147 if (result == DMERR_OK) |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
148 result = jssLoadJSSMOD(file, &mod, FALSE); |
0 | 149 } |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
150 else |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
151 { |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
152 dmMsg(2, "* Trying XM...\n"); |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
153 result = jssLoadXM(file, &mod, FALSE); |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
154 } |
794
319792b2af4a
Fix non-fallback loading of JSSMod.
Matti Hamalainen <ccr@tnsp.org>
parents:
793
diff
changeset
|
155 #endif |
67 | 156 dmf_close(file); |
0 | 157 |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
158 // Check for errors, we still might have some data tho |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
159 if (result != DMERR_OK) |
0 | 160 { |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
161 dmErrorMsg("Error loading module file, %d: %s\n", |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
162 result, dmErrorStr(result)); |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
163 } |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
164 |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
165 // Check if we have anything |
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
166 if (mod == NULL) |
0 | 167 return 3; |
1204
0d173edbabe2
Do the file probing stuff here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
168 |
0 | 169 |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
170 // Try to convert it |
67 | 171 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:
8
diff
changeset
|
172 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
173 fprintf(stderr, "Could not convert module for playing, %d: %s\n", |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
174 result, dmErrorStr(result)); |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
175 return 3; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
176 } |
0 | 177 |
178 // Initialize SDL audio | |
67 | 179 afmt.freq = 48000; |
180 afmt.format = AUDIO_S16SYS; | |
181 afmt.channels = 2; | |
303 | 182 |
0 | 183 // Initialize mixing device |
184 fprintf(stderr, "Initializing miniJSS mixer with: %d, %d, %d\n", | |
67 | 185 JSS_AUDIO_S16, afmt.channels, afmt.freq); |
0 | 186 |
67 | 187 dev = jvmInit(JSS_AUDIO_S16, afmt.channels, afmt.freq, JMIX_AUTO); |
188 if (dev == NULL) | |
189 { | |
0 | 190 fprintf(stderr, "jvmInit() returned NULL\n"); |
191 return 3; | |
192 } | |
2046 | 193 |
303 | 194 afmt.samples = afmt.freq / 4; |
67 | 195 afmt.callback = audioCallback; |
196 afmt.userdata = (void *) dev; | |
0 | 197 |
198 // Open the audio device | |
199 fprintf(stderr, "Trying to init SDL with: %d, %d, %d\n", | |
67 | 200 afmt.format, afmt.channels, afmt.freq); |
2046 | 201 |
67 | 202 if (SDL_OpenAudio(&afmt, NULL) < 0) |
0 | 203 { |
204 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); | |
205 return 4; | |
206 } | |
2046 | 207 |
0 | 208 // Initialize player |
67 | 209 if ((plr = jmpInit(dev)) == NULL) |
0 | 210 { |
211 fprintf(stderr, "jmpInit() returned NULL\n"); | |
212 return 4; | |
213 } | |
2046 | 214 |
0 | 215 // Initialize playing |
67 | 216 jvmSetCallback(dev, jmpExec, plr); |
217 jmpSetModule(plr, mod); | |
218 jmpPlayOrder(plr, 0); | |
1603 | 219 jvmSetGlobalVol(dev, 100); |
0 | 220 |
221 // okay, main loop here ... "play" module and print out info | |
222 printf("----------------------------------------------------\n"); | |
292 | 223 SDL_LockAudio(); |
0 | 224 SDL_PauseAudio(0); |
292 | 225 SDL_UnlockAudio(); |
226 BOOL playing = TRUE; | |
227 while (playing) | |
0 | 228 { |
793
21ad08cdf408
Fix a potential NULL pointer usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
777
diff
changeset
|
229 JSSPattern *pattern = NULL; |
292 | 230 int currRow, prevRow; |
231 | |
232 JSS_LOCK(plr); | |
233 currRow = prevRow = plr->row; | |
234 JSS_UNLOCK(plr); | |
235 | |
236 while (currRow == prevRow && playing) | |
237 { | |
238 JSS_LOCK(plr); | |
239 currRow = plr->row; | |
240 playing = plr->isPlaying; | |
241 pattern = plr->pattern; | |
242 JSS_UNLOCK(plr); | |
0 | 243 SDL_Delay(50); |
292 | 244 } |
0 | 245 |
793
21ad08cdf408
Fix a potential NULL pointer usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
777
diff
changeset
|
246 if (playing && pattern != NULL) |
292 | 247 { |
248 printRow(stdout, pattern, currRow); | |
249 printf("\n"); | |
250 } | |
0 | 251 } |
2046 | 252 |
0 | 253 printf("----------------------------------------------------\n"); |
254 | |
292 | 255 SDL_LockAudio(); |
67 | 256 SDL_PauseAudio(1); |
257 jmpClose(plr); | |
258 jvmClose(dev); | |
259 jssFreeModule(mod); | |
260 SDL_UnlockAudio(); | |
261 | |
262 SDL_Quit(); | |
0 | 263 |
69
7b97df6f8a97
Add jssClose() in the shutdown.
Matti Hamalainen <ccr@tnsp.org>
parents:
67
diff
changeset
|
264 jssClose(); |
7b97df6f8a97
Add jssClose() in the shutdown.
Matti Hamalainen <ccr@tnsp.org>
parents:
67
diff
changeset
|
265 |
0 | 266 return 0; |
267 } |