snapshot
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Mozzi Input example: we get the input from a generator and
|
||||
* output the audio via i2s again. In this example we use the
|
||||
* default value range of 0-1023 for the getAudioInput() method.
|
||||
* However usually it is easier to define the desired range in
|
||||
* the Mozzi configuration object: e.g. cfg.input_range_from = -244;
|
||||
* cfg.input_range_to = 243;
|
||||
*/
|
||||
#include "AudioTools.h"
|
||||
#include "AudioTools/AudioLibs/AudioBoardStream.h"
|
||||
#include "AudioTools/AudioLibs/MozziStream.h"
|
||||
|
||||
const int sample_rate = 16000;
|
||||
AudioInfo info(sample_rate, 1, 16);
|
||||
AudioBoardStream i2s(AudioKitEs8388V1); // final output of decoded stream
|
||||
MozziStream mozzi;
|
||||
SineWaveGenerator<int16_t> sineWave; // subclass of SoundGenerator with max amplitude of 32000
|
||||
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
|
||||
|
||||
StreamCopy copier(i2s, mozzi);
|
||||
// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
|
||||
|
||||
// setup mozzi
|
||||
auto cfg = mozzi.defaultConfig();
|
||||
cfg.control_rate = CONTROL_RATE;
|
||||
cfg.copyFrom(info);
|
||||
mozzi.begin(cfg);
|
||||
|
||||
// setup data source for mozzi
|
||||
mozzi.setInput(sound);
|
||||
sineWave.begin(info, N_B4);
|
||||
|
||||
// setup output
|
||||
auto out_cfg = i2s.defaultConfig(RXTX_MODE);
|
||||
out_cfg.copyFrom(info);
|
||||
i2s.begin(out_cfg);
|
||||
i2s.setVolume(1.0);
|
||||
}
|
||||
|
||||
void updateControl() {}
|
||||
|
||||
AudioOutputMozzi updateAudio() {
|
||||
int asig = mozzi.getAudioInput(); // range 0-1023
|
||||
asig = asig - 512; // now range is -512 to 511
|
||||
// output range in STANDARD mode is -244 to 243,
|
||||
// so you might need to adjust your signal to suit
|
||||
return asig;
|
||||
}
|
||||
|
||||
void loop() { copier.copy(); }
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* 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
|
||||
* The result is published to a bluetooth speaker. We use the more efficient
|
||||
* A2DP base API with a callback
|
||||
*/
|
||||
#include "AudioTools.h"
|
||||
#include "AudioTools/Communication/A2DPStream.h"
|
||||
#include "AudioTools/AudioLibs/MozziStream.h"
|
||||
#include <Oscil.h> // oscillator template
|
||||
#include <tables/sin2048_int8.h> // sine table for oscillator
|
||||
|
||||
const int sample_rate = 44100;
|
||||
AudioInfo info(sample_rate, 2, 16); // bluetooth requires 44100, stereo, 16 bits
|
||||
BluetoothA2DPSource a2dp_source;
|
||||
MozziStream mozzi; // audio source
|
||||
const int16_t BYTES_PER_FRAME = 4;
|
||||
// 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;
|
||||
|
||||
// callback used by A2DP to provide the sound data - usually len is 128 2 channel int16 frames
|
||||
int32_t get_sound_data(uint8_t* data, int32_t size) {
|
||||
int32_t result = mozzi.readBytes(data, size);
|
||||
//LOGI("get_sound_data %d->%d",size, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Arduino Setup
|
||||
void setup(void) {
|
||||
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);
|
||||
|
||||
aSin.setFreq(3320); // set the frequency
|
||||
|
||||
// start the bluetooth
|
||||
Serial.println("starting A2DP...");
|
||||
a2dp_source.set_data_callback(get_sound_data);
|
||||
a2dp_source.start("LEXON MINO L");
|
||||
//a2dp_source.set_volume(100);
|
||||
}
|
||||
|
||||
void updateControl() {
|
||||
// as byte, this will automatically roll around to 255 when it passes 0
|
||||
gain = gain - 3;
|
||||
}
|
||||
|
||||
AudioOutputMozzi updateAudio() {
|
||||
// shift back to STANDARD audio range, like /256 but faster
|
||||
return (aSin.next() * gain) >> 8;
|
||||
}
|
||||
|
||||
// Arduino loop - repeated processing
|
||||
void loop() {
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,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
|
||||
}
|
||||
Reference in New Issue
Block a user