FabGL2 2.0.0
ESP32 Display Controller and Graphics Library for ESP-IDF
Loading...
Searching...
No Matches
Audio

Interactive sound mixer built with the FabGL GUI: each window is an audio channel with its own waveform generator, volume and frequency, all mixed together by fabgl::SoundGenerator in real time.

At startup there are four channels, plus the Main Controls window. All the channels start disabled: tick Enable in a channel to hear it.

Controls

Each Channel window:

Control Function
Volume Channel volume, 0...127
Type Generator: Sine, Square, Triangle, Sawtooth, Noise, or a sampled sound: Loop (drum loop), Speech, Mario
Freq Frequency, 0...8000 Hz (for the sampled sounds it changes the playback speed)
Enable Turns the channel on and off

Main Controls window:

Control Function
Add Channel Adds a new channel window (until the memory runs out)
Reset Restarts the board
Enable Global play/stop
Vol (vertical slider) Master volume, 0...127

Windows can be moved, minimized and maximized with the mouse; with the keyboard, Tab moves the focus and the arrow keys move the sliders.

What it demonstrates

Hardware

  • VGA output (default pins)
  • Audio output on GPIO25 (connect an amplifier or active speakers through a capacitor)
  • PS/2 mouse on port 1 (recommended) and/or PS/2 keyboard on port 0

Build

See Building the examples.

Source code

Main file of the example, main/Audio.cpp. The complete project is in the examples/VGA/Audio folder.

