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// ----------------------------------------------------------------------------
28enum sampleFormat : unsigned
29{
32 int16Sample = 0x00020001,
33 int24Sample = 0x00040001,
34 floatSample = 0x0004000F,
35
39};
40
41// ----------------------------------------------------------------------------
42// Provide the number of bytes a specific sample will take
43// ----------------------------------------------------------------------------
44#define SAMPLE_SIZE(SampleFormat) (SampleFormat >> 16)
45
46// ----------------------------------------------------------------------------
47// Generic pointer to sample data
48// ----------------------------------------------------------------------------
49using samplePtr = char *;
50using constSamplePtr = const char *;
51
52// Used to determine how to fill in empty areas of audio.
53typedef enum {
55 fillTwo = 2
57
59#define SAMPLE_SIZE_DISK(SampleFormat) (((SampleFormat) == int24Sample) ? \
60 size_t{ 3 } : SAMPLE_SIZE(SampleFormat) )
61
64
65//
66// Allocating/Freeing Samples
67//
68
70
71public:
73 : mPtr(0)
74 {}
76 : mPtr((samplePtr)malloc(count * SAMPLE_SIZE(format)))
77 {}
79 {
80 Free();
81 }
83 {
84 mPtr = other.mPtr;
85 other.mPtr = nullptr;
86 }
88 {
89 auto ptr = other.mPtr;
90 other.mPtr = nullptr;
91 mPtr = ptr;
92 return *this;
93 }
94
95 // WARNING! May not preserve contents.
97 {
98 Free();
99 mPtr = (samplePtr)malloc(count * SAMPLE_SIZE(format));
100 return *this;
101 }
102
103
104 void Free()
105 {
106 free(mPtr);
107 mPtr = 0;
108 }
109
110 samplePtr ptr() const { return mPtr; }
111
112
113private:
115};
116
118{
119public:
121 : SampleBuffer()
122 , mCount(0)
123 {}
124
126 : SampleBuffer(count, format)
127 , mCount(count)
128 {}
129
131 {
132 if (!ptr() || mCount < count) {
133 Allocate(count, format);
134 mCount = count;
135 }
136 return *this;
137 }
138
139 void Free()
140 {
142 mCount = 0;
143 }
144
145 using SampleBuffer::ptr;
146
147private:
148 size_t mCount;
149};
150
151//
152// Copying, Converting and Clearing Samples
153//
154
155MATH_API
157
166 float *dst, size_t len, size_t srcStride = 1, size_t dstStride = 1);
167
168MATH_API
170
175void CopySamples(constSamplePtr src, sampleFormat srcFormat,
176 samplePtr dst, sampleFormat dstFormat, size_t len,
177 DitherType ditherType = gHighQualityDither,
178 unsigned int srcStride=1, unsigned int dstStride=1);
179
180MATH_API
182 size_t start, size_t len);
183
184MATH_API
186 int start, int len);
187
188//
189// This must be called on startup and everytime NEW ditherers
190// are set in preferences.
191//
192
193MATH_API
194void InitDitherers();
195
196// These are so commonly done for processing samples in floating point form in memory,
197// let's have abbreviations.
201
202#endif
DitherType
These ditherers are currently available:
Definition: Dither.h:19
int format
Definition: ExportPCM.cpp:56
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)
MATH_API void ClearSamples(samplePtr buffer, sampleFormat format, size_t start, size_t len)
sampleFormat
Definition: SampleFormat.h:29
@ narrowestSampleFormat
Two synonyms for previous values that might change if more values were added.
Definition: SampleFormat.h:37
@ widestSampleFormat
Definition: SampleFormat.h:38
@ floatSample
Definition: SampleFormat.h:34
@ int16Sample
Definition: SampleFormat.h:32
@ int24Sample
Definition: SampleFormat.h:33
char * samplePtr
Definition: SampleFormat.h:49
MATH_API DitherType gLowQualityDither
These global variables are assigned at application startup or after change of preferences.
#define SAMPLE_SIZE(SampleFormat)
Definition: SampleFormat.h:44
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:50
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.
fillFormat
Definition: SampleFormat.h:53
@ fillZero
Definition: SampleFormat.h:54
@ fillTwo
Definition: SampleFormat.h:55
GrowableSampleBuffer & Resize(size_t count, sampleFormat format)
Definition: SampleFormat.h:130
samplePtr ptr() const
Definition: SampleFormat.h:110
GrowableSampleBuffer(size_t count, sampleFormat format)
Definition: SampleFormat.h:125
SampleBuffer & Allocate(size_t count, sampleFormat format)
Definition: SampleFormat.h:96
SampleBuffer(size_t count, sampleFormat format)
Definition: SampleFormat.h:75
SampleBuffer(SampleBuffer &&other)
Definition: SampleFormat.h:82
SampleBuffer & operator=(SampleBuffer &&other)
Definition: SampleFormat.h:87
samplePtr ptr() const
Definition: SampleFormat.h:110
samplePtr mPtr
Definition: SampleFormat.h:114
Holds a msgid for the translation catalog; may also bind format arguments.