arduino-audio-tools
Loading...
Searching...
No Matches
MozziStream.h
Go to the documentation of this file.
1
2
3#include "AudioTools.h"
4
5// prevent naming conflict with audiotools
6#define AudioOutput AudioOutputMozzi
7#include <Mozzi.h>
8#undef AudioOutput
9
11AudioOutputMozzi updateAudio();
12
13namespace audio_tools {
14
21
28class MozziStream : public AudioStream, public VolumeSupport {
29 public:
30 MozziStream() = default;
31
33
35
37
38 int audioRate() { return cfg.sample_rate; }
39
40 void setInput(Stream& in) { p_input = &in; }
41
42 AudioInfo audioInfo() { return cfg; }
43
45 this->cfg = cfg;
46 return begin();
47 }
48
49 bool begin() {
50 if (cfg.bits_per_sample != 16) {
51 LOGE("bits_per_sample must be 16 and not %d", cfg.bits_per_sample);
52 return false;
53 }
54
55 // initialize range for input range determination
56 input_min = 32767;
57 input_max = -32768;
58
59 // setup control counter
61 if (control_counter_max <= 0) {
63 }
65 active = true;
66 return true;
67 }
68
69 void end() { active = false; }
70
76
78 size_t readBytes(uint8_t* data, size_t len) {
79 if (!active) return 0;
80 int samples = len / sizeof(int16_t);
81 int frames = samples / cfg.channels;
82 int16_t* data16 = (int16_t*)data;
83 int idx = 0;
84 for (int j = 0; j < frames; j++) {
85 int16_t sample = nextSample();
86 for (int ch = 0; ch < cfg.channels; ch++) {
87 data16[idx++] = sample;
88 }
89 }
90 return idx * sizeof(int16_t);
91 }
92
94 size_t write(const uint8_t* data, size_t len) {
95 if (!active) return 0;
96 if (buffer.size() == 0) {
97 buffer.resize(len * 2);
98 }
99 return buffer.writeArray(data, len);
100 }
101
106 int16_t result[cfg.channels] = {0};
107 if (p_input != nullptr) {
108 p_input->readBytes((uint8_t*)&result, sizeof(int16_t) * cfg.channels);
109 } else {
110 buffer.readArray((uint8_t*)&result, sizeof(int16_t) * cfg.channels);
111 }
112 // when we have multiple channels we provide the avg value
113 int sample = avg(result);
114 // calculate dynamic range
115 if (sample < input_min) input_min = sample;
116 if (sample > input_max) input_max = sample;
119 return sample1;
120 }
121
122 protected:
127 Stream* p_input = nullptr;
128 bool active = false;
129 int input_min = 32767;
130 int input_max = -32768;
131
132 int16_t avg(int16_t* values) {
133 int total = 0;
134 for (int ch = 0; ch < cfg.channels; ch++) {
135 total += values[ch];
136 }
137 return total / cfg.channels;
138 }
139
141 // control update
142 if (--control_counter < 0) {
144 LOGD("updateControl");
146 }
147
148 // updateAudio() STANDARD mode is between -244 and 243 inclusive
149 auto result = updateAudio() * cfg.output_volume;
150 // clip result
151 if (result > 32767) result = 32767;
152 if (result < -32768) result = -32768;
153 // Serial.println(result);
154 return result;
155 }
156};
157
158} // namespace audio_tools
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
void updateControl()
AudioOutputMozzi updateAudio()
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:123
AudioInfo info
Definition BaseStream.h:174
virtual int readArray(T data[], int len)
reads multiple values
Definition Buffers.h:33
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:55
Stream that provides audio information that was generated using the Mozzi API using the updateControl...
Definition MozziStream.h:28
bool active
Definition MozziStream.h:128
int input_min
Definition MozziStream.h:129
int control_counter
Definition MozziStream.h:125
int16_t avg(int16_t *values)
Definition MozziStream.h:132
int input_max
Definition MozziStream.h:130
Stream * p_input
Definition MozziStream.h:127
bool setVolume(int16_t vol)
Defines the multiplication factor to scale the Mozzi value range to int16_t.
Definition MozziStream.h:72
MozziStream(Stream &in)
Definition MozziStream.h:32
int getAudioInput()
Definition MozziStream.h:105
int audioRate()
Definition MozziStream.h:38
int control_counter_max
Definition MozziStream.h:124
bool begin(MozziConfig cfg)
Definition MozziStream.h:44
void setAudioInfo(AudioInfo info)
Defines the input AudioInfo.
Definition MozziStream.h:36
bool begin()
Definition MozziStream.h:49
AudioInfo audioInfo()
provides the actual input AudioInfo
Definition MozziStream.h:42
MozziConfig defaultConfig()
Definition MozziStream.h:34
size_t readBytes(uint8_t *data, size_t len)
Provides the data filled by calling updateAudio()
Definition MozziStream.h:78
void end()
Definition MozziStream.h:69
int16_t nextSample()
Definition MozziStream.h:140
MozziConfig cfg
Definition MozziStream.h:123
RingBuffer< uint8_t > buffer
Definition MozziStream.h:126
void setInput(Stream &in)
Definition MozziStream.h:40
size_t write(const uint8_t *data, size_t len)
Write data to buffer so that we can access them by calling getAudioInput()
Definition MozziStream.h:94
Implements a typed Ringbuffer.
Definition Buffers.h:341
virtual size_t size() override
Returns the maximum capacity of the buffer.
Definition Buffers.h:428
virtual bool resize(int len)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:418
Definition NoArduino.h:142
virtual size_t readBytes(uint8_t *data, size_t len)
Definition NoArduino.h:147
Supports the setting and getting of the volume.
Definition AudioTypes.h:191
virtual bool setVolume(float volume)
define the actual volume in the range of 0.0f to 1.0f
Definition AudioTypes.h:196
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
long map(long x, long in_min, long in_max, long out_min, long out_max)
Maps input to output values.
Definition NoArduino.h:189
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:55
void copyFrom(AudioInfo info)
Same as set.
Definition AudioTypes.h:105
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61
Definition MozziStream.h:15
uint16_t control_rate
Definition MozziStream.h:16
int input_range_to
Definition MozziStream.h:18
int input_range_from
Definition MozziStream.h:17
int16_t output_volume
Definition MozziStream.h:19