• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef AUDIO_SPEED_H
16 #define AUDIO_SPEED_H
17 
18 #include <cstdint>
19 #include <string>
20 
21 #include "sonic.h"
22 #include "audio_info.h"
23 namespace OHOS {
24 namespace AudioStandard {
25 namespace {
26 constexpr float SPEED_NORMAL = 1.0f;
27 static const int32_t MAX_SPEED_BUFFER_SIZE = 614400; // 192khz 100ms 8ch float
28 }
29 class AudioSpeed {
30 public:
31     AudioSpeed();
32     AudioSpeed(size_t rate, size_t format, size_t channels);
33 
34     ~AudioSpeed();
35     static float GetPitchForSpeed(float speed);
36     int32_t Init();
37     int32_t LoadChangeSpeedFunc();
38     int32_t SetSpeed(float speed);
39     int32_t SetPitch(float pitch);
40     float GetSpeed();
41     int32_t Flush();
42 
43     int32_t ChangeSpeedFor8Bit(uint8_t *buffer, int32_t bufferSize,
44         std::unique_ptr<uint8_t []> &outBuffer, int32_t &outBufferSize);
45     int32_t ChangeSpeedFor16Bit(uint8_t *buffer, int32_t bufferSize,
46         std::unique_ptr<uint8_t []> &outBuffer, int32_t &outBufferSize);
47     int32_t ChangeSpeedFor24Bit(uint8_t *buffer, int32_t bufferSize,
48         std::unique_ptr<uint8_t []> &outBuffer, int32_t &outBufferSize);
49     int32_t ChangeSpeedFor32Bit(uint8_t *buffer, int32_t bufferSize,
50         std::unique_ptr<uint8_t []> &outBuffer, int32_t &outBufferSize);
51     int32_t ChangeSpeedForFloat(float *buffer, int32_t bufferSize, float* outBuffer, int32_t &outBufferSize);
52 
53     std::function<int32_t(uint8_t *, int32_t, std::unique_ptr<uint8_t []>&, int32_t&)> ChangeSpeedFunc;
54 private:
55     float speed_ = 0.0f;
56     size_t rate_;
57     size_t format_;
58 
59     std::unique_ptr<uint8_t[]> speedBuffer_ = nullptr;
60     int32_t speedBufferSize_ = 0;
61     AudioStreamParams streamParam_;
62     size_t formatSize_ = 1;
63     size_t channels_ = 2;
64     sonicStream sonicStream_ = nullptr;
65 };
66 } // namespace AudioStandard
67 } // namespace OHOS
68 #endif // AUDIO_SPEED_H