ScummVM API documentation
rate.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AUDIO_RATE_H
23 #define AUDIO_RATE_H
24 
25 #include "common/frac.h"
26 
27 namespace Audio {
36 class AudioStream;
37 
38 typedef uint16 st_volume_t;
39 typedef uint32 st_size_t;
40 typedef uint32 st_rate_t;
41 
42 enum MixMode {
43  MIX_ADD,
44  MIX_CLAMPED_ADD
45 };
46 
47 // This assumes that 'a' and 'b' are 24-bit samples at most
48 template <MixMode Mode, typename T>
49 static inline void processSample(T& a, int b) {
50  constexpr unsigned long long highestBit = 1ULL << (sizeof(T) * 8 - 1);
51  constexpr int maxVal = (int)(highestBit - 1);
52  constexpr int minVal = ~maxVal;
53 
54 #ifdef OUTPUT_UNSIGNED_AUDIO
55  constexpr int signMask = (int)highestBit;
56 #endif
57 
58  if (Mode == MIX_ADD) {
59  a += b;
60  } else if (Mode == MIX_CLAMPED_ADD) {
61  int val;
62 #ifdef OUTPUT_UNSIGNED_AUDIO
63  val = ((int)a ^ signMask) + b;
64 #else
65  val = (int)a + b;
66 #endif
67 
68  if (val > maxVal)
69  val = maxVal;
70  else if (val < minVal)
71  val = minVal;
72 
73 #ifdef OUTPUT_UNSIGNED_AUDIO
74  a = (T)(val ^ signMask);
75 #else
76  a = (T)val;
77 #endif
78  }
79 }
80 
81 // Fallback for engine code
82 template <typename T>
83 static inline void clampedAdd(T& a, int b) {
84  processSample<MIX_CLAMPED_ADD>(a, b);
85 }
86 
94 public:
95  RateConverter() {}
96  virtual ~RateConverter() {}
97 
111  virtual int convert(AudioStream &input, byte *outBuffer, uint outBytesPerSample, st_size_t numSamples, st_volume_t vol_l, st_volume_t vol_r, MixMode mixMode) = 0;
112 
113  virtual void setInputRate(st_rate_t inputRate) = 0;
114  virtual void setOutputRate(st_rate_t outputRate) = 0;
115 
116  virtual st_rate_t getInputRate() const = 0;
117  virtual st_rate_t getOutputRate() const = 0;
118 
124  virtual bool needsDraining() const = 0;
125 };
126 
127 RateConverter *makeRateConverter(st_rate_t inRate, st_rate_t outRate, bool inStereo, bool outStereo, bool reverseStereo);
128 
130 } // End of namespace Audio
131 
132 #endif
virtual int convert(AudioStream &input, byte *outBuffer, uint outBytesPerSample, st_size_t numSamples, st_volume_t vol_l, st_volume_t vol_r, MixMode mixMode)=0
Definition: rate.h:93
Definition: audiostream.h:50
virtual bool needsDraining() const =0
Definition: system.h:39