Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWaveFormat.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cWaveFormat_H
7 #define _INC_cWaveFormat_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cSoundBase.h"
14 #ifdef _WIN32
15 #include <mmreg.h> // WAVE_FORMAT_ADPCM
16 #endif
17 
18 #ifdef __GNUC__
19 enum WAVE_FORMAT_TYPE_
20 {
21 #ifdef __linux__
22  WAVE_FORMAT_PCM = 1, // WORD wFormatTag
23  WAVE_FORMAT_ADPCM = 2,
24  WAVE_FORMAT_ALAW = 6, // telephone
25  WAVE_FORMAT_MULAW = 7,
26  WAVE_FORMAT_DIGIADPCM = 0x0036, // big endian PCM.
27  WAVE_FORMAT_MPEG = 0x0050,
28  WAVE_FORMAT_MPEGLAYER3 = 0x0055, // ISO/MPEG Layer3 Format Tag
29 #endif
30 };
31 #endif
32 
33 #ifndef _WIN32
34 typedef struct waveformat_tag
35 {
36  // Old fashioned define for WAVEFORMAT.
37  WORD wFormatTag; // format type
38  WORD nChannels; // number of channels (i.e. mono, stereo, etc.)
39  DWORD nSamplesPerSec; // sample rate
40  DWORD nAvgBytesPerSec; // for buffer estimation
41  WORD nBlockAlign; // block size of data
43 typedef struct pcmwaveformat_tag
44 {
48 #endif
49 
50 #if ! defined(_WAVEFORMATEX_) && ( ! defined(_WIN32) || (WINVER < 0x0400))
51 #define _WAVEFORMATEX_
52 typedef struct tWAVEFORMATEX
53 {
54  WORD wFormatTag;
55  WORD nChannels;
56  UINT32 nSamplesPerSec;
57  UINT32 nAvgBytesPerSec;
58  WORD nBlockAlign;
60  WORD cbSize;
61  // extra information (after cbSize)
63 #endif // _WAVEFORMATEX_
64 
65 namespace GrayLib
66 {
67  class cRIFFReader;
68  typedef UINT WAVE_BLOCKS_t;
69 
70  //***********************************************************************
71 
73  {
77 
78  public:
79  void Init() noexcept
80  {
81  cMem::Zero(this, sizeof(WAVEFORMATEX));
82  }
83  void Init(WORD wFormatTag2, WORD nChannels2 = 1, UINT nSamplesPerSec2 = 8000, WORD wBitsPerSample2 = 8, WORD wSizeEx2 = 0) noexcept
84  {
86  this->wFormatTag = wFormatTag2;
87  this->nChannels = nChannels2; // number of channels (i.e. mono, stereo...)
88  this->nSamplesPerSec = nSamplesPerSec2; // sample rate
89  this->wBitsPerSample = wBitsPerSample2; // Number of bits per sample of mono data
90  this->nAvgBytesPerSec = 0; // for buffer estimation. set/calculated later.
91  this->nBlockAlign = 0; // block size of data. set/calculated later.
92  this->cbSize = wSizeEx2; // the count in bytes of the size of extra stuff
93  // ASSUME ReCalc is called.
94  }
95  bool isPCM() const noexcept
96  {
97  return(this->wFormatTag == WAVE_FORMAT_PCM);
98  }
99  WORD get_Channels() const noexcept
100  {
101  return(this->nChannels);
102  }
103  size_t get_BlockSize() const noexcept
104  {
106  return(this->nBlockAlign);
107  }
108  UINT get_SamplesPerSec() const noexcept
109  {
110  return(this->nSamplesPerSec);
111  }
112  size_t get_BytesPerSec() const noexcept
113  {
114  return(this->nAvgBytesPerSec);
115  }
116  size_t get_SampleSize() const // valid for PCM only.
117  {
119  ASSERT(isPCM());
120  return((this->wBitsPerSample == 8) ? 1 : 2); // sizeof(WORD)
121  }
122  UINT get_SampleRangeHalf(void) const // valid for PCM only.
123  {
125  ASSERT(isPCM());
126  ASSERT(this->wBitsPerSample);
127  return((1 << (this->wBitsPerSample - 1)) - 1);
128  }
129  size_t GetStructSize(bool bMin) const noexcept
130  {
133  switch (this->wFormatTag)
134  {
135  case 0:
136  return 0;
137  case WAVE_FORMAT_PCM:
138  // Last part (cbSize) is optional. because we know it is 0
139  if (bMin)
140  return sizeof(PCMWAVEFORMAT); // old implementations used this much.
141  return sizeof(WAVEFORMATEX); // must really allocate this much.
142  }
143  return(sizeof(WAVEFORMATEX) + this->cbSize);
144  }
145 
146  void ReCalc(void);
147  bool isValidFormat() const;
148  };
149 
151  {
157  typedef cHeapBlock SUPER_t;
158 
159  protected:
160  size_t GetStructSize(bool bMin) const;
161 
162  public:
163  cWaveFormat();
164  virtual ~cWaveFormat();
165 
166  void SetFormatNull();
167  HRESULT SetFormat(const cWaveFormatEx* pForm);
168  HRESULT SetFormatBytes(const void* pFormData, size_t iSize);
169  HRESULT SetFormatEx(WORD wFormatTag, WORD nChannels = 1, UINT nSamplesPerSec = 8000, WORD wBitsPerSample = 8, WORD wSizeEx = 0);
170  HRESULT SetFormatPCM(WORD nChannels = 1, UINT32 nSamplesPerSec = 11025, WORD wBitsPerSample = 8);
171 
172  bool isValidFormat() const;
173  virtual void ReCalc(void);
174 
175  bool IsSameAs(const cWaveFormatEx* pForm) const;
176 
177  WAVE_BLOCKS_t CvtSrcToDstSize(WAVE_BLOCKS_t SrcSize, const cWaveFormatEx* pDstForm) const;
178  UINT GetRateDiff(const cWaveFormatEx* pDstForm) const
179  {
181  return((get_SamplesPerSec() << 16) / pDstForm->nSamplesPerSec);
182  }
183 
185  const cWaveFormatEx* get_WF() const
186  {
187  return (const cWaveFormatEx*)get_Data();
188  }
190  {
191  return (cWaveFormatEx*)get_Data();
192  }
193  operator const cWaveFormatEx* () const
194  {
195  return (const cWaveFormatEx*)get_Data();
196  }
197  bool isPCM() const
198  {
199  if (get_WF() == nullptr)
200  return false;
201  return get_WF()->isPCM();
202  }
203  size_t get_BlockSize() const
204  {
206  if (get_WF() == nullptr)
207  return 0;
208  return(get_WF()->get_BlockSize());
209  }
210  UINT get_SamplesPerSec() const
211  {
212  if (get_WF() == nullptr)
213  return 0;
214  return(get_WF()->get_SamplesPerSec());
215  }
216  size_t get_BytesPerSec() const
217  {
218  if (get_WF() == nullptr)
219  return 0;
220  return(get_WF()->get_BytesPerSec());
221  }
222  WORD get_Channels() const
223  {
224  if (get_WF() == nullptr)
225  return 0;
226  return(get_WF()->get_Channels());
227  }
228  size_t get_SampleSize() const // valid for PCM only.
229  {
231  if (get_WF() == nullptr)
232  return 0;
233  return(get_WF()->get_SampleSize());
234  }
235  UINT get_SampleRangeHalf(void) const // valid for PCM only.
236  {
238  if (get_WF() == nullptr)
239  return 0;
240  return(get_WF()->get_SampleRangeHalf());
241  }
242 
244 
246  {
247  return(index * get_BlockSize());
248  }
249  WAVE_BLOCKS_t CvtBytesToBlocks(size_t sizebytes) const
250  {
251  return((WAVE_BLOCKS_t)(sizebytes / get_BlockSize()));
252  }
253 
254  UINT CvtBlocksToSamples(WAVE_BLOCKS_t index) const;
255  WAVE_BLOCKS_t CvtSamplesToBlocks(UINT dwSamples) const;
256 
257  TIMESYSD_t CvtBlocksTomSec(WAVE_BLOCKS_t index) const;
258  WAVE_BLOCKS_t CvtmSecToBlocks(TIMESYSD_t mSec) const;
259 
260  // static size_t GRAYCALL FormatCalcSize( const cWaveFormatEx* pForm );
261  bool ReAllocFormatSize(size_t iSize);
262 
263  HRESULT WriteStream(cStreamOutput* pFile) const;
264  HRESULT ReadStream(cStreamInput& strIn, size_t iSizeMax);
265 
267  };
268 
269  class GRAYLIB_LINK cWaveFormatN : public cWaveFormat // cRefBase
270  {
275  typedef cWaveFormat SUPER_t;
276 
277  protected:
279 
280  public:
281  virtual ~cWaveFormatN()
282  {
283  }
284 
286  {
287  return m_sFormName;
288  }
289 
290  HRESULT SetFormatCheck(const cWaveFormatEx* pForm);
291  virtual void ReCalc(void);
292  };
293 };
294 #endif // _INC_cWaveFormat_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
struct waveformat_tag WAVEFORMAT
struct tWAVEFORMATEX WAVEFORMATEX
struct pcmwaveformat_tag PCMWAVEFORMAT
Definition: cWaveFormat.h:73
bool isPCM() const noexcept
Definition: cWaveFormat.h:95
size_t get_SampleSize() const
Definition: cWaveFormat.h:116
size_t GetStructSize(bool bMin) const noexcept
Definition: cWaveFormat.h:129
UINT get_SampleRangeHalf(void) const
Definition: cWaveFormat.h:122
size_t get_BytesPerSec() const noexcept
Definition: cWaveFormat.h:112
UINT get_SamplesPerSec() const noexcept
Definition: cWaveFormat.h:108
WORD get_Channels() const noexcept
Definition: cWaveFormat.h:99
void Init(WORD wFormatTag2, WORD nChannels2=1, UINT nSamplesPerSec2=8000, WORD wBitsPerSample2=8, WORD wSizeEx2=0) noexcept
Definition: cWaveFormat.h:83
size_t get_BlockSize() const noexcept
Definition: cWaveFormat.h:103
void Init() noexcept
Definition: cWaveFormat.h:79
Definition: cWaveFormat.h:270
cString get_FormName() const
Definition: cWaveFormat.h:285
virtual ~cWaveFormatN()
Definition: cWaveFormat.h:281
cString m_sFormName
text to describe the format from ACM
Definition: cWaveFormat.h:278
Definition: cWaveFormat.h:151
bool isPCM() const
Definition: cWaveFormat.h:197
size_t get_BytesPerSec() const
Definition: cWaveFormat.h:216
size_t CvtBlocksToBytes(WAVE_BLOCKS_t index) const
Convert Samples, Time(ms), and Bytes to Blocks and back.
Definition: cWaveFormat.h:245
const cWaveFormatEx * get_WF() const
Get info about the format.
Definition: cWaveFormat.h:185
UNITTEST_FRIEND(cWaveFormat)
size_t get_SampleSize() const
Definition: cWaveFormat.h:228
UINT get_SampleRangeHalf(void) const
Definition: cWaveFormat.h:235
UINT get_SamplesPerSec() const
Definition: cWaveFormat.h:210
cWaveFormatEx * ref_WF()
Definition: cWaveFormat.h:189
UINT GetRateDiff(const cWaveFormatEx *pDstForm) const
Definition: cWaveFormat.h:178
WORD get_Channels() const
Definition: cWaveFormat.h:222
WAVE_BLOCKS_t CvtBytesToBlocks(size_t sizebytes) const
Definition: cWaveFormat.h:249
size_t get_BlockSize() const
Definition: cWaveFormat.h:203
Definition: cHeap.h:156
Definition: cMem.h:311
Definition: cStream.h:306
Definition: cStream.h:126
Definition: cMesh.h:22
UINT WAVE_BLOCKS_t
Index in blocks (not bytes or samples) of current wave format in sound buffer.
Definition: cWaveFormat.h:67
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
uint16 index
Definition: sample3.cpp:29
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100
Definition: cWaveFormat.h:44
WAVEFORMAT wf
Definition: cWaveFormat.h:45
WORD wBitsPerSample
Definition: cWaveFormat.h:46
Definition: cWaveFormat.h:53
WORD cbSize
The count in bytes of the size of.
Definition: cWaveFormat.h:60
UINT32 nSamplesPerSec
sample rate
Definition: cWaveFormat.h:56
UINT32 nAvgBytesPerSec
for buffer estimation
Definition: cWaveFormat.h:57
WORD wBitsPerSample
Number of bits per sample of mono data.
Definition: cWaveFormat.h:59
WORD nBlockAlign
block size of data
Definition: cWaveFormat.h:58
WORD nChannels
number of channels (i.e. mono, stereo...) (left is always first)
Definition: cWaveFormat.h:55
WORD wFormatTag
format type = WAVE_FORMAT_PCM
Definition: cWaveFormat.h:54
Definition: cWaveFormat.h:35
DWORD nSamplesPerSec
Definition: cWaveFormat.h:39
WORD nBlockAlign
Definition: cWaveFormat.h:41
WORD nChannels
Definition: cWaveFormat.h:38
WORD wFormatTag
Definition: cWaveFormat.h:37
DWORD nAvgBytesPerSec
Definition: cWaveFormat.h:40