You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.1KB

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