arduino-audio-tools
Int24_4bytes_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_4bytes_t() {
19  value = 0;
20  }
21 
22  int24_4bytes_t(void *ptr) {
23  memcpy(&value, ptr, 4);
24  }
25 
26  int24_4bytes_t(const int16_t in) {
27  set(in) ;
28  }
29 
30  int24_4bytes_t(const int32_t in) {
31  set(in);
32  }
33 
34  int24_4bytes_t(const int64_t in) {
35  set((int32_t)in) ;
36  }
37 
38  int24_4bytes_t(const int24_4bytes_t &in) {
39  value = in.value;
40  }
41 
42  int24_4bytes_t(const float in) {
43  set((int32_t)in);
44  }
45 
46 #if defined(USE_INT24_FROM_INT)
47 
48  int24_4bytes_t(const int in) {
49  set(in);
50  }
51 
52 #endif
53 
55  inline void set(const int32_t &in) {
56  if (in>INT24_MAX){
57  value = INT24_MAX << 8;
58  } else if (in<-INT24_MAX){
59  value = -(INT24_MAX << 8);
60  } else {
61  value = in << 8;
62  }
63  }
64 
65  int24_4bytes_t& operator=(const int24_4bytes_t& other){
66  value = other.value;
67  return *this;
68  }
69 
70  int24_4bytes_t& operator=(const float& other){
71  set(((int32_t)other));
72  return *this;
73  }
74 
75  operator int() {
76  return toInt();
77  }
78 
79  explicit operator float() {
80  return toInt();
81  }
82 
83  explicit operator int64_t() {
84  return toInt();
85  }
86 
87  int24_4bytes_t& operator +=(int32_t valueA){
88  int32_t temp = toInt();
89  temp += valueA;
90  set(temp);
91  return *this;
92  }
93 
94  int24_4bytes_t& operator -=(int32_t valueA){
95  int32_t temp = toInt();
96  temp -= valueA;
97  set(temp);
98  return *this;
99  }
100 
102  int toInt() const {
103  return value >> 8;
104  }
105 
107  float toFloat() const { return (float)toInt(); }
108 
110  int16_t scale16() const {
111  return static_cast<int16_t>(toInt() >> 8) ;
112  }
113 
115  int32_t scale32() const {
116  return static_cast<int32_t>(toInt() << 8);
117  }
118 
120  float scaleFloat() const { return toFloat() / INT24_MAX; }
121 
122  void setAndScale16(int16_t i16) {
123  value = i16;
124  value = value << 16;
125  }
126 
127  int16_t getAndScale16() {
128  return static_cast<int16_t>(value >> 16);
129  }
130 
131  private:
132  // store 24 bit value shifted by 1 byte to the left
133  int32_t value;
134 };
135 
136 
137 } // namespace audio_tools
138 
139 #ifdef USE_TYPETRAITS
140 
141 namespace std {
142  template<> class numeric_limits<audio_tools::int24_4bytes_t> {
143  public:
144  static audio_tools::int24_4bytes_t lowest() {return audio_tools::int24_4bytes_t(-0x7FFFFF);};
145  static audio_tools::int24_4bytes_t min() {return audio_tools::int24_4bytes_t(-0x7FFFFF);};
146  static audio_tools::int24_4bytes_t max() {return audio_tools::int24_4bytes_t(0x7FFFFF);};
147  };
148 }
149 
150 #endif
24bit integer which is used for I2S sound processing. The values are represented as int32_t,...
Definition: Int24_4bytes_t.h:16
int32_t scale32() const
provides value between -2,147,483,647 and 2,147,483,647
Definition: Int24_4bytes_t.h:115
void set(const int32_t &in)
values are clipped and shifted by 1 byte
Definition: Int24_4bytes_t.h:55
int toInt() const
Standard Conversion to Int.
Definition: Int24_4bytes_t.h:102
int16_t scale16() const
provides value between -32767 and 32767
Definition: Int24_4bytes_t.h:110
float scaleFloat() const
provides value between -1.0 and 1.0
Definition: Int24_4bytes_t.h:120
float toFloat() const
convert to float
Definition: Int24_4bytes_t.h:107
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823