Mercurial > hg > dmlib
annotate testpl.c @ 303:2b5472173355
Fix buffer length.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 11 Oct 2012 18:07:00 +0300 |
parents | 43312a36811f |
children |
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 |
120 if (argc > 1) | |
121 sname = argv[1]; | |
122 | |
123 // Open the files | |
124 if (sname == NULL) | |
67 | 125 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
|
126 else if ((file = dmf_create_stdio(sname, "rb")) == NULL) |
0 | 127 { |
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
|
128 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
|
129 sname, strerror(errno)); |
0 | 130 return 1; |
131 } | |
132 | |
133 // Initialize miniJSS | |
134 fprintf(stderr, "Initializing miniJSS\n"); | |
135 jssInit(); | |
136 | |
137 | |
138 // Read module file | |
139 fprintf(stderr, "Reading file: %s\n", sname); | |
140 #ifdef JSS_SUP_XM | |
141 fprintf(stderr, "* Trying XM...\n"); | |
67 | 142 result = jssLoadXM(file, &mod); |
0 | 143 #endif |
144 #ifdef JSS_SUP_JSSMOD | |
145 if (result != 0) | |
146 { | |
67 | 147 size_t bufgot, bufsize = dmfsize(file); |
0 | 148 Uint8 *buf = dmMalloc(bufsize); |
67 | 149 dmfseek(file, 0L, SEEK_SET); |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
150 fprintf(stderr, "* Trying JSSMOD (%d bytes, %p)...\n", bufsize, buf); |
67 | 151 if ((bufgot = dmfread(buf, 1, bufsize, file)) != bufsize) |
0 | 152 { |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
153 fprintf(stderr, "Error reading file (not enough data %d), #%d: %s\n", |
67 | 154 bufgot, dmferror(file), dmErrorStr(dmferror(file))); |
0 | 155 return 2; |
156 } | |
67 | 157 result = jssLoadJSSMOD(buf, bufsize, &mod); |
0 | 158 dmFree(buf); |
159 } | |
160 #endif | |
67 | 161 dmf_close(file); |
0 | 162 |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
163 if (result != DMERR_OK) |
0 | 164 { |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
165 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
|
166 result, dmErrorStr(result)); |
0 | 167 return 3; |
168 } | |
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 } | |
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); |
0 | 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 } | |
207 | |
208 // Initialize player | |
67 | 209 if ((plr = jmpInit(dev)) == NULL) |
0 | 210 { |
211 fprintf(stderr, "jmpInit() returned NULL\n"); | |
212 return 4; | |
213 } | |
214 | |
215 // Initialize playing | |
67 | 216 jvmSetCallback(dev, jmpExec, plr); |
217 jmpSetModule(plr, mod); | |
218 jmpPlayOrder(plr, 0); | |
219 jvmSetGlobalVol(dev, 60); | |
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 { |
292 | 229 JSSPattern *pattern; |
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 |
292 | 246 if (playing) |
247 { | |
248 printRow(stdout, pattern, currRow); | |
249 printf("\n"); | |
250 } | |
0 | 251 } |
252 | |
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 } |