• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "audio_cache.h"
17 
18 #include <string>
19 
20 #include "securec.h"
21 
22 namespace KWS {
23 const int32_t MAX_BUFFER_SIZE = 36000;
24 const int32_t MIN_BUFFER_SIZE = 8000;
25 
AudioCache()26 AudioCache::AudioCache()
27     : maxBufferSize_(0), left_(0), right_(0), buffer_(nullptr), prepared_(false)
28 {
29 }
30 
~AudioCache()31 AudioCache::~AudioCache()
32 {
33     if (prepared_ && buffer_ != nullptr) {
34         delete[] buffer_;
35         buffer_ = nullptr;
36     }
37     prepared_ = false;
38     maxBufferSize_ = 0;
39     left_ = 0;
40     right_ = 0;
41 }
42 
Init(int32_t maxSize)43 bool AudioCache::Init(int32_t maxSize)
44 {
45     if (prepared_) {
46         printf("[AudioCache]Cache has ready init.\n");
47         return false;
48     }
49     if (maxSize > MAX_BUFFER_SIZE || maxSize < MIN_BUFFER_SIZE) {
50         printf("[AudioCache]maxSize out of range, init failed.\n");
51         return false;
52     }
53     maxBufferSize_ = maxSize;
54     buffer_ = new (std::nothrow) uint8_t[maxBufferSize_];
55     if (buffer_ == nullptr) {
56         printf("[AudioCache]Fail to allocate buffer for given size.\n");
57         return false;
58     }
59     errno_t ret = memset_s(buffer_, maxBufferSize_, 0x00, maxBufferSize_);
60     if (ret != EOK) {
61         printf("[AudioCache]Cache buffer init failed.\n");
62         return false;
63     }
64     prepared_ = true;
65     return true;
66 }
67 
AppendBuffer(int32_t bufferSize,const uint8_t * buffer)68 bool AudioCache::AppendBuffer(int32_t bufferSize, const uint8_t *buffer)
69 {
70     if (!prepared_) {
71         return false;
72     }
73     if (bufferSize + right_ >= maxBufferSize_) {
74         left_ = 0;
75         right_ = 0;
76     }
77     size_t remainLength = maxBufferSize_ - right_;
78     size_t length = (bufferSize > remainLength) ? remainLength : bufferSize;
79     size_t copySize = sizeof(uint8_t) * length;
80     errno_t ret = memcpy_s(&buffer_[right_], copySize, buffer, copySize);
81     if (ret != EOK) {
82         printf("[AudioCache]AppendBuffer failed.\n");
83         return false;
84     }
85     right_ += length;
86     return true;
87 }
88 
GetCapturedBuffer(uintptr_t & samplePtr)89 size_t AudioCache::GetCapturedBuffer(uintptr_t &samplePtr)
90 {
91     if (right_ - left_ <= 0 || buffer_ == nullptr) {
92         return 0;
93     }
94     samplePtr = reinterpret_cast<uintptr_t>(buffer_ + left_);
95     size_t length = right_ - left_;
96     left_ = right_;
97     return length;
98 }
99 }  // namespace KWS