arduino-audio-tools
Loading...
Searching...
No Matches
CodecNetworkFormat.h
Go to the documentation of this file.
1#pragma once
2
5#if defined(ARDUINO) && !defined(IS_MIN_DESKTOP)
6#include "Print.h"
7#endif
8
9namespace audio_tools {
10
19 public:
21
26
30
32
34
35 bool begin() { return true; }
36
37 void end() {}
38
39 size_t write(const uint8_t *data, size_t len) {
40 TRACED();
41 switch (audioInfo().bits_per_sample) {
42 case 8:
43 // nothing to do
44 break;
45 case 16: {
46 int16_t *data16 = (int16_t *)data;
47 for (int i = 0; i < len / sizeof(int16_t); i++) {
48 data16[i] = ntohs(data16[i]);
49 }
50 } break;
51 case 24:
52 case 32: {
53 int32_t *data32 = (int32_t *)data;
54 for (int i = 0; i < len / sizeof(int32_t); i++) {
55 data32[i] = ntohl(data32[i]);
56 }
57 } break;
58 default:
59 LOGE("bits_per_sample not supported: %d",
60 (int)audioInfo().bits_per_sample);
61 break;
62 }
63 return pt_print->write((uint8_t *)data, len);
64 }
65
66 operator bool() { return true; }
67
69 virtual bool isResultPCM() { return true; }
70
71 protected:
72 Print *pt_print = nullptr;
73};
74
83 public:
85
90
94
96
98
99 bool begin() { return true; }
100
101 void end() {}
102
103 size_t write(const uint8_t *data, size_t len) {
104 TRACED();
105 switch (audioInfo().bits_per_sample) {
106 case 8:
107 // nothing to do
108 break;
109 case 16: {
110 int16_t *data16 = (int16_t *)data;
111 for (int i = 0; i < len / sizeof(int16_t); i++) {
112 data16[i] = htons(data16[i]);
113 }
114 } break;
115 case 24:
116 case 32: {
117 int32_t *data32 = (int32_t *)data;
118 for (int i = 0; i < len / sizeof(int32_t); i++) {
119 data32[i] = htonl(data32[i]);
120 }
121 } break;
122 default:
123 LOGE("bits_per_sample not supported: %d",
124 (int)audioInfo().bits_per_sample);
125 break;
126 }
127 return pt_print->write((uint8_t *)data, len);
128 }
129
130 operator bool() { return true; }
131
132 const char *mime() { return "audio/pcm"; }
133
134 protected:
135 Print *pt_print = nullptr;
136};
137
138} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define htons(x)
Definition Net.h:7
#define ntohl(x)
Definition Net.h:8
#define ntohs(x)
Definition Net.h:9
#define htonl(x)
Definition Net.h:6
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioCodecsBase.h:25
Encoding of PCM data.
Definition AudioCodecsBase.h:97
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioCodecsBase.h:107
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:135
PCM decoder which converts from network format to the host format.
Definition CodecNetworkFormat.h:18
virtual bool isResultPCM()
The result is encoded data - by default this is false.
Definition CodecNetworkFormat.h:69
virtual void setOutput(Print &out_stream)
Defines where the decoded result is written to.
Definition CodecNetworkFormat.h:33
DecoderNetworkFormat(Print &out_stream)
Definition CodecNetworkFormat.h:22
bool begin()
Definition CodecNetworkFormat.h:35
~DecoderNetworkFormat()
Definition CodecNetworkFormat.h:31
void end()
Definition CodecNetworkFormat.h:37
Print * pt_print
Definition CodecNetworkFormat.h:72
DecoderNetworkFormat(Print &out_stream, AudioInfoSupport &bi)
Definition CodecNetworkFormat.h:27
size_t write(const uint8_t *data, size_t len)
Definition CodecNetworkFormat.h:39
Encoder which converts from the host format to the network format.
Definition CodecNetworkFormat.h:82
EncoderNetworkFormat(Print &out_stream, AudioInfoSupport &bi)
Definition CodecNetworkFormat.h:91
virtual void setOutput(Print &out_stream)
Default output assignment (encoders may override to store Print reference)
Definition CodecNetworkFormat.h:97
bool begin()
Definition CodecNetworkFormat.h:99
const char * mime()
Provides the mime type of the encoded result.
Definition CodecNetworkFormat.h:132
~EncoderNetworkFormat()
Definition CodecNetworkFormat.h:95
EncoderNetworkFormat()
Definition CodecNetworkFormat.h:84
void end()
Definition CodecNetworkFormat.h:101
Print * pt_print
Definition CodecNetworkFormat.h:135
EncoderNetworkFormat(Print &out_stream)
Definition CodecNetworkFormat.h:86
size_t write(const uint8_t *data, size_t len)
Definition CodecNetworkFormat.h:103
Definition NoArduino.h:62
virtual size_t write(const uint8_t *data, size_t len)
Definition NoArduino.h:126
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512