Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
wav_header.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_sndio/wav_header.h
10 //! @brief WAV header.
11 
12 #ifndef ROC_SNDIO_WAV_HEADER_H_
13 #define ROC_SNDIO_WAV_HEADER_H_
14 
15 #include "roc_core/attributes.h"
16 #include "roc_core/stddefs.h"
17 
18 namespace roc {
19 namespace sndio {
20 
21 //! WAV header
22 //! @remarks
23 //! Holds data of a WAV header
24 //! Allows easy generation of WAV header
25 class WavHeader {
26 public:
27  //! WAV header data
29  //! Constructor
30  WavHeaderData(uint32_t chunk_id,
31  uint32_t format,
32  uint32_t subchunk1_id,
33  uint32_t subchunk1_size,
34  uint16_t audio_format,
35  uint16_t num_channels,
36  uint32_t sample_rate,
37  uint32_t byte_rate,
38  uint16_t block_align,
39  uint16_t bits_per_sample,
40  uint32_t subchunk2_id);
41  // RIFF header
42  //! Chunk ID
43  const uint32_t chunk_id_;
44  //! Chunk size
45  uint32_t chunk_size_;
46  //! Format
47  const uint32_t format_;
48  // WAVE fmt
49  //! Subchunk1 ID
50  const uint32_t subchunk1_id_;
51  //! Subchunk1 size
52  const uint32_t subchunk1_size_;
53  //! Audio format
54  const uint16_t audio_format_;
55  //! Num channels
56  const uint16_t num_channels_;
57  //! Sample rate
58  const uint32_t sample_rate_;
59  //! Byte rate
60  const uint32_t byte_rate_;
61  //! Block align
62  const uint16_t block_align_;
63  //! Bits per sample
64  const uint16_t bits_per_sample_;
65  // WAVE data
66  //! Subchunk2 ID
67  const uint32_t subchunk2_id_;
68  //! Subchunk2 size
69  uint32_t subchunk2_size_;
71 
72  //! Initialize
73  WavHeader(uint16_t num_channels, uint32_t sample_rate, uint16_t bits_per_sample);
74 
75  //! Get number of channels
76  uint16_t num_channels() const;
77 
78  //! Get sample rate
79  uint32_t sample_rate() const;
80 
81  //! Get number of bits per sample
82  uint16_t bits_per_sample() const;
83 
84  //! Resets samples counter
85  void reset_sample_counter(uint32_t num_samples);
86 
87  //! Updates samples num and returns header data
88  const WavHeaderData& update_and_get_header(uint32_t num_samples);
89 
90 private:
91  static const uint32_t METADATA_SIZE_ = 36;
92 
93  WavHeaderData data_;
94  // Help data
95  uint32_t num_samples_;
96 };
97 
98 } // namespace sndio
99 } // namespace roc
100 
101 #endif // ROC_SNDIO_WAV_HEADER_H_
Compiler attributes.
#define ROC_ATTR_PACKED_BEGIN
Pack structure fields. Place these before class or struct keyword.
Definition: attributes.h:55
#define ROC_ATTR_PACKED_END
Pack structure fields. Place these between '}' and ';'.
Definition: attributes.h:58
const WavHeaderData & update_and_get_header(uint32_t num_samples)
Updates samples num and returns header data.
WavHeader(uint16_t num_channels, uint32_t sample_rate, uint16_t bits_per_sample)
Initialize.
void reset_sample_counter(uint32_t num_samples)
Resets samples counter.
uint16_t bits_per_sample() const
Get number of bits per sample.
uint32_t sample_rate() const
Get sample rate.
uint16_t num_channels() const
Get number of channels.
Root namespace.
Commonly used types and functions.
const uint32_t format_
Format.
Definition: wav_header.h:47
const uint32_t subchunk1_id_
Subchunk1 ID.
Definition: wav_header.h:50
WavHeaderData(uint32_t chunk_id, uint32_t format, uint32_t subchunk1_id, uint32_t subchunk1_size, uint16_t audio_format, uint16_t num_channels, uint32_t sample_rate, uint32_t byte_rate, uint16_t block_align, uint16_t bits_per_sample, uint32_t subchunk2_id)
Constructor.
const uint32_t sample_rate_
Sample rate.
Definition: wav_header.h:58
const uint16_t bits_per_sample_
Bits per sample.
Definition: wav_header.h:64
uint32_t subchunk2_size_
Subchunk2 size.
Definition: wav_header.h:69
const uint32_t byte_rate_
Byte rate.
Definition: wav_header.h:60
const uint32_t subchunk1_size_
Subchunk1 size.
Definition: wav_header.h:52
uint32_t chunk_size_
Chunk size.
Definition: wav_header.h:45
const uint32_t chunk_id_
Chunk ID.
Definition: wav_header.h:43
const uint32_t subchunk2_id_
Subchunk2 ID.
Definition: wav_header.h:67
const uint16_t num_channels_
Num channels.
Definition: wav_header.h:56
const uint16_t block_align_
Block align.
Definition: wav_header.h:62
const uint16_t audio_format_
Audio format.
Definition: wav_header.h:54