-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavtop86.c
227 lines (188 loc) · 5.73 KB
/
wavtop86.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
/*
* pcmtop86 tool for use with PMD
* COPYRIGHT : MIT
* See COPYING for more info on license
*
* This is for use with PMD on the PC-98 and other supported platforms.
* P86 is for PMD86 and PPC is for PMDB2.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#ifdef RESAMPLER
#include <speex/speex_resampler.h>
#endif
static uint8_t *pcm_data_list[256];
static uint32_t pcm_data_sizes[256];
typedef struct {
char chunk_id[4];
uint32_t chunk_size;
char format[4];
} RiffHeader;
typedef struct {
char chunk_id[4];
uint32_t chunk_size;
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bits_per_sample;
} FmtHeader;
int read_wav_file(const char *filename, uint8_t **data, uint32_t *size, unsigned char resample) {
char chunk_id[4];
uint32_t chunk_size, i;
RiffHeader riff_header;
FmtHeader fmt_header;
FILE *file;
int16_t *temp_data;
#ifdef RESAMPLER
uint32_t input_frames, output_frames;
int16_t *resampled_data;
int err;
#endif
file = fopen(filename, "rb");
if (!file) {
puts("Can't open input file");
return 1;
}
fread(&riff_header, sizeof(RiffHeader), 1, file);
if (strncmp(riff_header.chunk_id, "RIFF", 4) != 0 || strncmp(riff_header.format, "WAVE", 4) != 0) {
puts("Invalid WAV file");
fclose(file);
return 1;
}
fread(&fmt_header, sizeof(FmtHeader), 1, file);
if (strncmp(fmt_header.chunk_id, "fmt ", 4) != 0) {
puts("Invalid WAV file");
fclose(file);
return 1;
}
if (fmt_header.audio_format != 1 || fmt_header.num_channels != 1 || fmt_header.bits_per_sample != 16) {
puts("Unsupported WAV format. Must be 16-bit signed little-endian MONO");
fclose(file);
return 1;
}
if (fmt_header.sample_rate != 16540)
{
#ifdef RESAMPLER
if (resample)
{
puts("Input sample rate is not 16540 Hz, resampling it");
}
else
#endif
{
puts("Warning: Input sample rate is not 16540 Hz");
}
}
while (1) {
fread(&chunk_id, sizeof(chunk_id), 1, file);
fread(&chunk_size, sizeof(chunk_size), 1, file);
if (strncmp(chunk_id, "data", 4) == 0) {
break;
} else {
fseek(file, chunk_size, SEEK_CUR);
}
}
chunk_size = (chunk_size + 31) & ~31; // Pad to 32 bytes for Yamaha chip
temp_data = malloc(chunk_size);
memset(temp_data, 0, chunk_size);
fread(temp_data, 1, chunk_size, file);
#ifdef RESAMPLER
if (resample && fmt_header.sample_rate != 16540) {
input_frames = chunk_size / (sizeof(int16_t) * fmt_header.num_channels);
output_frames = (input_frames * 16540) / fmt_header.sample_rate;
resampled_data = malloc(output_frames * sizeof(int16_t) * fmt_header.num_channels);
SpeexResamplerState *resampler = speex_resampler_init(1, fmt_header.sample_rate, 16540, SPEEX_RESAMPLER_QUALITY_MAX, &err);
speex_resampler_process_int(resampler, 0, temp_data, &input_frames, resampled_data, &output_frames);
speex_resampler_destroy(resampler);
free(temp_data);
temp_data = resampled_data;
chunk_size = output_frames * sizeof(int16_t) * fmt_header.num_channels;
}
#endif
*size = chunk_size / 2;
*data = malloc(*size);
for (i = 0; i < *size; i++) {
(*data)[i] = (int8_t)(temp_data[i] >> 8); // Must be Signed 8-bits PCM, not unsigned
}
free(temp_data);
fclose(file);
return 0;
}
int create_p86(char *input_files[], int input_files_count, char *output_file, uint8_t p86drv_version, unsigned char resample) {
int i;
uint32_t all_size = 0x10; // Initialize all_size with 0x10
uint8_t header[0x610];
uint32_t start;
FILE *p86_file;
// Read all input files and calculate the total size
for (i = 0; i < input_files_count; i++)
{
if (read_wav_file(input_files[i],&pcm_data_list[i], &pcm_data_sizes[i], resample) != 0)
{
return 1;
}
all_size += pcm_data_sizes[i]; // Add each file's size to all_size
}
// Fill header with Zeros
memset(header, 0, sizeof(header));
all_size += 0x6; // Add 6 bytes for the size of the header
// Prepare the header
memcpy(header, "PCM86 DATA\x0A\x00", 12);
header[12] = p86drv_version;
memcpy(header + 13, &all_size, 3);
// Set START and SIZE for each input file
start = 0x610;
for (i = 0; i < input_files_count; i++) {
memcpy(header + 16 + i * 6, &start, 3);
memcpy(header + 19 + i * 6, &pcm_data_sizes[i], 3);
start += pcm_data_sizes[i];
}
// Write the output file
p86_file = fopen(output_file, "wb");
if (!p86_file) {
puts("Error opening output file");
return 1;
}
fwrite(header, 1, sizeof(header), p86_file);
for (i = 0; i < input_files_count; i++) {
fwrite(pcm_data_list[i], 1, pcm_data_sizes[i], p86_file);
free(pcm_data_list[i]);
}
fclose(p86_file);
return 0;
}
int main(int argc, char *argv[])
{
int input_files_count;
char **input_files;
char *output_file;
unsigned char resample;
resample = 0;
if (argc < 3) {
puts("Usage: pcmtop86 input1.pcm [input2.pcm ...] output.p86");
#ifdef RESAMPLER
puts(" -r: Force resampling input WAV files to 16540 Hz");
#endif
return 1;
}
#ifdef RESAMPLER
if (strcmp(argv[1], "-r") == 0) {
resample = 1;
argc--;
argv++;
}
#endif
input_files_count = argc - 2;
input_files = argv + 1;
output_file = argv[argc - 1];
if (input_files_count > 255)
{
puts("Too many input files !\n Limit is 255");
return 1;
}
return create_p86(input_files, input_files_count, output_file, 0x11, resample);
}