Beatmup
sample_arithmetic.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "../basic_types.h"
21 #include "../utils/utils.hpp"
22 
23 namespace Beatmup {
24 
25  /**
26  Format of audio samples
27  */
29  Int8, //!< signed integer, 8 bit per sample
30  Int16, //!< signed integer, 16 bit per sample
31  Int32, //!< signed integer, 32 bit per sample
32  Float32 //!< floating point, 32 bit per sample
33  };
34 
35  const int AUDIO_SAMPLE_SIZE[] = { 1, 2, 3, 4 };
36  const char* const AUDIO_FORMAT_NAME[] = { "8 bit", "16 bit", "32 bit", "32 bit float" };
37 
38  struct sample8;
39  struct sample16;
40  struct sample32;
41  struct sample32f;
42 
43  struct sample8 {
44  signed char x;
45  inline bool operator < (const sample8& sample) const { return x < sample.x; }
46  operator sample16() const;
47  operator sample32() const;
48  operator sample32f() const;
49  void operator =(const sample16 S);
50  void operator =(const sample32 S);
51  void operator =(const sample32f S);
52  static const int
53  MIN_VALUE = -128,
54  MAX_VALUE = +127;
55  };
56 
57  struct sample16 {
58  signed short int x;
59  inline bool operator < (const sample16& sample) const { return x < sample.x; }
60  operator sample8() const;
61  operator sample32() const;
62  operator sample32f() const;
63  void operator =(const sample8 S);
64  void operator =(const sample32 S);
65  void operator =(const sample32f S);
66  static const int
67  MIN_VALUE = -32768,
68  MAX_VALUE = +32767;
69  };
70 
71  struct sample32 {
72  int x;
73  inline bool operator < (const sample32& sample) const { return x < sample.x; }
74  operator sample8() const;
75  operator sample16() const;
76  operator sample32f() const;
77  void operator =(const sample8 S);
78  void operator =(const sample16 S);
79  void operator =(const sample32f S);
80  static const long
81  MIN_VALUE = -2147483647-1,
82  MAX_VALUE = +2147483647;
83  };
84 
85  struct sample32f {
86  float x;
87  inline bool operator < (const sample32f& sample) const { return x < sample.x; }
88  operator sample8() const;
89  operator sample16() const;
90  operator sample32() const;
91  void operator =(const sample8 S);
92  void operator =(const sample16 S);
93  void operator =(const sample32 S);
94  static const float
97  };
98 
99  //////////////////////////////////////////////////////////////
100  // 8 bit integer //
101  //////////////////////////////////////////////////////////////
102 
103  inline sample8::operator sample16() const {
104  return sample16{ (decltype(sample16::x)) (x << 8) };
105  }
106 
107  inline sample8::operator sample32() const {
108  return sample32{ x << 2 };
109  }
110 
111  inline sample8::operator sample32f() const {
112  return sample32f{ x / 127.0f };
113  }
114 
115  inline void sample8::operator =(const sample16 S) {
116  x = (decltype(x))( S.x >> 8 );
117  }
118 
119  inline void sample8::operator =(const sample32 S) {
120  x = (decltype(x))( S.x >> 24 );
121  }
122 
123  inline void sample8::operator =(const sample32f S) {
124  x = (decltype(x)) roundf_fast(S.x * 127);
125  }
126 
127 
128  //////////////////////////////////////////////////////////////
129  // 16 bit integer //
130  //////////////////////////////////////////////////////////////
131 
132  inline sample16::operator sample8() const {
133  return sample8{ (decltype(sample8::x))( x >> 8 ) };
134  }
135 
136  inline sample16::operator sample32() const {
137  return sample32{ x << 16 };
138  }
139 
140  inline sample16::operator sample32f() const {
141  return sample32f{ x / 32767.0f };
142  }
143 
144  inline void sample16::operator =(const sample8 S) {
145  x = S.x << 8;
146  }
147 
148  inline void sample16::operator =(const sample32 S) {
149  x = (decltype(x))( S.x >> 8 );
150  }
151 
152  inline void sample16::operator =(const sample32f S) {
153  x = (decltype(x)) roundf_fast(S.x * 32767);
154  }
155 
156  //////////////////////////////////////////////////////////////
157  // 32 bit integer //
158  //////////////////////////////////////////////////////////////
159 
160  inline sample32::operator sample8() const {
161  return sample8{ (decltype(sample8::x)) (x >> 24) };
162  }
163 
164  inline sample32::operator sample16() const {
165  return sample16{ (decltype(sample16::x)) (x >> 16) };
166  }
167 
168  inline sample32::operator sample32f() const {
169  return sample32f{ x / 2147483647.0f };
170  }
171 
172  inline void sample32::operator =(const sample8 S) {
173  x = S.x << 24;
174  }
175 
176  inline void sample32::operator =(const sample16 S) {
177  x = S.x << 16;
178  }
179 
180  inline void sample32::operator =(const sample32f S) {
181  x = roundf_fast(S.x * 2147483647);
182  }
183 
184  //////////////////////////////////////////////////////////////
185  // 32 bit float //
186  //////////////////////////////////////////////////////////////
187 
188  inline sample32f::operator sample8() const {
189  return sample8{ (decltype(sample8::x)) roundf_fast(x *127) };
190  }
191 
192  inline sample32f::operator sample16() const {
193  return sample16{ (decltype(sample16::x)) roundf_fast(x * 32767) };
194  }
195 
196  inline sample32f::operator sample32() const {
197  return sample32{ roundf_fast(x * 2147483647) };
198  }
199 
200  inline void sample32f::operator =(const sample8 S) {
201  x = S.x / 128.0f;
202  }
203 
204  inline void sample32f::operator =(const sample16 S) {
205  x = S.x / 32768.0f;
206  }
207 
208  inline void sample32f::operator =(const sample32 S) {
209  x = S.x / 2147483648.0f;
210  }
211 
212  //////////////////////////////////////////////////////////////
213  // Miscellaneous //
214  //////////////////////////////////////////////////////////////
215 
216  inline bool operator!=(const sample16& A, const sample16& B) {
217  return A.x != B.x;
218  }
219 
220 
221  template<typename in, typename out> void convertSamples(in* const input, void* output, msize count) {
222  const in* stop = input + count;
223  out* po = (out*)output;
224  for (const in* pi = input; pi < stop; pi++, po++)
225  *po = *pi;
226  }
227 }
const char *const AUDIO_FORMAT_NAME[]
uint32_t msize
memory size
Definition: basic_types.h:30
const int B
Definition: bitmap_access.h:31
const float pi
Definition: basic_types.h:29
const int A
Definition: bitmap_access.h:31
const int AUDIO_SAMPLE_SIZE[]
bool operator!=(const sample16 &A, const sample16 &B)
AudioSampleFormat
Format of audio samples.
@ Int8
signed integer, 8 bit per sample
@ Int32
signed integer, 32 bit per sample
@ Float32
floating point, 32 bit per sample
@ Int16
signed integer, 16 bit per sample
void convertSamples(in *const input, void *output, msize count)
void operator=(const sample8 S)
static const int MAX_VALUE
static const int MIN_VALUE
signed short int x
bool operator<(const sample16 &sample) const
bool operator<(const sample32 &sample) const
static const long MIN_VALUE
void operator=(const sample8 S)
static const long MAX_VALUE
bool operator<(const sample32f &sample) const
static const float MAX_VALUE
void operator=(const sample8 S)
static const float MIN_VALUE
static const int MIN_VALUE
void operator=(const sample16 S)
static const int MAX_VALUE
bool operator<(const sample8 &sample) const
#define roundf_fast(X)
rounding (nearest integer)
Definition: utils.hpp:22
JNIEnv jlong jint jint count
jobject jlong jint x
JNIEnv jlong jint out