AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
WavUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_WAVUTILS_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_WAVUTILS_H_
18 
19 #include <chrono>
20 #include <string>
21 #include <vector>
22 
23 namespace alexaClientSDK {
24 namespace avsCommon {
25 namespace utils {
26 
27 // WAV definitions
28 constexpr const char* ID_RIFF = "RIFF";
29 constexpr const char* ID_WAVE = "WAVE";
30 constexpr const char* ID_FMT = "fmt ";
31 constexpr const char* ID_DATA = "data";
32 constexpr uint32_t PCM_SUBCHUNK_SZ = 16;
33 
34 // PCM definitions
35 constexpr uint16_t FORMAT_PCM = 1;
36 constexpr uint16_t BITS_PER_BYTE = 8;
37 constexpr uint16_t PLAY_BITS_PER_SAMPLE = 16;
38 
39 // FLOAT definitions
40 constexpr uint16_t FORMAT_IEEE_FLOAT = 3;
41 constexpr uint32_t FLOAT_SUBCHUNK_SZ = 18;
42 constexpr uint32_t FACT_CHUNK_SZ = 4;
43 constexpr const char* ID_FACT = "fact";
44 
45 // WAV Header Sizes
46 constexpr unsigned int PCM_HEADER_SIZE = 44;
47 constexpr unsigned int NON_PCM_HEADER_SIZE = 58;
48 
49 // WAV Header Offsets
50 constexpr unsigned int RIFF_ID_OFFSET = 0;
51 constexpr unsigned int RIFF_SZ_OFFSET = 4;
52 constexpr unsigned int RIFF_FMT_OFFSET = 8;
53 constexpr unsigned int FMT_ID_OFFSET = 12;
54 constexpr unsigned int FMT_SZ_OFFSET = 16;
55 constexpr unsigned int AUDIO_FORMAT_OFFSET = 20;
56 constexpr unsigned int NUM_CHANNELS_OFFSET = 22;
57 constexpr unsigned int SAMPLE_RATE_OFFSET = 24;
58 constexpr unsigned int BYTE_RATE_OFFSET = 28;
59 constexpr unsigned int BLOCK_ALIGN_OFFSET = 32;
60 constexpr unsigned int BITS_PER_SAMPLE_OFFSET = 34;
61 constexpr unsigned int DATA_ID_OFFSET = 36;
62 constexpr unsigned int DATA_SZ_OFFSET = 40;
63 
64 // Non-PCM Fields
65 constexpr unsigned int CB_SZ_OFFSET = 36;
66 constexpr unsigned int FACT_ID_OFFSET = 38;
67 constexpr unsigned int FACT_SZ_OFFSET = 42;
68 constexpr unsigned int FACT_SAMPLE_LEN_OFFSET = 46;
69 
70 // OFFSET for Non-PCM fields
71 constexpr unsigned int NON_PCM_OFFSET = 14;
72 
73 using ByteVector = std::vector<unsigned char>;
74 
78 struct WavHeader {
79  uint32_t riffId;
80  uint32_t riffSz;
81  uint32_t riffFmt;
82  uint32_t fmtId;
83  uint32_t fmtSz;
84  uint16_t audioFormat;
85  uint16_t numChannels;
86  uint32_t sampleRate;
87  uint32_t byteRate;
88  uint16_t blockAlign;
89  uint16_t bitsPerSample;
90  uint16_t cbSz;
91  uint32_t factId;
92  uint32_t factSz;
93  uint32_t factSampleLen;
94  uint32_t dataId;
95  uint32_t dataSz;
96 };
97 
110  unsigned int bytesPerSample,
111  unsigned int channels,
112  unsigned int rate,
113  std::chrono::milliseconds totalDuration,
114  bool isPCM = true);
115 
126 bool readWAVFile(
127  const std::string& absoluteFilePath,
128  std::vector<uint16_t>* audioBuffer,
129  WavHeader& wavHeader,
130  bool isPCM = true);
131 
132 } // namespace utils
133 } // namespace avsCommon
134 } // namespace alexaClientSDK
135 
136 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_WAVUTILS_H_
uint32_t byteRate
Definition: WavUtils.h:87
uint32_t fmtId
Definition: WavUtils.h:82
constexpr unsigned int SAMPLE_RATE_OFFSET
Definition: WavUtils.h:57
constexpr unsigned int CB_SZ_OFFSET
Definition: WavUtils.h:65
uint16_t numChannels
Definition: WavUtils.h:85
constexpr uint16_t BITS_PER_BYTE
Definition: WavUtils.h:36
constexpr uint32_t PCM_SUBCHUNK_SZ
Definition: WavUtils.h:32
uint16_t audioFormat
Definition: WavUtils.h:84
uint32_t riffFmt
Definition: WavUtils.h:81
constexpr unsigned int AUDIO_FORMAT_OFFSET
Definition: WavUtils.h:55
::std::string string
Definition: gtest-port.h:1097
constexpr uint32_t FLOAT_SUBCHUNK_SZ
Definition: WavUtils.h:41
constexpr uint16_t PLAY_BITS_PER_SAMPLE
Definition: WavUtils.h:37
constexpr const char * ID_FACT
Definition: WavUtils.h:43
constexpr unsigned int RIFF_FMT_OFFSET
Definition: WavUtils.h:52
constexpr unsigned int FACT_SZ_OFFSET
Definition: WavUtils.h:67
uint32_t sampleRate
Definition: WavUtils.h:86
constexpr unsigned int NUM_CHANNELS_OFFSET
Definition: WavUtils.h:56
uint32_t factSampleLen
Definition: WavUtils.h:93
constexpr unsigned int PCM_HEADER_SIZE
Definition: WavUtils.h:46
constexpr unsigned int FMT_SZ_OFFSET
Definition: WavUtils.h:54
ByteVector generateWavHeader(unsigned int bytesPerSample, unsigned int channels, unsigned int rate, std::chrono::milliseconds totalDuration, bool isPCM=true)
uint16_t blockAlign
Definition: WavUtils.h:88
constexpr unsigned int RIFF_SZ_OFFSET
Definition: WavUtils.h:51
constexpr uint16_t FORMAT_PCM
Definition: WavUtils.h:35
constexpr unsigned int FACT_SAMPLE_LEN_OFFSET
Definition: WavUtils.h:68
std::vector< unsigned char > ByteVector
Definition: WavUtils.h:73
constexpr const char * ID_DATA
Definition: WavUtils.h:31
constexpr unsigned int FMT_ID_OFFSET
Definition: WavUtils.h:53
uint32_t factId
Definition: WavUtils.h:91
uint16_t cbSz
Definition: WavUtils.h:90
uint32_t riffSz
Definition: WavUtils.h:80
uint32_t dataSz
Definition: WavUtils.h:95
bool readWAVFile(const std::string &absoluteFilePath, std::vector< uint16_t > *audioBuffer, WavHeader &wavHeader, bool isPCM=true)
constexpr uint16_t FORMAT_IEEE_FLOAT
Definition: WavUtils.h:40
constexpr unsigned int FACT_ID_OFFSET
Definition: WavUtils.h:66
uint32_t dataId
Definition: WavUtils.h:94
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
constexpr unsigned int RIFF_ID_OFFSET
Definition: WavUtils.h:50
constexpr unsigned int BYTE_RATE_OFFSET
Definition: WavUtils.h:58
constexpr unsigned int DATA_ID_OFFSET
Definition: WavUtils.h:61
uint16_t bitsPerSample
Definition: WavUtils.h:89
constexpr const char * ID_FMT
Definition: WavUtils.h:30
constexpr unsigned int NON_PCM_OFFSET
Definition: WavUtils.h:71
constexpr unsigned int DATA_SZ_OFFSET
Definition: WavUtils.h:62
constexpr unsigned int NON_PCM_HEADER_SIZE
Definition: WavUtils.h:47
constexpr unsigned int BLOCK_ALIGN_OFFSET
Definition: WavUtils.h:59
uint32_t factSz
Definition: WavUtils.h:92
constexpr const char * ID_RIFF
Definition: WavUtils.h:28
uint32_t riffId
Definition: WavUtils.h:79
constexpr uint32_t FACT_CHUNK_SZ
Definition: WavUtils.h:42
uint32_t fmtSz
Definition: WavUtils.h:83
constexpr const char * ID_WAVE
Definition: WavUtils.h:29
constexpr unsigned int BITS_PER_SAMPLE_OFFSET
Definition: WavUtils.h:60

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0