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