Beatmup
wav_utilities.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "wav_utilities.h"
20 
21 using namespace Beatmup::Audio::WAV;
22 
23 
24 void Header::set(uint32_t sampleRate, uint16_t bitsPerSample, uint16_t channelCount, uint32_t dataSize) {
25  m_RIFF = __RIFF;
26  m_WAVE = __WAVE;
27  m_fmt_ = __fmt_;
28  m_data = __data;
29  __16 = 16;
30  audioFormat = 1;
31  this->sampleRate = sampleRate;
32  this->bitsPerSample = bitsPerSample;
33  this->numChannels = channelCount;
34  blockAlign = channelCount * bitsPerSample / 8;
36  dataSizeBytes = 0;
37  this->dataSizeBytes = dataSize;
38  this->chunkSize = sizeof(Header) + dataSize - 8;
39 }
40 
41 
43  if (header.m_RIFF != Header::__RIFF)
44  throw InvalidWavFile("Incorrect WAV file: 'RIFF' marker missing");
45 
46  if (header.m_WAVE != Header::__WAVE)
47  throw InvalidWavFile("Incorrect WAV file: 'WAVE' marker missing");
48 
49  if (header.m_fmt_ != Header::__fmt_)
50  throw InvalidWavFile("Incorrect WAV file: 'fmt ' marker missing");
51 
52  if (header.m_data != Header::__data)
53  throw InvalidWavFile("Incorrect WAV file: 'data' marker missing");
54 
55  if (header.__16 != 16)
56  throw InvalidWavFile("Incorrect WAV file: bad subchunk size");
57 
58  if (header.audioFormat != 1)
59  throw InvalidWavFile("Incorrect WAV file: unsupported audio format");
60 
61  if (sizeof(header) + header.dataSizeBytes != header.chunkSize + 8)
62  throw InvalidWavFile("Incorrect WAV file: size mismatch");
63 }
static const uint32_t __RIFF
Definition: wav_utilities.h:56
static const uint32_t __fmt_
Definition: wav_utilities.h:58
static const uint32_t __WAVE
Definition: wav_utilities.h:57
void set(uint32_t sampleRate, uint16_t bitsPerSample, uint16_t channelCount, uint32_t dataSize)
static const uint32_t __data
Definition: wav_utilities.h:59
static void check(Header &header)
InvalidWavFile(const char *message)
Definition: wav_utilities.h:70
WAV files reading and writing.
Definition: wav_utilities.h:29