Audacity  3.0.3
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 
21 enum DitherType : unsigned;
24 
25 // ----------------------------------------------------------------------------
26 // Supported sample formats
27 // ----------------------------------------------------------------------------
28 enum 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 // ----------------------------------------------------------------------------
49 using samplePtr = char *;
50 using constSamplePtr = const char *;
51 
52 // Used to determine how to fill in empty areas of audio.
53 typedef enum {
54  fillZero = 0,
55  fillTwo = 2
57 
59 #define SAMPLE_SIZE_DISK(SampleFormat) (((SampleFormat) == int24Sample) ? \
60  size_t{ 3 } : SAMPLE_SIZE(SampleFormat) )
61 
62 class TranslatableString;
64 
65 //
66 // Allocating/Freeing Samples
67 //
68 
69 class SampleBuffer {
70 
71 public:
73  : mPtr(0)
74  {}
76  : mPtr((samplePtr)malloc(count * SAMPLE_SIZE(format)))
77  {}
79  {
80  Free();
81  }
82 
83  // WARNING! May not preserve contents.
85  {
86  Free();
87  mPtr = (samplePtr)malloc(count * SAMPLE_SIZE(format));
88  return *this;
89  }
90 
91 
92  void Free()
93  {
94  free(mPtr);
95  mPtr = 0;
96  }
97 
98  samplePtr ptr() const { return mPtr; }
99 
100 
101 private:
103 };
104 
106 {
107 public:
109  : SampleBuffer()
110  , mCount(0)
111  {}
112 
114  : SampleBuffer(count, format)
115  , mCount(count)
116  {}
117 
119  {
120  if (!ptr() || mCount < count) {
121  Allocate(count, format);
122  mCount = count;
123  }
124  return *this;
125  }
126 
127  void Free()
128  {
130  mCount = 0;
131  }
132 
133  using SampleBuffer::ptr;
134 
135 private:
136  size_t mCount;
137 };
138 
139 //
140 // Copying, Converting and Clearing Samples
141 //
142 
143 MATH_API
145 
153 void SamplesToFloats(constSamplePtr src, sampleFormat srcFormat,
154  float *dst, size_t len, size_t srcStride = 1, size_t dstStride = 1);
155 
156 MATH_API
158 
163 void CopySamples(constSamplePtr src, sampleFormat srcFormat,
164  samplePtr dst, sampleFormat dstFormat, size_t len,
165  DitherType ditherType = gHighQualityDither,
166  unsigned int srcStride=1, unsigned int dstStride=1);
167 
168 MATH_API
170  size_t start, size_t len);
171 
172 MATH_API
174  int start, int len);
175 
176 //
177 // This must be called on startup and everytime NEW ditherers
178 // are set in preferences.
179 //
180 
181 MATH_API
182 void InitDitherers();
183 
184 // These are so commonly done for processing samples in floating point form in memory,
185 // let's have abbreviations.
189 
190 #endif
narrowestSampleFormat
@ narrowestSampleFormat
Two synonyms for previous values that might change if more values were added.
Definition: SampleFormat.h:37
GrowableSampleBuffer::Resize
GrowableSampleBuffer & Resize(size_t count, sampleFormat format)
Definition: SampleFormat.h:118
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
SampleBuffer::Allocate
SampleBuffer & Allocate(size_t count, sampleFormat format)
Definition: SampleFormat.h:84
GrowableSampleBuffer::Free
void Free()
Definition: SampleFormat.h:127
GrowableSampleBuffer::ptr
samplePtr ptr() const
Definition: SampleFormat.h:98
GetSampleFormatStr
MATH_API TranslatableString GetSampleFormatStr(sampleFormat format)
Definition: SampleFormat.cpp:60
fillFormat
fillFormat
Definition: SampleFormat.h:53
SampleBuffer::SampleBuffer
SampleBuffer()
Definition: SampleFormat.h:72
fillTwo
@ fillTwo
Definition: SampleFormat.h:55
SAMPLE_SIZE
#define SAMPLE_SIZE(SampleFormat)
Definition: SampleFormat.h:44
int24Sample
@ int24Sample
Definition: SampleFormat.h:33
GrowableSampleBuffer::GrowableSampleBuffer
GrowableSampleBuffer()
Definition: SampleFormat.h:108
fillZero
@ fillZero
Definition: SampleFormat.h:54
ClearSamples
MATH_API void ClearSamples(samplePtr buffer, sampleFormat format, size_t start, size_t len)
Definition: SampleFormat.cpp:77
SampleBuffer::Free
void Free()
Definition: SampleFormat.h:92
floatSample
@ floatSample
Definition: SampleFormat.h:34
ReverseSamples
MATH_API void ReverseSamples(samplePtr buffer, sampleFormat format, int start, int len)
Definition: SampleFormat.cpp:84
GrowableSampleBuffer::GrowableSampleBuffer
GrowableSampleBuffer(size_t count, sampleFormat format)
Definition: SampleFormat.h:113
widestSampleFormat
@ widestSampleFormat
Definition: SampleFormat.h:38
ArraysOf< float >
SamplesToFloats
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.
Definition: SampleFormat.cpp:102
int16Sample
@ int16Sample
Definition: SampleFormat.h:32
constSamplePtr
const char * constSamplePtr
Definition: SampleFormat.h:50
GrowableSampleBuffer::mCount
size_t mCount
Definition: SampleFormat.h:136
GrowableSampleBuffer
Definition: SampleFormat.h:106
format
int format
Definition: ExportPCM.cpp:56
SampleBuffer::SampleBuffer
SampleBuffer(size_t count, sampleFormat format)
Definition: SampleFormat.h:75
SampleBuffer
Definition: SampleFormat.h:69
sampleFormat
sampleFormat
Definition: SampleFormat.h:29
samplePtr
char * samplePtr
Definition: SampleFormat.h:49
DitherType
DitherType
These ditherers are currently available:
Definition: Dither.h:19
InitDitherers
MATH_API void InitDitherers()
Definition: SampleFormat.cpp:53
CopySamples
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.
Definition: SampleFormat.cpp:111
MemoryX.h
gLowQualityDither
DitherType gLowQualityDither
Definition: SampleFormat.cpp:49
gHighQualityDither
DitherType gHighQualityDither
Definition: SampleFormat.cpp:50
SampleBuffer::mPtr
samplePtr mPtr
Definition: SampleFormat.h:102
ArrayOf< float >
SampleBuffer::~SampleBuffer
~SampleBuffer()
Definition: SampleFormat.h:78
SampleBuffer::ptr
samplePtr ptr() const
Definition: SampleFormat.h:98