-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
mdxplay.c
206 lines (167 loc) · 5.24 KB
/
mdxplay.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
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <portaudio.h>
#include "cmdline.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 "vgm_logger.h"
#include "fm_opm_emu_driver.h"
#include "mamedef.h"
#define SAMPLE_RATE 44100
#define BUFFER_SIZE 2048
int opt_channel_mask = 0xffff;
int opt_loops = 1;
char *opt_vgm_file = 0;
stream_sample_t bufL[BUFFER_SIZE], bufR[BUFFER_SIZE], chipBufL[BUFFER_SIZE], chipBufR[BUFFER_SIZE];
int16_t buf[BUFFER_SIZE * 2];
struct mdx_driver mdx_driver;
static void sigint_handler(int s) {
signal(SIGINT, SIG_DFL);
printf("Fading out...\n");
mdx_driver_start_fadeout(&mdx_driver, 4);
}
int main(int argc, char **argv) {
int optind = cmdline_parse_args(argc, argv, (struct cmdline_option[]){
{
'm', "mask",
"Channel mask",
"mask",
TYPE_REQUIRED,
TYPE_INT, &opt_channel_mask
},
{
'l', "loops",
"Loop <n> times.",
"n",
TYPE_REQUIRED,
TYPE_INT, &opt_loops
},
{
'V', "vgm-file",
"Enable VGM logging",
"file",
TYPE_REQUIRED,
TYPE_STRING, &opt_vgm_file
},
CMDLINE_ARG_TERMINATOR
}, 1, -1, "<file.mdx>");
if(optind < 0) exit(-optind);
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);
struct vgm_logger vgm_logger;
if(opt_vgm_file && *opt_vgm_file)
vgm_logger_init(&vgm_logger, opt_vgm_file);
fm_opm_emu_driver_init(&fm_driver, opt_vgm_file && *opt_vgm_file ? &vgm_logger : 0, opt_sample_rate);
mdx_driver_init(
&mdx_driver,
(struct timer_driver *)&timer_driver,
(struct fm_driver *)&fm_driver,
(struct adpcm_driver *)&adpcm_driver
);
stream_sample_t bufL[BUFFER_SIZE], bufR[BUFFER_SIZE];
stream_sample_t mixBufL[BUFFER_SIZE], mixBufR[BUFFER_SIZE];
signal(SIGINT, sigint_handler);
PaStream *stream;
PaError err;
err = Pa_Initialize();
if( err != paNoError ) goto error;
err = Pa_OpenDefaultStream(
&stream,
0, /* no input channels */
2, /* stereo output */
paInt16, /* 32 bit floating point output */
SAMPLE_RATE,
BUFFER_SIZE, /* frames per buffer */
NULL,
NULL
);
if( err != paNoError ) goto error;
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
for(int i = optind; i < argc; i++) {
struct mdx_file mdx_file;
struct pdx_file pdx_file;
size_t mdx_file_size;
uint8_t *mdx_file_data = load_file(argv[i], &mdx_file_size);
if(!mdx_file_data)
continue;
mdx_file_load(&mdx_file, mdx_file_data, mdx_file_size);
printf("Loaded MDX file \"%s\"\n", argv[i]);
if(mdx_file.pdx_filename_len > 0) {
char buf[256];
find_pdx_file(argv[i], (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");
mdx_driver_load(&mdx_driver, &mdx_file, &pdx_file);
while(!mdx_driver.ended) {
stream_sample_t *mixBufLp = mixBufL, *mixBufRp = mixBufR;
int16_t *out = buf;
int samples_remaining = BUFFER_SIZE;
memset(mixBufL, 0, sizeof(mixBufL));
memset(mixBufR, 0, sizeof(mixBufR));
while(samples_remaining > 0) {
// compute the number of samples to render
int samples = samples_remaining;
int timer_samples = pcm_timer_driver_estimate(&timer_driver, samples_remaining);
if(timer_samples < samples) samples = timer_samples;
int fm_samples = fm_opm_emu_driver_estimate(&fm_driver, samples_remaining);
if(fm_samples < samples) samples = fm_samples;
int adpcm_samples = adpcm_pcm_mix_driver_estimate(&adpcm_driver, samples_remaining);
if(adpcm_samples < samples) samples = adpcm_samples;
adpcm_pcm_mix_driver_run(&adpcm_driver, bufL, bufR, samples);
for(int n = 0; n < samples; n++) {
mixBufLp[n] += bufL[n];
mixBufRp[n] += bufR[n];
}
fm_opm_emu_driver_run(&fm_driver, bufL, bufR, samples);
for(int n = 0; n < samples; n++) {
mixBufLp[n] += bufL[n];
mixBufRp[n] += bufR[n];
}
if(opt_vgm_file && *opt_vgm_file)
vgm_logger_write_wait(&vgm_logger, samples);
pcm_timer_driver_advance(&timer_driver, samples);
samples_remaining -= samples;
mixBufLp += samples;
mixBufRp += samples;
}
for(int n = 0; n < BUFFER_SIZE; n++) {
*out++ = mixBufL[n];
*out++ = mixBufR[n];
}
err = Pa_WriteStream( stream, buf, BUFFER_SIZE);
if( err ) goto error;
}
}
err = Pa_StopStream( stream );
if( err != paNoError ) goto error;
err = Pa_CloseStream( stream );
if( err != paNoError ) goto error;
Pa_Terminate();
if(opt_vgm_file && *opt_vgm_file)
vgm_logger_end(&vgm_logger);
return err;
error:
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return err;
}