FastLED 3.9.15
Loading...
Searching...
No Matches
AudioUrl.ino
Go to the documentation of this file.
1// @filter: (memory is large)
2// AudioUrl - Demonstrates loading audio from a URL in the WASM UI.
3//
4// When running in the FastLED web compiler, the browser will automatically
5// load and play the specified MP3 URL on page load. Audio samples drive
6// a simple volume-to-brightness visualization.
7//
8// Usage:
9// pip install fastled
10// cd examples/AudioUrl
11// fastled
12
13#include <FastLED.h>
14#if defined(FL_IS_TEENSY)
15// Keep fbuild's library scanner aware of PJRC Audio sources for Teensy.
16#include <Audio.h>
17#endif
18
19#include "fl/asset/asset.h"
20#include "fl/ui/ui.h"
21#include "fl/stl/url.h"
22
23#define NUM_LEDS 64
24#define LED_PIN 2
25
27
28// Two ways to provide the URL -- both are supported.
29
30// 1) Legacy: hardcode the URL inline. The WASM frontend will auto-load and
31// auto-play it. Kept here so the pre-asset-pipeline code path stays tested.
33 fl::url("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"));
34
35// 2) v1 asset pipeline (issue #2284): point at a file under data/ whose real
36// URL lives in a sibling *.lnk file. Uncomment to switch to the pipeline.
37// The .lnk at data/track.mp3.lnk resolves to the same SoundHelix URL.
38//
39// fl::UIAudio audio("Audio", FL_ASSET("data/track.mp3"));
40
41void setup() {
42 FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
43 FastLED.setBrightness(128);
44}
45
46void loop() {
47 // Drain all available audio samples, keeping track of peak volume.
48 int32_t peak = 0;
49 while (fl::audio::Sample sample = audio.next()) {
50 for (size_t i = 0; i < sample.pcm().size(); ++i) {
51 int32_t v = abs(sample.pcm()[i]);
52 if (v > peak) {
53 peak = v;
54 }
55 }
56 }
57
58 // Map peak amplitude to brightness (0-255).
59 uint8_t brightness = map(peak, 0, 32768, 0, 255);
61
62 FastLED.show();
63}
fl::UIAudio audio("Audio Input")
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
#define LED_PIN
fl::UIAudio audio("Audio", fl::url("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"))
void setup()
Definition AudioUrl.ino:41
void loop()
Definition AudioUrl.ino:46
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NOEXCEPT
First-class asset handles for sketches that live under <sketch>/data/.
Definition url.h:15
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NOEXCEPT
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:9
constexpr EOrder GRB
Definition eorder.h:19
fl::hsv8 CHSV
Definition chsv.h:11
fl::CRGB CRGB
Definition crgb.h:25
Aggregator header for the fl/ui/ family of per-element UI types.
Lightweight URL parser for embedded environments.