File: SoundBuffer.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (284 lines) | stat: -rw-r--r-- 8,273 bytes parent folder | download | duplicates (3)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "SoundBuffer.h"


#include "System/Sound/SoundLog.h"
#include "ALShared.h"
#include "VorbisShared.h"
#include "System/Platform/byteorder.h"

#include <vorbis/vorbisfile.h>
#include <ogg/ogg.h>
#include <algorithm>
#include <cassert>
#include <cstring>

namespace
{

struct VorbisInputBuffer
{
	const std::uint8_t* data;
	size_t pos;
	size_t size;
};

size_t VorbisRead(void* ptr, size_t size, size_t nmemb, void* datasource)
{
	VorbisInputBuffer* buffer = static_cast<VorbisInputBuffer*>(datasource);
	const size_t maxRead = std::min(size * nmemb, buffer->size - buffer->pos);
	memcpy(ptr, buffer->data + buffer->pos, maxRead);
	buffer->pos += maxRead;
	return maxRead;
}

int	VorbisClose(void* datasource)
{
	return 0; // nothing to be done here
}
}


SoundBuffer::bufferMapT SoundBuffer::bufferMap;
SoundBuffer::bufferVecT SoundBuffer::buffers;

static std::vector<std::uint8_t> decodeBuffer;


#pragma pack(push, 1)
// Header copied from WavLib by Michael McTernan
struct WAVHeader
{
	std::uint8_t riff[4];        // "RIFF"
	std::int32_t totalLength;
	std::uint8_t wavefmt[8];     // WAVEfmt "
	std::int32_t length;         // Remaining length 4 bytes
	std::int16_t format_tag;
	std::int16_t channels;       // Mono=1 Stereo=2
	std::int32_t SamplesPerSec;
	std::int32_t AvgBytesPerSec;
	std::int16_t BlockAlign;
	std::int16_t BitsPerSample;
	std::uint8_t data[4];        // "data"
	std::int32_t datalen;        // Raw data length 4 bytes
};
#pragma pack(pop)


bool SoundBuffer::LoadWAV(const std::string& file, const std::vector<std::uint8_t>& buffer)
{
	WAVHeader* header = (WAVHeader*)(&buffer[0]);

	if ((buffer.empty()) || memcmp(header->riff, "RIFF", 4) || memcmp(header->wavefmt, "WAVEfmt", 7)) {
		LOG_L(L_ERROR, "[%s(%s)] invalid header", __func__, file.c_str());
		return false;
	}

#define hswabword(c) swabWordInPlace(header->c)
#define hswabdword(c) swabDWordInPlace(header->c)
	hswabword(format_tag);
	hswabword(channels);
	hswabword(BlockAlign);
	hswabword(BitsPerSample);

	hswabdword(totalLength);
	hswabdword(length);
	hswabdword(SamplesPerSec);
	hswabdword(AvgBytesPerSec);
	hswabdword(datalen);
#undef hswabword
#undef hswabdword

	if (header->format_tag != 1) { // Microsoft PCM format?
		LOG_L(L_ERROR, "[%s(%s)] invalid format tag", __func__, file.c_str());
		return false;
	}

	ALenum format;
	if (header->channels == 1) {
		if (header->BitsPerSample == 8) format = AL_FORMAT_MONO8;
		else if (header->BitsPerSample == 16) format = AL_FORMAT_MONO16;
		else {
			LOG_L(L_ERROR, "[%s(%s)] invalid number of bits per sample (mono; %d)", __func__, file.c_str(), header->BitsPerSample);
			return false;
		}
	}
	else if (header->channels == 2) {
		if (header->BitsPerSample == 8) format = AL_FORMAT_STEREO8;
		else if (header->BitsPerSample == 16) format = AL_FORMAT_STEREO16;
		else {
			LOG_L(L_ERROR, "[%s(%s)] invalid number of bits per sample (stereo; %d)", __func__, file.c_str(), header->BitsPerSample);
			return false;
		}
	}
	else {
		LOG_L(L_ERROR, "[%s(%s)] invalid number of channels (%d)", __func__, file.c_str(), header->channels);
		return false;
	}

	if (static_cast<unsigned>(header->datalen) > buffer.size() - sizeof(WAVHeader)) {
		LOG_L(L_ERROR,
				"[%s(%s)] data length %i greater than actual data length %i",
				__func__, file.c_str(), header->datalen,
				(int)(buffer.size() - sizeof(WAVHeader)));

//		LOG_L(L_WARNING, "OpenAL: size %d\n", size);
//		LOG_L(L_WARNING, "OpenAL: sizeof(WAVHeader) %d\n", sizeof(WAVHeader));
//		LOG_L(L_WARNING, "OpenAL: format_tag %d\n", header->format_tag);
//		LOG_L(L_WARNING, "OpenAL: channels %d\n", header->channels);
//		LOG_L(L_WARNING, "OpenAL: BlockAlign %d\n", header->BlockAlign);
//		LOG_L(L_WARNING, "OpenAL: BitsPerSample %d\n", header->BitsPerSample);
//		LOG_L(L_WARNING, "OpenAL: totalLength %d\n", header->totalLength);
//		LOG_L(L_WARNING, "OpenAL: length %d\n", header->length);
//		LOG_L(L_WARNING, "OpenAL: SamplesPerSec %d\n", header->SamplesPerSec);
//		LOG_L(L_WARNING, "OpenAL: AvgBytesPerSec %d\n", header->AvgBytesPerSec);

		header->datalen = std::uint32_t(buffer.size() - sizeof(WAVHeader))&(~std::uint32_t((header->BitsPerSample*header->channels)/8 -1));
	}

	if (!AlGenBuffer(file, format, &buffer[sizeof(WAVHeader)], header->datalen, header->SamplesPerSec))
		LOG_L(L_WARNING, "[%s(%s)] failed generating buffer", __func__, file.c_str());

	filename = file;
	channels = header->channels;
	length   = float(header->datalen) / (header->channels * header->SamplesPerSec * header->BitsPerSample);

	return true;
}

