1 /*
2 * Copyright 2019 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 "IntegerRatio.h"
18
19 using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
20
21 // Enough primes to cover the common sample rates.
22 static const int kPrimes[] = {
23 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
24 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
25 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
26 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199};
27
reduce()28 void IntegerRatio::reduce() {
29 for (int prime : kPrimes) {
30 if (mNumerator < prime || mDenominator < prime) {
31 break;
32 }
33
34 // Find biggest prime factor for numerator.
35 while (true) {
36 int top = mNumerator / prime;
37 int bottom = mDenominator / prime;
38 if ((top >= 1)
39 && (bottom >= 1)
40 && (top * prime == mNumerator) // divided evenly?
41 && (bottom * prime == mDenominator)) {
42 mNumerator = top;
43 mDenominator = bottom;
44 } else {
45 break;
46 }
47 }
48
49 }
50 }
51