-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
mdx-driver-test.c
115 lines (99 loc) · 3.4 KB
/
mdx-driver-test.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
#include <stdio.h>
#include <string.h>
#include <sndfile.h>
#include "tools.h"
#include "mdx.h"
#include "pdx.h"
#include "mdx_driver.h"
#include "pcm_timer_driver.h"
#include "adpcm_pcm_mix_driver.h"
#include "fm_opm_emu_driver.h"
int main(int argc, char **argv) {
struct mdx_driver mdx_driver;
struct pcm_timer_driver timer_driver;
struct adpcm_pcm_mix_driver adpcm_driver;
struct fm_opm_emu_driver fm_driver;
int opt_sample_rate = 44100;
pcm_timer_driver_init(&timer_driver, opt_sample_rate);
adpcm_pcm_mix_driver_init(&adpcm_driver, opt_sample_rate, 0);
fm_opm_emu_driver_init(&fm_driver, 0, opt_sample_rate);
mdx_driver_init(
&mdx_driver,
(struct timer_driver *)&timer_driver,
(struct fm_driver *)&fm_driver,
(struct adpcm_driver *)&adpcm_driver
);
struct mdx_file mdx_file;
struct pdx_file pdx_file;
size_t mdx_file_size;
uint8_t *mdx_file_data = load_file(argv[1], &mdx_file_size);
mdx_file_load(&mdx_file, mdx_file_data, mdx_file_size);
printf("Loaded MDX file \"%s\"\n", argv[1]);
if(mdx_file.pdx_filename_len > 0) {
char buf[256];
find_pdx_file(argv[1], (char *)mdx_file.pdx_filename, buf, sizeof(buf));
if(buf[0]) {
printf("Loading PDX \"%s\" from \"%s\"\n", mdx_file.pdx_filename, buf);
size_t pdx_data_size;
uint8_t *pdx_file_data = load_file(buf, &pdx_data_size);
if(pdx_file_data)
pdx_file_load(&pdx_file, pdx_file_data, pdx_data_size);
} else {
printf("Could not find \"%s\"\n", mdx_file.pdx_filename);
}
} else printf("No PDX file\n");
// load files above
mdx_driver_load(&mdx_driver, &mdx_file, &pdx_file);
SF_INFO sfinfo = {
.samplerate = opt_sample_rate,
.channels = 2,
.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16,
};
char wavname[256];
printf("Outputting to %s\n", wavname);
replace_ext(wavname, sizeof(wavname), argv[1], "wav");
SNDFILE *file = sf_open(wavname, SFM_WRITE, &sfinfo);
if(!file) {
printf("Failed to open file for writing\n");
return 1;
}
#define BUFFER_SIZE 1024
stream_sample_t bufL[BUFFER_SIZE], bufR[BUFFER_SIZE];
int16_t mixBuf[BUFFER_SIZE * 2];
int16_t *mixBufp = mixBuf;
for(int i = 0; i < 1000; i++) {
memset(mixBuf, 0, sizeof(mixBuf));
mixBufp = mixBuf;
int samples_remaining = BUFFER_SIZE;
while(samples_remaining > 0) {
int timer_samples = pcm_timer_driver_estimate(&timer_driver, samples_remaining);
int fm_samples = fm_opm_emu_driver_estimate(&fm_driver, samples_remaining);
int adpcm_samples = adpcm_pcm_mix_driver_estimate(&adpcm_driver, samples_remaining);
int samples = timer_samples;
if(fm_samples < samples)
samples = fm_samples;
if(adpcm_samples < samples)
samples = adpcm_samples;
// printf("running adpcm\n");
adpcm_pcm_mix_driver_run(&adpcm_driver, bufL, bufR, samples);
for(int n = 0; n < samples; n++) {
mixBufp[n * 2] += bufL[n];
mixBufp[n * 2 + 1] += bufR[n];
}
// printf("running fm\n");
fm_opm_emu_driver_run(&fm_driver, bufL, bufR, samples);
for(int n = 0; n < samples; n++) {
mixBufp[n * 2] += bufL[n];
mixBufp[n * 2 + 1] += bufR[n];
}
// printf("running pcm_timer_driver\n");
pcm_timer_driver_advance(&timer_driver, samples);
samples_remaining -= samples;
// printf("timer_samples=%d fm_samples=%d adpcm_samples=%d samples=%d samples_remaining=%d\n", timer_samples, fm_samples, adpcm_samples, samples, samples_remaining);
mixBufp += samples * 2;
}
sf_write_short(file, mixBuf, BUFFER_SIZE);
}
sf_close(file);
return 0;
}