blob: 13d1589787f424460fdd68dbf4ad940adfc5f008 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef ASR_WAV2LETTER_PREPROCESS_HPP
18#define ASR_WAV2LETTER_PREPROCESS_HPP
19
20#include "Wav2LetterModel.hpp"
21#include "Wav2LetterMfcc.hpp"
22#include "AudioUtils.hpp"
23#include "DataStructures.hpp"
alexander31ae9f02022-02-10 16:15:54 +000024#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000025
26namespace arm {
27namespace app {
28namespace audio {
29namespace asr {
30
31 /* Class to facilitate pre-processing calculation for Wav2Letter model
32 * for ASR. */
33 using AudioWindow = SlidingWindow <const int16_t>;
34
35 class Preprocess {
36 public:
37 /**
38 * @brief Constructor.
39 * @param[in] numMfccFeatures Number of MFCC features per window.
40 * @param[in] windowLen Number of elements in a window.
41 * @param[in] windowStride Stride (in number of elements) for
42 * moving the window.
43 * @param[in] numMfccVectors Number of MFCC vectors per window.
44 */
45 Preprocess(
46 uint32_t numMfccFeatures,
47 uint32_t windowLen,
48 uint32_t windowStride,
49 uint32_t numMfccVectors);
50 Preprocess() = delete;
51 ~Preprocess() = default;
52
53 /**
54 * @brief Calculates the features required from audio data. This
55 * includes MFCC, first and second order deltas,
56 * normalisation and finally, quantisation. The tensor is
57 * populated with feature from a given window placed along
58 * in a single row.
59 * @param[in] audioData Pointer to the first element of audio data.
60 * @param[in] audioDataLen Number of elements in the audio data.
61 * @param[in] tensor Tensor to be populated.
62 * @return true if successful, false in case of error.
63 */
64 bool Invoke(const int16_t * audioData,
65 uint32_t audioDataLen,
66 TfLiteTensor * tensor);
67
68 protected:
69 /**
70 * @brief Computes the first and second order deltas for the
71 * MFCC buffers - they are assumed to be populated.
72 *
73 * @param[in] mfcc MFCC buffers.
74 * @param[out] delta1 Result of the first diff computation.
75 * @param[out] delta2 Result of the second diff computation.
76 * @return true if successful, false otherwise.
77 */
alexanderc350cdc2021-04-29 20:36:09 +010078 static bool ComputeDeltas(Array2d<float>& mfcc,
79 Array2d<float>& delta1,
80 Array2d<float>& delta2);
alexander3c798932021-03-26 21:42:19 +000081
82 /**
83 * @brief Given a 2D vector of floats, computes the mean.
84 * @param[in] vec Vctor of vector of floats.
85 * @return Mean value.
86 */
alexanderc350cdc2021-04-29 20:36:09 +010087 static float GetMean(Array2d<float>& vec);
alexander3c798932021-03-26 21:42:19 +000088
89 /**
90 * @brief Given a 2D vector of floats, computes the stddev.
91 * @param[in] vec Vector of vector of floats.
92 * @param[in] mean Mean value of the vector passed in.
93 * @return stddev value.
94 */
alexanderc350cdc2021-04-29 20:36:09 +010095 static float GetStdDev(Array2d<float>& vec,
96 const float mean);
alexander3c798932021-03-26 21:42:19 +000097
98 /**
99 * @brief Given a 2D vector of floats, normalises it using
100 * the mean and the stddev.
101 * @param[in,out] vec Vector of vector of floats.
102 */
alexanderc350cdc2021-04-29 20:36:09 +0100103 static void NormaliseVec(Array2d<float>& vec);
alexander3c798932021-03-26 21:42:19 +0000104
105 /**
106 * @brief Normalises the MFCC and delta buffers.
107 */
alexanderc350cdc2021-04-29 20:36:09 +0100108 void Normalise();
alexander3c798932021-03-26 21:42:19 +0000109
110 /**
111 * @brief Given the quantisation and data type limits, computes
112 * the quantised values of a floating point input data.
113 * @param[in] elem Element to be quantised.
114 * @param[in] quantScale Scale.
115 * @param[in] quantOffset Offset.
116 * @param[in] minVal Numerical limit - minimum.
117 * @param[in] maxVal Numerical limit - maximum.
118 * @return Floating point quantised value.
119 */
alexanderc350cdc2021-04-29 20:36:09 +0100120 static float GetQuantElem(
alexander3c798932021-03-26 21:42:19 +0000121 float elem,
122 float quantScale,
123 int quantOffset,
124 float minVal,
125 float maxVal);
126
127 /**
128 * @brief Quantises the MFCC and delta buffers, and places them
129 * in the output buffer. While doing so, it transposes
130 * the data. Reason: Buffers in this class are arranged
131 * for "time" axis to be row major. Primary reason for
132 * this being the convolution speed up (as we can use
133 * contiguous memory). The output, however, requires the
134 * time axis to be in column major arrangement.
135 * @param[in] outputBuf Pointer to the output buffer.
136 * @param[in] outputBufSz Output buffer's size.
137 * @param[in] quantScale Quantisation scale.
138 * @param[in] quantOffset Quantisation offset.
139 */
140 template <typename T>
alexanderc350cdc2021-04-29 20:36:09 +0100141 bool Quantise(
alexander3c798932021-03-26 21:42:19 +0000142 T * outputBuf,
143 const uint32_t outputBufSz,
144 const float quantScale,
145 const int quantOffset)
146 {
147 /* Check the output size will fit everything. */
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100148 if (outputBufSz < (this->m_mfccBuf.size(0) * 3 * sizeof(T))) {
alexander3c798932021-03-26 21:42:19 +0000149 printf_err("Tensor size too small for features\n");
150 return false;
151 }
152
153 /* Populate. */
154 T * outputBufMfcc = outputBuf;
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100155 T * outputBufD1 = outputBuf + this->m_numMfccFeats;
156 T * outputBufD2 = outputBufD1 + this->m_numMfccFeats;
157 const uint32_t ptrIncr = this->m_numMfccFeats * 2; /* (3 vectors - 1 vector) */
alexander3c798932021-03-26 21:42:19 +0000158
159 const float minVal = std::numeric_limits<T>::min();
160 const float maxVal = std::numeric_limits<T>::max();
161
162 /* Need to transpose while copying and concatenating the tensor. */
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100163 for (uint32_t j = 0; j < this->m_numFeatVectors; ++j) {
164 for (uint32_t i = 0; i < this->m_numMfccFeats; ++i) {
alexanderc350cdc2021-04-29 20:36:09 +0100165 *outputBufMfcc++ = static_cast<T>(Preprocess::GetQuantElem(
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100166 this->m_mfccBuf(i, j), quantScale,
alexanderc350cdc2021-04-29 20:36:09 +0100167 quantOffset, minVal, maxVal));
168 *outputBufD1++ = static_cast<T>(Preprocess::GetQuantElem(
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100169 this->m_delta1Buf(i, j), quantScale,
alexanderc350cdc2021-04-29 20:36:09 +0100170 quantOffset, minVal, maxVal));
171 *outputBufD2++ = static_cast<T>(Preprocess::GetQuantElem(
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100172 this->m_delta2Buf(i, j), quantScale,
alexanderc350cdc2021-04-29 20:36:09 +0100173 quantOffset, minVal, maxVal));
alexander3c798932021-03-26 21:42:19 +0000174 }
175 outputBufMfcc += ptrIncr;
176 outputBufD1 += ptrIncr;
177 outputBufD2 += ptrIncr;
178 }
179
180 return true;
181 }
182
183 private:
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100184 Wav2LetterMFCC m_mfcc; /* MFCC instance. */
alexander3c798932021-03-26 21:42:19 +0000185
186 /* Actual buffers to be populated. */
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100187 Array2d<float> m_mfccBuf; /* Contiguous buffer 1D: MFCC */
188 Array2d<float> m_delta1Buf; /* Contiguous buffer 1D: Delta 1 */
189 Array2d<float> m_delta2Buf; /* Contiguous buffer 1D: Delta 2 */
alexander3c798932021-03-26 21:42:19 +0000190
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100191 uint32_t m_windowLen; /* Window length for MFCC. */
192 uint32_t m_windowStride; /* Window stride len for MFCC. */
193 uint32_t m_numMfccFeats; /* Number of MFCC features per window. */
194 uint32_t m_numFeatVectors; /* Number of m_numMfccFeats. */
195 AudioWindow m_window; /* Sliding window. */
alexander3c798932021-03-26 21:42:19 +0000196
197 };
198
199} /* namespace asr */
200} /* namespace audio */
201} /* namespace app */
202} /* namespace arm */
203
204#endif /* ASR_WAV2LETTER_PREPROCESS_HPP */