AudioChannel.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2005 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #ifndef AUDIOCHANNEL_H_INCLUDED
00020 #define AUDIOCHANNEL_H_INCLUDED
00021 
00022 namespace Pentagram {
00023 
00024 class AudioSample;
00025 
00026 class AudioChannel
00027 {
00028         // We have:
00029         // 1x decompressor size
00030         // 2x frame size
00031         uint8                   *playdata;                      //
00032         uint32                  playdata_size;
00033         uint32                  decompressor_size;      // Persistent data for the decompressor
00034         uint32                  frame_size;                     // 
00035 
00036         uint32                  sample_rate;
00037         bool                    stereo;
00038 
00039         sint32                  loop;
00040         AudioSample             *sample;
00041 
00042         // Info for sampling
00043         uint32                  frame_evenodd;  // which buffer is 'frame0'
00044         uint32                  frame0_size;    // Size of the frame0 buffer in samples
00045         uint32                  frame1_size;    // Size of the frame1 buffer in samples
00046         uint32                  position;               // Position in frame0 buffer
00047         int                             lvol, rvol;             // 0-256
00048         uint32                  pitch_shift;    // 0x10000 = no shift
00049         int                             priority;               // anything. 
00050         bool                    paused;                 // true/false
00051 
00052 public:
00053         AudioChannel(uint32 sample_rate, bool stereo);
00054         ~AudioChannel(void);
00055 
00056         void stop() { sample = 0; }
00057 
00058         void playSample(AudioSample *sample, int loop, int priority, bool paused, uint32 pitch_shift, int lvol, int rvol);
00059         void resampleAndMix(sint16 *stream, uint32 bytes);
00060 
00061         bool isPlaying() { return sample != 0; }
00062 
00063         void setPitchShift(int pitch_shift_) { pitch_shift = pitch_shift_; }
00064         uint32 getPitchShift() const { return pitch_shift; }
00065 
00066         void setLoop(int loop_) { loop = loop_; }
00067         sint32 getLoop() const { return loop;}
00068 
00069         void setVolume(int lvol_, int rvol_) { lvol = lvol_;  rvol = rvol_; }
00070         void getVolume(int &lvol_, int &rvol_) const { lvol_ = lvol; rvol_ = rvol; }
00071 
00072         void setPriority(int priority_) { priority = priority_; }
00073         int getPriority() const { return priority; }
00074 
00075         void setPaused(bool paused_) { paused = paused_; }
00076         bool isPaused() const { return paused; }
00077 private:
00078 
00079         //
00080         void DecompressNextFrame();
00081 
00082         //
00083         // Resampling
00084         //
00085         class CubicInterpolator {
00086                 protected:
00087                         int x0, x1, x2, x3;
00088                         int a, b, c, d;
00089                         
00090                 public:
00091                         CubicInterpolator() : x0(0), x1(0), x2(0), x3(0)
00092                         { 
00093                                 updateCoefficients();
00094                         }
00095                         
00096                         CubicInterpolator(int a0, int a1, int a2, int a3) : x0(a0), x1(a1), x2(a2), x3(a3)
00097                         {
00098                                 updateCoefficients();
00099                         }
00100                         
00101                         CubicInterpolator(int a1, int a2, int a3) : x0(2*a1-a2), x1(a1), x2(a2), x3(a3)
00102                         {
00103                                 // We use a simple linear interpolation for x0
00104                                 updateCoefficients();
00105                         }
00106 
00107                         inline void init(int a0, int a1, int a2, int a3)
00108                         {
00109                                 x0 = a0;
00110                                 x1 = a1;
00111                                 x2 = a2;
00112                                 x3 = a3;
00113                                 updateCoefficients();
00114                         }
00115                         
00116                         inline void init(int a1, int a2, int a3)
00117                         {
00118                                 // We use a simple linear interpolation for x0
00119                                 x0 = 2*a1-a2;
00120                                 x1 = a1;
00121                                 x2 = a2;
00122                                 x3 = a3;
00123                                 updateCoefficients();
00124                         }
00125 
00126                         inline void feedData()
00127                         {
00128                                 x0 = x1;
00129                                 x1 = x2;
00130                                 x2 = x3;
00131                                 x3 = 2*x2-x1;   // Simple linear interpolation
00132                                 updateCoefficients();
00133                         }
00134 
00135                         inline void feedData(int xNew)
00136                         {
00137                                 x0 = x1;
00138                                 x1 = x2;
00139                                 x2 = x3;
00140                                 x3 = xNew;
00141                                 updateCoefficients();
00142                         }
00143                         
00144                         /* t must be a 16.16 fixed point number between 0 and 1 */
00145                         inline int interpolate(uint32 fp_pos)
00146                         {
00147                                 int result = 0;
00148                                 int t = fp_pos >> 8;
00149                                 result = (a*t + b) >> 8;
00150                                 result = (result * t + c) >> 8;
00151                                 result = (result * t + d) >> 8;
00152                                 result = (result/3 + 1) >> 1;
00153                                 
00154                                 return result;
00155                         }
00156                                 
00157                 protected:
00158                         inline void updateCoefficients()
00159                         {
00160                                 a = ((-x0*2)+(x1*5)-(x2*4)+x3);
00161                                 b = ((x0+x2-(2*x1))*6) << 8;
00162                                 c = ((-4*x0)+x1+(x2*4)-x3) << 8;
00163                                 d = (x1*6) << 8;
00164                         }
00165         };
00166 
00167         // Resampler stuff
00168         CubicInterpolator       interp_l;
00169         CubicInterpolator       interp_r;
00170         int                                     fp_pos;
00171         int                                     fp_speed;
00172 
00173         void resampleFrameM8toS(sint16 *&samples, uint32 &bytes);
00174         void resampleFrameM8toM(sint16 *&samples, uint32 &bytes);
00175 
00176 
00177 
00178 };
00179 
00180 };
00181 
00182 #endif

Generated on Fri Jul 27 22:27:09 2007 for pentagram by  doxygen 1.4.7