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.

294 lines
7.3KB

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