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 bluetooth::common::MessageLoopThread message_loop_thread("main message loop");
get_main_thread()23 bluetooth::common::MessageLoopThread* get_main_thread() {
24   return &message_loop_thread;
25 }
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,
38                            10000) {}
39 
40   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)41   void Resample(double ratio, const T* in, size_t in_length, size_t* in_count,
42                 T* out, size_t out_length, size_t* out_count) {
43     auto resamplers = *resamplers_;
44     auto channels = resamplers.size();
45     unsigned sub_q26;
46 
47     for (auto& r : resamplers)
48       r.Resample(round(ldexp(ratio, 26)), in, channels, in_length / channels,
49                  in_count, out, channels, out_length / channels, out_count,
50                  &sub_q26);
51   }
52 };
53 
resample_i16(int channels,int bitdepth,double ratio,const int16_t * in,size_t in_length,int16_t * out,size_t out_length)54 extern "C" void resample_i16(int channels, int bitdepth, double ratio,
55                              const int16_t* in, size_t in_length, int16_t* out,
56                              size_t out_length) {
57   size_t in_count, out_count;
58 
59   SourceAudioHalAsrcTest(channels, bitdepth)
60       .Resample<int16_t>(ratio, in, in_length, &in_count, out, out_length,
61                          &out_count);
62 
63   if (out_count < out_length)
64     fprintf(stderr, "wrong output size: %zu:%zu %zu:%zu\n", in_length, in_count,
65             out_length, out_count);
66 
67   return;
68 }
69 
resample_i32(int channels,int bitdepth,double ratio,const int32_t * in,size_t in_length,int32_t * out,size_t out_length)70 extern "C" void resample_i32(int channels, int bitdepth, double ratio,
71                              const int32_t* in, size_t in_length, int32_t* out,
72                              size_t out_length) {
73   size_t in_count, out_count;
74 
75   SourceAudioHalAsrcTest(channels, bitdepth)
76       .Resample<int32_t>(ratio, in, in_length, &in_count, out, out_length,
77                          &out_count);
78 
79   if (out_count < out_length)
80     fprintf(stderr, "wrong output size: %zu:%zu %zu:%zu\n", in_length, in_count,
81             out_length, out_count);
82 
83   return;
84 }
85 
86 }  // namespace bluetooth::audio::asrc
87