bool SoundBuffer::LoadVorbis(const std::string& file, const std::vector<std::uint8_t>& buffer)
{
	VorbisInputBuffer buf;
	buf.data = &buffer[0];
	buf.pos = 0;
	buf.size = buffer.size();

	ov_callbacks vorbisCallbacks;
	vorbisCallbacks.read_func  = VorbisRead;
	vorbisCallbacks.close_func = VorbisClose;
	vorbisCallbacks.seek_func  = nullptr;
	vorbisCallbacks.tell_func  = nullptr;

	OggVorbis_File oggStream;
	const int result = ov_open_callbacks(&buf, &oggStream, nullptr, 0, vorbisCallbacks);
	if (result < 0) {
		LOG_L(L_WARNING, "[%s(%s)] could not open Ogg stream (%s)", __func__, file.c_str(), ErrorString(result).c_str());
		return false;
	}

	const vorbis_info* vorbisInfo = ov_info(&oggStream, -1);
	// const vorbis_comment* vorbisComment = ov_comment(&oggStream, -1);

	ALenum format;

	switch (vorbisInfo->channels) {
		case  1: { format = AL_FORMAT_MONO16  ; } break;
		case  2: { format = AL_FORMAT_STEREO16; } break;
		default: {
			LOG_L(L_ERROR, "[%s(%s)] invalid number of channels (%i)", __func__, file.c_str(), vorbisInfo->channels);
			return false;
		}
	}

	size_t pos = 0;
	int section = 0;
	long read = 0;

	decodeBuffer.clear();
	decodeBuffer.resize(512 * 1024); // 512kb read buffer

	do {
		// enlarge buffer so ov_read has enough space
		if ((4 * pos) > (3 * decodeBuffer.size()))
			decodeBuffer.resize(decodeBuffer.size() * 2);

		switch ((read = ov_read(&oggStream, (char*)&decodeBuffer[pos], decodeBuffer.size() - pos, 0, 2, 1, &section))) {
			case OV_HOLE:
				LOG_L(L_WARNING, "[%s(%s)] garbage or corrupt page in stream (non-fatal)", __func__, file.c_str());
				continue; // read next
			case OV_EBADLINK:
				LOG_L(L_WARNING, "[%s(%s)] corrupted stream", __func__, file.c_str());
				return false; // abort
			case OV_EINVAL:
				LOG_L(L_WARNING, "[%s(%s)] corrupted headers", __func__, file.c_str());
				return false; // abort
			default:
				break; // all good
		}

		pos += read;
	} while (read > 0); // read == 0 indicated EOF, read < 0 is error

	if (!AlGenBuffer(file, format, &decodeBuffer[0], pos, vorbisInfo->rate))
		LOG_L(L_WARNING, "[%s(%s)] failed generating buffer", __func__, file.c_str());

	// for non-seekable streams, ov_time_total returns OV_EINVAL (-131) while
	// ov_time_tell always[?] returns the decoding time offset relative to EOS
	filename = file;
	channels = vorbisInfo->channels;
	length   = (ov_seekable(&oggStream) == 0)? ov_time_tell(&oggStream): ov_time_total(&oggStream, -1);
	return true;
}


bool SoundBuffer::AlGenBuffer(const std::string& file, ALenum format, const std::uint8_t* data, size_t datalength, int rate)
{
	alGenBuffers(1, &id);
	if (!CheckError("SoundBuffer::alGenBuffers"))
		return false;
	alBufferData(id, format, (ALvoid*) data, datalength, rate);
	return CheckError("SoundBuffer::alBufferData");
}

bool SoundBuffer::Release() {
	if (id == 0)
		return false;
	alDeleteBuffers(1, &id);
	return true;
}


int SoundBuffer::BufferSize() const
{
	ALint size;
	alGetBufferi(id, AL_SIZE, &size);
	return static_cast<int>(size);
}


size_t SoundBuffer::GetId(const std::string& name)
{
	const auto it = bufferMap.find(name);

	if (it != bufferMap.end())
		return it->second;

	return 0;
}

SoundBuffer& SoundBuffer::GetById(const size_t id)
{
	assert(id < buffers.size());
	return buffers.at(id);
}


size_t SoundBuffer::AllocedSize()
{
	size_t numBytes = 0;
	for (auto it = ++buffers.cbegin(); it != buffers.cend(); ++it)
		numBytes += it->BufferSize();
	return numBytes;
}

size_t SoundBuffer::Insert(SoundBuffer&& buffer)
{
	const size_t bufId = buffers.size();

	bufferMap[buffer.GetFilename()] = bufId;
	buffers.emplace_back(std::move(buffer));

	return bufId;
}