10 #include "sid-api/libcsid.h"
11 #include "AudioTools.h"
13 namespace audio_tools {
27 const unsigned char *tune_data =
nullptr;
28 int tune_data_length = 0;
40 char author[32] = {0};
41 char sid_info[32] = {0};
46 LOGI(
"SID Title: %s", title);
47 LOGI(
"SID Author: %s", author);
48 LOGI(
"SID Info: %s", sid_info);
49 LOGI(
"SID Number of tunes: %d", total_tunes);
50 LOGI(
"SID Default tune: %d", default_tune);
74 SIDStream(
const unsigned char *tunedata,
int tunedatalen,
int subtune = 0) {
75 cfg.tune_data = tunedata;
76 cfg.tune_data_length = tunedatalen;
77 cfg.subtune = subtune;
88 if (config.bits_per_sample!=16){
89 LOGE(
"bits_per_sample %d: must be 16", config.bits_per_sample);
93 if (config.tune_data ==
nullptr && cfg.tune_data !=
nullptr) {
94 config.tune_data = cfg.tune_data;
95 config.tune_data_length = cfg.tune_data_length;
98 if (config.subtune==0 && cfg.subtune!=0){
99 config.subtune = cfg.subtune;
102 this->frame_size =
sizeof(int16_t) * cfg.channels;
105 libcsid_init(cfg.sample_rate, cfg.sid_model);
108 if (cfg.tune_data !=
nullptr) {
109 setSID(cfg.tune_data, cfg.tune_data_length, cfg.subtune);
111 LOGI(
"call setSID()");
128 void setSID(
const unsigned char *tunedata,
int tunedatalen,
int subtune = 0) {
129 LOGI(
"setSID len %d, subtune: %d", tunedatalen, subtune);
132 cfg.tune_data = tunedata;
133 cfg.tune_data_length = tunedatalen;
136 libcsid_load((
unsigned char *)tunedata, tunedatalen);
139 memcpy(meta.author, libcsid_getauthor(),
sizeof(meta.author));
140 memcpy(meta.title, libcsid_gettitle(),
sizeof(meta.author));
141 memcpy(meta.sid_info, libcsid_getinfo(),
sizeof(meta.sid_info));
142 meta.total_tunes = libcsid_get_total_tunes_number();
143 meta.default_tune = libcsid_get_default_tune_number();
147 void setTune(
int subtune = 0) {
150 LOGI(
"setTune: %d", subtune);
151 cfg.subtune = subtune;
153 libcsid_play(cfg.subtune);
157 size_t readBytes(uint8_t *buffer,
size_t bytes)
override {
163 if (bytes<frame_size){
164 LOGE(
"readBytes: %d - too small", bytes);
169 LOGD(
"readBytes %d", bytes);
171 int16_t *ptr = (int16_t *)buffer;
172 int frames = bytes / frame_size;
173 for (
int j = 0; j < frames; j++) {
175 for (
int i = 0; i < cfg.channels; i++) {
181 updateTimeOfLastSound((int16_t*)buffer, bytes/2);
193 return active && (time_of_last_sound > millis() - timeoutMs);
201 uint64_t time_of_last_sound = 0;
205 unsigned short result;
207 libcsid_render(&result, 1);
209 return ((int16_t)result);
212 void updateTimeOfLastSound(int16_t *data,
int len){
213 for (
int j=0;j<len;j++){
215 time_of_last_sound = millis();