1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2009 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 // -----------------------------------------------------------------------
26
27 #include "systems/base/voice_archive.h"
28
29 #include <boost/filesystem/fstream.hpp>
30
31 #include <algorithm>
32 #include <cstring>
33 #include <fstream>
34 #include <sstream>
35
36 #include "utilities/exception.h"
37 #include "xclannad/endian.hpp"
38
39 namespace fs = boost::filesystem;
40
41 namespace {
42
43 // Header at the beginning of WAV data.
44 unsigned char orig_header[0x2c] = {
45 0x52, 0x49, 0x46, 0x46, /* +00 "RIFF" */
46 0x00, 0x00, 0x00, 0x00, /* +04 file size - 8 */
47 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, /* +08 "WAVEfmt " */
48 0x10, 0x00, 0x00, 0x00, /* +10 fmt size */
49 0x01, 0x00, /* +14 wFormatTag */
50 0x02, 0x00, /* +16 Channels */
51 0x44, 0xac, 0x00, 0x00, /* +18 rate */
52 0x10, 0xb1, 0x02, 0x00, /* +1c BytesPerSec = rate * BlockAlign */
53 0x04, 0x00, /* +20 BlockAlign = channels*BytesPerSample */
54 0x10, 0x00, /* +22 BitsPerSample */
55 0x64, 0x61, 0x74, 0x61, /* +24 "data" */
56 0x00, 0x00, 0x00, 0x00 /* +28 filesize - 0x2c */
57 };
58
59 } // namespace
60
61 // -----------------------------------------------------------------------
62 // VoiceSample
63 // -----------------------------------------------------------------------
~VoiceSample()64 VoiceSample::~VoiceSample() {}
65
66 // static
MakeWavHeader(int rate,int ch,int bps,int size)67 const char* VoiceSample::MakeWavHeader(int rate, int ch, int bps, int size) {
68 static char header[0x2c];
69 memcpy(header, (const char*)orig_header, 0x2c);
70 write_little_endian_int(header + 0x04, size - 8);
71 write_little_endian_int(header + 0x28, size - 0x2c);
72 write_little_endian_int(header + 0x18, rate);
73 write_little_endian_int(header + 0x1c, rate * ch * bps);
74 header[0x16] = ch;
75 header[0x20] = ch * bps;
76 header[0x22] = bps * 8;
77 return header;
78 }
79
80 // -----------------------------------------------------------------------
81 // VoiceArchive
82 // -----------------------------------------------------------------------
VoiceArchive(int file_number)83 VoiceArchive::VoiceArchive(int file_number) : file_number_(file_number) {}
84
~VoiceArchive()85 VoiceArchive::~VoiceArchive() {}
86
ReadVisualArtsTable(boost::filesystem::path file,int entry_length,std::vector<Entry> & entries)87 void VoiceArchive::ReadVisualArtsTable(boost::filesystem::path file,
88 int entry_length,
89 std::vector<Entry>& entries) {
90 fs::ifstream ifs(file, fs::ifstream::in | fs::ifstream::binary);
91 if (!ifs) {
92 std::ostringstream oss;
93 oss << "Could not open file \"" << file << "\".";
94 throw rlvm::Exception(oss.str());
95 }
96
97 // Copied from koedec.
98 char head[0x20];
99 ifs.read(head, 4);
100 int table_len = read_little_endian_int(head);
101 entries.reserve(table_len);
102
103 for (int i = 0; i < table_len; ++i) {
104 ifs.read(head, entry_length);
105 int length = read_little_endian_int(head);
106 int offset = read_little_endian_int(head + 4);
107 int koe_num = read_little_endian_int(head + 8);
108 entries.emplace_back(koe_num, length, offset);
109 }
110
111 std::sort(entries.begin(), entries.end());
112 }
113
Entry(int ikoe_num,int ilength,int ioffset)114 VoiceArchive::Entry::Entry(int ikoe_num, int ilength, int ioffset)
115 : koe_num(ikoe_num), length(ilength), offset(ioffset) {}
116