FabGL
ESP32 VGA Controller and Graphics Library
soundgen.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 
35 #include <stdint.h>
36 #include <stddef.h>
37 
38 #include "freertos/FreeRTOS.h"
39 
40 #include "fabglconf.h"
41 #include "fabutils.h"
42 
43 
44 
45 namespace fabgl {
46 
47 
48 #define DEFAULT_SAMPLE_RATE 16000
49 
50 // 512 samples, at 16KHz generate a send every 512/16000*1000 = 32ms (16000/512=31.25 sends per second)
51 // 200 samples, at 16Khz generate a send every 200/16000*1000 = 12.5ms (16000/200=80 sends per second)
52 #define I2S_SAMPLE_BUFFER_SIZE 200 // must be even
53 
54 #define WAVEGENTASK_STACK_SIZE 1024
55 
56 
59 public:
60  WaveformGenerator() : next(nullptr), m_sampleRate(0), m_volume(100), m_enabled(false) { }
61 
62  virtual ~WaveformGenerator() { }
63 
69  virtual void setFrequency(int value) = 0;
70 
76  virtual int getSample() = 0;
77 
83  void setVolume(int value) { m_volume = value; }
84 
90  int volume() { return m_volume; }
91 
97  bool enabled() { return m_enabled; }
98 
106  void enable(bool value) { m_enabled = value; }
107 
115  void setSampleRate(int value) { m_sampleRate = value; }
116 
122  uint16_t sampleRate() { return m_sampleRate; }
123 
124  WaveformGenerator * next;
125 
126 private:
127  uint16_t m_sampleRate;
128  int8_t m_volume;
129  int8_t m_enabled; // 0 = disabled, 1 = enabled
130 };
131 
132 
135 public:
137 
138  void setFrequency(int value);
139 
140  int getSample();
141 
142 private:
143  uint32_t m_phaseInc;
144  uint32_t m_phaseAcc;
145  uint16_t m_frequency;
146  int16_t m_lastSample;
147 };
148 
149 
152 public:
154 
155  void setFrequency(int value);
156 
162  void setDutyCycle(int dutyCycle);
163 
164  int getSample();
165 
166 private:
167  uint32_t m_phaseInc;
168  uint32_t m_phaseAcc;
169  uint16_t m_frequency;
170  int8_t m_lastSample;
171  uint8_t m_dutyCycle;
172 };
173 
174 
177 public:
179 
180  void setFrequency(int value);
181 
182  int getSample();
183 
184 private:
185  uint32_t m_phaseInc;
186  uint32_t m_phaseAcc;
187  uint16_t m_frequency;
188  int8_t m_lastSample;
189 };
190 
191 
194 public:
196 
197  void setFrequency(int value);
198 
199  int getSample();
200 
201 private:
202  uint32_t m_phaseInc;
203  uint32_t m_phaseAcc;
204  uint16_t m_frequency;
205  int8_t m_lastSample;
206 };
207 
208 
211 public:
213 
214  void setFrequency(int value);
215 
216  int getSample();
217 
218 private:
219  uint16_t m_noise;
220 };
221 
222 
230 public:
231  SamplesGenerator(int8_t const * data, int length);
232 
233  void setFrequency(int value);
234 
235  int getSample();
236 
237 private:
238  int8_t const * m_data;
239  int m_length;
240  int m_index;
241 };
242 
243 
260 
261 public:
262 
266  SoundGenerator(int sampleRate = DEFAULT_SAMPLE_RATE);
267 
268  ~SoundGenerator();
269 
273  void clear();
274 
286  bool play(bool value);
287 
293  bool playing();
294 
295  WaveformGenerator * channels() { return m_channels; }
296 
309  void attach(WaveformGenerator * value);
310 
316  void detach(WaveformGenerator * value);
317 
323  void setVolume(int value) { m_volume = value; }
324 
330  int volume() { return m_volume; }
331 
332 
333 private:
334 
335  void i2s_audio_init();
336  static void waveGenTask(void * arg);
337  bool suspendPlay(bool value);
338  void mutizeOutput();
339 
340 
341  TaskHandle_t m_waveGenTaskHandle;
342 
343  WaveformGenerator * m_channels;
344 
345  uint16_t * m_sampleBuffer;
346 
347  int8_t m_volume;
348 
349  uint16_t m_sampleRate;
350 
351 };
352 
353 
354 
355 
356 } // end of namespace
357 
Noise generator.
Definition: soundgen.h:210
void setFrequency(int value)
Sets output frequency.
Definition: soundgen.cpp:319
void setDutyCycle(int dutyCycle)
Sets square wave duty cycle.
Definition: soundgen.cpp:144
Samples generator.
Definition: soundgen.h:229
int getSample()
Gets next sample.
Definition: soundgen.cpp:324
void clear()
Stops playing and removes all attached waveform generators.
Definition: soundgen.cpp:367
void setFrequency(int value)
Sets output frequency.
Definition: soundgen.cpp:285
Base abstract class for waveform generators. A waveform generator can be seen as an audio channel tha...
Definition: soundgen.h:58
Triangle waveform generator.
Definition: soundgen.h:176
bool play(bool value)
Starts or stops playing.
Definition: soundgen.cpp:398
bool playing()
Determines whether sound generator is playing.
Definition: soundgen.cpp:432
void setFrequency(int value)
Sets output frequency.
Definition: soundgen.cpp:135
virtual void setFrequency(int value)=0
Sets output frequency.
int getSample()
Gets next sample.
Definition: soundgen.cpp:150
void enable(bool value)
Enables or disabled this generator.
Definition: soundgen.h:106
void setVolume(int value)
Sets volume of this generator.
Definition: soundgen.h:83
int getSample()
Gets next sample.
Definition: soundgen.cpp:198
void setFrequency(int value)
Sets output frequency.
Definition: soundgen.cpp:190
int getSample()
Gets next sample.
Definition: soundgen.cpp:290
int volume()
Determines current overall volume.
Definition: soundgen.h:330
This file contains some utility classes and functions.
Definition: canvas.cpp:47
int volume()
Determines current volume.
Definition: soundgen.h:90
Sine waveform generator.
Definition: soundgen.h:134
int getSample()
Gets next sample.
Definition: soundgen.cpp:90
SoundGenerator handles audio output.
Definition: soundgen.h:259
This file contains FabGL library configuration settings, like number of supported colors...
void detach(WaveformGenerator *value)
Detaches a waveform generator.
Definition: soundgen.cpp:452
uint16_t sampleRate()
Determines the sample rate.
Definition: soundgen.h:122
Sawtooth waveform generator.
Definition: soundgen.h:193
void attach(WaveformGenerator *value)
Attaches a waveform generator.
Definition: soundgen.cpp:439
SoundGenerator(int sampleRate=DEFAULT_SAMPLE_RATE)
Creates an instance of the sound generator. Only one instance is allowed.
Definition: soundgen.cpp:348
int getSample()
Gets next sample.
Definition: soundgen.cpp:247
void setFrequency(int value)
Sets output frequency.
Definition: soundgen.cpp:82
virtual int getSample()=0
Gets next sample.
void setFrequency(int value)
Sets output frequency.
Definition: soundgen.cpp:239
void setSampleRate(int value)
Sets the sample rate.
Definition: soundgen.h:115
Square waveform generator.
Definition: soundgen.h:151
void setVolume(int value)
Sets the overall volume.
Definition: soundgen.h:323
bool enabled()
Determines whether this generator is enabled or disabled.
Definition: soundgen.h:97