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