arduino-audio-tools
CodecFloat.h
1 #pragma once
2 
3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4 
5 
6 namespace audio_tools {
7 
15 class DecoderFloat : public AudioDecoder {
16  public:
18  DecoderFloat() = default;
24  DecoderFloat(Print &out_stream, bool active=true){
25  TRACED();
26  p_print = &out_stream;
27  }
28 
36  DecoderFloat(Print &out_stream, AudioInfoSupport &bi){
37  TRACED();
38  p_print = &out_stream;
40  }
41 
43  void setOutput(Print &out_stream) override {
44  p_print = &out_stream;
45  }
46 
48  virtual size_t write(const uint8_t *data, size_t len) override {
49  if (p_print==nullptr) return 0;
50  int samples = len/sizeof(float);
51  buffer.resize(samples);
52  float* p_float = (float*) data;
53  for (int j=0;j<samples;j++){
54  buffer[j] = p_float[j]*32767;
55  }
56  return p_print->write((uint8_t*)buffer.data(), samples*sizeof(int16_t)) * 2;
57  }
58 
59  virtual operator bool() override {
60  return p_print!=nullptr;;
61  }
62 
63  protected:
64  Print *p_print=nullptr;
65  Vector<int16_t> buffer;
66 
67 };
68 
77 class EncoderFloat : public AudioEncoder {
78  public:
80  EncoderFloat() = default;
81 
84  p_print = &out;
85  }
86 
88  void setOutput(Print &out_stream) override {
89  p_print = &out_stream;
90  }
91 
93  const char* mime() override{
94  return mime_pcm;
95  }
96 
98  virtual bool begin() override{
99  is_open = true;
100  return true;
101  }
102 
104  bool begin(Print &out) {
105  p_print = &out;
106  return begin();
107  }
108 
110  void end() override {
111  is_open = false;
112  }
113 
115  virtual size_t write(const uint8_t *data, size_t len) override {
116  if (p_print==nullptr) return 0;
117  int16_t *pt16 = (int16_t*)data;
118  size_t samples = len / sizeof(int16_t);
119  buffer.resize(samples);
120  for (size_t j=0;j<samples;j++){
121  buffer[j] = static_cast<float>(pt16[j]) / 32768.0;
122  }
123  return p_print->write((uint8_t*)buffer.data(), samples*sizeof(float));
124  }
125 
126  operator bool() override {
127  return is_open;
128  }
129 
130  bool isOpen(){
131  return is_open;
132  }
133 
134  protected:
135  Print* p_print=nullptr;;
136  volatile bool is_open;
137  Vector<float> buffer;
138 
139 
140 };
141 
142 }
Docoding of encoded audio into PCM data.
Definition: AudioCodecsBase.h:16
Encoding of PCM data.
Definition: AudioCodecsBase.h:84
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition: AudioTypes.h:162
Supports changes to the sampling rate, bits and channels.
Definition: AudioTypes.h:139
DecoderFloat - Converts Stream of floats into 2 byte integers.
Definition: CodecFloat.h:15
virtual size_t write(const uint8_t *data, size_t len) override
Converts data from float to int16_t.
Definition: CodecFloat.h:48
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition: CodecFloat.h:43
DecoderFloat()=default
Empty Constructor.
DecoderFloat(Print &out_stream, AudioInfoSupport &bi)
Construct a new DecoderFloat object.
Definition: CodecFloat.h:36
DecoderFloat(Print &out_stream, bool active=true)
Construct a new DecoderFloat object.
Definition: CodecFloat.h:24
EncoderFloats - Encodes 16 bit PCM data stream to floats data.
Definition: CodecFloat.h:77
virtual size_t write(const uint8_t *data, size_t len) override
Converts data from int16_t to float.
Definition: CodecFloat.h:115
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition: CodecFloat.h:88
EncoderFloat(Print &out)
Constructor providing the output stream.
Definition: CodecFloat.h:83
virtual bool begin() override
starts the processing using the actual RAWAudioInfo
Definition: CodecFloat.h:98
const char * mime() override
Provides "audio/pcm".
Definition: CodecFloat.h:93
void end() override
stops the processing
Definition: CodecFloat.h:110
bool begin(Print &out)
starts the processing
Definition: CodecFloat.h:104
EncoderFloat()=default
Empty Constructor.
Definition: NoArduino.h:58
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868
static const char * mime_pcm
Mime type for PCM.
Definition: AudioTypes.h:523