-
Notifications
You must be signed in to change notification settings - Fork 6
/
read_sdt.cpp
133 lines (122 loc) · 4.44 KB
/
read_sdt.cpp
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
/************************************************************************
* Copyright (c) 2005-2007 [email protected] *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <sstream>
#include <cassert>
#include "m_exceptions.h"
#include "fx_sdt.h"
#ifdef SOUND_DUMPER
#include <iostream>
#endif
namespace OpenGTA {
SoundsDB::Entry::Entry(PHYSFS_uint32 r1, PHYSFS_uint32 r2, PHYSFS_uint32 sr) {
rawStart = r1;
rawSize = r2;
sampleRate = sr;
}
SoundsDB::SoundsDB() {
dataFile = 0;
}
SoundsDB::SoundsDB(const std::string & sdt_file) {
dataFile = PHYSFS_openRead(sdt_file.c_str());
if (!dataFile) {
std::string sdt2(sdt_file);
transform(sdt2.begin(), sdt2.end(), sdt2.begin(), tolower);
dataFile = PHYSFS_openRead(sdt2.c_str());
}
if (!dataFile)
throw E_FILENOTFOUND(sdt_file);
//throw std::string("FileNotFound: ") + sdt_file;
PHYSFS_uint32 num_e = PHYSFS_fileLength(dataFile);
if (num_e % 12) {
//throw std::string("Ups: invalid SDT file?");
std::stringstream o;
o << "SDT filesize " << num_e << " % 12 != 0";
throw E_INVALIDFORMAT(o.str());
}
num_e /= 12;
PHYSFS_uint32 r1, r2, sr;
for (PHYSFS_uint16 i = 0; i < num_e; i++) {
PHYSFS_readULE32(dataFile, &r1);
PHYSFS_readULE32(dataFile, &r2);
PHYSFS_readULE32(dataFile, &sr);
#ifdef SOUND_DUMPER
std::cout << i << " " << r1 << " " << r2 << " " << sr << std::endl;
#endif
knownEntries.insert( std::make_pair<KeyType, Entry>(i, Entry(r1, r2, sr)));
}
PHYSFS_close(dataFile);
std::string raw_file(sdt_file);
raw_file.replace(raw_file.size() - 3, 3, "RAW");
dataFile = PHYSFS_openRead(raw_file.c_str());
if (!dataFile) {
std::string sdt2(raw_file);
transform(sdt2.begin(), sdt2.end(), sdt2.begin(), tolower);
dataFile = PHYSFS_openRead(sdt2.c_str());
}
assert(dataFile);
}
SoundsDB::~SoundsDB() {
clear();
if (dataFile)
PHYSFS_close(dataFile);
}
void SoundsDB::clear() {
knownEntries.clear();
if (dataFile)
PHYSFS_close(dataFile);
dataFile = 0;
}
SoundsDB::Entry & SoundsDB::getEntry(KeyType key) {
MapType::iterator i = knownEntries.find(key);
if (i == knownEntries.end()) {
//throw std::string("Unknown sound-db entry");
std::ostringstream o;
o << "Querying for sound id: " << key;
throw E_UNKNOWNKEY(o.str());
}
return i->second;
}
unsigned char* SoundsDB::getBuffered(KeyType key) {
Entry & e = getEntry(key);
unsigned int t_len = e.rawSize;// + 36 + 8;
unsigned char* buf = new unsigned char[t_len];
assert(buf);
memset(buf, 0, t_len);
PHYSFS_seek(dataFile, e.rawStart);
PHYSFS_read(dataFile, buf, 1, e.rawSize);
return buf;
}
}
#ifdef SOUND_DUMPER
int main(int argc, char*argv[]) {
PHYSFS_init(argv[0]);
PHYSFS_addToSearchPath("gtadata.zip", 1);
try {
OpenGTA::SoundsDB sounds(argv[1]);
}
catch (std::string & e) {
std::cerr << e << std::endl;
}
PHYSFS_deinit();
}
#endif