Home | History | Annotate | Download | only in lb2
      1 /*
      2  * Copyright (C) 2017 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 #ifndef LB2_SAMPLE_H_
     18 #define LB2_SAMPLE_H_
     19 
     20 #include <cmath>
     21 #include <limits>
     22 
     23 using sample_t = int16_t;  // For flexibility. May change to a float type if needed.
     24 
     25 static_assert(std::is_integral<sample_t>::value,
     26         "FULL_SAMPLE_SCALE assumes sample values are of integer type");
     27 // FIXME: Would we plan to use floats, the maximum value will be 1.0.
     28 constexpr double FULL_SAMPLE_SCALE = std::numeric_limits<sample_t>::max() + 1;
     29 
     30 inline double convertSampleType(sample_t s) {
     31     static_assert(std::numeric_limits<sample_t>::is_signed, "sample value is assumed to be signed");
     32     return s / FULL_SAMPLE_SCALE;
     33 }
     34 
     35 inline sample_t convertSampleType(double d) {
     36     return std::trunc(d * FULL_SAMPLE_SCALE);
     37 }
     38 
     39 #endif  // LB2_SAMPLE_H_
     40