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.

211 lines
6.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* 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_AudioCH->Register ("Volume", &m_GUIArgs.Volume);
  69. m_AudioCH->Register ("Pitch", &m_GUIArgs.PitchMod, ChannelHandler::INPUT);
  70. m_AudioCH->RegisterData ("FileName", ChannelHandler::INPUT,
  71. &m_GUIArgs.FileName, sizeof (m_GUIArgs.FileName));
  72. m_AudioCH->Register ("Time", &m_GUIArgs.Time);
  73. m_AudioCH->Register ("TimeOut", &m_GUIArgs.TimeOut, ChannelHandler::OUTPUT);
  74. m_AudioCH->Register ("MaxTime", &m_GUIArgs.MaxTime, ChannelHandler::OUTPUT);
  75. }
  76. StreamPlugin::~StreamPlugin()
  77. {
  78. }
  79. PluginInfo &StreamPlugin::Initialise(const HostInfo *Host)
  80. {
  81. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  82. return Info;
  83. }
  84. SpiralGUIType *StreamPlugin::CreateGUI() {
  85. return new StreamPluginGUI(m_PluginInfo.Width, m_PluginInfo.Height, this, m_AudioCH, m_HostInfo);
  86. }
  87. void StreamPlugin::Execute() {
  88. if (m_File.IsOpen()) {
  89. for (int n=0; n<m_HostInfo->BUFSIZE; n++) {
  90. bool FinTrig = false;
  91. float CVPitch = GetInput(0, n)*10.0f;
  92. if (GetInput (1, n) > 0) Play();
  93. if (GetInput (2, n) > 0) Stop();
  94. if (m_Pos<0) {
  95. m_Pos = m_SampleSize - 1;
  96. m_StreamPos -= m_SampleSize;
  97. FinTrig = m_StreamPos < 0;
  98. if (FinTrig) {
  99. m_StreamPos = m_File.GetSize() - m_SampleSize;
  100. m_GlobalPos = m_StreamPos;
  101. }
  102. m_File.SeekToChunk (m_StreamPos);
  103. m_File.LoadChunk (m_SampleSize, m_SampleL, m_SampleR);
  104. }
  105. else if (m_Pos >= m_SampleSize) {
  106. m_Pos = 0;
  107. m_StreamPos += m_SampleSize;
  108. FinTrig = m_StreamPos >= m_File.GetSize();
  109. if (FinTrig) {
  110. m_StreamPos = 0;
  111. m_GlobalPos = 0;
  112. }
  113. m_File.SeekToChunk (m_StreamPos);
  114. m_File.LoadChunk (m_SampleSize, m_SampleL, m_SampleR);
  115. }
  116. SetOutput (0, n, m_SampleL[m_Pos] * m_GUIArgs.Volume);
  117. SetOutput (1, n, m_SampleR[m_Pos] * m_GUIArgs.Volume);
  118. if (FinTrig) SetOutput (2, n, 1); else SetOutput (2, n, 0);
  119. if (m_Mode==PLAYM) {
  120. m_Pos += m_GUIArgs.PitchMod + CVPitch;
  121. m_GlobalPos += m_GUIArgs.PitchMod + CVPitch;
  122. }
  123. }
  124. m_GUIArgs.TimeOut = GetTime();
  125. }
  126. }
  127. void StreamPlugin::ExecuteCommands() {
  128. if (m_AudioCH->IsCommandWaiting()) {
  129. switch (m_AudioCH->GetCommand()) {
  130. case SET_TIME: SetTime(); break;
  131. case LOAD: OpenStream(); break;
  132. case RESTART: Restart(); break;
  133. case STOP: Stop(); break;
  134. case PLAY: Play(); break;
  135. }
  136. }
  137. }
  138. void StreamPlugin::SetTime (void) {
  139. m_GlobalPos = m_SampleRate * m_GUIArgs.Time;
  140. m_StreamPos = (int)(m_SampleRate * m_GUIArgs.Time);
  141. m_Pos = m_SampleSize;
  142. }
  143. void StreamPlugin::OpenStream (void) {
  144. m_StreamPos = 0;
  145. m_GlobalPos = 0;
  146. if (m_File.IsOpen ()) m_File.Close ();
  147. m_File.Open (m_GUIArgs.FileName, WavFile::READ);
  148. m_SampleL.Allocate (m_SampleSize);
  149. m_SampleR.Allocate (m_SampleSize);
  150. m_Pitch = m_SampleRate / (float)m_HostInfo->SAMPLERATE;
  151. if (m_File.IsStereo ()) {
  152. m_Pitch *= 2;
  153. m_GUIArgs.MaxTime = GetLength();
  154. }
  155. else m_GUIArgs.MaxTime = GetLength() / 2;
  156. }
  157. float StreamPlugin::GetLength (void) {
  158. if (m_File.IsStereo()) return m_File.GetSize() / (float)m_File.GetSamplerate ();
  159. else return m_File.GetSize() / (float)m_File.GetSamplerate () * 2;
  160. }
  161. void StreamPlugin::StreamOut (ostream &s) {
  162. s << m_Version << " "
  163. << m_GUIArgs.Volume << " "
  164. << m_GUIArgs.PitchMod << " "
  165. << strlen (m_GUIArgs.FileName) << " "
  166. << m_GUIArgs.FileName << " "
  167. // is it really necessary to save this lot??
  168. << m_Pos << " "
  169. << m_StreamPos << " "
  170. << m_GlobalPos << " "
  171. << m_Pitch << " " << endl;
  172. }
  173. void StreamPlugin::StreamIn(istream &s) {
  174. int version;
  175. s >> version;
  176. s >> m_GUIArgs.Volume >> m_GUIArgs.PitchMod;
  177. int size;
  178. s >> size;
  179. if (size > 255) size = 255;
  180. s.ignore (1);
  181. s.get (m_GUIArgs.FileName, size+1);
  182. if (m_GUIArgs.FileName != "None") OpenStream ();
  183. // is it really necessary to load this lot??
  184. s >> m_Pos;
  185. s >> m_StreamPos;
  186. s >> m_GlobalPos;
  187. s >> m_Pitch;
  188. }