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.

261 lines
6.0KB

  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. string GetGroupName()
  40. {
  41. return "SpiralSound";
  42. }
  43. }
  44. ///////////////////////////////////////////////////////
  45. StreamPlugin::StreamPlugin() :
  46. m_SampleSize(256),
  47. m_Pos(0),
  48. m_StreamPos(0),
  49. m_GlobalPos(0),
  50. m_Mode(STOPM)
  51. {
  52. m_PluginInfo.Name="Stream";
  53. m_PluginInfo.Width=245;
  54. m_PluginInfo.Height=165;
  55. m_PluginInfo.NumInputs=1;
  56. m_PluginInfo.NumOutputs=2;
  57. m_PluginInfo.PortTips.push_back("Pitch CV");
  58. m_PluginInfo.PortTips.push_back("Left Out");
  59. m_PluginInfo.PortTips.push_back("Right Out");
  60. m_StreamDesc.Volume = 1.0f;
  61. m_StreamDesc.Pitch = 1.0f;
  62. m_StreamDesc.PitchMod = 1.0f;
  63. m_StreamDesc.SamplePos = -1;
  64. m_StreamDesc.Loop = false;
  65. m_StreamDesc.Note = 0;
  66. m_StreamDesc.Pathname = "None";
  67. m_StreamDesc.TriggerUp = true;
  68. m_StreamDesc.SampleRate = 44100;
  69. m_StreamDesc.Stereo = false;
  70. m_AudioCH->Register("Volume",&m_StreamDesc.Volume);
  71. m_AudioCH->Register("Pitch",&m_StreamDesc.PitchMod,ChannelHandler::INPUT);
  72. m_AudioCH->RegisterData("FileName",ChannelHandler::INPUT,&m_FileNameArg,sizeof(m_FileNameArg));
  73. m_AudioCH->Register("Time",&m_TimeArg);
  74. m_AudioCH->Register("TimeOut",&m_TimeOut,ChannelHandler::OUTPUT);
  75. m_AudioCH->Register("MaxTime",&m_MaxTime,ChannelHandler::OUTPUT);
  76. }
  77. StreamPlugin::~StreamPlugin()
  78. {
  79. }
  80. PluginInfo &StreamPlugin::Initialise(const HostInfo *Host)
  81. {
  82. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  83. return Info;
  84. }
  85. SpiralGUIType *StreamPlugin::CreateGUI()
  86. {
  87. return new StreamPluginGUI(m_PluginInfo.Width,
  88. m_PluginInfo.Height,
  89. this,m_AudioCH,m_HostInfo);
  90. }
  91. void StreamPlugin::Execute()
  92. {
  93. float CVPitch=0;
  94. if (m_File.IsOpen())
  95. {
  96. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  97. {
  98. CVPitch=GetInput(0,n)*10.0f;
  99. if (m_Pos<0)
  100. {
  101. m_Pos=m_SampleSize-1;
  102. m_StreamPos-=m_SampleSize;
  103. if (m_StreamPos<0)
  104. {
  105. m_StreamPos=m_File.GetSize()/2-m_SampleSize;
  106. m_GlobalPos=m_StreamPos;
  107. }
  108. m_File.SeekToChunk(m_StreamPos);
  109. m_File.LoadChunk(m_SampleSize, m_SampleL, m_SampleR);
  110. }
  111. else
  112. {
  113. if (m_Pos>=m_SampleSize)
  114. {
  115. //m_SampleSize=(int)(BUFSECONDS*m_HostInfo->SAMPLERATE*m_StreamDesc.PitchMod);
  116. m_Pos=0;
  117. m_StreamPos+=m_SampleSize;
  118. if (m_StreamPos>=m_File.GetSize()/2)
  119. {
  120. m_StreamPos=0;
  121. m_GlobalPos=0;
  122. }
  123. m_File.SeekToChunk(m_StreamPos);
  124. m_File.LoadChunk(m_SampleSize, m_SampleL, m_SampleR);
  125. }
  126. }
  127. SetOutput(0,n,m_SampleL[m_Pos]*m_StreamDesc.Volume);
  128. SetOutput(1,n,m_SampleR[m_Pos]*m_StreamDesc.Volume);
  129. m_Pos+=m_StreamDesc.PitchMod+CVPitch;
  130. m_GlobalPos+=m_StreamDesc.PitchMod+CVPitch;
  131. }
  132. m_TimeOut=GetTime();
  133. }
  134. }
  135. void StreamPlugin::ExecuteCommands()
  136. {
  137. if (m_AudioCH->IsCommandWaiting())
  138. {
  139. switch(m_AudioCH->GetCommand())
  140. {
  141. case (LOAD) : OpenStream(m_FileNameArg); break;
  142. case (RESTART) : Restart(); break;
  143. case (STOP) : Stop(); break;
  144. case (PLAY) : Play(); break;
  145. case (NUDGE) : Nudge(); break;
  146. case (SET_TIME) : SetTime(m_TimeArg); break;
  147. }
  148. }
  149. }
  150. void StreamPlugin::StreamOut(ostream &s)
  151. {
  152. s<<m_Version<<" ";
  153. s<<m_StreamDesc.Volume<<" "<<
  154. m_StreamDesc.PitchMod<<" "<<
  155. m_StreamDesc.Pathname.size()<<" "<<
  156. m_StreamDesc.Pathname<<" ";
  157. s<<m_Pos<<" ";
  158. s<<m_StreamPos<<" ";
  159. s<<m_GlobalPos<<" ";
  160. s<<m_Pitch<<" "<<endl;
  161. }
  162. void StreamPlugin::StreamIn(istream &s)
  163. {
  164. int version;
  165. s>>version;
  166. s>>m_StreamDesc.Volume>>
  167. m_StreamDesc.PitchMod;
  168. char Buf[4096];
  169. int size;
  170. s>>size;
  171. s.ignore(1);
  172. s.get(Buf,size+1);
  173. m_StreamDesc.Pathname=Buf;
  174. if (m_StreamDesc.Pathname!="None")
  175. OpenStream(m_StreamDesc.Pathname);
  176. s>>m_Pos;
  177. s>>m_StreamPos;
  178. s>>m_GlobalPos;
  179. s>>m_Pitch;
  180. }
  181. void StreamPlugin::OpenStream(const string &Name)
  182. {
  183. m_StreamPos=0;
  184. m_GlobalPos=0;
  185. if (m_File.IsOpen()) m_File.Close();
  186. m_File.Open(Name,WavFile::READ);
  187. m_SampleL.Allocate(m_SampleSize);
  188. m_SampleR.Allocate(m_SampleSize);
  189. m_StreamDesc.Pathname=Name;
  190. m_StreamDesc.SampleRate=m_File.GetSamplerate();
  191. m_StreamDesc.Stereo=m_File.IsStereo();
  192. m_StreamDesc.Pitch = m_StreamDesc.SampleRate/(float)m_HostInfo->SAMPLERATE;
  193. if (m_StreamDesc.Stereo)
  194. {
  195. m_StreamDesc.Pitch*=2;
  196. m_MaxTime=GetLength();
  197. }
  198. else m_MaxTime=GetLength()/2;
  199. }
  200. void StreamPlugin::Restart()
  201. {
  202. m_StreamPos=0; m_GlobalPos=0;
  203. }
  204. void StreamPlugin::Play()
  205. {
  206. if (m_Mode==PLAYM) return;
  207. m_StreamDesc.PitchMod=m_Pitch;
  208. m_Mode=PLAYM;
  209. }
  210. void StreamPlugin::Stop()
  211. {
  212. if (m_Mode==STOPM) return;
  213. m_Pitch=m_StreamDesc.PitchMod;
  214. m_StreamDesc.PitchMod=0.0f;
  215. m_Mode=STOPM;
  216. }
  217. float StreamPlugin::GetLength()
  218. {
  219. if (m_StreamDesc.Stereo) return (m_File.GetSize()/(float)m_StreamDesc.SampleRate)*0.5f;
  220. else return m_File.GetSize()/(float)m_StreamDesc.SampleRate;
  221. }
  222. void StreamPlugin::SetTime(float t)
  223. {
  224. m_GlobalPos=m_StreamDesc.SampleRate*t;
  225. m_StreamPos=(int)(m_StreamDesc.SampleRate*t);
  226. m_Pos=m_SampleSize;
  227. }