Skip to content

Commit a6398c8

Browse files
committed
RTTTLOutput: setTransposeOctaves()
1 parent 4fbd190 commit a6398c8

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/AudioTools/CoreAudio/RTTTLOutput.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,19 @@ class RTTTLOutput : public AudioOutput {
198198
noteCallback = cb;
199199
}
200200

201-
// Provide reference for callback
201+
/// Provide reference for callback
202202
void setReference(void* ref) { reference = ref; }
203203

204+
/// transpose all notes by the specified number of octaves e.g. -2 = 2 octaves
205+
/// down
206+
void setTransposeOctaves(int8_t octaves) { m_tranpose_octaves = octaves; }
207+
204208
protected:
205209
MusicalNotes m_notes;
206210
SoundGenerator<T>* p_generator = nullptr;
207211
RingBuffer<uint8_t> ring_buffer{0};
208212
Print* p_print = nullptr;
213+
int8_t m_tranpose_octaves = 0;
209214
bool is_start = true;
210215
char m_actual = 0;
211216
char m_prec = 0;
@@ -311,6 +316,11 @@ class RTTTLOutput : public AudioOutput {
311316
LOGI("msec per semi: %.2f", m_msec_semi);
312317
}
313318

319+
float transpose(float frequency, int8_t octaves) {
320+
if (octaves == 0) return frequency;
321+
return frequency * powf(2.0f, octaves);
322+
}
323+
314324
void parse_notes() {
315325
// Ensure we start reading after the defaults
316326
// section
@@ -399,6 +409,7 @@ class RTTTLOutput : public AudioOutput {
399409
if (next_char() == 0) {
400410
// compute and play
401411
float freq = m_notes.frequency(noteEnum, (uint8_t)octave);
412+
freq = transpose(freq, m_tranpose_octaves);
402413
int msec = (int)((m_msec_semi / duration) * 1.0);
403414
int midi = m_notes.frequencyToMidiNote(freq);
404415
play_note(freq, msec, midi);
@@ -414,6 +425,7 @@ class RTTTLOutput : public AudioOutput {
414425
}
415426

416427
float freq = m_notes.frequency(noteEnum, (uint8_t)octave);
428+
freq = transpose(freq, m_tranpose_octaves);
417429
int msec = (int)((m_msec_semi / duration) * mult_duration);
418430
int midi = m_notes.frequencyToMidiNote(freq);
419431
play_note(freq, msec, midi);

0 commit comments

Comments
 (0)