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