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.

132 lines
3.2KB

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