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 // TODO(b/369381361) Enfore -Wmissing-prototypes
23 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
24 
25 bluetooth::common::MessageLoopThread message_loop_thread("main message loop");
get_main_thread()26 bluetooth::common::MessageLoopThread* get_main_thread() { return &message_loop_thread; }
27 
28 namespace bluetooth::hal {
Register(ReadClockHandler *)29 void LinkClocker::Register(ReadClockHandler*) {}
Unregister()30 void LinkClocker::Unregister() {}
31 }  // namespace bluetooth::hal
32 
33 namespace bluetooth::audio::asrc {
34 
35 class SourceAudioHalAsrcTest : public SourceAudioHalAsrc {
36 public:
SourceAudioHalAsrcTest(int channels,int bitdepth)37   SourceAudioHalAsrcTest(int channels, int bitdepth)
38       : SourceAudioHalAsrc(&message_loop_thread, channels, 48000, bitdepth, 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, T* out,
42                 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, in_count, out,
49                  channels, out_length / channels, out_count, &sub_q26);
50     }
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, const int16_t* in,
55                              size_t in_length, int16_t* out, size_t out_length) {
56   size_t in_count, out_count;
57 
58   SourceAudioHalAsrcTest(channels, bitdepth)
59           .Resample<int16_t>(ratio, in, in_length, &in_count, out, out_length, &out_count);
60 
61   if (out_count < out_length) {
62     fprintf(stderr, "wrong output size: %zu:%zu %zu:%zu\n", in_length, in_count, out_length,
63             out_count);
64   }
65 
66   return;
67 }
68 
resample_i32(int channels,int bitdepth,double ratio,const int32_t * in,size_t in_length,int32_t * out,size_t out_length)69 extern "C" void resample_i32(int channels, int bitdepth, double ratio, const int32_t* in,
70                              size_t in_length, int32_t* out, size_t out_length) {
71   size_t in_count, out_count;
72 
73   SourceAudioHalAsrcTest(channels, bitdepth)
74           .Resample<int32_t>(ratio, in, in_length, &in_count, out, out_length, &out_count);
75 
76   if (out_count < out_length) {
77     fprintf(stderr, "wrong output size: %zu:%zu %zu:%zu\n", in_length, in_count, out_length,
78             out_count);
79   }
80 
81   return;
82 }
83 
84 }  // namespace bluetooth::audio::asrc
85