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 #ifndef KWS_MANAGER_H 17 #define KWS_MANAGER_H 18 19 #include <mutex> 20 #include <thread> 21 #include <unistd.h> 22 #include <vector> 23 24 #include "ai_datatype.h" 25 #include "audio_cache.h" 26 #include "audio_capturer.h" 27 #include "audio_wrapper.h" 28 #include "kws_callback.h" 29 #include "kws_sdk.h" 30 #include "media_errors.h" 31 #include "media_info.h" 32 33 namespace KWS { 34 const int32_t AUDIO_SAMPLE_RATE = 16000; // 16kHz 35 const int32_t AUDIO_CODEC_BITRATE = 32000; // 32kHz 36 37 const std::vector<std::string> WORD_CONTENT = { 38 "Hi Xiaowen", 39 "Nihao Wenwen", 40 "Unknown" 41 }; 42 43 enum KwsStatus { 44 IDLE = 1000, 45 PREPARED, 46 RUNNING, 47 }; 48 49 class KwsManager { 50 public: 51 KwsManager(int32_t sampleRate, int32_t bitRate); 52 ~KwsManager(); 53 void Start(); 54 void Stop(); 55 bool Prepare(); 56 void ProduceSamples(); 57 void ConsumeSamples(); 58 59 private: 60 void OnStart(); 61 void OnStop(); 62 bool PreparedAudioCapturer(); 63 bool PreparedAudioWrapper(); 64 bool PreparedInference(); 65 bool ConfiguredAudioCapturer(); 66 bool ConfiguredAudioWrapper(); 67 void StopAudioCapturer(); 68 void StopAudioWrapper(); 69 void StopInference(); 70 71 private: 72 std::shared_ptr<AudioCache> cache_ = nullptr; 73 std::shared_ptr<OHOS::AI::KWSSdk> plugin_ = nullptr; 74 std::shared_ptr<OHOS::Audio::AudioCapturer> capturer_ = nullptr; 75 int32_t sampleRate_ = AUDIO_SAMPLE_RATE; 76 int32_t bitRate_ = AUDIO_CODEC_BITRATE; 77 KwsStatus status_ = IDLE; 78 intptr_t aacHandler_ = -1; 79 std::mutex mutex_; 80 }; 81 82 class ThreadTask { 83 public: 84 static void AudioProducer(KwsManager *kwsManager); 85 static void AudioConsumer(KwsManager *kwsManager); 86 }; 87 } // namespace KWS 88 #endif