forked from jbaiter/wav2msu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wav2msu.c
235 lines (211 loc) · 7.4 KB
/
wav2msu.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
// Copyright (c) 2011 Johannes Baiter <johannes.baiter@gmail.com>
// Based on C# code by Kawa <http://helmet.kafuka.org/thepile/Wav2msu>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include <errno.h>
#include "getopt.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef WIN32
#include <io.h>
#include <fcntl.h>
#endif
static const size_t BufferSize = 8192;
void print_usage();
void print_help();
int32_t validate(FILE*);
void print_usage() {
fprintf(stderr, "Usage: wav2msu [-o outfile] [-l looppoint] [-i introfile] FILE.wav\n");
}
void print_help() {
printf("wav2msu 0.1\n\n"
"Usage: wav2msu [-o outfile] [-l looppoint] [-i introfile] FILE.wav\n"
"Converts wave-files to a MSU1-compatible format.\n"
"Input is required to be a RIFF WAVE file in 16bit, 44.1kHz, 2ch PCM format.\n"
"Set filename to \'-\' to read from stdin.\n\n"
"Arguments:\n"
" -i <file.wav> Put <file.wav> before the main input file.\n"
" -l <looppoint> Set sample (relative to beginning of input file)\n"
" from which to loop, decimal or hexadecimal (0xabcd)\n"
" -o <outfile.pcm> Outputs to given filename, default is stdout\n"
" -h Print this help\n"
);
}
// Checks a Wavefile for compliance with the MSU1-specifications[1]
// [1] http://board.byuu.org/viewtopic.php?f=16&t=947
int32_t validate(FILE *ifp) {
int riff_header;
fread(&riff_header, sizeof(riff_header), 1, ifp);
// 'RIFF' = little-endian
if (riff_header != 0x46464952) {
fprintf(stderr, "wav2msu: Incorrect header: Invalid format or endianness\n");
fprintf(stderr, " Value was: 0x%x\n", riff_header);
return -1;
}
// stdin doesn't support fseek
if (ifp == stdin) {
int i = 0;
while (++i <= 16) {
getc(ifp);
}
} else
fseek(ifp, 20, SEEK_SET);
// Format has to be PCM (=1)
int16_t format;
fread(&format, sizeof(format), 1, ifp);
if (format != 1) {
fprintf(stderr, "wav2msu: Not in PCM format! (format was: %d)\n", format);
return -1;
}
// The data needs to be in stereo format
int16_t channels;
fread(&channels, sizeof(channels), 1, ifp);
int32_t sample_rate;
// Sample Rate has to be 44.1kHz
fread(&sample_rate, sizeof(sample_rate), 1, ifp);
if (ifp == stdin) {
int i = 0;
while (++i <= 6) {
getc(ifp);
}
} else
fseek(ifp, 34, SEEK_SET);
int16_t bits_per_sample;
// We need 16bps
fread(&bits_per_sample, sizeof(bits_per_sample), 1, ifp);
if (channels != 2 || sample_rate != 44100 || bits_per_sample != 16) {
fprintf(stderr, "wav2msu: Not in 16bit 44.1kHz stereo!\n");
fprintf(stderr, " Got instead: %dbit, %dHz, %dch\n", bits_per_sample, sample_rate, channels);
return -1;
}
int32_t data_header;
// 'DATA'
fread(&data_header, sizeof(data_header), 1, ifp);
if (data_header != 0x61746164) {
fprintf(stderr, "wav2msu: Sample data not where expected!\n");
return -1;
}
int32_t data_size;
fread(&data_size, sizeof(data_size), 1, ifp);
return data_size;
}
int main(int argc, char *argv[]) {
FILE *introfile, *infile;
FILE *outfile = stdout;
int intro_flag = 0;
int32_t intro_size, data_size;
long loop_point = 0;
// Disable libc error printing
opterr = 0;
int opt;
char *p;
while ((opt = getopt(argc, argv, "ho:l:i:")) != -1)
switch (opt) {
case 'h':
print_help();
exit(0);
case 'o':
if ((outfile = fopen(optarg, "wb")) == NULL) {
fprintf(stderr, "wav2msu: can't open %s\n", optarg);
exit(1);
}
break;
case 'l':
loop_point = strtol(optarg, &p, 0);
break;
case 'i':
intro_flag = 1;
if ((introfile = fopen(optarg, "rb")) == NULL) {
fprintf(stderr, "wav2msu: can't open %s\n", optarg);
exit(1);
}
break;
case '?':
if (optopt == 'l' || optopt== 'i')
fprintf(stderr, "wav2msu: Option -%c requires an argument\n", optopt);
else
fprintf(stderr, "wav2msu: Unknown option -%c\n", optopt);
print_usage();
exit(1);
break;
default:
exit(1);
break;
}
if (optind == argc) {
print_help();
exit(0);
} else if (argc - optind > 1) {
fprintf(stderr, "Too many input files.\n");
print_usage();
exit(1);
} else {
if (*argv[argc-1] == '-') {
fprintf(stderr, "Reading from stdin.\n");
infile = stdin;
// We need to switch stdin to binary mode, or else we run
// into problems under Windows
#ifdef WIN32
_setmode(fileno(stdin), _O_BINARY);
#endif
} else if ((infile = fopen(argv[argc-1], "rb")) == NULL) {
fprintf(stderr, "wav2msu: can't open %s\n", argv[argc-1]);
exit(1);
}
}
if (intro_flag == 1) {
intro_size = validate(introfile);
loop_point += intro_size/4;
if (intro_size == -1) {
fclose(introfile);
fclose(outfile);
fclose(infile);
fprintf(stderr, "wav2msu: Intro file did not validate.\n");
exit(1);
}
}
data_size = validate(infile);
if (data_size == -1) {
fclose(outfile);
fclose(infile);
fprintf(stderr, "wav2msu: Input WAV data did not validate.\n");
exit(1);
}
uint8_t readBuffer[BufferSize];
loop_point = (int32_t) loop_point;
fprintf(outfile, "MSU1");
fwrite(&loop_point, sizeof(loop_point), 1, outfile);
if (intro_flag == 1)
{
size_t size = 0;
while(size = fread(readBuffer, 1, BufferSize, introfile))
{
fwrite(readBuffer, 1, size, outfile);
}
}
size_t size = 0;
while(size = fread(readBuffer, 1, BufferSize, infile))
{
fwrite(readBuffer, 1, size, outfile);
}
fclose(outfile);
fclose(infile);
return 0;
}