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.

237 lines
5.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 "StreamPlugin.h"
  19. #include "StreamPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. #include "../../RiffWav.h"
  23. #include "../../NoteTable.h"
  24. static const float TRIG_THRESH = 0.1;
  25. static const float BUFSECONDS = 1.0f;
  26. extern "C" {
  27. SpiralPlugin* CreateInstance()
  28. {
  29. return new StreamPlugin;
  30. }
  31. char** GetIcon()
  32. {
  33. return SpiralIcon_xpm;
  34. }
  35. int GetID()
  36. {
  37. return 0x0119;
  38. }
  39. }
  40. ///////////////////////////////////////////////////////
  41. StreamPlugin::StreamPlugin() :
  42. m_SampleSize(256),
  43. m_Pos(0),
  44. m_StreamPos(0),
  45. m_GlobalPos(0),
  46. m_Mode(PLAYM)
  47. {
  48. m_PluginInfo.Name="Stream";
  49. m_PluginInfo.Width=245;
  50. m_PluginInfo.Height=165;
  51. m_PluginInfo.NumInputs=1;
  52. m_PluginInfo.NumOutputs=2;
  53. m_PluginInfo.PortTips.push_back("Pitch CV");
  54. m_PluginInfo.PortTips.push_back("Left Out");
  55. m_PluginInfo.PortTips.push_back("Right Out");
  56. m_StreamDesc.Volume = 1.0f;
  57. m_StreamDesc.Pitch = 1.0f;
  58. m_StreamDesc.PitchMod = 1.0f;
  59. m_StreamDesc.SamplePos = -1;
  60. m_StreamDesc.Loop = false;
  61. m_StreamDesc.Note = 0;
  62. m_StreamDesc.Pathname = "None";
  63. m_StreamDesc.TriggerUp = true;
  64. m_StreamDesc.SampleRate = 44100;
  65. m_StreamDesc.Stereo = false;
  66. }
  67. StreamPlugin::~StreamPlugin()
  68. {
  69. }
  70. PluginInfo &StreamPlugin::Initialise(const HostInfo *Host)
  71. {
  72. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  73. return Info;
  74. }
  75. SpiralGUIType *StreamPlugin::CreateGUI()
  76. {
  77. m_GUI = new StreamPluginGUI(m_PluginInfo.Width,
  78. m_PluginInfo.Height,
  79. this,m_HostInfo);
  80. m_GUI->hide();
  81. return m_GUI;
  82. }
  83. void StreamPlugin::Execute()
  84. {
  85. float CVPitch=0;
  86. if (m_File.IsOpen())
  87. {
  88. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  89. {
  90. CVPitch=GetInput(0,n)*10.0f;
  91. if (m_Pos<0)
  92. {
  93. m_Pos=m_SampleSize-1;
  94. m_StreamPos-=m_SampleSize;
  95. if (m_StreamPos<0)
  96. {
  97. m_StreamPos=m_File.GetSize()/2-m_SampleSize;
  98. m_GlobalPos=m_StreamPos;
  99. }
  100. m_File.SeekToChunk(m_StreamPos);
  101. m_File.LoadChunk(m_SampleSize, m_SampleL, m_SampleR);
  102. }
  103. else
  104. {
  105. if (m_Pos>=m_SampleSize)
  106. {
  107. //m_SampleSize=(int)(BUFSECONDS*m_HostInfo->SAMPLERATE*m_StreamDesc.PitchMod);
  108. m_Pos=0;
  109. m_StreamPos+=m_SampleSize;
  110. if (m_StreamPos>=m_File.GetSize()/2)
  111. {
  112. m_StreamPos=0;
  113. m_GlobalPos=0;
  114. }
  115. m_File.SeekToChunk(m_StreamPos);
  116. m_File.LoadChunk(m_SampleSize, m_SampleL, m_SampleR);
  117. }
  118. }
  119. SetOutput(0,n,m_SampleL[m_Pos]*m_StreamDesc.Volume);
  120. SetOutput(1,n,m_SampleR[m_Pos]*m_StreamDesc.Volume);
  121. m_Pos+=m_StreamDesc.PitchMod+CVPitch;
  122. m_GlobalPos+=m_StreamDesc.PitchMod+CVPitch;
  123. }
  124. if (((StreamPluginGUI*)m_GUI)->visible())
  125. ((StreamPluginGUI*)m_GUI)->SetTime(GetTime());
  126. }
  127. }
  128. void StreamPlugin::StreamOut(ostream &s)
  129. {
  130. s<<m_Version<<" ";
  131. s<<m_StreamDesc.Volume<<" "<<
  132. m_StreamDesc.PitchMod<<" "<<
  133. m_StreamDesc.Pathname.size()<<" "<<
  134. m_StreamDesc.Pathname<<" ";
  135. s<<m_Pos<<" ";
  136. s<<m_StreamPos<<" ";
  137. s<<m_GlobalPos<<" ";
  138. s<<m_Pitch<<" "<<endl;
  139. }
  140. void StreamPlugin::StreamIn(istream &s)
  141. {
  142. int version;
  143. s>>version;
  144. s>>m_StreamDesc.Volume>>
  145. m_StreamDesc.PitchMod;
  146. char Buf[4096];
  147. int size;
  148. s>>size;
  149. s.ignore(1);
  150. s.get(Buf,size+1);
  151. m_StreamDesc.Pathname=Buf;
  152. if (m_StreamDesc.Pathname!="None")
  153. OpenStream(m_StreamDesc.Pathname);
  154. s>>m_Pos;
  155. s>>m_StreamPos;
  156. s>>m_GlobalPos;
  157. s>>m_Pitch;
  158. }
  159. void StreamPlugin::OpenStream(const string &Name)
  160. {
  161. m_StreamPos=0;
  162. m_GlobalPos=0;
  163. if (m_File.IsOpen()) m_File.Close();
  164. m_File.Open(Name,WavFile::READ);
  165. m_SampleL.Allocate(m_SampleSize);
  166. m_SampleR.Allocate(m_SampleSize);
  167. m_StreamDesc.Pathname=Name;
  168. m_StreamDesc.SampleRate=m_File.GetSamplerate();
  169. m_StreamDesc.Stereo=m_File.IsStereo();
  170. m_StreamDesc.Pitch = m_StreamDesc.SampleRate/(float)m_HostInfo->SAMPLERATE;
  171. if (m_StreamDesc.Stereo)
  172. {
  173. m_StreamDesc.Pitch*=2;
  174. ((StreamPluginGUI*)m_GUI)->SetMaxTime(GetLength());
  175. }
  176. else ((StreamPluginGUI*)m_GUI)->SetMaxTime(GetLength()/2);
  177. }
  178. void StreamPlugin::Restart()
  179. {
  180. m_StreamPos=0; m_GlobalPos=0;
  181. }
  182. void StreamPlugin::Play()
  183. {
  184. if (m_Mode==PLAYM) return;
  185. m_StreamDesc.PitchMod=m_Pitch;
  186. m_Mode=PLAYM;
  187. }
  188. void StreamPlugin::Stop()
  189. {
  190. if (m_Mode==STOPM) return;
  191. m_Pitch=m_StreamDesc.PitchMod;
  192. m_StreamDesc.PitchMod=0.0f;
  193. m_Mode=STOPM;
  194. }
  195. float StreamPlugin::GetLength()
  196. {
  197. if (m_StreamDesc.Stereo) return (m_File.GetSize()/(float)m_StreamDesc.SampleRate)*0.5f;
  198. else return m_File.GetSize()/(float)m_StreamDesc.SampleRate;
  199. }
  200. void StreamPlugin::SetTime(float t)
  201. {
  202. m_GlobalPos=m_StreamDesc.SampleRate*t;
  203. m_StreamPos=(int)(m_StreamDesc.SampleRate*t);
  204. m_Pos=m_SampleSize;
  205. }