-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_samples.c
More file actions
65 lines (53 loc) · 1.63 KB
/
dump_samples.c
File metadata and controls
65 lines (53 loc) · 1.63 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mod_file.h"
#include "wav_file.h"
static void dump_sample(const struct MOD_SAMPLE *sample, int frequency, const char *out_file_prefix, int num)
{
char filename[1024];
snprintf(filename, sizeof(filename), "%s%02d.wav", out_file_prefix, num+1);
unsigned char *data = malloc(sample->len);
if (! data) {
printf("ERROR: out of memory for sample %d\n", num+1);
return;
}
for (uint32_t i = 0; i < sample->len; i++) {
data[i] = sample->data[i] + 128;
}
printf("-> writing %s\n", filename);
if (wav_write_file(filename, frequency, 8, 1, data, sample->len) != 0) {
printf("ERROR: can't write '%s'\n", filename);
}
free(data);
}
int main(int argc, char *argv[])
{
if (argc < 2 || argc > 4) {
printf("USAGE: %s filename.mod [frequency] [sample_file_prefix]\n", argv[0]);
return 1;
}
char *mod_filename = argv[1];
int frequency = (argc > 2) ? atoi(argv[2]) : 11025;
char *sample_file_prefix = (argc > 3) ? argv[3] : "sample_";
if (frequency <= 0) {
printf("%s: invalid frequency: '%s'\n", argv[0], argv[2]);
exit(1);
}
// read mod file
struct MOD_FILE *mod_file = mod_file_read(mod_filename);
if (! mod_file) {
printf("%s: can't open '%s'\n", argv[0], mod_filename);
return 1;
}
struct MOD_DATA *mod = &mod_file->mod;
// dump smaples
for (int i = 0; i < 31; i++) {
struct MOD_SAMPLE *sample = &mod->samples[i];
if (sample->len == 0 || ! sample->data) continue;
dump_sample(sample, frequency, sample_file_prefix, i);
}
// cleanup
mod_file_free(mod_file);
return 0;
}