1 /* 2 * Copyright (c) 2024 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 AUDIO_TONEPLAYER_IMPL_H 17 #define AUDIO_TONEPLAYER_IMPL_H 18 19 #include <map> 20 #include <thread> 21 #include <mutex> 22 23 #include "tone_player.h" 24 #include "audio_renderer.h" 25 #include "audio_utils.h" 26 27 namespace OHOS { 28 namespace AudioStandard { 29 class TonePlayerImpl : public AudioRendererWriteCallback, public AudioRendererCallback, public TonePlayer, 30 public std::enable_shared_from_this<TonePlayerImpl> { 31 public: 32 TonePlayerImpl(const std::string cachePath, const AudioRendererInfo &rendererInfo); 33 ~TonePlayerImpl(); 34 35 // for audio renderer callback 36 void OnInterrupt(const InterruptEvent &interruptEvent) override; 37 void OnStateChange(const RendererState state, const StateChangeCmdType __attribute__((unused)) cmdType) override; 38 void OnWriteData(size_t length) override; 39 40 // for toneplayer 41 bool LoadTone(ToneType toneType) override; 42 bool StartTone() override; 43 bool StopTone() override; 44 bool Release() override; 45 46 enum ToneState : uint8_t { 47 TONE_IDLE, 48 TONE_INIT, 49 TONE_STARTING, 50 TONE_RUNNING, 51 TONE_STOPPING, 52 TONE_STOPPED, 53 TONE_RELEASED, 54 }; 55 56 private: 57 bool InitAudioRenderer(); 58 bool InitToneWaveInfo(); 59 bool AudioToneSequenceGen(BufferDesc &bufDesc); 60 bool ContinueToneplay(uint32_t sampleCnt, int8_t *audioBuf); 61 bool CheckToneStarted(uint32_t sampleCnt, int8_t *audioBuf); 62 bool CheckToneStopped(); 63 void GetCurrentSegmentUpdated(); 64 bool CheckToneContinuity(); 65 int32_t GetSamples(uint16_t *freqs, int8_t *buffer, uint32_t samples); 66 67 AudioRendererOptions rendererOptions_ = {}; 68 std::string cachePath_; // NAPI interface to create AudioRenderer 69 std::unique_ptr<AudioRenderer> audioRenderer_; // Pointer to AudioRenderer used for playback 70 bool isRendererInited_ = false; 71 72 std::mutex optMutex_; 73 ToneType toneType_ = NUM_TONES; 74 int32_t amplitudeType_ = 0; 75 uint32_t currSegment_ = 0; // Current segment index in ToneDescriptor segments[] 76 uint32_t currCount_ = 0; // Current sequence repeat count 77 std::shared_ptr<ToneInfo> toneInfo_; // pointer to active tone Info 78 std::shared_ptr<ToneInfo> initialToneInfo_; // pointer to new active tone Info 79 std::vector<int32_t> supportedTones_; 80 81 ToneState toneState_ = TONE_IDLE; 82 83 uint32_t loopCounter_ = 0; // Current tone loopback count 84 uint32_t totalSample_ = 0; // Total no. of tone samples played 85 uint32_t nextSegSample_ = 0; // Position of next segment transition expressed in samples 86 uint32_t maxSample_ = 0; // Maximum number of audio samples played (maximun tone duration) 87 uint32_t samplingRate_ = 0; // Audio Sampling rate 88 uint32_t sampleCount_ = 0; // Initial value should be zero before any new Tone renderering 89 90 // to wait for audio rendere callback completion after a change is requested 91 FILE *dumpFile_ = nullptr; 92 uint32_t processSize_ = 0; // In audioRenderer, Size of audio blocks generated at a time 93 }; 94 } // namespace AudioStandard 95 } // namespace OHOS 96 #endif /* AUDIO_TONEPLAYER_IMPL_H */ 97