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 #define PLUGINGUI_IN_MODULE_TEST
23 
24 #include <assert.h>
25 #include <limits.h>
26 #include <cstring>
27 #include <iostream>
28 
29 //#define DEBUG
30 
Linear(float bot,float top,float pos,float val1,float val2)31 inline float Linear(float bot,float top,float pos,float val1,float val2)
32 {
33     float t=(pos-bot)/(top-bot);
34     return val1*t + val2*(1.0f-t);
35 }
36 
feq(float a,float b,float tol)37 inline bool feq(float a, float b, float tol)
38 {
39 	return (a>b-tol && a<b+tol);
40 }
41 
42 class Sample
43 {
44 public:
45 	enum SampleType {AUDIO=0, IMAGE, MIDI};
46 
47 	Sample(int Len=0);
48 	Sample(const Sample &rhs);
49 	Sample(const float *S, int Len);
50 	~Sample();
51 
52 	bool Allocate(int Size);
53 	void Clear();
54 	void Zero();
55 	void Set(float Val);
56 	void Insert(const Sample &S, int Pos);
57 	void Add(const Sample &S);
58 	void Mix(const Sample &S, int Pos);
59 	void Remove(int Start, int End);
60 	void Reverse(int Start, int End);
61 	void Move(int Dist);
62 	void GetRegion(Sample &S, int Start, int End) const;
GetBuffer()63 	const float *GetBuffer() const {return m_Data;}
GetNonConstBuffer()64 	float *GetNonConstBuffer() {return m_Data;}
GetLength()65 	int  GetLength() const {return m_Length;}
GetLengthInBytes()66 	int  GetLengthInBytes() const {return m_Length*sizeof(float);}
67 	void Expand(int Length);
68 	void Shrink(int Length);
69 	void CropTo(int NewLength);
IsEmpty()70 	bool IsEmpty() const { return m_IsEmpty; }
71 
72 	inline float &operator[](int i) const
73 	{
74 		#ifdef DEBUG
75 			assert(i>=0 && i<m_Length);
76 		#endif
77 		return m_Data[i];
78 	}
79 
80 	// Linear interpolated
81 	inline float operator[](float i) const
82 	{
83 		int ii=(int)i;
84 
85 		#ifdef DEBUG
86 			assert(ii>=0 && ii<m_Length);
87 		#endif
88 
89 		if (ii==m_Length-1) return m_Data[ii];
90 		float t=i-ii;
91 		return ((m_Data[ii]*(1-t))+(m_Data[ii+1])*t);
92 	}
93 
94 
Set(int i,float v)95 	inline void Set(int i, float v)
96 	{
97 		m_IsEmpty=false;
98 		#ifdef DEBUG
99 			assert(i>=0 && i<m_Length);
100 		#endif
101 		m_Data[i]=v;
102 	}
103 
104 	inline Sample &operator=(const Sample &rhs)
105 	{
106 		Allocate(rhs.GetLength());
107 		memcpy(m_Data,rhs.GetBuffer(),GetLengthInBytes());
108 		m_IsEmpty=rhs.m_IsEmpty;
109 		return *this;
110 	}
111 
SetDataGranularity(int s)112 	void SetDataGranularity(int s) { m_DataGranularity=s; }
113 
setSpecificData(void * ptr)114     void setSpecificData(void *ptr) { m_PluginSpecificData = ptr; }
getSpecificData()115     void *getSpecificData() { return m_PluginSpecificData; }
setSampleType(SampleType t)116     void setSampleType(SampleType t) { m_SampleType = t; }
getSampleType()117     SampleType getSampleType() { return m_SampleType; }
118 
119 protected:
120 	bool m_IsEmpty;
121 
122 private:
123 	int m_DataGranularity;
124 	float *m_Data;
125 	long  int  m_Length;
126 
127 	void *m_PluginSpecificData;
128     SampleType m_SampleType;
129 };
130 
131 #endif
132