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