Skip to content

Commit be1207e

Browse files
committed
CodecNetworkFormat
1 parent 4db016d commit be1207e

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed

src/AudioTools/AudioCodecs/AudioCodecs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
#include "AudioTools/AudioCodecs/MultiDecoder.h"
2929
#include "AudioTools/AudioCodecs/CodecMTS.h"
3030
#include "AudioTools/AudioCodecs/CodecADTS.h"
31-
31+
#include "AudioTools/AudioCodecs/CodecNetworkFormat.h"
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#pragma once
2+
3+
#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4+
#include "AudioTools/CoreAudio/AudioBasic/Net.h"
5+
#if defined(ARDUINO) && !defined(IS_MIN_DESKTOP)
6+
#include "Print.h"
7+
#endif
8+
9+
namespace audio_tools {
10+
11+
/**
12+
* @brief PCM decoder which converts from network format to the host format.
13+
* @ingroup codecs
14+
* @ingroup decoder
15+
* @author Phil Schatzmann
16+
* @copyright GPLv3
17+
*/
18+
class DecoderNetworkFormat : public AudioDecoder {
19+
public:
20+
DecoderNetworkFormat() = default;
21+
22+
DecoderNetworkFormat(Print &out_stream) {
23+
TRACED();
24+
pt_print = &out_stream;
25+
}
26+
27+
DecoderNetworkFormat(Print &out_stream, AudioInfoSupport &bi) {
28+
pt_print = &out_stream;
29+
}
30+
31+
~DecoderNetworkFormat() {}
32+
33+
virtual void setOutput(Print &out_stream) { pt_print = &out_stream; }
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+
68+
/// The result is encoded data - by default this is false
69+
virtual bool isResultPCM() { return true; }
70+
71+
protected:
72+
Print *pt_print = nullptr;
73+
};
74+
75+
/**
76+
* @brief Encoder which converts from the host format to the network format.
77+
* @ingroup codecs
78+
* @ingroup encoder
79+
* @author Phil Schatzmann
80+
* @copyright GPLv3
81+
*/
82+
class EncoderNetworkFormat : public AudioEncoder {
83+
public:
84+
EncoderNetworkFormat() { TRACED(); }
85+
86+
EncoderNetworkFormat(Print &out_stream) {
87+
TRACED();
88+
pt_print = &out_stream;
89+
}
90+
91+
EncoderNetworkFormat(Print &out_stream, AudioInfoSupport &bi) {
92+
pt_print = &out_stream;
93+
}
94+
95+
~EncoderNetworkFormat() {}
96+
97+
virtual void setOutput(Print &out_stream) { pt_print = &out_stream; }
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

src/AudioTools/CoreAudio/AudioBasic/Net.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
34
#ifndef htons
45
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
56
# define htonl(x) (((x) << 24 & 0xFF000000UL) | ((x) << 8 & 0x00FF0000UL) | ((x) >> 8 & 0x0000FF00UL) | ((x) >> 24 & 0x000000FFUL))
@@ -15,3 +16,12 @@
1516
#error Could not determine byte order
1617
#endif
1718
#endif
19+
20+
/// support for 64 bytes
21+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
22+
# define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
23+
# define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
24+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
25+
# define htonll(x) x
26+
# define ntohll(x) x
27+
#endif

0 commit comments

Comments
 (0)