"Fossies" - the Fresh Open Source Software Archive

Member "ssr-0.4.2/src/AV/SampleCast.h" (18 May 2020, 3670 Bytes) of package /linux/privat/ssr-0.4.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "SampleCast.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.4.1_vs_0.4.2.

    1 /*
    2 Copyright (c) 2012-2020 Maarten Baert <maarten-baert@hotmail.com>
    3 
    4 This file is part of SimpleScreenRecorder.
    5 
    6 SimpleScreenRecorder is free software: you can redistribute it and/or modify
    7 it under the terms of the GNU General Public License as published by
    8 the Free Software Foundation, either version 3 of the License, or
    9 (at your option) any later version.
   10 
   11 SimpleScreenRecorder is distributed in the hope that it will be useful,
   12 but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 GNU General Public License for more details.
   15 
   16 You should have received a copy of the GNU General Public License
   17 along with SimpleScreenRecorder.  If not, see <http://www.gnu.org/licenses/>.
   18 */
   19 
   20 #pragma once
   21 #include "Global.h"
   22 
   23 template<typename IN, typename OUT> OUT SampleCast(IN x);
   24 template<> inline int16_t SampleCast<int16_t, int16_t>(int16_t x) { return x; }
   25 template<> inline int16_t SampleCast<int32_t, int16_t>(int32_t x) { return (x + (1 << 15)) >> 16; }
   26 template<> inline int16_t SampleCast<float  , int16_t>(float   x) { return lrint(fmin(fmax(x * 32768.0f, -32768.0f), 32767.0f)); }
   27 template<> inline float   SampleCast<int16_t, float  >(int16_t x) { return (float) x * (1.0f / 32768.0f); }
   28 template<> inline float   SampleCast<int32_t, float  >(int32_t x) { return (float) x * (1.0f / 2147483648.0f); }
   29 template<> inline float   SampleCast<float  , float  >(float   x) { return x; }
   30 
   31 template<typename IN> IN SampleMix(IN a, IN b);
   32 template<> inline int16_t SampleMix<int16_t>(int16_t a, int16_t b) { return (a + b) >> 1; }
   33 template<> inline float   SampleMix<float  >(float   a, float   b) { return (a + b) * 0.5f; }
   34 
   35 // Simple sample format conversion.
   36 // The in_step and out_step parameters are useful for converting between planar and interleaved audio.
   37 template<typename IN, typename OUT>
   38 inline void SampleCopy(unsigned int sample_count, const IN* in_data, int in_step, OUT* out_data, int out_step) {
   39     for(unsigned int i = 0; i < sample_count; ++i) {
   40         *out_data = SampleCast<IN, OUT>(*in_data);
   41         in_data += in_step;
   42         out_data += out_step;
   43     }
   44 }
   45 
   46 // Sample format conversion and channel remapping in one step.
   47 // This function only supports interleaved audio.
   48 template<typename IN, typename OUT>
   49 inline void SampleChannelRemap(unsigned int sample_count, const IN* in_data, unsigned int in_channels, OUT* out_data, unsigned int out_channels) {
   50     if(in_channels == out_channels) { // no remapping needed
   51         for(unsigned int i = 0; i < sample_count * in_channels; ++i) {
   52             *(out_data++) = SampleCast<IN, OUT>(*(in_data++));
   53         }
   54     } else if(in_channels == 1 && out_channels == 2) { // mono to stereo
   55         for(unsigned int i = 0; i < sample_count; ++i) {
   56             OUT val = SampleCast<IN, OUT>(*(in_data++));
   57             *(out_data++) = val;
   58             *(out_data++) = val;
   59         }
   60     } else if(in_channels == 2 && out_channels == 1) { // stereo to mono
   61         for(unsigned int i = 0; i < sample_count; ++i) {
   62             OUT val1 = SampleCast<IN, OUT>(*(in_data++));
   63             OUT val2 = SampleCast<IN, OUT>(*(in_data++));
   64             *(out_data++) = SampleMix(val1, val2);
   65         }
   66     } else if(in_channels < out_channels) { // fill additional channels with silence
   67         for(unsigned int i = 0; i < sample_count; ++i) {
   68             for(unsigned int j = 0; j < in_channels; ++j)
   69                 *(out_data++) = SampleCast<IN, OUT>(*(in_data++));
   70             for(unsigned int j = 0; j < out_channels - in_channels; ++j)
   71                 *(out_data++) = (OUT) 0;
   72         }
   73     } else { // drop redundant channels
   74         for(unsigned int i = 0; i < sample_count; ++i) {
   75             for(unsigned int j = 0; j < out_channels; ++j)
   76                 *(out_data++) = SampleCast<IN, OUT>(*(in_data++));
   77             in_data += in_channels - out_channels;
   78         }
   79     }
   80 }