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