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