arduino-audio-tools
AnalogAudioStream.h
1 #pragma once
2 #include "AudioConfig.h"
3 
4 // Support AnalogAudioStream
5 #if defined(USE_ANALOG)
6 # include "AnalogDriverBase.h"
7 # include "AnalogDriverESP32V1.h"
8 # include "AnalogDriverESP32.h"
9 # include "AnalogDriverMBED.h"
10 # include "AnalogDriverArduino.h"
11 
12 namespace audio_tools {
13 
23  public:
25  AnalogAudioStream()=default;
26 
29  p_analog = &driver;
30  }
31 
33  virtual ~AnalogAudioStream() {
34  end();
35  }
36 
39  TRACED();
40  AnalogConfig config(mode);
41  return config;
42  }
43 
45  virtual void setAudioInfo(AudioInfo info) {
46  TRACEI();
47  if (adc_config.sample_rate != info.sample_rate
48  || adc_config.channels != info.channels
49  || adc_config.bits_per_sample != info.bits_per_sample) {
50  adc_config.sample_rate = info.sample_rate;
51  adc_config.bits_per_sample = info.bits_per_sample;
52  adc_config.channels = info.channels;
53  adc_config.logInfo();
54  end();
55  begin(adc_config);
56  }
57  }
58 
60  bool begin() override {
61  return begin(adc_config);
62  }
63 
65  bool begin(AnalogConfig cfg) {
66  TRACEI();
67  return p_analog->begin(cfg);
68  }
69 
71  void end() override {
72  TRACEI();
73  p_analog->end();
74  }
75 
76  AnalogConfig &config() {
77  return adc_config;
78  }
79 
81  size_t write(const uint8_t *data, size_t len) override {
82  TRACED();
83  return p_analog->write(data, len);
84  }
85 
86  size_t readBytes(uint8_t *data, size_t len) override {
87  return p_analog->readBytes(data, len);
88  }
89 
90  int available() override {
91  return p_analog->available();
92  }
93 
94  int availableForWrite() override {
95  return p_analog->availableForWrite();
96  }
97 
100  return p_analog;
101  }
102 
103 protected:
104  AnalogDriver default_analog;
105  AnalogDriverBase* p_analog = &default_analog;
106  AnalogConfig adc_config;
107 };
108 
109 }
110 
111 #endif
112 
113 // Support AnalogAudioArduino
114 #if defined(USE_TIMER)
115 # include "AnalogAudioArduino.h"
116 #endif
ESP32: A very fast ADC and DAC using the ESP32 I2S interface. For all other architectures we support ...
Definition: AnalogAudioStream.h:22
bool begin(AnalogConfig cfg)
starts the DAC
Definition: AnalogAudioStream.h:65
AnalogDriverBase * driver()
Provides access to the driver.
Definition: AnalogAudioStream.h:99
void end() override
stops the I2S and unistalls the analog
Definition: AnalogAudioStream.h:71
AnalogAudioStream(AnalogDriverBase &driver)
Constructor to define a custom driver.
Definition: AnalogAudioStream.h:28
size_t write(const uint8_t *data, size_t len) override
ESP32 only: writes the data to the I2S interface.
Definition: AnalogAudioStream.h:81
virtual void setAudioInfo(AudioInfo info)
updates the sample rate dynamically
Definition: AnalogAudioStream.h:45
AnalogAudioStream()=default
Default constructor.
virtual ~AnalogAudioStream()
Destructor.
Definition: AnalogAudioStream.h:33
AnalogConfig defaultConfig(RxTxMode mode=TX_MODE)
Provides the default configuration.
Definition: AnalogAudioStream.h:38
bool begin() override
Reopen with last config.
Definition: AnalogAudioStream.h:60
ESP32 specific configuration for i2s input via adc. The default input pin is GPIO34....
Definition: AnalogConfigESP32.h:21
Please use the AnalogAudioStream: Reading Analog Data using a timer and the Arduino analogRead() meth...
Definition: AnalogDriverArduino.h:23
Definition: AnalogDriverBase.h:13
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition: BaseStream.h:109
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
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:55
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