| 1 |
/* |
| 2 |
* Copyright (C) 2020-2021 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include <cstdint> |
| 29 |
#include <vector> |
| 30 |
#include "binary_container.hpp" |
| 31 |
|
| 32 |
namespace io |
| 33 |
{ |
| 34 |
class WavContainer |
| 35 |
{ |
| 36 |
public: |
| 37 |
using value_type = BinaryContainer::value_type; |
| 38 |
using size_type = BinaryContainer::size_type; |
| 39 |
using iterator = BinaryContainer::iterator; |
| 40 |
using const_iterator = BinaryContainer::const_iterator; |
| 41 |
using reverse_iterator = BinaryContainer::reverse_iterator; |
| 42 |
using const_reverse_iterator = BinaryContainer::const_reverse_iterator; |
| 43 |
|
| 44 |
explicit WavContainer(uint32_t rate = 44100, uint16_t nCh = 2, uint16_t getBitSize = 16); |
| 45 |
explicit WavContainer(const BinaryContainer& bc); |
| 46 |
|
| 47 |
inline iterator begin() noexcept { return buf_.begin(); } |
| 48 |
inline const_iterator begin() const noexcept { return buf_.begin(); } |
| 49 |
|
| 50 |
inline iterator end() noexcept { return buf_.end(); } |
| 51 |
inline const_iterator end() const noexcept { return buf_.end(); } |
| 52 |
|
| 53 |
inline const_iterator cbegin() const noexcept { return buf_.cbegin(); } |
| 54 |
inline const_iterator cend() const noexcept { return buf_.cend(); } |
| 55 |
|
| 56 |
inline reverse_iterator rbegin() noexcept { return buf_.rbegin(); } |
| 57 |
inline const_reverse_iterator rbegin() const noexcept { return buf_.rbegin(); } |
| 58 |
|
| 59 |
inline reverse_iterator rend() noexcept { return buf_.rend(); } |
| 60 |
inline const_reverse_iterator rend() const noexcept { return buf_.rend(); } |
| 61 |
|
| 62 |
inline const_reverse_iterator crbegin() const noexcept { return buf_.crbegin(); } |
| 63 |
inline const_reverse_iterator crend() const noexcept { return buf_.crend(); } |
| 64 |
|
| 65 |
inline size_type size() const { return buf_.size(); } |
| 66 |
|
| 67 |
void setChannelCount(uint16_t n); |
| 68 |
inline uint16_t getChannelCount() const noexcept { return nCh_; } |
| 69 |
void setBitSize(uint16_t size); |
| 70 |
inline uint16_t getBitSize() const noexcept { return bitSize_; } |
| 71 |
void setSampleRate(uint32_t rate); |
| 72 |
inline uint32_t getSampleRate() const noexcept { return rate_; } |
| 73 |
|
| 74 |
size_type getSampleCount() const; |
| 75 |
|
| 76 |
void appendSample(const int16_t* sample, size_type nSamples); |
| 77 |
void appendSample(const std::vector<int16_t>& sample); |
| 78 |
void appendSample(const BinaryContainer& sample); |
| 79 |
BinaryContainer getSample() const noexcept; |
| 80 |
|
| 81 |
// static WavContainer* resample(const WavContainer* src, uint32_t rate); |
| 82 |
// static WavContainer* mono(const WavContainer* src); |
| 83 |
|
| 84 |
private: |
| 85 |
uint16_t nCh_, bitSize_, blockSize_; |
| 86 |
uint32_t rate_, byteRate_; |
| 87 |
BinaryContainer buf_; |
| 88 |
|
| 89 |
void updateBlockSize(); |
| 90 |
void updateByteRate(); |
| 91 |
void updateSizeDataAfterAppendSample(); |
| 92 |
}; |
| 93 |
} |