1 /*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "asrc_resampler.cc"
18
19 #include <cstdio>
20 #include <iostream>
21
22 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
23
24 bluetooth::common::MessageLoopThread message_loop_thread("main message loop");
get_main_thread()25 bluetooth::common::MessageLoopThread* get_main_thread() { return &message_loop_thread; }
26
27 namespace bluetooth::hal {
Register(ReadClockHandler *)28 void LinkClocker::Register(ReadClockHandler*) {}
Unregister()29 void LinkClocker::Unregister() {}
30 } // namespace bluetooth::hal
31
32 namespace bluetooth::audio::asrc {
33
34 class SourceAudioHalAsrcTest : public SourceAudioHalAsrc {
35 public:
SourceAudioHalAsrcTest(int channels,int bitdepth)36 SourceAudioHalAsrcTest(int channels, int bitdepth)
37 : SourceAudioHalAsrc(&message_loop_thread, channels, 48000, bitdepth, 10000) {}
38
39 template <typename T>
Resample(double ratio,const T * in,size_t in_length,size_t * in_count,T * out,size_t out_length,size_t * out_count)40 void Resample(double ratio, const T* in, size_t in_length, size_t* in_count, T* out,
41 size_t out_length, size_t* out_count) {
42 auto resamplers = *resamplers_;
43 auto channels = resamplers.size();
44 unsigned sub_q26;
45
46 for (auto& r : resamplers) {
47 r.Resample(round(ldexp(ratio, 26)), in, channels, in_length / channels, in_count, out,
48 channels, out_length / channels, out_count, &sub_q26);
49 }
50 }
51 };
52
resample_i16(int channels,int bitdepth,double ratio,const int16_t * in,size_t in_length,int16_t * out,size_t out_length)53 extern "C" void resample_i16(int channels, int bitdepth, double ratio, const int16_t* in,
54 size_t in_length, int16_t* out, size_t out_length) {
55 size_t in_count, out_count;
56
57 SourceAudioHalAsrcTest(channels, bitdepth)
58 .Resample<int16_t>(ratio, in, in_length, &in_count, out, out_length, &out_count);
59
60 if (out_count < out_length) {
61 fprintf(stderr, "wrong output size: %zu:%zu %zu:%zu\n", in_length, in_count, out_length,
62 out_count);
63 }
64
65 return;
66 }
67
resample_i32(int channels,int bitdepth,double ratio,const int32_t * in,size_t in_length,int32_t * out,size_t out_length)68 extern "C" void resample_i32(int channels, int bitdepth, double ratio, const int32_t* in,
69 size_t in_length, int32_t* out, size_t out_length) {
70 size_t in_count, out_count;
71
72 SourceAudioHalAsrcTest(channels, bitdepth)
73 .Resample<int32_t>(ratio, in, in_length, &in_count, out, out_length, &out_count);
74
75 if (out_count < out_length) {
76 fprintf(stderr, "wrong output size: %zu:%zu %zu:%zu\n", in_length, in_count, out_length,
77 out_count);
78 }
79
80 return;
81 }
82
83 } // namespace bluetooth::audio::asrc
84