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
20
21#if defined(IS_I2S_IMPLEMENTED)
22
23#ifndef SOFT_MUTE_VALUE
24# define SOFT_MUTE_VALUE 0
25#endif
26
27namespace audio_tools {
28
40class I2SStream : public AudioStream {
41 public:
42 I2SStream() = default;
44 ~I2SStream() { end(); }
45
47 TRACED();
48 this->mute_pin = mute_pin;
49 if (IS_GPIO(mute_pin)) {
51 mute(true);
52 }
53 }
54
59
60 bool begin() {
61 TRACEI();
62 I2SConfig cfg = p_driver->config();
63 if (!cfg){
64 LOGE("unsuported AudioInfo: sample_rate: %d / channels: %d / bits_per_sample: %d", (int) cfg.sample_rate, (int)cfg.channels, (int)cfg.bits_per_sample);
65 return false;
66 }
67 cfg.copyFrom(audioInfo());
68 if (cfg.rx_tx_mode == UNDEFINED_MODE){
70 }
71 is_active = p_driver->begin(cfg);
72 mute(false);
73 return is_active;
74 }
75
77 bool begin(I2SConfig cfg) {
78 TRACED();
79 if (!cfg){
80 LOGE("unsuported AudioInfo: sample_rate: %d / channels: %d / bits_per_sample: %d", (int) cfg.sample_rate, (int)cfg.channels, (int)cfg.bits_per_sample);
81 return false;
82 }
84 is_active = p_driver->begin(cfg);
85 // unmute
86 mute(false);
87 return is_active;
88 }
89
91 void end() {
92 if (is_active) {
93 TRACEI();
94 is_active = false;
95 mute(true);
96 p_driver->end();
97 }
98 }
99
102 TRACEI();
103 assert(info.sample_rate != 0);
104 assert(info.channels != 0);
107 if (is_active) {
108 if (!p_driver->setAudioInfo(info)) {
110 if (!info.equals(current_cfg)) {
111 LOGI("restarting i2s");
112 info.logInfo("I2SStream");
113 p_driver->end();
114 current_cfg.copyFrom(info);
116 } else {
117 LOGI("no change");
118 }
119 }
120 }
121 }
122
124 virtual size_t write(const uint8_t *data, size_t len) {
125 LOGD("I2SStream::write: %d", len);
126 if (data == nullptr || len == 0 || !is_active) return 0;
127 return p_driver->writeBytes(data, len);
128 }
129
131 virtual size_t readBytes(uint8_t *data, size_t len) override {
132 return p_driver->readBytes(data, len);
133 }
134
136 virtual int available() override { return p_driver->available(); }
137
139 virtual int availableForWrite() override { return p_driver->availableForWrite(); }
140
141 void flush() override {}
142
145
147 operator bool() override { return is_active; }
148
150 bool isActive() { return is_active;}
151
152 protected:
155 bool is_active = false;
157
159 void mute(bool is_mute) {
160 if (IS_GPIO(mute_pin)) {
162 }
163 }
164};
165
166} // namespace audio_tools
167
168#endif
169#endif
#define OUTPUT
Definition Arduino.h:38
#define IS_GPIO(pin)
Definition Arduino.h:248
#define SOFT_MUTE_VALUE
Definition I2SStream.h:24
#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 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:40
void flush() override
Definition I2SStream.h:141
void mute(bool is_mute)
set mute pin on or off
Definition I2SStream.h:159
bool is_active
Definition I2SStream.h:155
I2SStream(digital_pin_t mute_pin)
Definition I2SStream.h:46
I2SDriverBase * p_driver
Definition I2SStream.h:154
bool begin()
Definition I2SStream.h:60
virtual size_t readBytes(uint8_t *data, size_t len) override
Reads the audio data.
Definition I2SStream.h:131
digital_pin_t mute_pin
Definition I2SStream.h:156
virtual int availableForWrite() override
Provides the available audio data.
Definition I2SStream.h:139
I2SConfig defaultConfig(RxTxMode mode=TX_MODE)
Provides the default configuration.
Definition I2SStream.h:56
I2SStream(I2SDriverBase &driver)
Definition I2SStream.h:43
virtual void setAudioInfo(AudioInfo info)
updates the sample rate dynamically
Definition I2SStream.h:101
virtual size_t write(const uint8_t *data, size_t len)
Writes the audio data to I2S.
Definition I2SStream.h:124
bool isActive()
Returns true if i2s is active.
Definition I2SStream.h:150
void end()
Stops the I2S interface.
Definition I2SStream.h:91
bool begin(I2SConfig cfg)
Starts the I2S interface.
Definition I2SStream.h:77
I2SDriver i2s
Definition I2SStream.h:153
I2SDriverBase * driver()
Provides access to the driver.
Definition I2SStream.h:144
~I2SStream()
Definition I2SStream.h:44
virtual int available() override
Provides the available audio data.
Definition I2SStream.h:136
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 AudioCodecsBase.h:10
gpio_dt_spec digital_pin_t
Zephyr GPIO spec as digital_pin_t.
Definition Arduino.h:266
void digitalWrite(digital_pin_t pin, bool value)
Definition Arduino.h:313
static gpio_dt_spec GPIO_NONE
GPIO_NONE is no pin defined.
Definition Arduino.h:269
void pinMode(digital_pin_t pin, int mode)
Definition Arduino.h:282
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
void copyFrom(AudioInfo info)
Same as set.
Definition AudioTypes.h:101
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
bool equals(AudioInfo alt)
Returns true if alt values are the same like the current values.
Definition AudioTypes.h:83
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57
virtual void logInfo(const char *source="")
Definition AudioTypes.h:121