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
23 TRACED();
24 pt_print = &out_stream;
25 }
26
28 pt_print = &out_stream;
29 }
30
32
33 const char *mime() override { return "audio/pcm"; }
34
35 virtual void setOutput(Print &out_stream) { pt_print = &out_stream; }
36
37 bool begin() { return true; }
38
39 void end() {}
40
41 size_t write(const uint8_t *data, size_t len) {
42 TRACED();
43 switch (audioInfo().bits_per_sample) {
44 case 8:
45 // nothing to do
46 break;
47 case 16: {
48 int16_t *data16 = (int16_t *)data;
49 for (int i = 0; i < len / sizeof(int16_t); i++) {
50 data16[i] = ntohs(data16[i]);
51 }
52 } break;
53 case 24:
54 case 32: {
55 int32_t *data32 = (int32_t *)data;
56 for (int i = 0; i < len / sizeof(int32_t); i++) {
57 data32[i] = ntohl(data32[i]);
58 }
59 } break;
60 default:
61 LOGE("bits_per_sample not supported: %d",
62 (int)audioInfo().bits_per_sample);
63 break;
64 }
65 return pt_print->write((uint8_t *)data, len);
66 }
67
68 operator bool() { return true; }
69
71 virtual bool isResultPCM() { return true; }
72
73 protected:
74 Print *pt_print = nullptr;
75};
76
85 public:
87
89 TRACED();
90 pt_print = &out_stream;
91 }
92
94 pt_print = &out_stream;
95 }
96
98
99 virtual void setOutput(Print &out_stream) { pt_print = &out_stream; }
100
101 bool begin() { return true; }
102
103 void end() {}
104
105 size_t write(const uint8_t *data, size_t len) {
106 TRACED();
107 switch (audioInfo().bits_per_sample) {
108 case 8:
109 // nothing to do
110 break;
111 case 16: {
112 int16_t *data16 = (int16_t *)data;
113 for (int i = 0; i < len / sizeof(int16_t); i++) {
114 data16[i] = htons(data16[i]);
115 }
116 } break;
117 case 24:
118 case 32: {
119 int32_t *data32 = (int32_t *)data;
120 for (int i = 0; i < len / sizeof(int32_t); i++) {
121 data32[i] = htonl(data32[i]);
122 }
123 } break;
124 default:
125 LOGE("bits_per_sample not supported: %d",
126 (int)audioInfo().bits_per_sample);
127 break;
128 }
129 return pt_print->write((uint8_t *)data, len);
130 }
131
132 operator bool() { return true; }
133
134 const char *mime() { return "audio/pcm"; }
135
136 protected:
137 Print *pt_print = nullptr;
138};
139
140} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define htons(x)
Definition Net.h:15
#define ntohl(x)
Definition Net.h:16
#define ntohs(x)
Definition Net.h:17
#define htonl(x)
Definition Net.h:14
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
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioCodecsBase.h:29
Encoding of PCM data.
Definition AudioCodecsBase.h:101
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioCodecsBase.h:111
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:132
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:71
virtual void setOutput(Print &out_stream)
Defines where the decoded result is written to.
Definition CodecNetworkFormat.h:35
DecoderNetworkFormat(Print &out_stream)
Definition CodecNetworkFormat.h:22
bool begin()
Definition CodecNetworkFormat.h:37
~DecoderNetworkFormat()
Definition CodecNetworkFormat.h:31
void end()
Definition CodecNetworkFormat.h:39
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecNetworkFormat.h:33
Print * pt_print
Definition CodecNetworkFormat.h:74
DecoderNetworkFormat(Print &out_stream, AudioInfoSupport &bi)
Definition CodecNetworkFormat.h:27
size_t write(const uint8_t *data, size_t len)
Definition CodecNetworkFormat.h:41
Encoder which converts from the host format to the network format.
Definition CodecNetworkFormat.h:84
EncoderNetworkFormat(Print &out_stream, AudioInfoSupport &bi)
Definition CodecNetworkFormat.h:93
virtual void setOutput(Print &out_stream)
Default output assignment (encoders may override to store Print reference)
Definition CodecNetworkFormat.h:99
bool begin()
Definition CodecNetworkFormat.h:101
const char * mime()
Provides the mime type of the encoded result.
Definition CodecNetworkFormat.h:134
~EncoderNetworkFormat()
Definition CodecNetworkFormat.h:97
EncoderNetworkFormat()
Definition CodecNetworkFormat.h:86
void end()
Definition CodecNetworkFormat.h:103
Print * pt_print
Definition CodecNetworkFormat.h:137
EncoderNetworkFormat(Print &out_stream)
Definition CodecNetworkFormat.h:88
size_t write(const uint8_t *data, size_t len)
Definition CodecNetworkFormat.h:105
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6