forked from theelims/ESP32-sveltekit
-
-
Notifications
You must be signed in to change notification settings - Fork 11
FastLED audio #130
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
Merged
+13,043
−12,718
Merged
FastLED audio #130
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1497e93
Add FastLED audio
ewowi d68030f
FastLED audio tweaks
ewowi abca48e
FastLED Audio: use IO Pins, add controls
ewowi 8046fe9
add percussionType, tune readPins
ewowi 8c3a7d8
Reorganise code, use start/stopService, updatePin() check for removed
ewowi da93d5d
Fix Firmware update - exclude webflash + updatePin move
ewowi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| /** | ||
| @title MoonLight | ||
| @file D_FastLEDAudio.h | ||
| @repo https://github.com/MoonModules/MoonLight, submit changes to this file as PRs | ||
| @Authors https://github.com/MoonModules/MoonLight/commits/main | ||
| @Doc https://moonmodules.org/MoonLight/moonlight/overview/ | ||
| @Copyright © 2026 Github MoonLight Commit Authors | ||
| @license GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 | ||
| @license For non GPL-v3 usage, commercial licenses must be purchased. Contact us for more information. | ||
| **/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #if FT_MOONLIGHT | ||
|
|
||
| #include "fl/audio.h" | ||
| #include "fl/audio/audio_processor.h" | ||
| #include "fl/audio_input.h" | ||
| #include "fl/time_alpha.h" | ||
|
|
||
| // https://github.com/FastLED/FastLED/blob/master/src/fl/audio/README.md | ||
|
|
||
| class FastLEDAudioDriver : public Node { | ||
| private: | ||
| // Member variables for audio configuration | ||
| fl::AudioConfigI2S* i2sConfig = nullptr; | ||
| fl::AudioConfig* config = nullptr; | ||
| fl::shared_ptr<fl::IAudioInput> audioInput; | ||
|
|
||
| public: | ||
| static const char* name() { return "FastLED Audio"; } | ||
| static uint8_t dim() { return _NoD; } | ||
| static const char* tags() { return "☸️"; } | ||
|
|
||
| fl::AudioProcessor audioProcessor; | ||
|
|
||
| bool signalConditioning = true; | ||
| bool autoGain = false; | ||
| bool noiseFloorTracking = false; | ||
| uint8_t channel = fl::Left; | ||
|
|
||
| void setup() override { | ||
| addControl(signalConditioning, "signalConditioning", "checkbox"); | ||
| addControl(autoGain, "autoGain", "checkbox"); | ||
| addControl(noiseFloorTracking, "noiseFloorTracking", "checkbox"); | ||
| addControl(channel, "channel", "select"); | ||
| addControlValue("Left"); | ||
| addControlValue("Right"); | ||
| addControlValue("Both"); | ||
|
|
||
| ioUpdateHandler = moduleIO->addUpdateHandler([this](const String& originId) { readPins(); }); | ||
| readPins(); // Node added at runtime so initial IO update not received so run explicitly | ||
|
|
||
| audioProcessor.onBeat([]() { | ||
| sharedData.beat = true; | ||
| // EXT_LOGD(ML_TAG, "onBeat"); | ||
| }); | ||
|
|
||
| audioProcessor.onVocalStart([]() { | ||
| sharedData.vocalsActive = true; | ||
| // EXT_LOGD(ML_TAG, "onVocalStart"); | ||
| }); | ||
|
|
||
| audioProcessor.onVocalEnd([]() { | ||
| sharedData.vocalsActive = false; | ||
| // EXT_LOGD(ML_TAG, "onVocalEnd"); | ||
| }); | ||
|
|
||
| audioProcessor.onVocalConfidence([](float confidence) { | ||
| sharedData.vocalConfidence = sharedData.vocalsActive ? confidence : 0.0; | ||
| // EXT_LOGD(ML_TAG, "onVocalConfidence %d", confidence); | ||
| }); | ||
|
|
||
| audioProcessor.onBass([](float level) { | ||
| if (level > 0.01f) { | ||
| sharedData.bassLevel = level; | ||
| // EXT_LOGD(ML_TAG, "onBass: %f", level); | ||
| } | ||
| }); | ||
|
|
||
| audioProcessor.onMid([](float level) { | ||
| if (level > 0.01f) { | ||
| sharedData.midLevel = level; | ||
| // EXT_LOGD(ML_TAG, "onMid: %f", level); | ||
| } | ||
| }); | ||
|
|
||
| audioProcessor.onTreble([](float level) { | ||
| if (level > 0.01f) { | ||
| sharedData.trebleLevel = level; | ||
| // EXT_LOGD(ML_TAG, "onTreble: %f", level); | ||
| } | ||
| }); | ||
| audioProcessor.onPercussion([](fl::PercussionType type) { | ||
| // EXT_LOGD(ML_TAG, "onPercussion: %d", type); | ||
| sharedData.percussionType = (uint8_t)type; | ||
| }); | ||
| } | ||
|
|
||
| void onUpdate(const Char<20>& oldValue, const JsonObject& control) override { | ||
| if (control["name"] == "signalConditioning") { | ||
| audioProcessor.setSignalConditioningEnabled(signalConditioning); | ||
| } | ||
| if (control["name"] == "autoGain") { | ||
| audioProcessor.setAutoGainEnabled(autoGain); | ||
| } | ||
| if (control["name"] == "noiseFloorTracking") { | ||
| audioProcessor.setNoiseFloorTrackingEnabled(noiseFloorTracking); | ||
| } | ||
| if (control["name"] == "channel" && oldValue != "") { // not on boot as readPins will do it then | ||
| // recreate with the new channel | ||
| stopService(); | ||
| startService(); | ||
| } | ||
| } | ||
|
|
||
| uint8_t pinI2SSD = UINT8_MAX; | ||
| uint8_t pinI2SWS = UINT8_MAX; | ||
| uint8_t pinI2SSCK = UINT8_MAX; | ||
|
|
||
| void readPins() { | ||
| if (safeModeMB) { | ||
| EXT_LOGW(ML_TAG, "Safe mode enabled, not adding pins"); | ||
| return; | ||
| } | ||
|
|
||
| bool changed = moduleIO->updatePin(pinI2SWS, pin_I2S_WS); | ||
| changed = moduleIO->updatePin(pinI2SSD, pin_I2S_SD) || changed; | ||
| changed = moduleIO->updatePin(pinI2SSCK, pin_I2S_SCK) || changed; | ||
|
|
||
| if (changed) { | ||
| EXT_LOGI(ML_TAG, "(re)creating audioInput %d %d %d", pinI2SWS, pinI2SSD, pinI2SSCK); | ||
| stopService(); | ||
| if (pinI2SWS != UINT8_MAX && pinI2SSD != UINT8_MAX && pinI2SSCK != UINT8_MAX) startService(); | ||
| } | ||
| } | ||
|
|
||
| void loop() override { | ||
| if (!audioInput) return; | ||
|
|
||
| sharedData.beat = false; | ||
| sharedData.percussionType = UINT8_MAX; | ||
|
|
||
| while (fl::AudioSample sample = audioInput->read()) { | ||
| audioProcessor.update(sample); | ||
| } | ||
| } | ||
|
|
||
| void startService() { | ||
| // Create configuration objects | ||
| i2sConfig = new fl::AudioConfigI2S(pinI2SWS, pinI2SSD, pinI2SSCK, 0, channel == 1 ? fl::Right : channel == 2 ? fl::Both : fl::Left, 44100, 16, fl::Philips); | ||
|
|
||
| config = new fl::AudioConfig(*i2sConfig); | ||
|
|
||
| fl::string errorMsg; | ||
| audioInput = fl::IAudioInput::create(*config, &errorMsg); | ||
| if (!audioInput) { | ||
| EXT_LOGE(ML_TAG, "Failed to create audio input: %s", errorMsg.c_str()); | ||
| return; | ||
| } | ||
| audioInput->start(); | ||
| } | ||
|
|
||
| void stopService() { | ||
| if (audioInput) { | ||
| audioInput->stop(); | ||
| audioInput.reset(); // Explicitly release shared_ptr, even makes it a nullptr... | ||
| } | ||
|
|
||
| // Clean up raw pointers | ||
| if (config) { | ||
| delete config; | ||
| config = nullptr; | ||
| } | ||
|
|
||
| if (i2sConfig) { | ||
| delete i2sConfig; | ||
| i2sConfig = nullptr; | ||
| } | ||
| } | ||
|
|
||
| ~FastLEDAudioDriver() override { | ||
| stopService(); | ||
| moduleIO->removeUpdateHandler(ioUpdateHandler); | ||
| } | ||
|
|
||
| private: | ||
| update_handler_id_t ioUpdateHandler; | ||
| }; | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.