Mercurial > hg > dmlib
annotate testpl.c @ 8:fc097f7717df
Fix JSSMod loading in viewmod and testpl.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 28 Sep 2012 04:47:10 +0300 |
parents | 32250b436bca |
children | 033c660c25f5 |
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 { | |
112 SDL_AudioSpec *a_desired, *a_obtained; | |
113 DMResource *inFile; | |
114 char *sname = NULL; | |
115 int result = -1; | |
116 JSSModule *m; | |
117 JSSMixer *d; | |
118 JSSPlayer *p; | |
119 int buflen = 4096; | |
120 | |
121 if (argc > 1) | |
122 sname = argv[1]; | |
123 | |
124 // Open the files | |
125 if (sname == NULL) | |
126 inFile = dmf_create_stdio_stream(stdin); | |
127 else if ((inFile = dmf_create_stdio(sname)) == NULL) | |
128 { | |
129 fprintf(stderr, "Error opening input file '%s'. (%s)\n", sname, strerror(errno)); | |
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"); | |
142 result = jssLoadXM(inFile, &m); | |
143 #endif | |
144 #ifdef JSS_SUP_JSSMOD | |
145 if (result != 0) | |
146 { | |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
147 size_t bufgot, bufsize = dmfsize(inFile); |
0 | 148 Uint8 *buf = dmMalloc(bufsize); |
8
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
149 dmfseek(inFile, 0L, SEEK_SET); |
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); |
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
151 if ((bufgot = dmfread(buf, 1, bufsize, inFile)) != 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", |
fc097f7717df
Fix JSSMod loading in viewmod and testpl.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
154 bufgot, dmferror(inFile), dmErrorStr(dmferror(inFile))); |
0 | 155 return 2; |
156 } | |
157 result = jssLoadJSSMOD(buf, bufsize, &m); | |
158 dmFree(buf); | |
159 } | |
160 #endif | |
161 dmf_close(inFile); | |
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 | |
170 | |
171 // Initialize SDL audio | |
172 fprintf(stderr, "Pre-initializing params\n"); | |
173 a_desired = dmMalloc(sizeof(SDL_AudioSpec)); | |
174 a_obtained = dmMalloc(sizeof(SDL_AudioSpec)); | |
175 if (!a_desired || !a_obtained) | |
176 { | |
177 fprintf(stderr, "Could not allocate SDL shit\n"); | |
178 return 3; | |
179 } | |
180 | |
181 a_desired->freq = 48000; | |
182 a_desired->format = AUDIO_S16SYS; | |
183 a_desired->channels = 2; | |
184 | |
185 // Initialize mixing device | |
186 fprintf(stderr, "Initializing miniJSS mixer with: %d, %d, %d\n", | |
187 JSS_AUDIO_S16, a_desired->channels, a_desired->freq); | |
188 | |
189 d = jvmInit(JSS_AUDIO_S16, a_desired->channels, a_desired->freq, JMIX_AUTO); | |
190 if (!d) { | |
191 fprintf(stderr, "jvmInit() returned NULL\n"); | |
192 return 3; | |
193 } | |
194 | |
195 a_desired->samples = buflen; | |
196 a_desired->callback = audioCallback; | |
197 a_desired->userdata = (void *) d; | |
198 | |
199 // Open the audio device | |
200 fprintf(stderr, "Trying to init SDL with: %d, %d, %d\n", | |
201 a_desired->format, a_desired->channels, a_desired->freq); | |
202 | |
203 if (SDL_OpenAudio(a_desired, a_obtained) < 0) | |
204 { | |
205 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); | |
206 return 4; | |
207 } | |
208 | |
209 fprintf(stderr, "Got: %d, %d, %d\n", | |
210 a_obtained->format, a_obtained->channels, a_obtained->freq); | |
211 | |
212 if ((a_obtained->format != a_desired->format) || | |
213 (a_obtained->channels != a_desired->channels) || | |
214 (a_obtained->freq != a_desired->freq)) | |
215 { | |
216 fprintf(stderr, "Could not get wanted audio parameters from SDL!\n"); | |
217 return 8; | |
218 } | |
219 | |
220 free(a_desired); | |
221 | |
222 // Initialize player | |
223 p = jmpInit(d); | |
224 if (!p) | |
225 { | |
226 fprintf(stderr, "jmpInit() returned NULL\n"); | |
227 return 4; | |
228 } | |
229 | |
230 // Set callback | |
231 jvmSetCallback(d, jmpExec, p); | |
232 | |
233 // Initialize playing | |
234 jmpSetModule(p, m); | |
235 jmpPlayOrder(p, 0); | |
236 jvmSetGlobalVol(d, 60); | |
237 | |
238 // okay, main loop here ... "play" module and print out info | |
239 printf("----------------------------------------------------\n"); | |
240 SDL_PauseAudio(0); | |
241 while (p->isPlaying) | |
242 { | |
243 int r = p->iRow; | |
244 while (r == p->iRow && p->isPlaying) | |
245 SDL_Delay(50); | |
246 | |
247 printRow(stdout, p->pPattern, p->iRow); | |
248 printf("\n"); | |
249 } | |
250 | |
251 printf("----------------------------------------------------\n"); | |
252 | |
253 // Free module data | |
254 jmpClose(p); | |
255 jvmClose(d); | |
256 jssFreeModule(m); | |
257 m = NULL; | |
258 | |
259 return 0; | |
260 } |