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
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 value = in << 8;
68 }
69 }
70
72 value = other.value;
73 return *this;
74 }
75
77 set(((int32_t)other));
78 return *this;
79 }
80
81 operator int() {
82 return toInt();
83 }
84
85 explicit operator float() {
86 return toInt();
87 }
88
89 explicit operator int64_t() {
90 return toInt();
91 }
92
94 int32_t temp = toInt();
95 temp += valueA;
96 set(temp);
97 return *this;
98 }
99
101 int32_t temp = toInt();
102 temp -= valueA;
103 set(temp);
104 return *this;
105 }
106
108 int toInt() const {
109 return value >> 8;
110 }
111
113 float toFloat() const { return (float)toInt(); }
114
116 int16_t scale16() const {
117 return static_cast<int16_t>(toInt() >> 8) ;
118 }
119
121 int32_t scale32() const {
122 return static_cast<int32_t>(toInt() << 8);
123 }
124
126 float scaleFloat() const { return toFloat() / INT24_MAX; }
127
129 value = i16;
130 value = value << 16;
131 }
132
134 return static_cast<int16_t>(value >> 16);
135 }
136
137 private:
138 // store 24 bit value shifted by 1 byte to the left
139 int32_t value;
140};
141
142
143} // namespace audio_tools
144
145#ifdef USE_TYPETRAITS
146
147namespace std {
148 template<> class numeric_limits<audio_tools::int24_4bytes_t> {
149 public:
150 static audio_tools::int24_4bytes_t lowest() {return audio_tools::int24_4bytes_t(-0x7FFFFF);};
151 static audio_tools::int24_4bytes_t min() {return audio_tools::int24_4bytes_t(-0x7FFFFF);};
152 static audio_tools::int24_4bytes_t max() {return audio_tools::int24_4bytes_t(0x7FFFFF);};
153 };
154}
155
156#endif
#define INT24_MAX
Definition Int24_3bytes_t.h:9
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:128
int24_4bytes_t & operator-=(int32_t valueA)
Definition Int24_4bytes_t.h:100
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:71
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:76
int16_t getAndScale16()
Definition Int24_4bytes_t.h:133
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:93
int32_t scale32() const
provides value between -2,147,483,647 and 2,147,483,647
Definition Int24_4bytes_t.h:121
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:108
int16_t scale16() const
provides value between -32767 and 32767
Definition Int24_4bytes_t.h:116
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:126
float toFloat() const
convert to float
Definition Int24_4bytes_t.h:113
int24_4bytes_t()
Definition Int24_4bytes_t.h:24
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