arduino-audio-tools
Loading...
Searching...
No Matches
CodecL16.h
Go to the documentation of this file.
1#pragma once
2
4namespace audio_tools {
5
13class DecoderL16 : public AudioDecoder {
14public:
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
44 const char *mime() override { return "audio/l16"; }
45
47 void setOutput(Print &out_stream) override { p_print = &out_stream; }
48
49 virtual size_t write(const uint8_t *data, size_t len) override {
50 if (p_print == nullptr)
51 return 0;
52 int16_t *data16 = (int16_t *)data;
53 for (int j = 0; j < len / 2; j++) {
54 data16[j] = ntohs(data16[j]);
55 }
56 return p_print->write((uint8_t *)data, len);
57 }
58
59 virtual operator bool() override { return p_print!=nullptr; }
60
61
62protected:
63 Print *p_print = nullptr;
64};
65
77class EncoderL16 : public AudioEncoder {
78public:
79 // Empty Constructor - the output stream must be provided with begin()
81
82 // Constructor providing the output stream
83 EncoderL16(Print &out) { p_print = &out; }
84
86 void setOutput(Print &out_stream) override { p_print = &out_stream; }
87
89 const char *mime() override { return "audio/l16"; }
90
92 virtual bool begin() override { is_open = true; return true;}
93
95 bool begin(Print &out) {
96 p_print = &out;
97 return begin();
98 }
99
101 void end() override { is_open = false; }
102
104 virtual size_t write(const uint8_t *data, size_t len) override {
105 if (p_print == nullptr)
106 return 0;
107
108 int16_t *data16 = (int16_t *)data;
109 for (int j = 0; j < len / 2; j++) {
110 data16[j] = htons(data16[j]);
111 }
112
113 return p_print->write((uint8_t *)data, len);
114 }
115
116 operator bool() override { return is_open; }
117
118 bool isOpen() { return is_open; }
119
120protected:
121 Print *p_print = nullptr;
123};
124
125} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define htons(x)
Definition Net.h:15
#define ntohs(x)
Definition Net.h:17
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
Encoding of PCM data.
Definition AudioCodecsBase.h:101
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:150
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:132
DecoderL16 - Converts an 16 Bit Stream into 16Bits network byte order.
Definition CodecL16.h:13
virtual size_t write(const uint8_t *data, size_t len) override
Definition CodecL16.h:49
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecL16.h:47
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
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecL16.h:44
Print * p_print
Definition CodecL16.h:63
EncoderL16s - Condenses 16 bit PCM data stream to 8 bits data. Most microcontrollers can not process ...
Definition CodecL16.h:77
virtual size_t write(const uint8_t *data, size_t len) override
Writes PCM data to be encoded as RAW.
Definition CodecL16.h:104
bool is_open
Definition CodecL16.h:122
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecL16.h:86
bool isOpen()
Definition CodecL16.h:118
virtual bool begin() override
starts the processing using the actual RAWAudioInfo
Definition CodecL16.h:92
void end() override
stops the processing
Definition CodecL16.h:101
bool begin(Print &out)
starts the processing
Definition CodecL16.h:95
EncoderL16()
Definition CodecL16.h:80
EncoderL16(Print &out)
Definition CodecL16.h:83
const char * mime() override
Provides "audio/pcm".
Definition CodecL16.h:89
Print * p_print
Definition CodecL16.h:121
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6