1 /* SpiralLoops 2 * Copyleft (C) 2000 David Griffiths <dave@pawfal.org> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 */ 18 19 #ifndef SAMPLE 20 #define SAMPLE 21 22 #include <assert.h> 23 #include <iostream> 24 #include <cstring> 25 #include "SpiralInfo.h" 26 27 static const unsigned short UMAX_LEV = 65535; 28 static const short MAX_LEV = 32768; 29 static const float FREQ_CONV = 5.0f; 30 static const float CV_CONV = UMAX_LEV/128; 31 static const float VAL2CV = 1.0f/MAX_LEV; 32 static const float UVAL2CV = 1.0f/UMAX_LEV; 33 34 inline float Linear(float bot,float top,float pos,float val1,float val2) 35 { 36 float t=(pos-bot)/(top-bot); 37 return val1*t + val2*(1.0f-t); 38 } 39 40 class Sample 41 { 42 public: 43 44 Sample(int Len=0); 45 Sample(const Sample &rhs); 46 Sample(short *S, int Len); 47 ~Sample(); 48 49 bool Allocate(int Size); 50 void Clear(); 51 void Zero(); 52 void Insert(const Sample &S, int Pos); 53 void Add(const Sample &S); 54 void Mix(const Sample &S, int Pos); 55 void Remove(int Start, int End); 56 void Reverse(int Start, int End); 57 void Move(int Dist); 58 void GetRegion(Sample &S, int Start, int End); 59 const short *GetBuffer() const {return m_Data;} 60 short *GetNonConstBuffer() {return m_Data;} 61 int GetLength() const {return m_Length;} 62 int GetLengthInBytes() const {return m_Length*2;} 63 void Expand(int Length); 64 void Shrink(int Length); 65 void CropTo(int NewLength); 66 67 inline short &operator[](int i) const 68 { 69 assert(i>=0 && i<m_Length); 70 return m_Data[i]; 71 } 72 73 inline void Set(int i, long int v) 74 { 75 assert(i>=0 && i<m_Length); 76 // clip 77 if (v<-SpiralInfo::MAXSAMPLE) v=-SpiralInfo::MAXSAMPLE; 78 if (v>SpiralInfo::MAXSAMPLE) v=SpiralInfo::MAXSAMPLE; 79 80 m_Data[i]=(short)v; 81 } 82 83 inline Sample &operator=(const Sample &rhs) 84 { 85 Allocate(rhs.GetLength()); 86 memcpy(m_Data,rhs.GetBuffer(),GetLengthInBytes()); 87 return *this; 88 } 89 90 91 private: 92 93 short *m_Data; 94 long int m_Length; 95 }; 96 97 #endif 98