Jack2  1.9.10
JackResampler.h
00001 /*
00002 Copyright (C) 2008 Grame
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #ifndef __JackResampler__
00021 #define __JackResampler__
00022 
00023 #include "ringbuffer.h"
00024 #include "types.h"
00025 
00026 namespace Jack
00027 {
00028 
00029 #define DEFAULT_RB_SIZE 32768
00030 #define DEFAULT_ADAPTATIVE_SIZE 2048
00031 
00032 inline float Range(float min, float max, float val)
00033 {
00034     return (val < min) ? min : ((val > max) ? max : val);
00035 }
00036 
00041 class JackRingBuffer
00042 {
00043 
00044     protected:
00045 
00046         jack_ringbuffer_t* fRingBuffer;
00047         unsigned int fRingBufferSize;
00048 
00049     public:
00050 
00051         JackRingBuffer(int size = DEFAULT_RB_SIZE);
00052         virtual ~JackRingBuffer();
00053 
00054         virtual void Reset(unsigned int new_size);
00055 
00056         // in frames
00057         virtual unsigned int Read(jack_default_audio_sample_t* buffer, unsigned int frames);
00058         virtual unsigned int Write(jack_default_audio_sample_t* buffer, unsigned int frames);
00059 
00060         // in bytes
00061         virtual unsigned int Read(void* buffer, unsigned int bytes);
00062         virtual unsigned int Write(void* buffer, unsigned int bytes);
00063 
00064         // in frames
00065         virtual unsigned int ReadSpace();
00066         virtual unsigned int WriteSpace();
00067 
00068         unsigned int GetError()
00069         {
00070             return (jack_ringbuffer_read_space(fRingBuffer) / sizeof(float)) - (fRingBufferSize / 2);
00071         }
00072 
00073 };
00074 
00079 class JackResampler : public JackRingBuffer
00080 {
00081 
00082     protected:
00083 
00084         double fRatio;
00085   
00086     public:
00087 
00088         JackResampler():JackRingBuffer(),fRatio(1)
00089         {}
00090         virtual ~JackResampler()
00091         {}
00092 
00093         virtual unsigned int ReadResample(jack_default_audio_sample_t* buffer, unsigned int frames);
00094         virtual unsigned int WriteResample(jack_default_audio_sample_t* buffer, unsigned int frames);
00095 
00096         void SetRatio(double ratio)
00097         {
00098             fRatio = Range(0.25, 4.0, ratio);
00099         }
00100 
00101         double GetRatio()
00102         {
00103             return fRatio;
00104         }
00105 
00106     };
00107 }
00108 
00109 #endif