Sound & Music¶
After this page you can play a melody and make a light react to sound.
The Cyber Fidget has a MAX98357A I2S amplifier with speaker for output and an ICS-43434 MEMS microphone for input. Your apps can play tones and react to sound levels. The device controls output volume; setVolume() has no effect in studio apps, on the device or in the browser emulator. The microphone isn't just for measuring loudness either - the device can capture real audio and save it to the memory card, which is exactly what the built-in Voice Notes recorder does.
What is this?¶
The device includes:
- Speaker - Plays tones and sounds. Your app sets a frequency (pitch) and duration.
- Microphone -- Listens to ambient sound. You can read how loud it is and react to it, and the firmware can record it to a file.
Both are opt-in: enable the mic when your app needs it, and disable it when you're done to save power.
Level metering vs. recording
The app-facing audio API on this page is about reacting to sound -- reading the live microphone level so your app can respond to it. Recording sound to the card is a step beyond that, and it lives inside the built-in Voice Notes app, which captures audio on its own. Studio apps can read levels but cannot record audio through this interface.
Concepts¶
Playing tones¶
Add each block into the studio starter where its comments say. Each block works on its own. Keep the starter's bottom-left exit button and its setColorsOff() calls in begin() and end().
You play a tone by specifying its frequency in Hertz (Hz). Higher Hz = higher pitch. Musical notes have standard frequencies:
| Note | Frequency (Hz) |
|---|---|
| C4 | 261.63 |
| D4 | 293.66 |
| E4 | 329.63 |
| F4 | 349.23 |
| G4 | 392.00 |
| A4 | 440.00 |
| B4 | 493.88 |
| C5 | 523.25 |
Octaves
Each octave doubles the frequency. C5 is twice C4. Use freq * pow(2, octave) to shift notes up or down.
Sounds that work on the speaker¶
The built-in speaker is tiny (13 mm across), and a speaker that small cannot push much air at low pitches. In practice it barely reproduces notes below about 700-800 Hz. Its sweet spot is roughly 1-4 kHz (1000-4000 Hz).
- Keep important beeps and alerts between C5 and C7 (523-2093 Hz), or anywhere in the 1-4 kHz range, so they are easy to hear.
- Low notes such as C4 (262 Hz) still work, because the Fidget's tones include harmonics (quieter copies of the note at 3x, 5x and 7x its frequency) that the speaker can reproduce. They just sound noticeably quieter, so avoid low notes for anything the user must notice.
- Tones are timed by the audio engine, independently of
update(). A 100 ms tone lasts 100 ms, plus a fade-out of about 5 ms to reduce clicks. Studio apps use this engine on the device and in the browser emulator, so a tone's length does not depend on your drawing speed. - Several notes at once (chords) work in your own apps too, with firmware 1.5.0 or newer: see Notes: several sounds at once. With older firmware, plan on one sound at a time.
Speaker EQ setting¶
The Fidget shapes the sound of its built-in speaker with an EQ (equalizer). You choose how in Settings > Speaker EQ, the last item in the Settings menu.
To get there, in the menu press the top-right button to move down to Settings and the bottom-right button to open it. Move down to Speaker EQ, below Reset to factory, and open it the same way. (Don't open Reset to factory on the way.)
The menu on a Cyber Fidget: down to Settings, open it, down past Reset to factory to Speaker EQ, open it, then one press right (Loud) and one press left (back to Balanced).
The screen shows one preset name with a short description under it. Press the middle-left or middle-right button to change preset. Each change takes effect at once and plays a short rising run of tones so you can hear the difference. Press the bottom-left button (the screen calls it Select) to go back. The choice is saved, so it is still there after the Fidget restarts.
| Preset | What you hear |
|---|---|
| Balanced | Clear and even. This is the default. |
| Loud | Louder and punchier, and a little sharper. |
| Off | Plain, no shaping. It adds no boost or cut, but it still removes the very low pitches the small speaker cannot play. |
The setting changes the built-in speaker only. Bluetooth speakers and headphones are not affected.
Playing a sequence¶
To play a short melody or alert without writing your own timing code, describe it as an array of ToneStep entries and hand it to playSequence():
| Field | Meaning |
|---|---|
freq |
Frequency in Hz; 0 is a rest (silence) |
durationMs |
Length of the tone or rest in milliseconds |
gapAfterMs |
Silence after that step, in milliseconds |
const marks data that this code cannot change. In a type such as const ToneStep*, * means a pointer, an address used to read the steps. In const ButtonEvent& e below, & passes the existing event by reference rather than making a copy.
// Insert inside begin(), below setColorsOff();
// Each step has frequency in Hz, duration in ms, then gap in ms.
static const AudioManager::ToneStep step[] = { {880, 100, 50} };
HAL::audioManager().playSequence(step, 1);
// Insert inside end(), above unregisterButtonCallbacks();
HAL::audioManager().stopSequence();
HAL::audioManager().stopTone();
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
In the starter, nullptr means a pointer has no object. * in a declaration marks a pointer (an address), and before a pointer reads its object. & in a type refers to an existing object; before a value it gives its address. const means the code cannot change the value through that declaration.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
// Each step has frequency in Hz, duration in ms, then gap in ms.
static const AudioManager::ToneStep step[] = { {880, 100, 50} };
HAL::audioManager().playSequence(step, 1);
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
HAL::audioManager().stopSequence();
HAL::audioManager().stopTone();
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
// Insert inside begin(), below setColorsOff();
static const AudioManager::ToneStep WIN_JINGLE[] = {
{ 1046.50f, 100, 50 }, // C6, then a 50 ms pause
{ 0, 80, 0 }, // Rest for 80 ms
{ 1318.51f, 200, 0 }, // E6
};
static const int WIN_JINGLE_LEN = sizeof(WIN_JINGLE) / sizeof(WIN_JINGLE[0]);
HAL::audioManager().playSequence(WIN_JINGLE, WIN_JINGLE_LEN);
// Insert inside end(), above unregisterButtonCallbacks();
HAL::audioManager().stopSequence();
HAL::audioManager().stopTone();
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
static const AudioManager::ToneStep WIN_JINGLE[] = {
{ 1046.50f, 100, 50 }, // C6, then a 50 ms pause
{ 0, 80, 0 }, // Rest for 80 ms
{ 1318.51f, 200, 0 }, // E6
};
static const int WIN_JINGLE_LEN = sizeof(WIN_JINGLE) / sizeof(WIN_JINGLE[0]);
HAL::audioManager().playSequence(WIN_JINGLE, WIN_JINGLE_LEN);
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
HAL::audioManager().stopSequence();
HAL::audioManager().stopTone();
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
playSequence(const ToneStep* steps, int count)starts playback and returns immediately; the sequence plays in the background while yourupdate()keeps running.stopSequence()stops it early.isSequencePlaying()returnstruewhile it is still playing in built-in firmware apps. In studio apps, on the device and in the browser emulator, it always returnsfalse, so time your app's own logic instead of polling it. See elapsed-time checks.- In a studio app, the host copies up to 64 steps when you call
playSequence(); a longer list is cut to 64. The host is the firmware code that runs your app, on the device or in the browser emulator. The array does not need to remain alive during playback. The examples usestatic constfor fixed melodies. Built-in firmware apps accept up to 128 steps.
Volume¶
In built-in firmware apps, setVolume() accepts a float, a number with a fractional part, from 0.0 (silent) to 1.0 (full). In studio apps, setVolume() has no effect on the device or in the browser emulator. Use the emulator's volume and mute controls to adjust browser playback.
Checking browser sound problems
On a computer, press F12 and open the browser developer tools' Console tab to look for loading or playback errors. This is separate from the studio's Console panel. Studio apps cannot print messages with Serial.println(). See emulator troubleshooting.
Microphone input¶
The mic is opt-in. Call enableMic(true) in begin() and enableMic(false) in end(). While enabled:
getMicVolumeLinear()-- Returns 0.0 to 1.0 (linear amplitude).getMicVolumeDb()-- Returns dBFS (decibels below full scale), typically negative (e.g. -60 to 0).
Notes: several sounds at once¶
playTone() makes one sound at a time. A note is a tone that can sound together with other notes, which is called polyphony (many voices at once). Notes are how you play a chord, or let another note start while one is still ringing.
// Insert at the top of the file, below the #include lines
static int noteHandle = -1;
// Insert inside begin(), below setColorsOff();
noteHandle = HAL::audioManager().playNote(1046.5f); // Hold C6 until stopped.
// Insert inside end(), above unregisterButtonCallbacks();
HAL::audioManager().stopNote(noteHandle);
HAL::audioManager().stopNotes();
HAL::audioManager().stopTone();
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
static int noteHandle = -1;
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
noteHandle = HAL::audioManager().playNote(1046.5f); // Hold C6 until stopped.
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
HAL::audioManager().stopNote(noteHandle);
HAL::audioManager().stopNotes();
HAL::audioManager().stopTone();
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
playNote()starts a note and returns a handle, a number greater than 0 that identifies that note. It returns-1if the note could not start. AdurationMsof0(the default) holds the note until you stop it.stopNote(handle)ends only that note. If the note was already replaced by a newer one, the call does nothing.stopNotes()ends every note at once.- Up to 7 notes sound at the same time, alongside one
playTone(). When all seven are busy, another note takes the place of one that is already fading out, or, if none is, the one that started first. - Every note plays about 3 dB quieter than
playTone()(a dB, or decibel, is a unit of loudness; 3 dB quieter is about half the power). That leaves room for several notes at once, so chords are less likely to distort.
Low notes below about 700 Hz are hard to hear on the speaker, so keep important notes higher than that (see Sounds that work on the speaker).
This example plays a C major chord (C5, E5, G5) while you are holding the first button, and stops it when you let go:
// Insert at the top of the file, below the #include lines
static int chord[3] = { -1, -1, -1 };
static void onFirstButton(const ButtonEvent& e) {
if (e.eventType == ButtonEvent_Pressed) {
chord[0] = HAL::audioManager().playNote(523.25f); // C5
chord[1] = HAL::audioManager().playNote(659.25f); // E5
chord[2] = HAL::audioManager().playNote(783.99f); // G5
}
if (e.eventType == ButtonEvent_Released) {
for (int i = 0; i < 3; ++i) {
HAL::audioManager().stopNote(chord[i]);
chord[i] = -1;
}
}
}
// Insert inside begin(), below setColorsOff();
buttonManager.registerCallback(button_TopLeftIndex, onFirstButton);
// Insert inside end(), above unregisterButtonCallbacks();
buttonManager.unregisterCallback(button_TopLeftIndex);
HAL::audioManager().stopNotes();
HAL::audioManager().stopTone();
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
static int chord[3] = { -1, -1, -1 };
static void onFirstButton(const ButtonEvent& e) {
if (e.eventType == ButtonEvent_Pressed) {
chord[0] = HAL::audioManager().playNote(523.25f); // C5
chord[1] = HAL::audioManager().playNote(659.25f); // E5
chord[2] = HAL::audioManager().playNote(783.99f); // G5
}
if (e.eventType == ButtonEvent_Released) {
for (int i = 0; i < 3; ++i) {
HAL::audioManager().stopNote(chord[i]);
chord[i] = -1;
}
}
}
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
buttonManager.registerCallback(button_TopLeftIndex, onFirstButton);
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
buttonManager.unregisterCallback(button_TopLeftIndex);
HAL::audioManager().stopNotes();
HAL::audioManager().stopTone();
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
Notes need firmware 1.5.0 or later
Apps that use notes need Cyber Fidget firmware 1.5.0 or newer. Apps are checked automatically against what the firmware on your device supports. On older firmware the device shows "App needs firmware 1.5.0 or newer"; press any button to return to the menu. Apps that only play tones with playTone() are not affected.
Reading the microphone¶
The microphone can drive your app: a level bar, an LED that brightens with sound, a clap detector. Turn it on, read the level every frame, and turn it off when you are done:
// Insert inside begin(), below setColorsOff();
HAL::audioManager().enableMic(true);
// Insert inside update(), just above display.display();
float level = HAL::audioManager().getMicVolumeLinear();
display.clear();
display.drawRect(10, 28, 108, 8);
display.fillRect(11, 29, (int)(level * 106), 6);
// Insert inside end(), above unregisterButtonCallbacks();
HAL::audioManager().enableMic(false);
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
HAL::audioManager().enableMic(true);
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
float level = HAL::audioManager().getMicVolumeLinear();
display.clear();
display.drawRect(10, 28, 108, 8);
display.fillRect(11, 29, (int)(level * 106), 6);
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
HAL::audioManager().enableMic(false);
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
getMicVolumeLinear() gives a loudness from 0.0 (silence) to 1.0 (the loudest the microphone can measure). getMicVolumeDb() gives the same level in dBFS (decibels relative to full scale): 0 is the loudest possible sound and quieter sounds are negative numbers, for example -60 for a quiet room.
- The emulator has no microphone, so in the emulator both functions read silence. Test microphone apps on a real Cyber Fidget.
- Just after the device wakes up, while it is checking for updates, the microphone may read silence for a few seconds. It then starts reporting normally.
If your app exits without turning the microphone off, the system turns it off for you (and stops any notes). Doing it yourself in end() is still good practice, because end() is where an app cleans up after itself.
The microphone functions need firmware 1.5.0 or later
Apps that read the microphone need Cyber Fidget firmware 1.5.0 or newer. On older firmware the device shows "App needs firmware 1.5.0 or newer"; press any button to return to the menu.
Code example: melody and sound reaction¶
// Insert at the top of the file, below the #include lines
static unsigned long lastNote = 0;
static int noteIndex = 0;
// Insert inside begin(), below setColorsOff();
lastNote = millis();
noteIndex = 0;
HAL::audioManager().enableMic(true);
// Insert inside update(), just above display.display();
static const float notes[] = { 1046.50f, 1318.51f, 1567.98f }; // C6, E6, G6
unsigned long frameTime = millis();
if (frameTime - lastNote >= 500) {
HAL::audioManager().playTone(notes[noteIndex], 200);
noteIndex = (noteIndex + 1) % 3;
lastNote = frameTime;
}
float micLevel = HAL::audioManager().getMicVolumeLinear();
uint8_t brightness = (uint8_t)(micLevel * 255);
HAL::setRgbLed(pixel_Front_Top, brightness, 0, 0, 0);
// Insert inside end(), above unregisterButtonCallbacks();
HAL::audioManager().stopTone();
HAL::audioManager().enableMic(false);
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
static unsigned long lastNote = 0;
static int noteIndex = 0;
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
lastNote = millis();
noteIndex = 0;
HAL::audioManager().enableMic(true);
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
static const float notes[] = { 1046.50f, 1318.51f, 1567.98f }; // C6, E6, G6
unsigned long frameTime = millis();
if (frameTime - lastNote >= 500) {
HAL::audioManager().playTone(notes[noteIndex], 200);
noteIndex = (noteIndex + 1) % 3;
lastNote = frameTime;
}
float micLevel = HAL::audioManager().getMicVolumeLinear();
uint8_t brightness = (uint8_t)(micLevel * 255);
HAL::setRgbLed(pixel_Front_Top, brightness, 0, 0, 0);
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
HAL::audioManager().stopTone();
HAL::audioManager().enableMic(false);
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
This example needs firmware 1.5.0 or later
It reads the microphone, so apps made from it need Cyber Fidget firmware 1.5.0 or newer.
Always disable the mic in end()
Call enableMic(false) in your app's end() to release the microphone.
Functions on this page¶
The note and microphone functions need firmware 1.5.0 or newer.
| Call | What it does |
|---|---|
AudioManager& HAL::audioManager()Example: AudioManager& audio = HAL::audioManager(); |
Get the sound manager for tones, notes, and microphone readings. |
void HAL::audioManager().playTone(float frequency, int durationMs = 0)Example: HAL::audioManager().playTone(880, 100); |
Play a tone at 880 Hz for 100 ms; duration 0 plays until stopped. |
void HAL::audioManager().stopTone()Example: HAL::audioManager().stopTone(); |
Stop the tone; call when leaving an app that uses tones. |
int HAL::audioManager().playNote(float frequency, int durationMs = 0)Example: HAL::audioManager().playNote(440, 100); |
Play a note alongside others (a chord); duration 0 holds it until stopNote(). Returns a handle, or -1 if the note could not start. |
void HAL::audioManager().stopNote(int handle)Example: HAL::audioManager().stopNote(handle); |
Stop the note identified by the handle returned from playNote(). |
void HAL::audioManager().stopNotes()Example: HAL::audioManager().stopNotes(); |
Stop all additional notes. |
void HAL::audioManager().playSequence(const ToneStep* steps, int count)Example: HAL::audioManager().playSequence(steps, count); |
Play up to 64 copied ToneStep entries in studio apps, on the device and in the browser emulator: freq in Hz (0 is rest), durationMs and gapAfterMs in ms. |
void HAL::audioManager().stopSequence()Example: HAL::audioManager().stopSequence(); |
Stop the tone sequence. |
bool HAL::audioManager().isSequencePlaying() constExample: HAL::audioManager().isSequencePlaying(); |
Always false in studio apps, on the device and in the browser emulator. Time your app logic with millis() instead. |
void HAL::audioManager().setVolume(float)Example: HAL::audioManager().setVolume(0.5f); |
Does nothing in studio apps, on the device and in the browser emulator. Use the emulator controls for browser volume. |
void HAL::audioManager().enableMic(bool on)Example: HAL::audioManager().enableMic(true); |
Enable device microphone readings; false disables them; emulator has no microphone. |
float HAL::audioManager().getMicVolumeLinear() constExample: HAL::audioManager().getMicVolumeLinear(); |
Read microphone level 0.0-1.0; emulator stays at 0.0 (silent). |
float HAL::audioManager().getMicVolumeDb() constExample: HAL::audioManager().getMicVolumeDb(); |
Read microphone dBFS; 0 is full scale, quieter is negative; emulator returns -60. |
Framework details¶
Advanced: how the firmware handles audio
You do not need these details to build apps in the studio. They describe the firmware internals for readers writing an app by hand and building it into the firmware.
AudioManager¶
The firmware shares one AudioManager object, accessed via HAL::audioManager(). It handles:
- Tone and note output - the audio engine (
AudioEngine, 8 voices at 44.1 kHz) mixes tones, notes and sequences, applies the volume and the speaker EQ, then sends the samples over I2S to the MAX98357A amplifier. Tones and notes use a soft square wave (a square wave with its harshest harmonics removed; harmonics are the fainter, higher-pitched tones at whole-number multiples of a note's pitch that give a sound its character) so low notes stay audible on the small speaker. - Mic input -- The ICS-43434 microphone sends audio over I2S.
VolumeMetermeasures its level and shares a value from 0 to 1 with the app.
The AudioManager mic path is metering-only: it publishes a level, not a stream of samples. The built-in Voice Notes recorder opens the same ICS-43434 microphone on its own I2S port and pulls the raw sample stream for capture, independent of AudioManager. Studio apps do not have access to that capture path.
I2S¶
Audio uses the ESP32's I2S peripherals:
- I2S0 -- Speaker (TX) on pins 27 (LRCLK), 26 (BCLK), 14 (DOUT)
- I2S1 -- Microphone (RX) on pins 25 (LRCLK), 32 (BCLK), 33 (DATA IN)
The mic runs in a separate FreeRTOS task and publishes micVolumeAtomic roughly every 20 ms.
