forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_opus.cpp
151 lines (118 loc) · 3.41 KB
/
decoder_opus.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
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player 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, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#include "system.h"
#ifdef HAVE_OPUS
#include <cstring>
#include <opus/opusfile.h>
#include "audio_decoder.h"
#include "decoder_opus.h"
static int custom_read(void* stream, unsigned char* ptr, int nbytes) {
FILE* f = reinterpret_cast<FILE*>(stream);
return fread(ptr, 1, nbytes, f);
}
static int custom_seek(void* stream, opus_int64 offset, int whence) {
FILE* f = reinterpret_cast<FILE*>(stream);
return fseek(f, offset, whence);
}
static opus_int64 custom_tell(void* stream) {
FILE* f = reinterpret_cast<FILE*>(stream);
return ftell(f);
}
static int custom_close(void* stream) {
FILE* f = reinterpret_cast<FILE*>(stream);
return fclose(f);
}
OpusDecoder::OpusDecoder() {
music_type = "opus";
}
OpusDecoder::~OpusDecoder() {
if (oof) {
op_free(oof);
}
}
bool OpusDecoder::Open(FILE* file) {
finished = false;
int res;
OpusFileCallbacks callbacks = {custom_read, custom_seek, custom_tell, custom_close};
oof = op_open_callbacks(file, &callbacks, nullptr, 0, &res);
if (res != 0) {
error_message = "Opus: Error reading file";
op_free(oof);
fclose(file);
return false;
}
return true;
}
bool OpusDecoder::Seek(size_t offset, Origin origin) {
if (offset == 0 && origin == Origin::Begin) {
if (oof) {
op_raw_seek(oof, 0);
}
finished = false;
return true;
}
return false;
}
bool OpusDecoder::IsFinished() const {
if (!oof)
return false;
return finished;
}
void OpusDecoder::GetFormat(int& freq, AudioDecoder::Format& format, int& chans) const {
freq = frequency;
format = Format::S16;
chans = channels;
}
bool OpusDecoder::SetFormat(int freq, AudioDecoder::Format format, int chans) {
if (freq != frequency || chans != channels || format != Format::S16)
return false;
return true;
}
int OpusDecoder::GetTicks() const {
if (!oof) {
return 0;
}
// According to the docs it is number of samples at 48 kHz
return op_pcm_tell(oof) / 48000;
}
int OpusDecoder::FillBuffer(uint8_t* buffer, int length) {
if (!oof)
return -1;
// op_read_stereo doesn't overwrite the buffer completely, must be cleared to prevent noise
memset(buffer, '\0', length);
// Use a 16bit buffer because op_read_stereo works on one
int length_16 = length / 2;
opus_int16* buffer_16 = reinterpret_cast<opus_int16*>(buffer);
int read = 0;
int to_read = length_16;
do {
read = op_read_stereo(oof, buffer_16 + (length_16 - to_read), to_read);
// stop decoding when error or end of file
if (read <= 0)
break;
// "read" contains number of samples per channel and the function filled 2 channels
to_read -= read * 2;
} while (to_read > 0);
if (read == 0)
finished = true;
if (read < 0) {
return -1;
}
// Return amount of read bytes in the 8 bit what the audio decoder expects
return (length_16 - to_read) * 2;
}
#endif