arduino-audio-tools
Loading...
Searching...
No Matches
Int24_3bytes_t.h
Go to the documentation of this file.
1#pragma once
2#include "AudioToolsConfig.h"
3#include <string.h> // std::memcpy
4#include <stdint.h> // std::int32_t
5#ifdef USE_TYPETRAITS
6# include <limits>
7#endif
8
9#define INT24_MAX 0x7FFFFF
10
11namespace audio_tools {
12
22 public:
24 value[0] = 0;
25 value[1] = 0;
26 value[2] = 0;
27 }
28
29 int24_3bytes_t(void *ptr) {
30 memcpy(value, ptr, 3);
31 }
32
34 value[2] = in > 0 ? 0 : 0xFF;
35 value[1] = (in >> 8) & 0xFF;
36 value[0] = in & 0xFF;
37 }
38
40 set(in);
41 }
42
44 set(in);
45 }
46
47 int24_3bytes_t(const float in) {
48 set((int32_t)in);
49 }
50
51#if defined(USE_INT24_FROM_INT)
52
53 explicit int24_3bytes_t(const int &in) {
54 set(in);
55 }
56
57#endif
58
59 void set(const int32_t &in) {
60 value[2] = (in >> 16) & 0xFF;
61 value[1] = (in >> 8) & 0xFF;
62 value[0] = in & 0xFF;
63 }
64
66 set(other);
67 return *this;
68 }
69
72 return *this;
73 }
74
75 operator int() const {
76 return toInt();
77 }
78
80 int32_t temp = toInt();
81 temp += value;
82 set(temp);
83 return *this;
84 }
85
87 int32_t temp = toInt();
88 temp -= value;
89 set(temp);
90 return *this;
91 }
92
94 int toInt() const {
95 int newInt = ((((int32_t)0xFF & value[0]) << 16) | (((int32_t)0xFF & value[1]) << 8) | ((int32_t)0xFF & value[2]));
96 if ((newInt & 0x00800000) > 0) {
97 newInt |= 0xFF000000;
98 } else {
99 newInt &= 0x00FFFFFF;
100 }
101 return newInt;
102 }
103
105 float toFloat() const { return int(); }
106
108 int16_t scale16() const {
109 return toInt() * INT16_MAX / INT24_MAX;
110 }
111
113 int32_t scale32() const {
114 return toInt() * (INT32_MAX / INT24_MAX);
115 }
116
118 float scaleFloat() const { return toFloat() / INT24_MAX; }
119
120
122 value[0] = 0; // clear trailing byte
123 int16_t *p16 = (int16_t *)&value[1];
124 *p16 = i16;
125 }
127 int16_t *p16 = (int16_t *)&value[1];
128 return *p16;
129 }
130
131
132 private:
133 uint8_t value[3];
134};
135
136
137} // namespace audio_tools
138
139#ifdef USE_TYPETRAITS
140
141namespace std {
142 template<> class numeric_limits<audio_tools::int24_3bytes_t> {
143 public:
144 static audio_tools::int24_3bytes_t lowest() {return audio_tools::int24_3bytes_t(-0x7FFFFF);};
145 static audio_tools::int24_3bytes_t min() {return audio_tools::int24_3bytes_t(-0x7FFFFF);};
146 static audio_tools::int24_3bytes_t max() {return audio_tools::int24_3bytes_t(0x7FFFFF);};
147 };
148}
149
150#endif
#define INT24_MAX
Definition Int24_3bytes_t.h:9
24bit integer which is used for I2S sound processing. The values are really using 3 bytes....
Definition Int24_3bytes_t.h:21
void setAndScale16(int16_t i16)
Definition Int24_3bytes_t.h:121
int24_3bytes_t & operator=(const int24_3bytes_t &other)
Definition Int24_3bytes_t.h:65
int24_3bytes_t()
Definition Int24_3bytes_t.h:23
int24_3bytes_t(const float in)
Definition Int24_3bytes_t.h:47
int24_3bytes_t & operator+=(int32_t value)
Definition Int24_3bytes_t.h:79
int16_t getAndScale16()
Definition Int24_3bytes_t.h:126
int24_3bytes_t(const int16_t &in)
Definition Int24_3bytes_t.h:33
int24_3bytes_t(const int32_t &in)
Definition Int24_3bytes_t.h:39
int32_t scale32() const
provides value between -2,147,483,647 and 2,147,483,647
Definition Int24_3bytes_t.h:113
void set(const int32_t &in)
Definition Int24_3bytes_t.h:59
int toInt() const
Standard Conversion to Int.
Definition Int24_3bytes_t.h:94
int24_3bytes_t(const int64_t &in)
Definition Int24_3bytes_t.h:43
int24_3bytes_t & operator-=(int32_t value)
Definition Int24_3bytes_t.h:86
int16_t scale16() const
provides value between -32767 and 32767
Definition Int24_3bytes_t.h:108
int24_3bytes_t & operator=(const float &other)
Definition Int24_3bytes_t.h:70
float scaleFloat() const
provides value between -1.0 and 1.0
Definition Int24_3bytes_t.h:118
float toFloat() const
convert to float
Definition Int24_3bytes_t.h:105
int24_3bytes_t(void *ptr)
Definition Int24_3bytes_t.h:29
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:508
Definition FFTReal.h:488