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
19
20#if defined(IS_I2S_IMPLEMENTED)
21
22namespace audio_tools {
23
33class I2SStream : public AudioStream {
34 public:
35 I2SStream() = default;
36 ~I2SStream() { end(); }
37
38#ifdef ARDUINO
39 I2SStream(int mute_pin) {
40 TRACED();
41 this->mute_pin = mute_pin;
42 if (mute_pin > 0) {
44 mute(true);
45 }
46 }
47#endif
48
51 return i2s.defaultConfig(mode);
52 }
53
54 bool begin() {
55 TRACEI();
56 I2SConfig cfg = i2s.config();
57 if (!cfg){
58 LOGE("unsuported AudioInfo: sample_rate: %d / channels: %d / bits_per_sample: %d", (int) cfg.sample_rate, (int)cfg.channels, (int)cfg.bits_per_sample);
59 return false;
60 }
61 cfg.copyFrom(audioInfo());
62 if (cfg.rx_tx_mode == UNDEFINED_MODE){
64 }
65 is_active = i2s.begin(cfg);
66 mute(false);
67 return is_active;
68 }
69
71 bool begin(I2SConfig cfg) {
72 TRACED();
73 if (!cfg){
74 LOGE("unsuported AudioInfo: sample_rate: %d / channels: %d / bits_per_sample: %d", (int) cfg.sample_rate, (int)cfg.channels, (int)cfg.bits_per_sample);
75 return false;
76 }
78 is_active = i2s.begin(cfg);
79 // unmute
80 mute(false);
81 return is_active;
82 }
83
85 void end() {
86 if (is_active) {
87 TRACEI();
88 is_active = false;
89 mute(true);
90 i2s.end();
91 }
92 }
93
95 virtual void setAudioInfo(AudioInfo info) {
96 TRACEI();
98 assert(info.channels != 0);
101 if (is_active) {
102 if (!i2s.setAudioInfo(info)) {
104 if (!info.equals(current_cfg)) {
105 LOGI("restarting i2s");
106 info.logInfo("I2SStream");
107 i2s.end();
108 current_cfg.copyFrom(info);
110 } else {
111 LOGI("no change");
112 }
113 }
114 }
115 }
116
118 virtual size_t write(const uint8_t *data, size_t len) {
119 LOGD("I2SStream::write: %d", len);
120 if (data == nullptr || len == 0 || !is_active) return 0;
121 return i2s.writeBytes(data, len);
122 }
123
125 virtual size_t readBytes(uint8_t *data, size_t len) override {
126 return i2s.readBytes(data, len);
127 }
128
130 virtual int available() override { return i2s.available(); }
131
133 virtual int availableForWrite() override { return i2s.availableForWrite(); }
134
135 void flush() override {}
136
138 I2SDriver *driver() { return &i2s; }
139
141 operator bool() override { return is_active; }
142
144 bool isActive() { return is_active;}
145
146 protected:
148 int mute_pin = -1;
149 bool is_active = false;
150
152 void mute(bool is_mute) {
153#ifdef ARDUINO
154 if (mute_pin > 0) {
156 }
157#endif
158 }
159};
160
161} // namespace audio_tools
162
163#endif
164#endif
#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 OUTPUT
Definition NoArduino.h:42
void pinMode(int pin, int mode)
Definition NoArduino.h:210
void digitalWrite(int pin, int value)
Definition NoArduino.h:206
#define SOFT_MUTE_VALUE
Definition arduino-zephyr.h:13
#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:122
AudioInfo info
Definition BaseStream.h:173
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition BaseStream.h:130
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition BaseStream.h:153
Configuration for ESP32 legacy i2s.
Definition I2SConfigESP32.h:24
RxTxMode rx_tx_mode
public settings
Definition I2SConfigESP32.h:60
Basic I2S API - for the ESP32. If we receive 1 channel, we expand the result to 2 channels.
Definition I2SESP32.h:25
int available()
we assume the data is already available in the buffer
Definition I2SESP32.h:79
bool setAudioInfo(AudioInfo info)
Potentially updates the sample rate (if supported)
Definition I2SESP32.h:37
int availableForWrite()
We limit the write size to the buffer size.
Definition I2SESP32.h:82
I2SConfigESP32 config()
provides the actual configuration
Definition I2SESP32.h:92
void end()
stops the I2C and unistalls the driver
Definition I2SESP32.h:85
bool begin(RxTxMode mode)
starts the DAC with the default config
Definition I2SESP32.h:53
I2SConfigESP32 defaultConfig(RxTxMode mode)
Provides the default configuration.
Definition I2SESP32.h:31
size_t writeBytes(const void *src, size_t size_bytes)
writes the data to the I2S interface
Definition I2SESP32.h:95
size_t readBytes(void *dest, size_t size_bytes)
Definition I2SESP32.h:112
We support the Stream interface for the I2S access. In addition we allow a separate mute pin which mi...
Definition I2SStream.h:33
void flush() override
Definition I2SStream.h:135
int mute_pin
Definition I2SStream.h:148
void mute(bool is_mute)
set mute pin on or off
Definition I2SStream.h:152
bool is_active
Definition I2SStream.h:149
bool begin()
Definition I2SStream.h:54
virtual size_t readBytes(uint8_t *data, size_t len) override
Reads the audio data.
Definition I2SStream.h:125
virtual int availableForWrite() override
Provides the available audio data.
Definition I2SStream.h:133
I2SConfig defaultConfig(RxTxMode mode=TX_MODE)
Provides the default configuration.
Definition I2SStream.h:50
I2SDriver * driver()
Provides access to the driver.
Definition I2SStream.h:138
virtual void setAudioInfo(AudioInfo info)
updates the sample rate dynamically
Definition I2SStream.h:95
virtual size_t write(const uint8_t *data, size_t len)
Writes the audio data to I2S.
Definition I2SStream.h:118
bool isActive()
Returns true if i2s is active.
Definition I2SStream.h:144
void end()
Stops the I2S interface.
Definition I2SStream.h:85
bool begin(I2SConfig cfg)
Starts the I2S interface.
Definition I2SStream.h:71
I2SDriver i2s
Definition I2SStream.h:147
~I2SStream()
Definition I2SStream.h:36
virtual int available() override
Provides the available audio data.
Definition I2SStream.h:130
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:30
@ RXTX_MODE
Definition AudioTypes.h:30
@ TX_MODE
Definition AudioTypes.h:30
@ UNDEFINED_MODE
Definition AudioTypes.h:30
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
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
bool equals(AudioInfo alt)
Returns true if alt values are the same like the current values.
Definition AudioTypes.h:87
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
virtual void logInfo(const char *source="")
Definition AudioTypes.h:125