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