arduino-audio-tools
CodecL16.h
1 #pragma once
2 
3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4 namespace audio_tools {
5 
13 class DecoderL16 : public AudioDecoder {
14 public:
19  DecoderL16() { TRACED(); }
20 
26  DecoderL16(Print &out_stream, bool active = true) {
27  TRACED();
28  p_print = &out_stream;
29  }
30 
38  DecoderL16(Print &out_stream, AudioInfoSupport &bi) {
39  TRACED();
40  setOutput(out_stream);
42  }
43 
45  void setOutput(Print &out_stream) override { p_print = &out_stream; }
46 
47  virtual size_t write(const uint8_t *data, size_t len) override {
48  if (p_print == nullptr)
49  return 0;
50  int16_t *data16 = (int16_t *)data;
51  for (int j = 0; j < len / 2; j++) {
52  data16[j] = ntohs(data16[j]);
53  }
54  return p_print->write((uint8_t *)data, len);
55  }
56 
57  virtual operator bool() override { return p_print!=nullptr; }
58 
59 
60 protected:
61  Print *p_print = nullptr;
62 };
63 
75 class EncoderL16 : public AudioEncoder {
76 public:
77  // Empty Constructor - the output stream must be provided with begin()
78  EncoderL16() {}
79 
80  // Constructor providing the output stream
81  EncoderL16(Print &out) { p_print = &out; }
82 
84  void setOutput(Print &out_stream) override { p_print = &out_stream; }
85 
87  const char *mime() override { return "audio/l16"; }
88 
90  virtual bool begin() override { is_open = true; return true;}
91 
93  bool begin(Print &out) {
94  p_print = &out;
95  return begin();
96  }
97 
99  void end() override { is_open = false; }
100 
102  virtual size_t write(const uint8_t *data, size_t len) override {
103  if (p_print == nullptr)
104  return 0;
105 
106  int16_t *data16 = (int16_t *)data;
107  for (int j = 0; j < len / 2; j++) {
108  data16[j] = htons(data16[j]);
109  }
110 
111  return p_print->write((uint8_t *)data, len);
112  }
113 
114  operator bool() override { return is_open; }
115 
116  bool isOpen() { return is_open; }
117 
118 protected:
119  Print *p_print = nullptr;
120  bool is_open;
121 };
122 
123 } // namespace audio_tools
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
DecoderL16 - Converts an 16 Bit Stream into 16Bits network byte order.
Definition: CodecL16.h:13
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition: CodecL16.h:45
DecoderL16(Print &out_stream, AudioInfoSupport &bi)
Construct a new DecoderL16 object.
Definition: CodecL16.h:38
DecoderL16(Print &out_stream, bool active=true)
Construct a new DecoderL16 object.
Definition: CodecL16.h:26
DecoderL16()
Construct a new DecoderL16 object.
Definition: CodecL16.h:19
EncoderL16s - Condenses 16 bit PCM data stream to 8 bits data. Most microcontrollers can not process ...
Definition: CodecL16.h:75
virtual size_t write(const uint8_t *data, size_t len) override
Writes PCM data to be encoded as RAW.
Definition: CodecL16.h:102
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition: CodecL16.h:84
virtual bool begin() override
starts the processing using the actual RAWAudioInfo
Definition: CodecL16.h:90
const char * mime() override
Provides "audio/pcm".
Definition: CodecL16.h:87
void end() override
stops the processing
Definition: CodecL16.h:99
bool begin(Print &out)
starts the processing
Definition: CodecL16.h:93
Definition: NoArduino.h:58
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823