NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
wavfile.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2021 Erik Haenel et al.
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#ifndef WAVFILE_HPP
20#define WAVFILE_HPP
21
22#include "audiofile.hpp"
23#include <fstream>
24
25namespace Audio
26{
34 {
35 uint16_t formatTag;
36 uint16_t channels;
37 uint32_t sampleRate;
39 uint16_t blockAlign;
40 uint16_t bitsPerSample;
41
43 };
44
45
52 class WavFile : public SeekableFile
53 {
54 private:
55 std::string sName;
56 mutable std::fstream m_WavFileStream;
59 const long long int m_StreamOffset = 44;
61
62 bool readHeader();
63 void closeFile();
64
65 public:
66 WavFile(const std::string& sFileName);
67 virtual ~WavFile();
68
76 virtual bool isValid() const
77 {
78 return m_WavFileStream.is_open() && m_WavFileStream.good();
79 }
80
81 virtual void newFile() override;
82
91 virtual void setChannels(size_t channels) override
92 {
93 m_Header.channels = channels;
94 }
95
104 virtual void setSampleRate(size_t freq) override
105 {
106 m_Header.sampleRate = freq;
107 }
108
109 virtual void write(const Sample& sample) override;
110
118 virtual size_t getChannels() const override
119 {
120 return m_Header.channels;
121 }
122
130 virtual size_t getSampleRate() const override
131 {
132 return m_Header.sampleRate;
133 }
134
142 virtual size_t getLength() const override
143 {
145 }
146
147 virtual Sample read() const override;
148
156 virtual size_t getPosition() const override
157 {
158 if (m_WavFileStream.good())
160
161 return 0;
162 }
163
172 virtual void setPosition(size_t pos) override
173 {
176 }
177
178 virtual std::vector<Sample> readSome(size_t len) const override;
179 virtual void writeSome(const std::vector<Sample> vSamples) override;
180 };
181}
182
183#endif // WAVFILE_HPP
184
This class extends the generic audio file with seeking functionalities as well as possibilities for r...
Definition: audiofile.hpp:85
This class implements the wave file type using PCM encoding (the simplest encoding)....
Definition: wavfile.hpp:53
std::string sName
Definition: wavfile.hpp:55
virtual std::vector< Sample > readSome(size_t len) const override
Read a block of samples from the audio stream.
Definition: wavfile.cpp:243
virtual size_t getLength() const override
Get the length in samples of the current file.
Definition: wavfile.hpp:142
bool isNewFile
Definition: wavfile.hpp:60
virtual void setSampleRate(size_t freq) override
Set the sample rate, which shall be used in the new file.
Definition: wavfile.hpp:104
virtual bool isValid() const
Returns, whether the currently opened file is actually open and readable.
Definition: wavfile.hpp:76
virtual void setChannels(size_t channels) override
Set the channels, which shall be used in the new file.
Definition: wavfile.hpp:91
bool readHeader()
Private member function to read the wave file header to memory. It will ensure that the current file ...
Definition: wavfile.cpp:90
WavFileHeader m_Header
Definition: wavfile.hpp:57
virtual void write(const Sample &sample) override
Write a sample to the audio stream.
Definition: wavfile.cpp:193
virtual ~WavFile()
Destructor. Will close the file stream and update the header.
Definition: wavfile.cpp:75
virtual void writeSome(const std::vector< Sample > vSamples) override
Write a block of samples to the audio stream.
Definition: wavfile.cpp:267
void closeFile()
Updates the wave file header, which is currently open, and closes the file afterwards.
Definition: wavfile.cpp:141
virtual size_t getChannels() const override
Get the number of channels of the current file.
Definition: wavfile.hpp:118
virtual Sample read() const override
Read a sample from the audio stream.
Definition: wavfile.cpp:215
virtual void newFile() override
Prepares the wave file header for a new file, opens the stream and writes the header to the file....
Definition: wavfile.cpp:167
virtual void setPosition(size_t pos) override
Set the current position in Samples in the current file.
Definition: wavfile.hpp:172
std::fstream m_WavFileStream
Definition: wavfile.hpp:56
WavFile(const std::string &sFileName)
Constructor. Tries to open the wave file, if it exists.
Definition: wavfile.cpp:61
virtual size_t getPosition() const override
Get the current position in samples of the current file.
Definition: wavfile.hpp:156
uint32_t m_DataBlockLength
Definition: wavfile.hpp:58
const long long int m_StreamOffset
Definition: wavfile.hpp:59
virtual size_t getSampleRate() const override
Get the sample rate of the current file.
Definition: wavfile.hpp:130
Defines a sample in the audio stream, i.e. a single value per channel.
Definition: audiofile.hpp:33
This structure represents the central part of the wave file header in a format, which can be directly...
Definition: wavfile.hpp:34
uint16_t bitsPerSample
Definition: wavfile.hpp:40
uint32_t sampleRate
Definition: wavfile.hpp:37
uint16_t blockAlign
Definition: wavfile.hpp:39
uint32_t bytesPerSecond
Definition: wavfile.hpp:38
uint16_t formatTag
Definition: wavfile.hpp:35