arduino-audio-tools
Loading...
Searching...
No Matches
I2SStream.h
Go to the documentation of this file.
1
2
3#pragma once
4#include "AudioToolsConfig.h"
5
6#if defined(USE_I2S)
7
21
22#if defined(IS_I2S_IMPLEMENTED)
23
24#ifndef SOFT_MUTE_VALUE
25# define SOFT_MUTE_VALUE 0
26#endif
27
28namespace audio_tools {
29
41class I2SStream : public AudioStream {
42 public:
43 I2SStream() = default;
45 ~I2SStream() { end(); }
46
48 TRACED();
49 this->mute_pin = mute_pin;
50 if (IS_GPIO(mute_pin)) {
52 mute(true);
53 }
54 }
55
60
61 bool begin() {
62 TRACEI();
63 I2SConfig cfg = p_driver->config();
64 if (!cfg){
65 LOGE("unsuported AudioInfo: sample_rate: %d / channels: %d / bits_per_sample: %d", (int) cfg.sample_rate, (int)cfg.channels, (int)cfg.bits_per_sample);
66 return false;
67 }
68 cfg.copyFrom(audioInfo());
69 if (cfg.rx_tx_mode == UNDEFINED_MODE){
71 }
72 is_active = p_driver->begin(cfg);
73 mute(false);
74 return is_active;
75 }
76
78 bool begin(I2SConfig cfg) {
79 TRACED();
80 if (!cfg){
81 LOGE("unsuported AudioInfo: sample_rate: %d / channels: %d / bits_per_sample: %d", (int) cfg.sample_rate, (int)cfg.channels, (int)cfg.bits_per_sample);
82 return false;
83 }
85 is_active = p_driver->begin(cfg);
86 // unmute
87 mute(false);
88 return is_active;
89 }
90
92 void end() {
93 if (is_active) {
94 TRACEI();
95 is_active = false;
96 mute(true);
97 p_driver->end();
98 }
99 }
100
103 TRACEI();
104 assert(info.sample_rate != 0);
105 assert(info.channels != 0);
108 if (is_active) {
109 if (!p_driver->setAudioInfo(info)) {
110 I2SConfig current_cfg = p_driver->config();
111 if (!info.equals(current_cfg)) {
112 LOGI("restarting i2s");
113 info.logInfo("I2SStream");
114 p_driver->end();
115 current_cfg.copyFrom(info);
116 p_driver->begin(current_cfg);
117 } else {
118 LOGI("no change");
119 }
120 }
121 }
122 }
123
125 virtual size_t write(const uint8_t *data, size_t len) {
126 LOGD("I2SStream::write: %d", len);
127 if (data == nullptr || len == 0 || !is_active) return 0;
128 return p_driver->writeBytes(data, len);
129 }
130
132 virtual size_t readBytes(uint8_t *data, size_t len) override {
133 return p_driver->readBytes(data, len);
134 }
135
137 virtual int available() override { return p_driver->available(); }
138
140 virtual int availableForWrite() override { return p_driver->availableForWrite(); }
141
146 void flush() override {
147 if (is_active) p_driver->flush();
148 }
149
152
154 operator bool() override { return is_active; }
155
157 bool isActive() { return is_active;}
158
159 protected:
162 bool is_active = false;
164
166 void mute(bool is_mute) {
167 if (IS_GPIO(mute_pin)) {
169 }
170 }
171};
172
173} // namespace audio_tools
174
175#endif
176#endif
#define OUTPUT
Definition Arduino.h:38
#define IS_GPIO(pin)
Definition Arduino.h:252
#define SOFT_MUTE_VALUE
Definition I2SStream.h:25
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define assert(T)
Definition avr.h:10
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:120
AudioInfo info
Definition BaseStream.h:171
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition BaseStream.h:128
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition BaseStream.h:151
Configuration for ESP32 legacy i2s.
Definition I2SConfigESP32.h:24
RxTxMode rx_tx_mode
public settings
Definition I2SConfigESP32.h:60
Definition I2SDriverBase.h:7
virtual bool begin(I2SConfig cfg)=0
virtual I2SConfig config()=0
virtual size_t readBytes(void *dest, size_t size_bytes)=0
virtual bool setAudioInfo(AudioInfo info)=0
virtual size_t writeBytes(const void *src, size_t size_bytes)=0
virtual int availableForWrite()=0
virtual void flush()
Definition I2SDriverBase.h:26
virtual int available()=0
virtual I2SConfig defaultConfig(RxTxMode mode)=0
Basic I2S API - for the ESP32. If we receive 1 channel, we expand the result to 2 channels.
Definition I2SDriverESP32.h:28
We support the Stream interface for the I2S access. In addition we allow a separate mute pin which mi...
Definition I2SStream.h:41
void flush() override
Definition I2SStream.h:146
void mute(bool is_mute)
set mute pin on or off
Definition I2SStream.h:166
bool is_active
Definition I2SStream.h:162
I2SStream(digital_pin_t mute_pin)
Definition I2SStream.h:47
I2SDriverBase * p_driver
Definition I2SStream.h:161
bool begin()
Definition I2SStream.h:61
virtual size_t readBytes(uint8_t *data, size_t len) override
Reads the audio data.
Definition I2SStream.h:132
digital_pin_t mute_pin
Definition I2SStream.h:163
virtual int availableForWrite() override
Provides the available audio data.
Definition I2SStream.h:140
I2SConfig defaultConfig(RxTxMode mode=TX_MODE)
Provides the default configuration.
Definition I2SStream.h:57
I2SStream(I2SDriverBase &driver)
Definition I2SStream.h:44
virtual void setAudioInfo(AudioInfo info)
updates the sample rate dynamically
Definition I2SStream.h:102
virtual size_t write(const uint8_t *data, size_t len)
Writes the audio data to I2S.
Definition I2SStream.h:125
bool isActive()
Returns true if i2s is active.
Definition I2SStream.h:157
void end()
Stops the I2S interface.
Definition I2SStream.h:92
bool begin(I2SConfig cfg)
Starts the I2S interface.
Definition I2SStream.h:78
I2SDriver i2s
Definition I2SStream.h:160
I2SDriverBase * driver()
Provides access to the driver.
Definition I2SStream.h:151
~I2SStream()
Definition I2SStream.h:45
virtual int available() override
Provides the available audio data.
Definition I2SStream.h:137
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:26
@ RXTX_MODE
Definition AudioTypes.h:26
@ TX_MODE
Definition AudioTypes.h:26
@ UNDEFINED_MODE
Definition AudioTypes.h:26
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
gpio_dt_spec digital_pin_t
Zephyr GPIO spec as digital_pin_t.
Definition Arduino.h:270
void digitalWrite(digital_pin_t pin, bool value)
Definition Arduino.h:317
static gpio_dt_spec GPIO_NONE
GPIO_NONE is no pin defined.
Definition Arduino.h:273
void pinMode(digital_pin_t pin, int mode)
Definition Arduino.h:286
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:56
void copyFrom(AudioInfo info)
Same as set.
Definition AudioTypes.h:106
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:58
bool equals(AudioInfo alt)
Returns true if alt values are the same like the current values.
Definition AudioTypes.h:88
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:60
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:62
virtual void logInfo(const char *source="")
Definition AudioTypes.h:127