forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_decoder_base.cpp
136 lines (111 loc) · 3.15 KB
/
audio_decoder_base.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
/*
* 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/>.
*/
// Headers
#include <cassert>
#include <cstring>
#include "audio_decoder_base.h"
#include "output.h"
#include "system.h"
#include "utils.h"
float AudioDecoderBase::AdjustVolume(float volume) {
// Adjust to RPG_RT (Direct Sound) volume scale
if (volume > 0) {
return 100.0f * std::pow(10.0f, (-100 + volume) / 60.0f);
}
return 0.0f;
}
int AudioDecoderBase::Decode(uint8_t* buffer, int length) {
return Decode(buffer, length, 0);
}
std::vector<uint8_t> AudioDecoderBase::DecodeAll() {
const int buffer_size = 8192;
std::vector<uint8_t> buffer;
buffer.resize(buffer_size);
while (!IsFinished()) {
int read = Decode(buffer.data() + buffer.size() - buffer_size, buffer_size);
if (read < buffer_size) {
buffer.resize(buffer.size() - (buffer_size - read));
break;
}
buffer.resize(buffer.size() + buffer_size);
}
return buffer;
}
void AudioDecoderBase::Rewind() {
if (!Seek(0, std::ios_base::beg)) {
// The libs guarantee that Rewind works
assert(false && "Rewind");
}
}
bool AudioDecoderBase::GetLooping() const {
return looping;
}
void AudioDecoderBase::SetLooping(bool enable) {
looping = enable;
}
int AudioDecoderBase::GetLoopCount() const {
return loop_count;
}
bool AudioDecoderBase::WasInited() const {
return true;
}
std::string AudioDecoderBase::GetError() const {
return error_message;
}
std::string AudioDecoderBase::GetType() const {
return music_type;
}
void AudioDecoderBase::UpdateMidi(std::chrono::microseconds delta) {
(void)delta;
}
bool AudioDecoderBase::SetFormat(int, AudioDecoderBase::Format, int) {
return false;
}
int AudioDecoderBase::GetPitch() const {
return 0;
}
bool AudioDecoderBase::SetPitch(int) {
return false;
}
std::streampos AudioDecoderBase::Tell() const {
return -1;
}
int AudioDecoderBase::Decode(uint8_t* buffer, int length, int recursion_depth) {
int res = FillBuffer(buffer, length);
if (res < 0) {
memset(buffer, '\0', length);
}
else if (res < length) {
memset(&buffer[res], '\0', length - res);
}
if (IsFinished() && looping && recursion_depth < 10) {
++loop_count;
Rewind();
if (length - res > 0) {
int res2 = Decode(&buffer[res], length - res, ++recursion_depth);
if (res2 <= 0) {
return res;
}
return res + res2;
}
}
if (recursion_depth == 10 && loop_count < 50) {
// Only report this a few times in the hope that this is only a temporary problem and to prevent log spamming
Output::Debug("Audio Decoder: Recursion depth exceeded. Probably stream error.");
}
return res;
}