Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate a simple tone on speaker #15

Open
spleenware opened this issue Oct 7, 2023 · 6 comments
Open

How to generate a simple tone on speaker #15

spleenware opened this issue Oct 7, 2023 · 6 comments
Labels

Comments

@spleenware
Copy link

Can you add an example sketch on how to generate a simple tone to play on the built-in speaker (over I2S, I assume?).

@technoblogy
Copy link

technoblogy commented Oct 8, 2023

Here's a simple version:

void beep (int freq, int duration) {
  I2S.setAllPins(7, 5, 6, 6, 6); // sckPin, fsPin, sdPin, outSdPin, inSdPin
  const int samplerate = 8000;
  int halfwave = samplerate / freq, amplitude = 500;
  unsigned long start = millis();
  if (!I2S.begin(I2S_PHILIPS_MODE, samplerate, 16)) return; // Error
  while (millis() < start + duration) {
    if (count % halfwave == 0) amplitude = -1 * amplitude;
    I2S.write(amplitude); I2S.write(amplitude);
  }
  I2S.end();
}

The frequency is in Hz and the duration in milliseconds.

@spleenware
Copy link
Author

Which header files are needed, and how is I2S object constructed? (specifically for ESP32/T-Deck target)

@technoblogy
Copy link

You need:

#include <I2S.h>

I also found I needed this:

#include "soc/periph_defs.h" // Not sure why necessary

It's based on the SimpleTone example which is in the ESP32 core at:

/Users/david/Library/Arduino15/packages/esp32/hardware/esp32/2.0.12/libraries/I2S/examples/SimpleTone

@spleenware
Copy link
Author

Ah, thank you. I hear a beep now! :-)

@aaron-924
Copy link

Is there a reason why "Play MP3" doesn't work in the UnitTest code?

Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants