forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_libsndfile.cpp
177 lines (149 loc) · 4.43 KB
/
decoder_libsndfile.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
/*
* 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_LIBSNDFILE
// Headers
#include <cassert>
#include <sys/stat.h>
#include "decoder_libsndfile.h"
#include "output.h"
static sf_count_t sf_vio_get_filelen_impl(void*) {
// Unknown. SF_COUNT_MAX is the size used by libsndfile for pipes.
return SF_COUNT_MAX;
}
static sf_count_t sf_vio_read_impl(void *ptr, sf_count_t count, void* userdata){
auto* f = reinterpret_cast<Filesystem_Stream::InputStream*>(userdata);
return f->read(reinterpret_cast<char*>(ptr), count).gcount();
}
static sf_count_t sf_vio_write_impl(const void* /* ptr */, sf_count_t count, void* /* user_data */){
//Writing of wav files is not necessary
return count;
}
static sf_count_t sf_vio_seek_impl(sf_count_t offset, int seek_type, void *userdata) {
auto* f = reinterpret_cast<Filesystem_Stream::InputStream*>(userdata);
if (f->eof()) f->clear(); //emulate behaviour of fseek
f->seekg(offset, Filesystem_Stream::CSeekdirToCppSeekdir(seek_type));
return f->tellg();
}
static sf_count_t sf_vio_tell_impl(void* userdata){
auto* f = reinterpret_cast<Filesystem_Stream::InputStream*>(userdata);
return f->tellg();
}
static SF_VIRTUAL_IO vio = {
sf_vio_get_filelen_impl,
sf_vio_seek_impl,
sf_vio_read_impl,
sf_vio_write_impl,
sf_vio_tell_impl
};
LibsndfileDecoder::LibsndfileDecoder()
{
music_type = "wav";
soundfile=0;
}
LibsndfileDecoder::~LibsndfileDecoder() {
if(soundfile != 0){
sf_close(soundfile);
}
}
bool LibsndfileDecoder::Open(Filesystem_Stream::InputStream stream) {
this->stream = std::move(stream);
soundfile=sf_open_virtual(&vio,SFM_READ,&soundinfo,&this->stream);
sf_command(soundfile, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE);
output_format=Format::S16;
finished=false;
decoded_samples = 0;
return soundfile!=0;
}
bool LibsndfileDecoder::Seek(std::streamoff offset, std::ios_base::seekdir origin) {
finished = false;
if(soundfile == 0)
return false;
// FIXME: Proper sample count for seek
decoded_samples = 0;
return sf_seek(soundfile, offset, Filesystem_Stream::CppSeekdirToCSeekdir(origin))!=-1;
}
bool LibsndfileDecoder::IsFinished() const {
return finished;
}
void LibsndfileDecoder::GetFormat(int& frequency, AudioDecoder::Format& format, int& channels) const {
if(soundfile==0) return;
frequency = soundinfo.samplerate;
channels = soundinfo.channels;
format = output_format;
}
bool LibsndfileDecoder::SetFormat(int freq, AudioDecoder::Format fmt, int channels) {
if(soundfile == 0)
return false;
switch(fmt){
case Format::F32:
case Format::S16:
case Format::S32:
output_format=fmt;
break;
default:
return false;
}
return soundinfo.samplerate==freq && soundinfo.channels==channels && output_format==fmt;
}
int LibsndfileDecoder::FillBuffer(uint8_t* buffer, int length) {
if(soundfile == 0)
return -1;
int decoded;
switch(output_format){
case Format::F32:
{
decoded=sf_read_float(soundfile,(float*)buffer,length/sizeof(float));
if(!decoded)
finished=true;
decoded_samples += decoded;
decoded *= sizeof(float);
}
break;
case Format::S16:
{
decoded=sf_read_short(soundfile,(int16_t*)buffer,length/sizeof(int16_t));
if(!decoded)
finished=true;
decoded_samples += decoded;
decoded *= sizeof(int16_t);
}
break;
case Format::S32:
{
// Uses int instead of int32_t because the 3ds toolchain typedefs
// to long int which is an incompatible pointer type
decoded=sf_read_int(soundfile,(int*)buffer,length/sizeof(int));
if(!decoded)
finished=true;
decoded_samples += decoded;
decoded *= sizeof(int);
}
break;
default:
decoded=-1;
break;
}
return decoded;
}
int LibsndfileDecoder::GetTicks() const {
if (soundfile == 0) {
return 0;
}
return decoded_samples / (soundinfo.samplerate * soundinfo.channels);
}
#endif