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.

235 lines
6.8KB

  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* SpiralPlugin_CreateInstance()
  28. {
  29. return new StreamPlugin;
  30. }
  31. char** SpiralPlugin_GetIcon()
  32. {
  33. return SpiralIcon_xpm;
  34. }
  35. int SpiralPlugin_GetID()
  36. {
  37. return 0x0119;
  38. }
  39. string SpiralPlugin_GetGroupName()
  40. {
  41. return "Delay/Sampling";
  42. }
  43. }
  44. ///////////////////////////////////////////////////////
  45. StreamPlugin::StreamPlugin() :
  46. m_SampleRate (44100),
  47. m_SampleSize (256),
  48. m_StreamPos (0),
  49. m_GlobalPos (0),
  50. m_Pitch (1.0f),
  51. m_SamplePos (-1),
  52. m_Pos (0),
  53. m_Mode(STOPM)
  54. {
  55. m_PluginInfo.Name = "Stream";
  56. m_PluginInfo.Width = 245;
  57. m_PluginInfo.Height = 165;
  58. m_PluginInfo.NumInputs = 3;
  59. m_PluginInfo.NumOutputs = 3;
  60. m_PluginInfo.PortTips.push_back ("Pitch CV");
  61. m_PluginInfo.PortTips.push_back ("Play Trigger");
  62. m_PluginInfo.PortTips.push_back ("Stop Trigger");
  63. m_PluginInfo.PortTips.push_back ("Left Out");
  64. m_PluginInfo.PortTips.push_back ("Right Out");
  65. m_PluginInfo.PortTips.push_back ("Finish Trigger");
  66. m_GUIArgs.Volume = 1.0f;
  67. m_GUIArgs.PitchMod = 1.0f;
  68. m_GUIArgs.PlayOut = false;
  69. m_AudioCH->Register ("Volume", &m_GUIArgs.Volume);
  70. m_AudioCH->Register ("Pitch", &m_GUIArgs.PitchMod, ChannelHandler::INPUT);
  71. m_AudioCH->RegisterData ("FileName", ChannelHandler::INPUT,
  72. &m_GUIArgs.FileName, sizeof (m_GUIArgs.FileName));
  73. m_AudioCH->Register ("Time", &m_GUIArgs.Time);
  74. m_AudioCH->Register ("TimeOut", &m_GUIArgs.TimeOut, ChannelHandler::OUTPUT);
  75. m_AudioCH->Register ("MaxTime", &m_GUIArgs.MaxTime, ChannelHandler::OUTPUT);
  76. m_AudioCH->Register ("Playing", &m_GUIArgs.PlayOut, ChannelHandler::OUTPUT);
  77. }
  78. StreamPlugin::~StreamPlugin()
  79. {
  80. }
  81. PluginInfo &StreamPlugin::Initialise(const HostInfo *Host)
  82. {
  83. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  84. return Info;
  85. }
  86. SpiralGUIType *StreamPlugin::CreateGUI() {
  87. return new StreamPluginGUI(m_PluginInfo.Width, m_PluginInfo.Height, this, m_AudioCH, m_HostInfo);
  88. }
  89. void StreamPlugin::Execute() {
  90. if (m_File.IsOpen()) {
  91. for (int n=0; n<m_HostInfo->BUFSIZE; n++) {
  92. bool FinTrig = false;
  93. float CVPitch = GetInput(0, n)*10.0f;
  94. if (GetInput (1, n) > 0) m_Mode = PLAYM;
  95. if (GetInput (2, n) > 0) {
  96. m_Mode = STOPM;
  97. m_Pos = 0;
  98. m_GlobalPos = 0;
  99. m_StreamPos = 0;
  100. }
  101. if (m_Pos<0) {
  102. m_Pos = m_SampleSize - 1;
  103. m_StreamPos -= m_SampleSize;
  104. FinTrig = m_StreamPos < 0;
  105. if (FinTrig) {
  106. m_StreamPos = m_File.GetSize() - m_SampleSize;
  107. m_GlobalPos = m_StreamPos;
  108. }
  109. m_File.SeekToChunk (m_StreamPos);
  110. m_File.LoadChunk (m_SampleSize, m_SampleL, m_SampleR);
  111. }
  112. else if (m_Pos >= m_SampleSize) {
  113. m_Pos = 0;
  114. m_StreamPos += m_SampleSize;
  115. FinTrig = m_StreamPos >= m_File.GetSize();
  116. if (FinTrig) {
  117. m_StreamPos = 0;
  118. m_GlobalPos = 0;
  119. }
  120. m_File.SeekToChunk (m_StreamPos);
  121. m_File.LoadChunk (m_SampleSize, m_SampleL, m_SampleR);
  122. }
  123. if (FinTrig) SetOutput (2, n, 1);
  124. else SetOutput (2, n, 0);
  125. if (m_Mode==PLAYM) {
  126. SetOutput (0, n, m_SampleL[m_Pos] * m_GUIArgs.Volume);
  127. SetOutput (1, n, m_SampleR[m_Pos] * m_GUIArgs.Volume);
  128. m_Pos += m_GUIArgs.PitchMod + CVPitch;
  129. m_GlobalPos += m_GUIArgs.PitchMod + CVPitch;
  130. }
  131. else {
  132. SetOutput (0, n, 0);
  133. SetOutput (1, n, 0);
  134. }
  135. }
  136. m_GUIArgs.TimeOut = m_GlobalPos / (float)m_SampleRate;
  137. m_GUIArgs.PlayOut = m_Mode==PLAYM;
  138. }
  139. }
  140. void StreamPlugin::ExecuteCommands() {
  141. if (m_AudioCH->IsCommandWaiting()) {
  142. switch (m_AudioCH->GetCommand()) {
  143. case SET_TIME:
  144. SetTime();
  145. break;
  146. case LOAD:
  147. OpenStream();
  148. break;
  149. case RESTART:
  150. m_StreamPos = 0;
  151. m_GlobalPos = 0;
  152. break;
  153. case STOP:
  154. m_Mode = STOPM;
  155. break;
  156. case PLAY:
  157. m_Mode = PLAYM;
  158. break;
  159. }
  160. }
  161. }
  162. void StreamPlugin::SetTime (void) {
  163. m_GlobalPos = m_SampleRate * m_GUIArgs.Time;
  164. m_StreamPos = (int)(m_SampleRate * m_GUIArgs.Time);
  165. m_Pos = m_SampleSize;
  166. }
  167. void StreamPlugin::OpenStream (void) {
  168. m_StreamPos = 0;
  169. m_GlobalPos = 0;
  170. if (m_File.IsOpen ()) m_File.Close ();
  171. m_File.Open (m_GUIArgs.FileName, WavFile::READ);
  172. m_SampleL.Allocate (m_SampleSize);
  173. m_SampleR.Allocate (m_SampleSize);
  174. m_Pitch = m_SampleRate / (float)m_HostInfo->SAMPLERATE;
  175. if (m_File.IsStereo ()) {
  176. m_Pitch *= 2;
  177. m_GUIArgs.MaxTime = GetLength();
  178. }
  179. else m_GUIArgs.MaxTime = GetLength() / 2;
  180. }
  181. float StreamPlugin::GetLength (void) {
  182. if (m_File.IsStereo()) return m_File.GetSize() / (float)m_File.GetSamplerate ();
  183. else return m_File.GetSize() / (float)m_File.GetSamplerate () * 2;
  184. }
  185. void StreamPlugin::StreamOut (ostream &s) {
  186. s << m_Version << " "
  187. << m_GUIArgs.Volume << " "
  188. << m_GUIArgs.PitchMod << " "
  189. << strlen (m_GUIArgs.FileName) << " "
  190. << m_GUIArgs.FileName << " "
  191. // is it really necessary to save this lot??
  192. << m_Pos << " "
  193. << m_StreamPos << " "
  194. << m_GlobalPos << " "
  195. << m_Pitch << " " << endl;
  196. }
  197. void StreamPlugin::StreamIn(istream &s) {
  198. int version;
  199. s >> version;
  200. s >> m_GUIArgs.Volume >> m_GUIArgs.PitchMod;
  201. int size;
  202. s >> size;
  203. if (size > 255) size = 255;
  204. s.ignore (1);
  205. s.get (m_GUIArgs.FileName, size+1);
  206. if (m_GUIArgs.FileName != "None") OpenStream ();
  207. // is it really necessary to load this lot??
  208. s >> m_Pos;
  209. s >> m_StreamPos;
  210. s >> m_GlobalPos;
  211. s >> m_Pitch;
  212. }