Audacity 3.2.0
SampleFormat.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file SampleFormat.h
6
7 Dominic Mazzoni
8
9**********************************************************************/
10
11#ifndef __AUDACITY_SAMPLE_FORMAT__
12#define __AUDACITY_SAMPLE_FORMAT__
13
14#include "MemoryX.h"
15#include <cstdlib>
16
17//
18// Definitions / Meta-Information
19//
20
21enum DitherType : unsigned;
24
25// ----------------------------------------------------------------------------
26// Supported sample formats
27// ----------------------------------------------------------------------------
29
30enum class sampleFormat : unsigned {
32 int16Sample = 0x00020001,
33 int24Sample = 0x00040001,
34 floatSample = 0x0004000F,
35
39};
40
41// C++20 using enum sampleFormat;
48
49// ----------------------------------------------------------------------------
50// Provide the number of bytes a specific sample will take
51// ----------------------------------------------------------------------------
52#define SAMPLE_SIZE(SampleFormat) (static_cast<unsigned>(SampleFormat) >> 16)
53
54// ----------------------------------------------------------------------------
55// Generic pointer to sample data
56// ----------------------------------------------------------------------------
57using samplePtr = char *;
58using constSamplePtr = const char *;
59
60// Used to determine how to fill in empty areas of audio.
61typedef enum class FillFormat {
62 fillZero = 0,
63 fillTwo = 2
65
67#define SAMPLE_SIZE_DISK(SampleFormat) (((SampleFormat) == int24Sample) ? \
68 size_t{ 3 } : SAMPLE_SIZE(SampleFormat) )
69
72
74
79class SampleFormats final {
80public:
83 sampleFormat effective,
84 sampleFormat stored
85 )
86 : m_Effective{ std::min( effective, stored ) }
87 , m_Stored{ stored }
88 {}
89
91 sampleFormat Stored() const { return m_Stored; }
92
94
99 {
100 if (effective > m_Effective)
101 m_Effective = std::min(effective, m_Stored);
102 }
103
104private:
107};
108
110{
111 return a.Effective() == b.Effective() &&
112 a.Stored() == b.Stored();
113}
114
116{
117 return !(a == b);
118}
119
120//
121// Allocating/Freeing Samples
122//
123
125
126public:
128 : mPtr(0)
129 {}
131 : mPtr((samplePtr)malloc(count * SAMPLE_SIZE(format)))
132 {}
134 {
135 Free();
136 }
138 {
139 mPtr = other.mPtr;
140 other.mPtr = nullptr;
141 }
143 {
144 auto ptr = other.mPtr;
145 other.mPtr = nullptr;
146 mPtr = ptr;
147 return *this;
148 }
149
150 // WARNING! May not preserve contents.
152 {
153 Free();
154 mPtr = (samplePtr)malloc(count * SAMPLE_SIZE(format));
155 return *this;
156 }
157
158
159 void Free()
160 {
161 free(mPtr);
162 mPtr = 0;
163 }
164
165 samplePtr ptr() const { return mPtr; }
166
167
168private:
170};
171
173{
174public:
176 : SampleBuffer()
177 , mCount(0)
178 {}
179
181 : SampleBuffer(count, format)
182 , mCount(count)
183 {}
184
186 {
187 if (!ptr() || mCount < count) {
188 Allocate(count, format);
189 mCount = count;
190 }
191 return *this;
192 }
193
194 void Free()
195 {
197 mCount = 0;
198 }
199
200 using SampleBuffer::ptr;
201
202private:
203 size_t mCount;
204};
205
206//
207// Copying, Converting and Clearing Samples
208//
209
210MATH_API
212
221 float *dst, size_t len, size_t srcStride = 1, size_t dstStride = 1);
222
223MATH_API
225
230void CopySamples(constSamplePtr src, sampleFormat srcFormat,
231 samplePtr dst, sampleFormat dstFormat, size_t len,
232 DitherType ditherType = gHighQualityDither,
233 unsigned int srcStride=1, unsigned int dstStride=1);
234
235MATH_API
237 size_t start, size_t len);
238
239MATH_API
241 int start, int len);
242
243//
244// This must be called on startup and everytime NEW ditherers
245// are set in preferences.
246//
247
248MATH_API
249void InitDitherers();
250
251// These are so commonly done for processing samples in floating point form in memory,
252// let's have abbreviations.
256
257#endif
int min(int a, int b)
DitherType
These ditherers are currently available:
Definition: Dither.h:19
constexpr sampleFormat int16Sample
Definition: SampleFormat.h:43
MATH_API DitherType gHighQualityDither
Definition: SampleFormat.h:23
MATH_API void InitDitherers()
MATH_API void ReverseSamples(samplePtr buffer, sampleFormat format, int start, int len)
MATH_API TranslatableString GetSampleFormatStr(sampleFormat format)
constexpr sampleFormat floatSample
Definition: SampleFormat.h:45
constexpr sampleFormat int24Sample
Definition: SampleFormat.h:44
bool operator==(SampleFormats a, SampleFormats b)
Definition: SampleFormat.h:109
MATH_API void ClearSamples(samplePtr buffer, sampleFormat format, size_t start, size_t len)
sampleFormat
The ordering of these values with operator < agrees with the order of increasing bit width.
Definition: SampleFormat.h:30
@ narrowestSampleFormat
Two synonyms for previous values that might change if more values were added.
char * samplePtr
Definition: SampleFormat.h:57
MATH_API DitherType gLowQualityDither
These global variables are assigned at application startup or after change of preferences.
#define SAMPLE_SIZE(SampleFormat)
Definition: SampleFormat.h:52
MATH_API void SamplesToFloats(constSamplePtr src, sampleFormat srcFormat, float *dst, size_t len, size_t srcStride=1, size_t dstStride=1)
Copy samples from any format into the widest format, which is 32 bit float, with no dithering.
const char * constSamplePtr
Definition: SampleFormat.h:58
MATH_API void CopySamples(constSamplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, size_t len, DitherType ditherType=gHighQualityDither, unsigned int srcStride=1, unsigned int dstStride=1)
Copy samples from any format to any other format; apply dithering only if narrowing the format.
constexpr sampleFormat undefinedSample
Definition: SampleFormat.h:42
constexpr sampleFormat widestSampleFormat
Definition: SampleFormat.h:47
bool operator!=(SampleFormats a, SampleFormats b)
Definition: SampleFormat.h:115
FillFormat
Definition: SampleFormat.h:61
enum FillFormat fillFormat
constexpr sampleFormat narrowestSampleFormat
Definition: SampleFormat.h:46
GrowableSampleBuffer & Resize(size_t count, sampleFormat format)
Definition: SampleFormat.h:185
samplePtr ptr() const
Definition: SampleFormat.h:165
GrowableSampleBuffer(size_t count, sampleFormat format)
Definition: SampleFormat.h:180
SampleBuffer & Allocate(size_t count, sampleFormat format)
Definition: SampleFormat.h:151
SampleBuffer(size_t count, sampleFormat format)
Definition: SampleFormat.h:130
SampleBuffer(SampleBuffer &&other)
Definition: SampleFormat.h:137
SampleBuffer & operator=(SampleBuffer &&other)
Definition: SampleFormat.h:142
samplePtr ptr() const
Definition: SampleFormat.h:165
samplePtr mPtr
Definition: SampleFormat.h:169
Two sample formats, remembering format of original source and describing stored format.
Definition: SampleFormat.h:79
sampleFormat m_Stored
Definition: SampleFormat.h:106
SampleFormats(sampleFormat effective, sampleFormat stored)
Definition: SampleFormat.h:82
sampleFormat m_Effective
Definition: SampleFormat.h:105
void UpdateEffective(sampleFormat effective)
Update the effective format, for insertion of more samples into the sequence.
Definition: SampleFormat.h:98
sampleFormat Stored() const
Definition: SampleFormat.h:91
sampleFormat Effective() const
Definition: SampleFormat.h:90
Holds a msgid for the translation catalog; may also bind format arguments.
void free(void *ptr)
Definition: VectorOps.h:34
STL namespace.