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 
22 class PWMAudioOutput : public AudioOutput {
23  public:
24  ~PWMAudioOutput() {
25  if (pwm.isTimerStarted()) {
26  end();
27  }
28  }
29 
30  virtual PWMConfig defaultConfig(RxTxMode mode=TX_MODE) {
31  if (mode!=TX_MODE) LOGE("mode not supported: using TX_MODE");
32  return pwm.defaultConfig();
33  }
34 
35  PWMConfig config() { return audio_config; }
36 
38  void setAudioInfo(AudioInfo info) override {
39  TRACEI();
40  AudioOutput::cfg = info;
41  PWMConfig cfg = audio_config;
42  if (cfg.sample_rate != info.sample_rate || cfg.channels != info.channels ||
43  cfg.bits_per_sample != info.bits_per_sample) {
44  cfg.sample_rate = info.sample_rate;
46  cfg.channels = info.channels;
47  end();
48  begin(cfg);
49  cfg.logInfo();
50  }
51  }
52 
53  AudioInfo audioInfoOut() override {
54  AudioInfo result = audioInfo();
56  return result;
57  }
58 
60  bool begin(PWMConfig config) {
61  TRACED();
62  this->audio_config = config;
63  return begin();
64  }
65 
66  bool begin() {
67  TRACED();
68  AudioOutput::setAudioInfo(audio_config);
69  return pwm.begin(audio_config);
70  }
71 
72  virtual void end() override { pwm.end(); }
73 
74  int availableForWrite() override { return pwm.availableForWrite(); }
75 
76  // blocking write for an array: we expect a singed value and convert it into a
77  // unsigned
78  size_t write(const uint8_t *data, size_t len) override {
79  return pwm.write(data, len);
80  }
81 
82  // When the timer does not have enough data we increase the underflow_count;
83  uint32_t underflowsPerSecond() { return pwm.underflowsPerSecond(); }
84  // provides the effectivly measured output frames per second
85  uint32_t framesPerSecond() { return pwm.framesPerSecond(); }
86 
88  PWMDriver *driver() { return &pwm; }
91  void setBuffer(BaseBuffer<uint8_t> *buffer) { pwm.setBuffer(buffer); }
92 
93  protected:
94  PWMConfig audio_config;
95  PWMDriver pwm; // platform specific pwm
96 };
97 
98 // legacy name
99 #if USE_OBSOLETE
100 using PWMAudioStream = PWMAudioOutput;
101 #endif
102 
103 } // namespace audio_tools
104 
105 #endif
Abstract Audio Ouptut class.
Definition: AudioOutput.h:22
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition: AudioOutput.h:46
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition: AudioOutput.h:59
virtual int effectiveOutputSampleRate()
Provides the effective sample rate.
Definition: PWMAudioBase.h:242
void setBuffer(BaseBuffer< uint8_t > *buffer)
Definition: PWMAudioBase.h:239
Common functionality for PWM output. We generate audio using PWM with a frequency that is above the h...
Definition: AudioPWM.h:22
PWMDriver * driver()
Provides access to the driver.
Definition: AudioPWM.h:88
void setBuffer(BaseBuffer< uint8_t > *buffer)
Definition: AudioPWM.h:91
AudioInfo audioInfoOut() override
provides the actual output AudioInfo: this is usually the same as audioInfo() unless we use a transfo...
Definition: AudioPWM.h:53
bool begin(PWMConfig config)
starts the processing using Streams
Definition: AudioPWM.h:60
void setAudioInfo(AudioInfo info) override
updates the sample rate dynamically
Definition: AudioPWM.h:38
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:872
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