arduino-audio-tools
I2SStream.h
1 
2 
3 #pragma once
4 #include "AudioConfig.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 
22 namespace audio_tools {
23 
33 class I2SStream : public AudioStream {
34  public:
35  I2SStream() = default;
36 
37 #ifdef ARDUINO
38  I2SStream(int mute_pin) {
39  TRACED();
40  this->mute_pin = mute_pin;
41  if (mute_pin > 0) {
42  pinMode(mute_pin, OUTPUT);
43  mute(true);
44  }
45  }
46 #endif
47 
49  I2SConfig defaultConfig(RxTxMode mode = TX_MODE) {
50  return i2s.defaultConfig(mode);
51  }
52 
53  bool begin() {
54  TRACEI();
55  I2SConfig cfg = i2s.config();
56  if (!cfg){
57  LOGE("unsuported AudioInfo: sample_rate: %d / channels: %d / bits_per_sample: %d", (int) cfg.sample_rate, (int)cfg.channels, (int)cfg.bits_per_sample);
58  return false;
59  }
60  cfg.copyFrom(audioInfo());
61  if (cfg.rx_tx_mode == UNDEFINED_MODE){
62  cfg.rx_tx_mode = RXTX_MODE;
63  }
64  is_active = i2s.begin(cfg);
65  mute(false);
66  return is_active;
67  }
68 
70  bool begin(I2SConfig cfg) {
71  TRACED();
72  if (!cfg){
73  LOGE("unsuported AudioInfo: sample_rate: %d / channels: %d / bits_per_sample: %d", (int) cfg.sample_rate, (int)cfg.channels, (int)cfg.bits_per_sample);
74  return false;
75  }
77  is_active = i2s.begin(cfg);
78  // unmute
79  mute(false);
80  return is_active;
81  }
82 
84  void end() {
85  TRACEI();
86  is_active = false;
87  mute(true);
88  i2s.end();
89  }
90 
92  virtual void setAudioInfo(AudioInfo info) {
93  TRACEI();
94  assert(info.sample_rate != 0);
95  assert(info.channels != 0);
96  assert(info.bits_per_sample != 0);
98  if (is_active) {
99  if (!i2s.setAudioInfo(info)) {
100  I2SConfig current_cfg = i2s.config();
101  if (!info.equals(current_cfg)) {
102  LOGI("restarting i2s");
103  info.logInfo("I2SStream");
104  i2s.end();
105  current_cfg.copyFrom(info);
106  i2s.begin(current_cfg);
107  } else {
108  LOGI("no change");
109  }
110  }
111  }
112  }
113 
115  virtual size_t write(const uint8_t *data, size_t len) {
116  LOGD("I2SStream::write: %d", len);
117  if (data == nullptr || len == 0 || !is_active) return 0;
118  return i2s.writeBytes(data, len);
119  }
120 
122  virtual size_t readBytes(uint8_t *data, size_t len) override {
123  return i2s.readBytes(data, len);
124  }
125 
127  virtual int available() override { return i2s.available(); }
128 
130  virtual int availableForWrite() override { return i2s.availableForWrite(); }
131 
132  void flush() override {}
133 
135  I2SDriver *driver() { return &i2s; }
136 
138  operator bool() override { return is_active; }
139 
141  bool isActive() { return is_active;}
142 
143  protected:
144  I2SDriver i2s;
145  int mute_pin;
146  bool is_active = false;
147 
149  void mute(bool is_mute) {
150 #ifdef ARDUINO
151  if (mute_pin > 0) {
152  digitalWrite(mute_pin, is_mute ? SOFT_MUTE_VALUE : !SOFT_MUTE_VALUE);
153  }
154 #endif
155  }
156 };
157 
158 } // namespace audio_tools
159 
160 #endif
161 #endif
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition: BaseStream.h:109
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition: BaseStream.h:117
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition: BaseStream.h:140
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:149
virtual size_t readBytes(uint8_t *data, size_t len) override
Reads the audio data.
Definition: I2SStream.h:122
I2SDriver * driver()
Provides access to the driver.
Definition: I2SStream.h:135
virtual int availableForWrite() override
Provides the available audio data.
Definition: I2SStream.h:130
I2SConfig defaultConfig(RxTxMode mode=TX_MODE)
Provides the default configuration.
Definition: I2SStream.h:49
virtual void setAudioInfo(AudioInfo info)
updates the sample rate dynamically
Definition: I2SStream.h:92
virtual size_t write(const uint8_t *data, size_t len)
Writes the audio data to I2S.
Definition: I2SStream.h:115
bool isActive()
Returns true if i2s is active.
Definition: I2SStream.h:141
void end()
Stops the I2S interface.
Definition: I2SStream.h:84
bool begin(I2SConfig cfg)
Starts the I2S interface.
Definition: I2SStream.h:70
virtual int available() override
Provides the available audio data.
Definition: I2SStream.h:127
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: AudioConfig.h:823
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:52
void copyFrom(AudioInfo info)
Same as set.
Definition: AudioTypes.h:107
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