arduino-audio-tools
AudioPWM.h
1 #pragma once
2 #include "AudioConfig.h"
3 #if defined(USE_PWM)
4 
5 #include "AudioTools/CoreAudio/AudioPWM/PWMAudioESP32.h"
6 #include "AudioTools/CoreAudio/AudioPWM/PWMAudioMBED.h"
7 #include "AudioTools/CoreAudio/AudioPWM/PWMAudioRP2040.h"
8 #include "AudioTools/CoreAudio/AudioPWM/PWMAudioRenesas.h"
9 #include "AudioTools/CoreAudio/AudioPWM/PWMAudioSTM32.h"
10 // this is experimental at the moment
11 #include "AudioTools/CoreAudio/AudioPWM/PWMAudioAVR.h"
12 
13 namespace audio_tools {
14 
21 class PWMAudioOutput : public AudioOutput {
22  public:
23  ~PWMAudioOutput() {
24  if (pwm.isTimerStarted()) {
25  end();
26  }
27  }
28 
29  virtual PWMConfig defaultConfig(RxTxMode mode=TX_MODE) {
30  if (mode!=TX_MODE) LOGE("mode not supported: using TX_MODE");
31  return pwm.defaultConfig();
32  }
33 
34  PWMConfig config() { return audio_config; }
35 
37  virtual void setAudioInfo(AudioInfo info) {
38  TRACEI();
39  PWMConfig cfg = audio_config;
40  if (cfg.sample_rate != info.sample_rate || cfg.channels != info.channels ||
41  cfg.bits_per_sample != info.bits_per_sample) {
42  cfg.sample_rate = info.sample_rate;
44  cfg.channels = info.channels;
45  cfg.logInfo();
46  end();
47  begin(cfg);
48  }
49  }
50 
52  bool begin(PWMConfig config) {
53  TRACED();
54  this->audio_config = config;
55  AudioOutput::setAudioInfo(audio_config);
56  return pwm.begin(audio_config);
57  }
58 
59  bool begin() {
60  TRACED();
61  AudioOutput::setAudioInfo(audio_config);
62  return pwm.begin(audio_config);
63  }
64 
65  virtual void end() override { pwm.end(); }
66 
67  int availableForWrite() override { return pwm.availableForWrite(); }
68 
69  // blocking write for an array: we expect a singed value and convert it into a
70  // unsigned
71  size_t write(const uint8_t *data, size_t len) override {
72  return pwm.write(data, len);
73  }
74 
75  // When the timer does not have enough data we increase the underflow_count;
76  uint32_t underflowsPerSecond() { return pwm.underflowsPerSecond(); }
77  // provides the effectivly measured output frames per second
78  uint32_t framesPerSecond() { return pwm.framesPerSecond(); }
79 
81  PWMDriver *driver() { return &pwm; }
84  void setBuffer(BaseBuffer<uint8_t> *buffer) { pwm.setBuffer(buffer); }
85 
86  protected:
87  PWMConfig audio_config;
88  PWMDriver pwm; // platform specific pwm
89 };
90 
91 // legacy name
92 #if USE_OBSOLETE
93 using PWMAudioStream = PWMAudioOutput;
94 #endif
95 
96 } // namespace audio_tools
97 
98 #endif
Abstract Audio Ouptut class.
Definition: AudioOutput.h:22
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition: AudioOutput.h:46
void setBuffer(BaseBuffer< uint8_t > *buffer)
Definition: PWMAudioBase.h:225
Common functionality for PWM output. Please use the PWMAudioOutput typedef instead which references t...
Definition: AudioPWM.h:21
PWMDriver * driver()
Provides access to the driver.
Definition: AudioPWM.h:81
void setBuffer(BaseBuffer< uint8_t > *buffer)
Definition: AudioPWM.h:84
virtual void setAudioInfo(AudioInfo info)
updates the sample rate dynamically
Definition: AudioPWM.h:37
bool begin(PWMConfig config)
starts the processing using Streams
Definition: AudioPWM.h:52
Experimental: Audio output to PWM pins for the AVR. The AVR supports only up to 2 channels.
Definition: PWMAudioAVR.h:22
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
Configuration data for PWM audio output.
Definition: PWMAudioBase.h:32