1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2026 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27
28#include "esp_system.h"
29#include "fabgl.h"
30#include "fabui.h"
31
32#include "speech.h"
33#include "loop.h"
34#include "mario.h"
35
36
37fabgl::VGA16Controller DisplayController;
38fabgl::PS2Controller PS2Controller;
39SoundGenerator soundGenerator;
40
41
42void setup()
43{
44
45 PS2Controller.begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);
46
47 DisplayController.begin();
48 DisplayController.setResolution(VGA_640x480_60Hz);
49
50 // adjust this to center screen in your monitor
51 //DisplayController.moveScreen(20, -2);
52 //DisplayController.moveScreen(-6, 0);
53}
54
55
56class ChannelFrame : public uiFrame {
57
63 SamplesGenerator loop = SamplesGenerator(loopSamples, sizeof(loopSamples));
64 SamplesGenerator speech = SamplesGenerator(speechSamples, sizeof(speechSamples));
65 SamplesGenerator mario = SamplesGenerator(marioSamples, sizeof(marioSamples));
66 WaveformGenerator * curGen = nullptr;
67
68 uiLabel * volumeLabel, * frequencyLabel;
69 uiSlider * volumeSlider, * frequencySlider;
70 uiComboBox * genComboBox;
71 uiCheckBox * enableCheck;
72
73public:
74
75 ChannelFrame(uiWindow * parent)
76 : uiFrame(parent, "", Point(0, 0), Size(408, 80))
77 {
78 static int channel_ = 0;
79 static int y = 4, x = 4;
80 static int startingFreq = 150;
81
82 app()->moveWindow(this, x, y);
83 y += size().height + 4;
84 if (y + size().height > parent->size().height) {
85 x += 25;
86 y = x;
87 }
88
89 frameProps().resizeable = false;
90 frameProps().hasCloseButton = false;
91
92 setTitleFmt("Channel %d", ++channel_);
93
94 // volume slider
95 volumeLabel = new uiLabel(this, "", Point(5, 20));
96 volumeSlider = new uiSlider(this, Point(72, 19), Size(150, 17), uiOrientation::Horizontal);
97 volumeSlider->onChange = [&]() {
98 volumeLabel->setTextFmt("Volume %d", volumeSlider->position());
99 if (curGen)
100 curGen->setVolume(volumeSlider->position());
101 };
102 volumeSlider->setup(0, 127, 16);
103 volumeSlider->setPosition(100);
104
105 // generator combobox
106 new uiStaticLabel(this, "Type", Point(230, 20));
107 genComboBox = new uiComboBox(this, Point(260, 20), Size(140, 16), 35);
108 genComboBox->listBoxStyle().textFont = &fabgl::FONT_std_12;
109 genComboBox->textEditStyle().textFont = &fabgl::FONT_std_12;
110 genComboBox->items().append("Sine Wave");
111 genComboBox->items().append("Square Wave");
112 genComboBox->items().append("Triangle Wave");
113 genComboBox->items().append("Sawtooth Wave");
114 genComboBox->items().append("Noise");
115 genComboBox->items().append("Loop");
116 genComboBox->items().append("Speech");
117 genComboBox->items().append("Mario");
118 genComboBox->selectItem(0);
119 genComboBox->onChange = [&]() {
120 setGenerator(genComboBox->selectedItem());
121 };
122
123 // frequency slider
124 frequencyLabel = new uiLabel(this, "", Point(5, 39));
125 frequencySlider = new uiSlider(this, Point(72, 38), Size(330, 17), uiOrientation::Horizontal);
126 frequencySlider->onChange = [&]() {
127 frequencyLabel->setTextFmt("Freq %d Hz", frequencySlider->position());
128 if (curGen)
129 curGen->setFrequency(frequencySlider->position());
130 };
131 frequencySlider->setup(0, 8000, 500);
132 frequencySlider->setPosition(startingFreq);
133 startingFreq += 50;
134
135 // enable checkbox
136 new uiStaticLabel(this, "Enable", Point(5, 76-17));
137 enableCheck = new uiCheckBox(this, Point(77, 75-17), Size(17, 17));
138 enableCheck->onChange = [&]() {
139 if (curGen)
140 curGen->enable(enableCheck->checked());
141 };
142
143 // set default generator
144 setGenerator(0);
145 }
146
147 void setGenerator(int index) {
148 soundGenerator.detach(curGen);
149 switch (index) {
150 case 0:
151 curGen = &sine;
152 break;
153 case 1:
154 curGen = &square;
155 break;
156 case 2:
157 curGen = &triangle;
158 break;
159 case 3:
160 curGen = &sawtooth;
161 break;
162 case 4:
163 curGen = &noise;
164 break;
165 case 5:
166 curGen = &loop;
167 break;
168 case 6:
169 curGen = &speech;
170 break;
171 case 7:
172 curGen = &mario;
173 break;
174 }
175 soundGenerator.attach(curGen);
176 curGen->enable(enableCheck->checked());
177 curGen->setFrequency(frequencySlider->position());
178 }
179};
180
181
182class CommandsFrame : public uiFrame {
183
184public:
185 CommandsFrame(uiWindow * parent)
186 : uiFrame(parent, "Main Controls", Point(500, 4), Size(133, 200))
187 {
188 frameStyle().titleFont = &fabgl::FONT_std_12;
189 frameProps().resizeable = false;
190 frameProps().hasMaximizeButton = false;
191 frameProps().hasMinimizeButton = false;
192 frameProps().hasCloseButton = false;
193
194 auto addChannelBtn = new uiButton(this, "Add Channel", Point(7, 22), Size(80, 16));
195 addChannelBtn->onClick = [=, this]() {
196 if (heap_caps_get_free_size(MALLOC_CAP_8BIT) < 5000)
197 app()->messageBox("Out of memory!", "I'm sorry, there is no more memory available!", "OK");
198 else
199 new ChannelFrame(parent);
200 };
201
202 auto resetBtn = new uiButton(this, "Reset", Point(7, 45), Size(80, 16));
203 resetBtn->onClick = [=, this]() {
204 if (app()->messageBox("Reset ESP32", "Are you sure?", "Yes", "No", nullptr, uiMessageBoxIcon::Question) == uiMessageBoxResult::Button1)
205 esp_restart();
206 };
207
208 // global enable checkbox
209 new uiStaticLabel(this, "Enable", Point(7, 70));
210 auto enableCheck = new uiCheckBox(this, Point(48, 68), Size(17, 17));
211 enableCheck->setChecked(true);
212 enableCheck->onChange = [=]() {
213 soundGenerator.play(enableCheck->checked());
214 };
215
216 // main volume slider
217 auto volumeLabel = new uiLabel(this, "", Point(90, 182));
218 auto volumeSlider = new uiSlider(this, Point(100, 20), Size(17, 160), uiOrientation::Vertical);
219 volumeSlider->onChange = [=]() {
220 volumeLabel->setTextFmt("Vol %d", volumeSlider->position());
221 soundGenerator.setVolume(volumeSlider->position());
222 };
223 volumeSlider->setup(0, 127, 16);
224 volumeSlider->setPosition(100);
225
226
227 }
228
229};
230
231
232class MyApp : public uiApp {
233
234 void init() {
235
236 rootWindow()->frameStyle().backgroundColor = RGB888(0, 0, 64);
237
238 new ChannelFrame(rootWindow());
239 new ChannelFrame(rootWindow());
240 new ChannelFrame(rootWindow());
241 new ChannelFrame(rootWindow());
242
243 new CommandsFrame(rootWindow());
244
245 soundGenerator.play(true);
246
247 }
248} app;
249
250
251void loop()
252{
253 app.run(&DisplayController);
254}
255
256
257extern "C" void app_main()
258{
259 setup();
260 while (true)
261 loop();
262}
Noise generator.
Definition soundgen.h:285
Samples generator.
Definition soundgen.h:338
Sawtooth waveform generator.
Definition soundgen.h:264
Sine waveform generator.
Definition soundgen.h:193
void attach(WaveformGenerator *value)
Attaches a waveform generator.
Definition soundgen.cpp:704
void detach(WaveformGenerator *value)
Detaches a waveform generator.
Definition soundgen.cpp:717
void setVolume(int value)
Sets the overall volume.
Definition soundgen.h:509
bool play(bool value)
Starts or stops playing.
Definition soundgen.cpp:664
SoundGenerator handles audio output.
Definition soundgen.h:382
Square waveform generator.
Definition soundgen.h:214
Triangle waveform generator.
Definition soundgen.h:243
void enable(bool value)
Enables or disables this generator.
Definition soundgen.h:154
virtual void setFrequency(int value)=0
Sets the output frequency.
void setVolume(int value)
Sets the volume of this generator.
Definition soundgen.h:131
Abstract base class for waveform generators. A waveform generator is an audio channel that SoundGener...
Definition soundgen.h:74
static void begin(gpio_num_t port0_clkGPIO, gpio_num_t port0_datGPIO, gpio_num_t port1_clkGPIO=GPIO_UNUSED, gpio_num_t port1_datGPIO=GPIO_UNUSED)
Initializes the PS/2 device controller.
PS/2 device controller.
VGA bitmapped display controller with 16 colors.
void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
Initializer for 8 colors (5 GPIOs).
Delegate onChange
Change event delegate.
Definition fabui.h:2498
uiListBoxStyle & listBoxStyle()
Sets or gets listbox style.
Definition fabui.h:2467
int selectedItem()
Represents currently selected item.
Definition fabui.h:2481
void selectItem(int index)
Selects an item.
Definition fabui.cpp:4502
Represents the whole application base class.
Definition fabui.h:4323
uiButton(uiWindow *parent, char const *text, const Point &pos, const Size &size, uiButtonKind kind=uiButtonKind::Button, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:2537
void setChecked(bool value)
Sets current checkbox or radiobutton checked status.
Definition fabui.cpp:4884
bool checked()
Determines whether the checkbox or radiobutton is checked.
Definition fabui.h:2744
Delegate onChange
Change event delegate.
Definition fabui.h:2777
uiCheckBox(uiWindow *parent, const Point &pos, const Size &size, uiCheckBoxKind kind=uiCheckBoxKind::CheckBox, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:4771
Represents a checkbox or a radiobutton.
Definition fabui.h:2712
StringList & items()
A list of strings representing items of the combobox.
Definition fabui.h:2570
uiTextEditStyle & textEditStyle()
Sets or gets text edit style.
Definition fabui.h:2577
uiComboBox(uiWindow *parent, const Point &pos, const Size &size, int listHeight, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:4694
This is a combination of a listbox and a single-line editable textbox.
Definition fabui.h:2545
A frame is a window with a title bar, maximize/minimize/close buttons and that is resizable or movabl...
Definition fabui.h:854
void setTextFmt(const char *format,...)
Sets label formatted text.
Definition fabui.cpp:3224
uiLabel(uiWindow *parent, char const *text, const Point &pos, const Size &size=Size(0, 0), bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:3188
A label is a static text UI element.
Definition fabui.h:1606
void setup(int min, int max, int ticksFrequency)
Sets the minimum and maximum positions and the tick frequency.
Definition fabui.cpp:4958
void setPosition(int value)
Sets the slider position.
Definition fabui.cpp:4948
Delegate onChange
Slider changed event delegate.
Definition fabui.h:2905
uiSlider(uiWindow *parent, const Point &pos, const Size &size, uiOrientation orientation, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:4920
int position()
Determines slider position.
Definition fabui.h:2867
A slider or track bar is a graphical control element with which a user may set a value by moving an i...
Definition fabui.h:2835
uiStaticLabel(uiWindow *parent, char const *text, const Point &pos, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:3304
Single include file of the FabGL library.
#define VGA_640x480_60Hz
Definition fabglconf.h:246
This file contains all classes related to FabGL Graphical User Interface.
Represents the coordinates of a point.
Definition fabutils.h:243
Represents a two-dimensional size.
Definition fabutils.h:263
FontInfo const * textFont
Definition fabui.h:2039
FontInfo const * textFont
Definition fabui.h:1427