2 #include "AudioConfig.h"
3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4 #include "vorbis-tremor.h"
13 #ifndef VARBIS_MAX_READ_SIZE
14 # define VARBIS_MAX_READ_SIZE 256
17 #define VORBIS_HEADER_OPEN_LIMIT 1024
42 callbacks.read_func = read_func;
43 callbacks.seek_func = seek_func;
44 callbacks.close_func = close_func;
45 callbacks.tell_func = tell_func;
47 if (p_input->available()>=VORBIS_HEADER_OPEN_LIMIT){
69 virtual operator bool()
override {
return active; }
71 virtual bool copy()
override {
75 if(p_input->available()<VORBIS_HEADER_OPEN_LIMIT){
79 LOGI(
"available: %d", p_input->available());
91 if(pcm.data()==
nullptr){
92 LOGE(
"Not enough memory");
97 long result = ov_read(&file, (
char *)pcm.data(), pcm.size(), &bitstream);
98 LOGI(
"copy: %ld", result);
101 if (current != cfg) {
104 notifyAudioChange(cfg);
106 p_print->write(pcm.data(), result);
112 LOGD(
"copy: %ld - %s", result, readError(result));
114 LOGE(
"copy: %ld - %s", result, readError(result));
125 ov_callbacks callbacks;
128 bool is_first =
true;
129 bool is_ov_open =
false;
132 pcm.resize(VARBIS_MAX_READ_SIZE);
133 int rc = ov_open_callbacks(
this, &file,
nullptr, 0, callbacks);
135 LOGE(
"ov_open_callbacks: %d", rc);
142 AudioInfo currentInfo() {
144 vorbis_info *info = ov_info(&file, -1);
145 result.sample_rate = info->rate;
146 result.channels = info->channels;
147 result.bits_per_sample = 16;
151 virtual size_t readBytes(uint8_t *data,
size_t len)
override {
152 size_t read_size = min(len,(
size_t)VARBIS_MAX_READ_SIZE);
153 size_t result = p_input->readBytes((uint8_t *)data, read_size);
154 LOGD(
"readBytes: %zu",result);
158 static size_t read_func(
void *ptr,
size_t size,
size_t nmemb,
160 VorbisDecoder *
self = (VorbisDecoder *)datasource;
161 return self->readBytes((uint8_t *)ptr, size * nmemb);
164 static int seek_func(
void *datasource, ogg_int64_t offset,
int whence) {
165 VorbisDecoder *
self = (VorbisDecoder *)datasource;
169 static long tell_func(
void *datasource) {
170 VorbisDecoder *
self = (VorbisDecoder *)datasource;
174 static int close_func(
void *datasource) {
175 VorbisDecoder *
self = (VorbisDecoder *)datasource;
180 const char* readError(
long error){
183 return "Interruption in the data";
185 return "Invalid stream section ";
187 return "Invalid header";