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.

292 lines
7.2KB

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