Skip to content

Add built-in octave shift feature#42

Open
fsecgin wants to merge 1 commit intoend2endzone:masterfrom
fsecgin:feature/octave-shift
Open

Add built-in octave shift feature#42
fsecgin wants to merge 1 commit intoend2endzone:masterfrom
fsecgin:feature/octave-shift

Conversation

@fsecgin
Copy link
Copy Markdown

@fsecgin fsecgin commented Feb 5, 2026

Summary

  • Adds an octaveShift property to rtttl_context_t that shifts all note frequencies up or down by N octaves before calling tone()
  • Default is 0 (no shift), fully backward compatible, no breaking changes
  • Eliminates the need for users to write a custom setToneFunction wrapper just to shift pitch for passive buzzers

Motivation

Passive buzzers have a resonant frequency sweet spot (often 2500Hz+), but RTTTL melodies are written for the standard musical range (200-800Hz). Currently the only way to shift frequencies is to override setToneFunction with a wrapper that multiplies the frequency before passing it to tone(). This is boilerplate that every passive buzzer user ends up writing.

API

// Global context (legacy API)
anyrtttl::setOctaveShift(2);  // shift up 2 octaves

// Specific context (multi-speaker)
anyrtttl::setOctaveShift(myContext, 2);

Changes

  • src/anyrtttl.h: Added int8_t octaveShift field to rtttl_context_t, added setOctaveShift() declarations
  • src/anyrtttl.cpp: Implemented setter, initialized field in initContext(), applied shift at frequency lookup in nextNote(), preserved shift across begin() calls
  • examples/OctaveShift/OctaveShift.ino: Example showing normal vs shifted playback
  • README.md: Documentation section with usage example
  • CHANGES: Entry for v2.7.0

Test plan

  • Clean build on ATmega32U4 (SparkFun Pro Micro 16MHz) via PlatformIO
  • Verified same RAM/Flash footprint as unmodified library (338B / 7810B)
  • Tested with passive buzzer, confirmed shifted playback matches previous setToneFunction wrapper behavior
  • Should also compile on ESP32 and ESP8266 (not tested yet)

Passive buzzers have a resonant frequency sweet spot well above the
standard RTTTL note range (200-800Hz). Previously users had to create
a custom setToneFunction wrapper to shift frequencies, which is
boilerplate that every buzzer user ends up writing.

This adds an octaveShift property to rtttl_context_t that shifts all
note frequencies up or down by N octaves before calling tone().
Default is 0 (no shift), so existing behavior is fully preserved.

API:
- anyrtttl::setOctaveShift(N) for the global context
- anyrtttl::setOctaveShift(context, N) for a specific context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant