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.

256 lines
5.9KB

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