Skip to content

Commit b3eb3d8

Browse files
committed
RTTTLOutput
1 parent bdbb002 commit b3eb3d8

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/AudioTools/Sandbox/RTTTLOutput.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class RTTTLOutput : public AudioOutput {
107107
setOutput(out);
108108
}
109109

110+
// Expose base-class begin overloads (e.g. begin(AudioInfo)) to avoid
111+
// hiding them with the no-arg begin() override below.
112+
using AudioOutput::begin;
113+
110114
/// Defines/Changes the output target (Print)
111115
void setOutput(Print& out) { p_print = &out; }
112116

@@ -128,13 +132,14 @@ class RTTTLOutput : public AudioOutput {
128132
addNotifyAudioChange(out);
129133
}
130134

135+
131136
/**
132137
* @brief Start the RTTTL stream: we start with
133138
* parsing the title and defaults
134139
* @return True if initialization was
135140
* successful.
136141
*/
137-
bool begin() {
142+
bool begin() override {
138143
is_start = true;
139144
return true;
140145
}
@@ -158,6 +163,7 @@ class RTTTLOutput : public AudioOutput {
158163
/// Writes RTTTL data to the parser and plays
159164
/// the notes
160165
size_t write(const uint8_t* data, size_t len) override {
166+
LOGD("write: %d",len);
161167
ring_buffer.resize(len);
162168
ring_buffer.writeArray(const_cast<uint8_t*>(data), len);
163169
if (is_start) {
@@ -200,20 +206,23 @@ class RTTTLOutput : public AudioOutput {
200206

201207
void play_note(float freq, int msec, int midi = -1) {
202208
// invoke the optional callback first
209+
LOGI("play_note: freq=%.2f Hz, msec=%d, midi=%d", freq, msec, midi);
203210
if (noteCallback) noteCallback(freq, msec, midi);
204211
if (p_print == nullptr || p_generator == nullptr) return;
205212
p_generator->setFrequency(freq);
206213
AudioInfo info = audioInfo();
207214
if (!info) return;
208215
int frames = (int)((uint64_t)info.sample_rate * (uint64_t)msec / 1000);
209-
int bytes = (info.channels * info.bits_per_sample) / 8;
210-
if (bytes <= 0) return;
211-
Vector<uint8_t> buffer(bytes);
212-
for (int j = 0; j < frames; j++) {
213-
p_generator->readBytes(buffer.data(), bytes);
214-
p_print->write((const uint8_t*)buffer.data(), bytes);
216+
int frame_size = (info.channels * info.bits_per_sample) / 8;
217+
int open = frames * frame_size;
218+
uint8_t buffer[1024];
219+
while (open > 0) {
220+
int toCopy = std::min(open, (int)sizeof(buffer));
221+
p_generator->readBytes(buffer, toCopy);
222+
p_print->write(buffer, toCopy);
223+
open -= toCopy;
215224
}
216-
}
225+
}
217226

218227
char next_char() {
219228
uint8_t c;
@@ -230,6 +239,8 @@ class RTTTLOutput : public AudioOutput {
230239
for (; m_actual != ':' && m_actual != '\0'; next_char()) {
231240
m_title += m_actual;
232241
}
242+
if (!m_title.isEmpty())
243+
LOGI("title: %s", m_title.c_str());
233244
}
234245

235246
int parse_num() {

0 commit comments

Comments
 (0)