/
ogg_file.cpp
183 lines (152 loc) · 5.11 KB
/
ogg_file.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
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
/*
Minetest
Copyright (C) 2022 DS
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
OpenAL support based on work by:
Copyright (C) 2011 Sebastian 'Bahamada' Rühl
Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; ifnot, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ogg_file.h"
#include <cstring> // memcpy
namespace sound {
/*
* OggVorbisBufferSource struct
*/
size_t OggVorbisBufferSource::read_func(void *ptr, size_t size, size_t nmemb,
void *datasource) noexcept
{
OggVorbisBufferSource *s = (OggVorbisBufferSource *)datasource;
size_t copied_size = MYMIN(s->buf.size() - s->cur_offset, size);
memcpy(ptr, s->buf.data() + s->cur_offset, copied_size);
s->cur_offset += copied_size;
return copied_size;
}
int OggVorbisBufferSource::seek_func(void *datasource, ogg_int64_t offset, int whence) noexcept
{
OggVorbisBufferSource *s = (OggVorbisBufferSource *)datasource;
if (whence == SEEK_SET) {
if (offset < 0 || (size_t)offset > s->buf.size()) {
// offset out of bounds
return -1;
}
s->cur_offset = offset;
return 0;
} else if (whence == SEEK_CUR) {
if ((size_t)MYMIN(-offset, 0) > s->cur_offset
|| s->cur_offset + offset > s->buf.size()) {
// offset out of bounds
return -1;
}
s->cur_offset += offset;
return 0;
} else if (whence == SEEK_END) {
if (offset > 0 || (size_t)-offset > s->buf.size()) {
// offset out of bounds
return -1;
}
s->cur_offset = s->buf.size() - offset;
return 0;
}
return -1;
}
int OggVorbisBufferSource::close_func(void *datasource) noexcept
{
auto s = reinterpret_cast<OggVorbisBufferSource *>(datasource);
delete s;
return 0;
}
long OggVorbisBufferSource::tell_func(void *datasource) noexcept
{
OggVorbisBufferSource *s = (OggVorbisBufferSource *)datasource;
return s->cur_offset;
}
const ov_callbacks OggVorbisBufferSource::s_ov_callbacks = {
&OggVorbisBufferSource::read_func,
&OggVorbisBufferSource::seek_func,
&OggVorbisBufferSource::close_func,
&OggVorbisBufferSource::tell_func
};
/*
* RAIIOggFile struct
*/
std::optional<OggFileDecodeInfo> RAIIOggFile::getDecodeInfo(const std::string &filename_for_logging)
{
OggFileDecodeInfo ret;
vorbis_info *pInfo = ov_info(&m_file, -1);
if (!pInfo)
return std::nullopt;
ret.name_for_logging = filename_for_logging;
if (pInfo->channels == 1) {
ret.is_stereo = false;
ret.format = AL_FORMAT_MONO16;
ret.bytes_per_sample = 2;
} else if (pInfo->channels == 2) {
ret.is_stereo = true;
ret.format = AL_FORMAT_STEREO16;
ret.bytes_per_sample = 4;
} else {
warningstream << "Audio: Can't decode. Sound is neither mono nor stereo: "
<< ret.name_for_logging << std::endl;
return std::nullopt;
}
ret.freq = pInfo->rate;
ret.length_samples = static_cast<ALuint>(ov_pcm_total(&m_file, -1));
ret.length_seconds = static_cast<f32>(ov_time_total(&m_file, -1));
return ret;
}
RAIIALSoundBuffer RAIIOggFile::loadBuffer(const OggFileDecodeInfo &decode_info,
ALuint pcm_start, ALuint pcm_end)
{
constexpr int endian = 0; // 0 for Little-Endian, 1 for Big-Endian
constexpr int word_size = 2; // we use s16 samples
constexpr int word_signed = 1; // ^
// seek
if (ov_pcm_tell(&m_file) != pcm_start) {
if (ov_pcm_seek(&m_file, pcm_start) != 0) {
warningstream << "Audio: Error decoding (could not seek) "
<< decode_info.name_for_logging << std::endl;
return RAIIALSoundBuffer();
}
}
const size_t size = static_cast<size_t>(pcm_end - pcm_start)
* decode_info.bytes_per_sample;
std::unique_ptr<char[]> snd_buffer(new char[size]);
// read size bytes
size_t read_count = 0;
int bitStream;
while (read_count < size) {
// Read up to a buffer's worth of decoded sound data
long num_bytes = ov_read(&m_file, &snd_buffer[read_count], size - read_count,
endian, word_size, word_signed, &bitStream);
if (num_bytes <= 0) {
warningstream << "Audio: Error decoding "
<< decode_info.name_for_logging << std::endl;
return RAIIALSoundBuffer();
}
read_count += num_bytes;
}
// load buffer to openal
RAIIALSoundBuffer snd_buffer_id = RAIIALSoundBuffer::generate();
alBufferData(snd_buffer_id.get(), decode_info.format, &(snd_buffer[0]), size,
decode_info.freq);
ALenum error = alGetError();
if (error != AL_NO_ERROR) {
warningstream << "Audio: OpenAL error: " << getAlErrorString(error)
<< "preparing sound buffer for sound \""
<< decode_info.name_for_logging << "\"" << std::endl;
}
return snd_buffer_id;
}
} // namespace sound