Skip to content

Fix playTone square wave pitch (missing 2*pi) - #2964

Open
p1oner15 wants to merge 1 commit into
BruceDevices:devfrom
p1oner15:fix/tone-frequency
Open

p1oner15 wants to merge 1 commit into
BruceDevices:devfrom
p1oner15:fix/tone-frequency

Conversation

@p1oner15

Copy link
Copy Markdown

Proposed Changes

playTone() builds its square wave as sin(hz * time) >= 0. AudioFileSourceFunction passes time in seconds ((p - sizeof(WavHeader)) / avg_bytes_per_sec), so the wave runs at hz / 2π: a 440 Hz tone plays at ~70 Hz, 880 Hz at ~140 Hz. The sine branch right below (waveType == 1) and the ESP8266Audio example PlayWAVFromFunction already use sin(TWO_PI * hz * time).

One-line fix: add the missing TWO_PI. Affects every _tone() / playTone() on boards that go through the I2S speaker path (not Cardputer, not M5Unified, not buzzer boards).

Types of Changes

Bugfix

Verification

audio.tone(440, 1500) and audio.tone(880, 1500) from a JS script, pitch read with a phone tuner app held at the speaker.

440 Hz requested 880 Hz requested
before (dev @ 4fb1615) ~70 Hz (computed: 440 / 2π) ~140 Hz (computed)
after A4 on the tuner A5 on the tuner

Testing

Tested on LilyGO T-Embed CC1101 Plus (lilygo-t-embed-cc1101). Build has no new warnings.

Linked Issues

None found (searched open/closed issues and PRs for tone / pitch / frequency).

User-Facing Change

Speaker tones (beeps, JS audio.tone) now play at the requested frequency; before they sounded about 6x lower.

Further Comments

AudioFileSourceFunction passes time in seconds, so sin(hz * time) runs
at hz / (2*pi): a 440 Hz tone came out at about 70 Hz. The sine wave
type (waveType 1) and the ESP8266Audio example already use
sin(TWO_PI * hz * time).

This branch has not been deployed

No deployments
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