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.

287 lines
7.1KB

  1. /* SpiralSound
  2. * Copyleft (C) 2001 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. #include "SamplerPlugin.h"
  19. #include "SamplerPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. #include "../../RiffWav.h"
  23. #include "../../NoteTable.h"
  24. static const int NOTETRIG = NUM_SAMPLES*2+1;
  25. extern "C" {
  26. SpiralPlugin* CreateInstance()
  27. {
  28. return new SamplerPlugin;
  29. }
  30. char** GetIcon()
  31. {
  32. return SpiralIcon_xpm;
  33. }
  34. int GetID()
  35. {
  36. return 0x0010;
  37. }
  38. }
  39. ///////////////////////////////////////////////////////
  40. SamplerPlugin::SamplerPlugin()
  41. {
  42. m_PluginInfo.Name="Sampler";
  43. m_PluginInfo.Width=245;
  44. m_PluginInfo.Height=420;
  45. m_PluginInfo.NumInputs=18;
  46. m_PluginInfo.NumOutputs=9;
  47. m_PluginInfo.PortTips.push_back("Sample 1 Pitch");
  48. m_PluginInfo.PortTips.push_back("Sample 1 Trigger");
  49. m_PluginInfo.PortTips.push_back("Sample 2 Pitch");
  50. m_PluginInfo.PortTips.push_back("Sample 2 Trigger");
  51. m_PluginInfo.PortTips.push_back("Sample 3 Pitch");
  52. m_PluginInfo.PortTips.push_back("Sample 3 Trigger");
  53. m_PluginInfo.PortTips.push_back("Sample 4 Pitch");
  54. m_PluginInfo.PortTips.push_back("Sample 4 Trigger");
  55. m_PluginInfo.PortTips.push_back("Sample 5 Pitch");
  56. m_PluginInfo.PortTips.push_back("Sample 5 Trigger");
  57. m_PluginInfo.PortTips.push_back("Sample 6 Pitch");
  58. m_PluginInfo.PortTips.push_back("Sample 6 Trigger");
  59. m_PluginInfo.PortTips.push_back("Sample 7 Pitch");
  60. m_PluginInfo.PortTips.push_back("Sample 7 Trigger");
  61. m_PluginInfo.PortTips.push_back("Sample 8 Pitch");
  62. m_PluginInfo.PortTips.push_back("Sample 8 Trigger");
  63. m_PluginInfo.PortTips.push_back("Input");
  64. m_PluginInfo.PortTips.push_back("Sample trigger pitch");
  65. m_PluginInfo.PortTips.push_back("Mixed Output");
  66. m_PluginInfo.PortTips.push_back("Sample 1 Output");
  67. m_PluginInfo.PortTips.push_back("Sample 2 Output");
  68. m_PluginInfo.PortTips.push_back("Sample 3 Output");
  69. m_PluginInfo.PortTips.push_back("Sample 4 Output");
  70. m_PluginInfo.PortTips.push_back("Sample 5 Output");
  71. m_PluginInfo.PortTips.push_back("Sample 6 Output");
  72. m_PluginInfo.PortTips.push_back("Sample 7 Output");
  73. m_PluginInfo.PortTips.push_back("Sample 8 Output");
  74. for (int n=0; n<NUM_SAMPLES; n++)
  75. {
  76. Sample* NewSample = new Sample;
  77. m_SampleVec.push_back(NewSample);
  78. SampleDesc* NewDesc = new SampleDesc;
  79. NewDesc->Volume = 1.0f;
  80. NewDesc->Pitch = 1.0f;
  81. NewDesc->PitchMod = 1.0f;
  82. NewDesc->SamplePos = -1;
  83. NewDesc->Loop = false;
  84. NewDesc->Note = n;
  85. NewDesc->Pathname = "None";
  86. NewDesc->TriggerUp = true;
  87. NewDesc->SampleRate = 44100;
  88. NewDesc->Stereo = false;
  89. m_SampleDescVec.push_back(NewDesc);
  90. }
  91. m_Version=2;
  92. }
  93. SamplerPlugin::~SamplerPlugin()
  94. {
  95. for (vector<Sample*>::iterator i=m_SampleVec.begin();
  96. i!=m_SampleVec.end(); i++)
  97. {
  98. delete(*i);
  99. }
  100. for (vector<SampleDesc*>::iterator i=m_SampleDescVec.begin();
  101. i!=m_SampleDescVec.end(); i++)
  102. {
  103. delete(*i);
  104. }
  105. }
  106. PluginInfo &SamplerPlugin::Initialise(const HostInfo *Host)
  107. {
  108. return SpiralPlugin::Initialise(Host);;
  109. }
  110. SpiralGUIType *SamplerPlugin::CreateGUI()
  111. {
  112. m_GUI = new SamplerPluginGUI(m_PluginInfo.Width,
  113. m_PluginInfo.Height,
  114. this,m_HostInfo);
  115. m_GUI->hide();
  116. return m_GUI;
  117. }
  118. void SamplerPlugin::Execute()
  119. {
  120. for (int s=0; s<NUM_SAMPLES+1; s++)
  121. {
  122. GetOutputBuf(s)->Zero();
  123. }
  124. float Freq=0;
  125. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  126. {
  127. Freq=GetInputPitch(NOTETRIG,n);
  128. for (int s=0; s<NUM_SAMPLES; s++)
  129. {
  130. SampleDesc* S=m_SampleDescVec[s];
  131. // if we have a sample here
  132. if (m_SampleVec[s]->GetLength())
  133. {
  134. // Convert the CV input into a useable trigger
  135. if (GetInput(s*2+1,n)>0 || feq(Freq,NoteTable[S->Note],0.01f))
  136. {
  137. if (S->TriggerUp)
  138. {
  139. S->SamplePos=0;
  140. S->TriggerUp=false;
  141. if (InputExists(s*2))
  142. {
  143. // Get the pitch from the CV
  144. float PlayFreq=GetInputPitch(s*2,n);
  145. // assumtion: base frequency = 440 (middle A)
  146. S->Pitch = PlayFreq/440;
  147. S->Pitch *= S->SampleRate/(float)m_HostInfo->SAMPLERATE;
  148. // sort of legacy
  149. if (S->Stereo) S->Pitch/=2;
  150. }
  151. }
  152. }
  153. else
  154. {
  155. S->TriggerUp=true;
  156. // end it if it's looping
  157. if (S->Loop)
  158. {
  159. S->SamplePos=-1;
  160. }
  161. }
  162. // if the sample has ended
  163. if (S->SamplePos>=m_SampleVec[s]->GetLength())
  164. {
  165. if (S->Loop) S->SamplePos=0;
  166. else
  167. {
  168. S->SamplePos=-1;
  169. }
  170. }
  171. if (S->SamplePos!=-1)
  172. {
  173. // mix the sample to the output.
  174. MixOutput(0,n,(*m_SampleVec[s])[S->SamplePos]*S->Volume);
  175. // copy the sample to it's individual output.
  176. SetOutput(s+1,n,((*m_SampleVec[s])[S->SamplePos]*S->Volume));
  177. S->SamplePos+=S->Pitch*S->PitchMod;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. void SamplerPlugin::StreamOut(ostream &s)
  184. {
  185. s<<m_Version<<" ";
  186. if (m_Version>1)
  187. {
  188. for (int n=0; n<NUM_SAMPLES; n++)
  189. {
  190. s<<m_SampleDescVec[n]->Volume<<" "<<
  191. m_SampleDescVec[n]->PitchMod<<" "<<
  192. m_SampleDescVec[n]->Pathname.size()<<" "<<
  193. m_SampleDescVec[n]->Pathname<<" "<<
  194. m_SampleDescVec[n]->Note<<endl;
  195. }
  196. }
  197. else
  198. {
  199. for (int n=0; n<NUM_SAMPLES; n++)
  200. {
  201. s<<m_SampleDescVec[n]->Volume<<" "<<
  202. m_SampleDescVec[n]->PitchMod<<" "<<
  203. m_SampleDescVec[n]->Pathname<<" "<<
  204. m_SampleDescVec[n]->Note<<endl;
  205. }
  206. }
  207. }
  208. void SamplerPlugin::StreamIn(istream &s)
  209. {
  210. int version;
  211. s>>version;
  212. if (version>1)
  213. {
  214. char Buf[4096];
  215. for (int n=0; n<NUM_SAMPLES; n++)
  216. {
  217. s>>m_SampleDescVec[n]->Volume>>
  218. m_SampleDescVec[n]->PitchMod;
  219. int size;
  220. s>>size;
  221. s.ignore(1);
  222. s.get(Buf,size+1);
  223. m_SampleDescVec[n]->Pathname=Buf;
  224. s>>m_SampleDescVec[n]->Note;
  225. if (m_SampleDescVec[n]->Pathname!="None") LoadSample(n, m_SampleDescVec[n]->Pathname);
  226. }
  227. }
  228. else
  229. {
  230. for (int n=0; n<NUM_SAMPLES; n++)
  231. {
  232. s>>m_SampleDescVec[n]->Volume>>
  233. m_SampleDescVec[n]->PitchMod>>
  234. m_SampleDescVec[n]->Pathname>>
  235. m_SampleDescVec[n]->Note;
  236. if (m_SampleDescVec[n]->Pathname!="None") LoadSample(n, m_SampleDescVec[n]->Pathname);
  237. }
  238. }
  239. }
  240. void SamplerPlugin::LoadSample(int n, const string &Name)
  241. {
  242. WavFile Wav;
  243. if (Wav.Open(Name,WavFile::READ))
  244. {
  245. m_SampleVec[n]->Allocate(Wav.GetSize());
  246. Wav.Load(*m_SampleVec[n]);
  247. m_SampleDescVec[n]->Pathname=Name;
  248. m_SampleDescVec[n]->SampleRate=Wav.GetSamplerate();
  249. m_SampleDescVec[n]->Stereo=Wav.IsStereo();
  250. m_SampleDescVec[n]->Pitch *= m_SampleDescVec[n]->SampleRate/(float)m_HostInfo->SAMPLERATE;
  251. }
  252. }