Mercurial > hg > dmlib
annotate tests/plrtest.c @ 793:21ad08cdf408
Fix a potential NULL pointer usage.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 18 Oct 2013 14:31:12 +0300 |
parents | ed60a7ee3ebb |
children | 319792b2af4a |
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"); | |
67 | 144 result = jssLoadXM(file, &mod); |
0 | 145 #endif |
146 #ifdef JSS_SUP_JSSMOD | |
147 if (result != 0) | |
148 { | |
67 | 149 dmfseek(file, 0L, SEEK_SET); |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
150 dmMsg(1, "* Trying JSSMOD ...\n"); |
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
151 result = jssLoadJSSMOD(file, &mod); |
0 | 152 } |
153 #endif | |
67 | 154 dmf_close(file); |
0 | 155 |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
156 if (result != DMERR_OK) |
0 | 157 { |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
158 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
|
159 result, dmErrorStr(result)); |
0 | 160 return 3; |
161 } | |
162 | |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
163 // Try to convert it |
67 | 164 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
|
165 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
166 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
|
167 result, dmErrorStr(result)); |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
168 return 3; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
169 } |
0 | 170 |
171 // Initialize SDL audio | |
67 | 172 afmt.freq = 48000; |
173 afmt.format = AUDIO_S16SYS; | |
174 afmt.channels = 2; | |
303 | 175 |
0 | 176 // Initialize mixing device |
177 fprintf(stderr, "Initializing miniJSS mixer with: %d, %d, %d\n", | |
67 | 178 JSS_AUDIO_S16, afmt.channels, afmt.freq); |
0 | 179 |
67 | 180 dev = jvmInit(JSS_AUDIO_S16, afmt.channels, afmt.freq, JMIX_AUTO); |
181 if (dev == NULL) | |
182 { | |
0 | 183 fprintf(stderr, "jvmInit() returned NULL\n"); |
184 return 3; | |
185 } | |
186 | |
303 | 187 afmt.samples = afmt.freq / 4; |
67 | 188 afmt.callback = audioCallback; |
189 afmt.userdata = (void *) dev; | |
0 | 190 |
191 // Open the audio device | |
192 fprintf(stderr, "Trying to init SDL with: %d, %d, %d\n", | |
67 | 193 afmt.format, afmt.channels, afmt.freq); |
0 | 194 |
67 | 195 if (SDL_OpenAudio(&afmt, NULL) < 0) |
0 | 196 { |
197 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); | |
198 return 4; | |
199 } | |
200 | |
201 // Initialize player | |
67 | 202 if ((plr = jmpInit(dev)) == NULL) |
0 | 203 { |
204 fprintf(stderr, "jmpInit() returned NULL\n"); | |
205 return 4; | |
206 } | |
207 | |
208 // Initialize playing | |
67 | 209 jvmSetCallback(dev, jmpExec, plr); |
210 jmpSetModule(plr, mod); | |
211 jmpPlayOrder(plr, 0); | |
212 jvmSetGlobalVol(dev, 60); | |
0 | 213 |
214 // okay, main loop here ... "play" module and print out info | |
215 printf("----------------------------------------------------\n"); | |
292 | 216 SDL_LockAudio(); |
0 | 217 SDL_PauseAudio(0); |
292 | 218 SDL_UnlockAudio(); |
219 BOOL playing = TRUE; | |
220 while (playing) | |
0 | 221 { |
793
21ad08cdf408
Fix a potential NULL pointer usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
777
diff
changeset
|
222 JSSPattern *pattern = NULL; |
292 | 223 int currRow, prevRow; |
224 | |
225 JSS_LOCK(plr); | |
226 currRow = prevRow = plr->row; | |
227 JSS_UNLOCK(plr); | |
228 | |
229 while (currRow == prevRow && playing) | |
230 { | |
231 JSS_LOCK(plr); | |
232 currRow = plr->row; | |
233 playing = plr->isPlaying; | |
234 pattern = plr->pattern; | |
235 JSS_UNLOCK(plr); | |
0 | 236 SDL_Delay(50); |
292 | 237 } |
0 | 238 |
793
21ad08cdf408
Fix a potential NULL pointer usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
777
diff
changeset
|
239 if (playing && pattern != NULL) |
292 | 240 { |
241 printRow(stdout, pattern, currRow); | |
242 printf("\n"); | |
243 } | |
0 | 244 } |
245 | |
246 printf("----------------------------------------------------\n"); | |
247 | |
292 | 248 SDL_LockAudio(); |
67 | 249 SDL_PauseAudio(1); |
250 jmpClose(plr); | |
251 jvmClose(dev); | |
252 jssFreeModule(mod); | |
253 SDL_UnlockAudio(); | |
254 | |
255 SDL_Quit(); | |
0 | 256 |
69
7b97df6f8a97
Add jssClose() in the shutdown.
Matti Hamalainen <ccr@tnsp.org>
parents:
67
diff
changeset
|
257 jssClose(); |
7b97df6f8a97
Add jssClose() in the shutdown.
Matti Hamalainen <ccr@tnsp.org>
parents:
67
diff
changeset
|
258 |
0 | 259 return 0; |
260 } |