Audacity  2.2.2
SampleFormat.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  SampleFormat.h
6 
7  Dominic Mazzoni
8 
9 **********************************************************************/
10 
11 #ifndef __AUDACITY_SAMPLE_FORMAT__
12 #define __AUDACITY_SAMPLE_FORMAT__
13 
14 #include "Audacity.h"
15 #include "MemoryX.h"
16 #include <wx/defs.h>
17 
18 #include "audacity/Types.h"
19 
20 //
21 // Definitions / Meta-Information
22 //
23 
24 #if 0
25 // Moved to audacity/types.h
26 typedef enum {
27  int16Sample = 0x00020001,
28  int24Sample = 0x00040001,
29  floatSample = 0x0004000F
30 } sampleFormat;
31 
33 #define SAMPLE_SIZE(SampleFormat) ( size_t{ (SampleFormat) >> 16 } )
34 #endif
35 
36 // Used to determine how to fill in empty areas of audio.
37 typedef enum {
38  fillZero = 0,
39  fillTwo = 2
40 }fillFormat;
41 
43 #define SAMPLE_SIZE_DISK(SampleFormat) (((SampleFormat) == int24Sample) ? \
44  size_t{ 3 } : SAMPLE_SIZE(SampleFormat) )
45 
46 const wxChar *GetSampleFormatStr(sampleFormat format);
47 
48 //
49 // Allocating/Freeing Samples
50 //
51 
52 class SampleBuffer {
53 
54 public:
56  : mPtr(0)
57  {}
58  SampleBuffer(size_t count, sampleFormat format)
59  : mPtr((samplePtr)malloc(count * SAMPLE_SIZE(format)))
60  {}
62  {
63  Free();
64  }
65 
66  // WARNING! May not preserve contents.
67  SampleBuffer &Allocate(size_t count, sampleFormat format)
68  {
69  Free();
70  mPtr = (samplePtr)malloc(count * SAMPLE_SIZE(format));
71  return *this;
72  }
73 
74 
75  void Free()
76  {
77  free(mPtr);
78  mPtr = 0;
79  }
80 
81  samplePtr ptr() const { return mPtr; }
82 
83 
84 private:
85  samplePtr mPtr;
86 };
87 
89 {
90 public:
92  : SampleBuffer()
93  , mCount(0)
94  {}
95 
96  GrowableSampleBuffer(size_t count, sampleFormat format)
97  : SampleBuffer(count, format)
98  , mCount(count)
99  {}
100 
101  GrowableSampleBuffer &Resize(size_t count, sampleFormat format)
102  {
103  if (!ptr() || mCount < count) {
104  Allocate(count, format);
105  mCount = count;
106  }
107  return *this;
108  }
109 
110  void Free()
111  {
113  mCount = 0;
114  }
115 
116  using SampleBuffer::ptr;
117 
118 private:
119  size_t mCount;
120 };
121 
122 //
123 // Copying, Converting and Clearing Samples
124 //
125 
126 void CopySamples(samplePtr src, sampleFormat srcFormat,
127  samplePtr dst, sampleFormat dstFormat,
128  unsigned int len, bool highQuality=true,
129  unsigned int srcStride=1,
130  unsigned int dstStride=1);
131 
132 void CopySamplesNoDither(samplePtr src, sampleFormat srcFormat,
133  samplePtr dst, sampleFormat dstFormat,
134  unsigned int len,
135  unsigned int srcStride=1,
136  unsigned int dstStride=1);
137 
138 void ClearSamples(samplePtr buffer, sampleFormat format,
139  size_t start, size_t len);
140 
141 void ReverseSamples(samplePtr buffer, sampleFormat format,
142  int start, int len);
143 
144 //
145 // This must be called on startup and everytime NEW ditherers
146 // are set in preferences.
147 //
148 
149 void InitDitherers();
150 
151 // These are so commonly done for processing samples in floating point form in memory,
152 // let's have abbeviations.
156 
157 #endif
GrowableSampleBuffer & Resize(size_t count, sampleFormat format)
Definition: SampleFormat.h:101
void ReverseSamples(samplePtr buffer, sampleFormat format, int start, int len)
GrowableSampleBuffer(size_t count, sampleFormat format)
Definition: SampleFormat.h:96
fillFormat
Definition: SampleFormat.h:37
SampleBuffer & Allocate(size_t count, sampleFormat format)
Definition: SampleFormat.h:67
void CopySamplesNoDither(samplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, unsigned int len, unsigned int srcStride=1, unsigned int dstStride=1)
int format
Definition: ExportPCM.cpp:56
SampleBuffer(size_t count, sampleFormat format)
Definition: SampleFormat.h:58
samplePtr ptr() const
Definition: SampleFormat.h:81
void CopySamples(samplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, unsigned int len, bool highQuality=true, unsigned int srcStride=1, unsigned int dstStride=1)
void ClearSamples(samplePtr buffer, sampleFormat format, size_t start, size_t len)
const wxChar * GetSampleFormatStr(sampleFormat format)
samplePtr mPtr
Definition: SampleFormat.h:85
void InitDitherers()