-
Notifications
You must be signed in to change notification settings - Fork 3
/
JUDASWAV.C
462 lines (427 loc) · 13.9 KB
/
JUDASWAV.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* JUDAS WAV handling
*/
#include <stdlib.h>
#include <stdio.h>
#include <mem.h>
#include <io.h>
#include <fcntl.h>
#include <string.h>
#include "judas.h"
#ifdef __DJGPP__
#include <sys/stat.h> /* for mode definitions */
#include <unistd.h> /* compatibility mode */
#define HANDLE_PRAGMA_PACK_PUSH_POP 1
#endif
#define MAXFILES 100 /* 100 max wav files per 1 library */
extern char *filewriterbuffer;
extern int filewriterbuffersize;
extern unsigned char judas_initialized;
extern void safemixer(void *address, int length);
SAMPLE *judas_loadwav(char *name);
void judas_setwavlib(char *name);
#pragma pack(push,1)
typedef struct
{
char rifftext[4];
unsigned totallength;
char wavetext[4];
char formattext[4];
unsigned formatlength;
unsigned short format;
unsigned short channels;
unsigned freq;
unsigned avgbytes;
unsigned short blockalign;
unsigned short bits;
char datatext[4];
unsigned datalength;
} WAV_HEADER;
typedef struct
{
unsigned offset; // offset in library for this wav entry
unsigned size; // size of this wav entry
char filename[64]; // wav filename
} WAV_ENTRY;
typedef struct
{
char logo[4]; // JDSL format sign (header has max 100 wav entries)
unsigned filescount; // number of wav files in library
unsigned encoding; // encoding method
WAV_ENTRY files[MAXFILES]; // single wav entry
} LIB_HEADER;
#pragma pack(pop)
static LIB_HEADER libheader;
static char libname[260] = {0};
/*
* Opens a file for wav writing.
* Returns nonnegative file handle if successful, -1 on error
*/
int judas_wavwriter_open(char *name)
{
WAV_HEADER header;
int handle;
if (!judas_initialized) return -1;
header.totallength = 0; // Will be determined after writing the pcm data
header.datalength = 0; // Will be determined after writing the pcm data
memcpy(header.rifftext, "RIFF", 4);
memcpy(header.wavetext, "WAVE", 4);
memcpy(header.formattext, "fmt ", 4);
memcpy(header.datatext, "data", 4);
header.formatlength = 16;
header.format = 1;
header.freq = judas_mixrate;
switch (judas_mixmode) {
case (MONO | EIGHTBIT):
header.channels = 1;
header.avgbytes = header.freq;
header.blockalign = 1;
header.bits = 8;
break;
case (MONO | SIXTEENBIT):
header.channels = 1;
header.avgbytes = header.freq * 2;
header.blockalign = 2;
header.bits = 16;
break;
case (STEREO | EIGHTBIT):
header.channels = 2;
header.avgbytes = header.freq * 2;
header.blockalign = 2;
header.bits = 8;
break;
case (STEREO | SIXTEENBIT):
header.channels = 2;
header.avgbytes = header.freq * 4;
header.blockalign = 4;
header.bits = 16;
break;
}
handle = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE);
if (handle == -1) {
return handle;
}
if (write(handle, &header, sizeof(WAV_HEADER)) < sizeof(WAV_HEADER)) {
close(handle);
return -1;
}
if (filewriterbuffer == NULL) {
if (!(filewriterbuffer = locked_malloc(filewriterbuffersize))) return -1;
}
return handle;
}
/*
* Wav writer. Writes one bufferlength of mixed data into the wav file.
* Returns nonnegative file handle if successful, -1 on error
*/
int judas_wavwriter_writesome(int handle)
{
if (!judas_initialized) return -1;
if (handle == -1) return -1;
safemixer(filewriterbuffer, filewriterbuffersize);
if (write(handle, filewriterbuffer, filewriterbuffersize) < filewriterbuffersize) {
close(handle);
return -1;
}
return handle;
}
/*
* Finishes wav writing and closes the wav writer file.
* Returns 0 on success, -1 on error
*/
int judas_wavwriter_close(int handle)
{
int totallength;
int datalength;
if (filewriterbuffer) locked_free(filewriterbuffer);
totallength = filelength(handle);
datalength = totallength - sizeof(WAV_HEADER);
totallength -= 8;
if (datalength < 0) {
close(handle);
return -1;
}
if (lseek(handle, 4, SEEK_SET) == -1) {
close(handle);
return -1;
};
if (write(handle, &totallength, 4) == -1) {
close(handle);
return -1;
}
if (lseek(handle, sizeof(WAV_HEADER)-4, SEEK_SET) == -1) {
close(handle);
return -1;
};
if (write(handle, &datalength, 4) == -1) {
close(handle);
return -1;
}
return close(handle);
}
SAMPLE *judas_loadwav(char *name)
{
int length;
int reallength;
int handle;
SAMPLE *smp;
WAV_HEADER header;
int fcount;
/* Don't waste memory if Nosound */
judas_error = JUDAS_OK;
if (judas_device == DEV_NOSOUND)
{
return &fakesample;
}
/*
* Try to open
*/
judas_error = JUDAS_OPEN_ERROR;
if (!libname[0]) strcpy (libname, "jdswav.lib");
handle = judas_open(libname);
if (handle != -1) {
/* library open success */
judas_error = JUDAS_READ_ERROR;
if (judas_read (handle, &libheader, sizeof(LIB_HEADER)) != sizeof(LIB_HEADER)) {
judas_close (handle);
return NULL;
}
/* check JDSL library logo */
if ((libheader.logo[0] != 'J') || (libheader.logo[1] != 'D') || (libheader.logo[2] != 'S')) {
judas_close (handle);
return NULL;
}
/* ni compare files and set library file pointer on success */
for (fcount = 0; fcount < libheader.filescount; fcount++) {
if (strnicmp(name, (char *)&libheader.files[fcount].filename, sizeof(name)) == 0) {
if (judas_seek(handle, libheader.files[fcount].offset, SEEK_SET) == -1) {
judas_close (handle);
return NULL;
}
break; // success - wav entry was found
}
}
/* close library handle if wav file not found in library */
if (fcount == libheader.filescount) {
judas_close (handle);
}
}
/* try to load as single wav file if wav entry not found in library */
if (handle == -1) {
handle = judas_open(name);
if (handle == -1) return NULL;
}
/*
* Read identification
*/
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, &header, 12) != 12)
{
judas_close(handle);
return NULL;
}
judas_error = JUDAS_WRONG_FORMAT;
if (memcmp("RIFF", header.rifftext, 4))
{
judas_close(handle);
return NULL;
}
if (memcmp("WAVE", header.wavetext, 4))
{
judas_close(handle);
return NULL;
}
/*
* Search for the FORMAT chunk
*/
for (;;)
{
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, &header.formattext, 8) != 8)
{
judas_close(handle);
return NULL;
}
if (!memcmp("fmt ", &header.formattext, 4)) break;
if (judas_seek(handle, header.formatlength, SEEK_CUR) == -1)
{
judas_close(handle);
return NULL;
}
}
/*
* Read in the FORMAT chunk
*/
if (judas_read(handle, &header.format, 16) != 16)
{
judas_close(handle);
return NULL;
}
/*
* Skip data if the format chunk was bigger than what we use
*/
if (judas_seek(handle, header.formatlength - 16, SEEK_CUR) == -1)
{
judas_close(handle);
return NULL;
}
/*
* Check for correct format
*/
judas_error = JUDAS_WRONG_FORMAT;
if (header.format != 1)
{
judas_close(handle);
return NULL;
}
/*
* Search for the DATA chunk
*/
for (;;)
{
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, &header.datatext, 8) != 8)
{
judas_close(handle);
return NULL;
}
if (!memcmp("data", &header.datatext, 4)) break;
if (judas_seek(handle, header.datalength, SEEK_CUR) == -1)
{
judas_close(handle);
return NULL;
}
}
/*
* Allocate sample, load audio data, do processing (unsigned->signed,
* stereo->mono)
*/
length = header.datalength;
reallength = length;
if (header.channels == 2) reallength >>= 1;
smp = judas_allocsample(reallength);
if (!smp)
{
judas_close(handle);
return NULL;
}
if (header.channels == 2)
{
if (header.bits == 16)
{
unsigned count = length >> 2;
short *buffer;
short *src;
short *dest;
judas_error = JUDAS_OUT_OF_MEMORY;
buffer = malloc(length);
if (!buffer)
{
judas_freesample(smp);
judas_close(handle);
return NULL;
}
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, buffer, length) != length)
{
free(buffer);
judas_freesample(smp);
judas_close(handle);
return NULL;
}
src = buffer;
dest = (short *)smp->start;
while (count--)
{
int average = (src[0] + src[1]) / 2;
*dest = average;
src += 2;
dest++;
}
free(buffer);
smp->repeat = smp->start;
smp->end = smp->start + reallength;
smp->voicemode = VM_ON | VM_16BIT;
}
else
{
unsigned count = length >> 1;
unsigned char *buffer;
unsigned char *src;
signed char *dest;
judas_error = JUDAS_OUT_OF_MEMORY;
buffer = malloc(length);
if (!buffer)
{
judas_freesample(smp);
judas_close(handle);
return NULL;
}
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, buffer, length) != length)
{
free(buffer);
judas_freesample(smp);
judas_close(handle);
return NULL;
}
src = buffer;
dest = (signed char *)smp->start;
while (count--)
{
int average = (src[0] + src[1] - 0x100) / 2;
*dest = average;
src += 2;
dest++;
}
free(buffer);
smp->repeat = smp->start;
smp->end = smp->start + reallength;
smp->voicemode = VM_ON;
}
}
else
{
if (header.bits == 16)
{
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, smp->start, length) != length)
{
judas_freesample(smp);
judas_close(handle);
return NULL;
}
smp->repeat = smp->start;
smp->end = smp->start + length;
smp->voicemode = VM_ON | VM_16BIT;
}
else
{
unsigned count = length;
char *src = smp->start;
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, smp->start, length) != length)
{
judas_freesample(smp);
judas_close(handle);
return NULL;
}
while (count--)
{
*src += 0x80;
src++;
}
smp->repeat = smp->start;
smp->end = smp->start + length;
smp->voicemode = VM_ON;
}
}
judas_ipcorrect(smp);
judas_error = JUDAS_OK;
judas_close(handle);
return smp;
}
void judas_setwavlib(char *name)
{
strcpy (libname, name);
}