-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsample.cpp
More file actions
203 lines (168 loc) · 5.34 KB
/
sample.cpp
File metadata and controls
203 lines (168 loc) · 5.34 KB
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
/* $Id$ */
/*
* catcodec is a tool to decode/encode the sample catalogue for OpenTTD.
* Copyright (C) 2009 Remko Bijker
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* @file sample.cpp Implementation of Samples
*
* The format of .wav PCM encoded files is fairly simple. The "verbatim"
* 'RIFF' etc. is written as is in the file. The numbers are always
* written in the little endian way.
* <ul>
* <li>'RIFF'</li>
* <li>dword with number of bytes following</li>
* <li>'WAVE'</li>
* <li>'fmt '</li>
* <li>dword with size of 'fmt ' chunk, always 16</li>
* <li>word with audio format, always 1 (PCM)</li>
* <li>word with number of channels, always 1</li>
* <li>dword with sample rate, always 11025, 22050 or 44100</li>
* <li>dword with byte rate, always sample rate * number of channels * bits per sample / 8</li>
* <li>word with block alignment, always number of channels * bits per sample / 8</li>
* <li>word with bits per sample, always 8 or 16</li>
* <li>'data'</li>
* <li>dword with number of bytes following</li>
* <li>actual raw PCM data</li>
* </ul>
*
* This makes the whole thing 44 bytes + the actual payload long.
*/
#include "stdafx.h"
#include "sample.hpp"
/** The size of the RIFF headers of a WAV file */
static const uint32_t RIFF_HEADER_SIZE = 44;
/**
* Read a string (byte length including termination, actual data) from a reader.
* @param reader the reader to read from
* @return the read string
*/
static string ReadString(FileReader &reader)
{
uint8_t name_len = reader.ReadByte();
char buffer[256];
reader.ReadRaw((uint8_t *)buffer, name_len);
buffer[name_len - 1] = '\0';
return buffer;
}
/**
* Write a string (byte length including termination, actual data) to a writer.
* @param str the string to write
* @param writer the writer to write to
*/
static void WriteString(const string str, FileWriter &writer)
{
uint8_t str_len = (uint8_t)(str.length() + 1);
writer.WriteByte(str_len);
writer.WriteRaw((const uint8_t *)str.c_str(), str_len);
}
Sample::Sample(FileReader &reader) :
sample_data(NULL)
{
this->offset = reader.ReadDword() & 0x7FFFFFFF;
this->size = reader.ReadDword();
}
Sample::Sample(string filename, string name) :
offset(0),
name(name),
filename(filename),
sample_data(NULL)
{
FileReader sample_reader(filename);
this->size = sample_reader.GetSize();
this->ReadSample(sample_reader, false);
}
Sample::~Sample()
{
free(this->sample_data);
}
void Sample::ReadSample(FileReader &reader, bool check_size)
{
assert(this->sample_data == NULL);
this->sample_data = (uint8_t *)malloc(this->size);
reader.ReadRaw(this->sample_data, this->size);
}
void Sample::ReadCatEntry(FileReader &reader, bool new_format)
{
assert(this->sample_data == NULL);
if (reader.GetPos() != this->GetOffset()) throw "Invalid offset in file " + reader.GetFilename();
this->name = ReadString(reader);
if (!new_format && this->GetName().compare("Corrupt sound") == 0) {
/* In the old format there was one sample that was raw PCM. */
this->sample_size = this->size;
this->sample_data = (uint8_t *)malloc(this->sample_size);
reader.ReadRaw(this->sample_data, this->sample_size);
this->size += RIFF_HEADER_SIZE;
} else {
this->ReadSample(reader);
}
if (!new_format) {
/* The old format had sometimes the wrong values for e.g.
* sample rate which made the playback too fast. */
this->num_channels = 1;
this->sample_rate = 11025;
this->bits_per_sample = 8;
}
/* Some kind of data byte, unused */
reader.ReadByte();
this->filename = ReadString(reader);
}
void Sample::WriteSample(FileWriter &writer) const
{
assert(this->sample_data != NULL);
writer.WriteRaw(this->sample_data, this->size);
}
void Sample::WriteCatEntry(FileWriter &writer) const
{
assert(this->sample_data != NULL);
if (writer.GetPos() != this->GetOffset()) throw "Invalid offset when writing file " + writer.GetFilename();
WriteString(this->GetName(), writer);
this->WriteSample(writer);
/* Some kind of separator byte */
writer.WriteByte(0);
WriteString(this->GetFilename(), writer);
}
string Sample::GetName() const
{
return this->name;
}
string Sample::GetFilename() const
{
return this->filename;
}
void Sample::SetOffset(uint32_t offset)
{
assert(this->offset == 0);
this->offset = offset;
}
uint32_t Sample::GetNextOffset() const
{
return this->offset +
1 + // length of the name
(this->name.length() + 1) + // the name + '\0'
this->size + // size of the data
1 + // the delimiter
1 + // length of the filename
(this->filename.length() + 1); // the filename + '\0'
}
uint32_t Sample::GetOffset() const
{
return this->offset;
}
uint32_t Sample::GetSize() const
{
return this->size;
}