1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <cstdio>
24 #include <cstring>
25 
get_be_uint32(char * pos)26 int get_be_uint32(char *pos) {
27 	unsigned char *ucpos = reinterpret_cast<unsigned char *>(pos);
28 	return (ucpos[0] << 24) | (ucpos[1] << 16) | (ucpos[2] << 8) | ucpos[3];
29 }
30 
write_le_uint32(unsigned val)31 void write_le_uint32(unsigned val) {
32 	putc(val, stdout);
33 	putc(val >> 8, stdout);
34 	putc(val >> 16, stdout);
35 	putc(val >> 24, stdout);
36 }
37 
write_le_uint16(unsigned short val)38 void write_le_uint16(unsigned short val) {
39 	putc(val, stdout);
40 	putc(val >> 8, stdout);
41 }
42 
main()43 int main() {
44 	char block[1024];
45 	fread(block, 8, 1, stdin);  // skip iMUS header
46 	fread(block, 8, 1, stdin);  // read MAP header
47 	int mapSize = get_be_uint32(block + 4);
48 	int numBits = 16, rate = 22050, channels = 2;
49 	for (int mapPos = 0; mapPos < mapSize;) {
50 		fread(block, 8, 1, stdin);
51 		int blockSize = get_be_uint32(block + 4);
52 		if (memcmp(block, "FRMT", 4) == 0) {
53 			fread(block, blockSize, 1, stdin);
54 			numBits = get_be_uint32(block + 8);
55 			rate = get_be_uint32(block + 12);
56 			channels = get_be_uint32(block + 16);
57 		} else {
58 			fread(block, blockSize, 1, stdin);
59 		}
60 		mapPos += (blockSize + 8);
61 	}
62 	fread(block, 8, 1, stdin);
63 	int dataSize = get_be_uint32(block + 4);
64 	fputs("RIFF", stdout);
65 	write_le_uint32(dataSize + 36);
66 	fputs("WAVEfmt ", stdout);
67 	write_le_uint32(16);
68 	write_le_uint16(1);
69 	write_le_uint16(channels);
70 	write_le_uint32(rate);
71 	write_le_uint32(channels * rate * (numBits / 8));
72 	write_le_uint16(channels * (numBits / 8));
73 	write_le_uint16(numBits);
74 	fputs("data", stdout);
75 	write_le_uint32(dataSize);
76 	while (dataSize > 1024) {
77 		fread(block, 1024, 1, stdin);
78 		fwrite(block, 1024, 1, stdout);
79 		dataSize -= 1024;
80 	}
81 	fread(block, dataSize, 1, stdin);
82 	fwrite(block, dataSize, 1, stdout);
83 	return 0;
84 }
85