blob: 8215feeeb55db324928fe148495d61dcb35857a4 [file] [log] [blame]
George Gekov23c26272021-08-16 11:32:10 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#include <cmath>
6#include <numeric>
7#include <algorithm>
8#include <memory>
9#include "MathUtils.hpp"
10#include "SlidingWindow.hpp"
11#include "DsCNNPreprocessor.hpp"
12
13std::vector<int8_t> kws::DsCNNPreprocessor::Invoke(const float* audioData, size_t dataSize,
14 int quantOffset, float quantScale)
15{
16 auto window = SlidingWindow<const float>(
17 audioData, dataSize,
18 this->m_windowLen, this->m_windowStride);
19
20 uint32_t mfccBufIdx = 0;
21 std::vector<int8_t> outputBuffer;
22 // While we can slide over the window
23 while (window.HasNext())
24 {
25 const float* mfccWindow = window.Next();
26 auto mfccAudioData = std::vector<float>(mfccWindow, mfccWindow + this->m_windowLen);
27
28 auto mfcc = this->m_mfcc->MfccComputeQuant<int8_t>(mfccAudioData, quantScale, quantOffset);
29
30 std::copy(mfcc.begin(), mfcc.end(), std::back_inserter(outputBuffer));
31
32 ++mfccBufIdx;
33 }
34
35 return outputBuffer;
36}
37
38kws::DsCNNPreprocessor::DsCNNPreprocessor(const uint32_t windowLen, const uint32_t windowStride,
39 std::unique_ptr<DsCnnMFCC> mfccInst) :
40 m_windowLen{windowLen}, m_windowStride{windowStride}, m_mfcc{std::move(mfccInst)} {}