arduino-audio-tools
AudioESP8266.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/AudioOutput.h"
5 #include "AudioTools/CoreAudio/AudioStreams.h"
6 #include "AudioTools/CoreAudio/Buffers.h"
7 #include "AudioOutput.h"
8 #include "SoundData.h"
9 
10 namespace audio_tools {
11 
12 
13 
25  public:
26  // Default constructor
27  AudioOutputWithCallback(int bufferSize, int bufferCount)
28  : BufferedStream(bufferSize) {
29  callback_buffer_ptr = new NBuffer<Frame>(bufferSize, bufferCount);
30  }
31 
32  virtual ~AudioOutputWithCallback() { delete callback_buffer_ptr; }
33 
35  virtual bool begin() {
36  active = true;
37  return true;
38  }
39 
41  virtual bool ConsumeSample(int16_t sample[2]) {
42  Frame c;
43  c.channel1 = sample[0];
44  c.channel2 = sample[1];
45  return callback_buffer_ptr->write(c);
46  };
47 
49  virtual bool stop() {
50  active = false;
51  return true;
52  };
53 
55  size_t read(Frame *src, size_t len) {
56  return active ? this->callback_buffer_ptr->readArray(src, len) : 0;
57  }
58 
59  protected:
60  NBuffer<Frame> *callback_buffer_ptr;
61  bool active;
62 
63  virtual size_t writeExt(const uint8_t *data, size_t len) {
64  return callback_buffer_ptr->writeArray((Frame *)data, len / sizeof(Frame));
65  }
66 
67  virtual size_t readExt(uint8_t *data, size_t len) {
68  return callback_buffer_ptr->readArray((Frame *)data, len / sizeof(Frame));
69  ;
70  }
71 };
72 
78  public:
79  ESP3288AudioOutput(::AudioOutput &out, int channels) {
80  p_out = &out;
81  this->channels = channels;
82  }
83 
84  virtual size_t write(const uint8_t *data, size_t len) {
85  size_t result = 0;
86  int16_t *v = (int16_t *)data;
87  if (channels == 2) {
88  result = p_out->ConsumeSamples(v, len / 2);
89  } else {
90  LOGE("Only 2 Channels are supported");
91  result = 0;
92  }
93  return result;
94  }
95 
96  protected:
97  ::AudioOutput *p_out = nullptr;
98  int channels;
99 };
100 } // namespace audio_tools
Abstract Audio Ouptut class.
Definition: AudioOutput.h:22
ESP8266Audio AudioOutput class which stores the data in a temporary buffer. The buffer can be consume...
Definition: AudioESP8266.h:24
size_t read(Frame *src, size_t len)
Provides the data from the internal buffer to the callback.
Definition: AudioESP8266.h:55
virtual bool stop()
stops the processing
Definition: AudioESP8266.h:49
virtual bool begin()
Activates the output.
Definition: AudioESP8266.h:35
virtual bool ConsumeSample(int16_t sample[2])
puts the sample into a buffer
Definition: AudioESP8266.h:41
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition: BaseStream.h:109
virtual int readArray(T data[], int len)
reads multiple values
Definition: Buffers.h:41
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition: Buffers.h:65
The Arduino Stream supports operations on single characters. This is usually not the best way to push...
Definition: AudioStreams.h:480
Stream Adapter for ESP8288-Audio AudioOutput.
Definition: AudioESP8266.h:77
bool write(T data)
write add an entry to the buffer
Definition: Buffers.h:595
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868