-
-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathcontrol_gain.ino
More file actions
52 lines (44 loc) · 1.53 KB
/
control_gain.ino
File metadata and controls
52 lines (44 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Demo how to use the Mozzi API to provide a stream of int16_t data.
* Inspired by https://sensorium.github.io/Mozzi/examples/#01.Basics
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
#include "AudioTools/AudioLibs/MozziStream.h"
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
const int sample_rate = 16000;
AudioInfo info(sample_rate, 1, 16);
AudioBoardStream i2s(AudioKitEs8388V1); // audio sink
MozziStream mozzi; // audio source
StreamCopy copier(i2s, mozzi); // copy source to sink
// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file
// of table #included above
Oscil<SIN2048_NUM_CELLS, sample_rate> aSin(SIN2048_DATA);
// control variable, use the smallest data size you can for anything used in
// audio
byte gain = 255;
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// setup mozzi
auto cfg = mozzi.defaultConfig();
cfg.control_rate = CONTROL_RATE;
cfg.copyFrom(info);
mozzi.begin(cfg);
// setup output
auto out_cfg = i2s.defaultConfig();
out_cfg.copyFrom(info);
i2s.begin(out_cfg);
// setup mozzi sine
aSin.setFreq(3320); // set the frequency
}
void loop() { copier.copy(); }
void updateControl() {
// as byte, this will automatically roll around to 255 when it passes 0
gain = gain - 3;
}
AudioOutputMozzi updateAudio() {
return (aSin.next() * gain) >>
8; // shift back to STANDARD audio range, like /256 but faster
}