arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
I2SStream.h
1
2
3#pragma once
4#include "AudioToolsConfig.h"
5
6#if defined(USE_I2S)
7
8#include "AudioTools/CoreAudio/AudioI2S/I2SConfig.h"
9#include "AudioTools/CoreAudio/AudioI2S/I2SESP32.h"
10#include "AudioTools/CoreAudio/AudioI2S/I2SESP32V1.h"
11#include "AudioTools/CoreAudio/AudioI2S/I2SESP8266.h"
12#include "AudioTools/CoreAudio/AudioI2S/I2SNanoSenseBLE.h"
13#include "AudioTools/CoreAudio/AudioI2S/I2SRP2040-MBED.h"
14#include "AudioTools/CoreAudio/AudioI2S/I2SRP2040.h"
15#include "AudioTools/CoreAudio/AudioI2S/I2SSAMD.h"
16#include "AudioTools/CoreAudio/AudioI2S/I2SSTM32.h"
17#include "AudioTools/CoreAudio/AudioStreams.h"
18#include "AudioTools/CoreAudio/AudioTypes.h"
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) {
43 pinMode(mute_pin, OUTPUT);
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){
63 cfg.rx_tx_mode = RXTX_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();
97 assert(info.sample_rate != 0);
98 assert(info.channels != 0);
99 assert(info.bits_per_sample != 0);
101 if (is_active) {
102 if (!i2s.setAudioInfo(info)) {
103 I2SConfig current_cfg = i2s.config();
104 if (!info.equals(current_cfg)) {
105 LOGI("restarting i2s");
106 info.logInfo("I2SStream");
107 i2s.end();
108 current_cfg.copyFrom(info);
109 i2s.begin(current_cfg);
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:
147 I2SDriver i2s;
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) {
155 digitalWrite(mute_pin, is_mute ? SOFT_MUTE_VALUE : !SOFT_MUTE_VALUE);
156 }
157#endif
158 }
159};
160
161} // namespace audio_tools
162
163#endif
164#endif
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:119
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition BaseStream.h:127
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition BaseStream.h:150
Configuration for ESP32 legacy i2s.
Definition I2SConfigESP32.h:23
RxTxMode rx_tx_mode
public settings
Definition I2SConfigESP32.h:59
Basic I2S API - for the ESP32. If we receive 1 channel, we expand the result to 2 channels.
Definition I2SESP32.h:27
int available()
we assume the data is already available in the buffer
Definition I2SESP32.h:81
bool setAudioInfo(AudioInfo info)
Potentially updates the sample rate (if supported)
Definition I2SESP32.h:39
int availableForWrite()
We limit the write size to the buffer size.
Definition I2SESP32.h:84
I2SConfigESP32 config()
provides the actual configuration
Definition I2SESP32.h:94
void end()
stops the I2C and unistalls the driver
Definition I2SESP32.h:87
bool begin(RxTxMode mode)
starts the DAC with the default config
Definition I2SESP32.h:55
I2SConfigESP32 defaultConfig(RxTxMode mode)
Provides the default configuration.
Definition I2SESP32.h:33
size_t writeBytes(const void *src, size_t size_bytes)
writes the data to the I2S interface
Definition I2SESP32.h:97
We support the Stream interface for the I2S access. In addition we allow a separate mute pin which mi...
Definition I2SStream.h:33
void mute(bool is_mute)
set mute pin on or off
Definition I2SStream.h:152
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
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:28
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:53
void copyFrom(AudioInfo info)
Same as set.
Definition AudioTypes.h:103
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:55
bool equals(AudioInfo alt)
Returns true if alt values are the same like the current values.
Definition AudioTypes.h:85
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:59