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
5.1KB

  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. // for lrintf()
  19. #define _ISOC9X_SOURCE 1
  20. #define _ISOC99_SOURCE 1
  21. #include <math.h>
  22. #include <sys/types.h>
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <limits.h>
  27. #include <sys/ioctl.h>
  28. #include <limits.h>
  29. #include "DiskWriterPlugin.h"
  30. #include "DiskWriterPluginGUI.h"
  31. #include <FL/fl_file_chooser.H>
  32. #include "SpiralIcon.xpm"
  33. using namespace std;
  34. // what's this for - should it still be here?
  35. #define CHECK_AND_REPORT_ERROR if (result<0) \
  36. { \
  37. perror("Sound device did not accept settings"); \
  38. m_OutputOk=false; \
  39. return false; \
  40. }
  41. extern "C"
  42. {
  43. SpiralPlugin* SpiralPlugin_CreateInstance()
  44. {
  45. return new DiskWriterPlugin;
  46. }
  47. char** SpiralPlugin_GetIcon()
  48. {
  49. return SpiralIcon_xpm;
  50. }
  51. int SpiralPlugin_GetID()
  52. {
  53. return 41;
  54. }
  55. string SpiralPlugin_GetGroupName()
  56. {
  57. return "InputOutput";
  58. }
  59. }
  60. ///////////////////////////////////////////////////////
  61. DiskWriterPlugin::DiskWriterPlugin()
  62. {
  63. m_PluginInfo.Name="DiskWriter";
  64. m_PluginInfo.Width=160;
  65. m_PluginInfo.Height=115;
  66. m_PluginInfo.NumInputs=3;
  67. m_PluginInfo.NumOutputs=0;
  68. m_PluginInfo.PortTips.push_back("Left Out");
  69. m_PluginInfo.PortTips.push_back("Right Out");
  70. m_PluginInfo.PortTips.push_back("Record Controller");
  71. m_GUIArgs.BitsPerSample = 16;
  72. m_GUIArgs.Stereo = true;
  73. m_GUIArgs.Recording = false;
  74. m_GUIArgs.TimeRecorded = 0;
  75. m_Version = 2;
  76. m_AudioCH->RegisterData("Filename",ChannelHandler::INPUT,m_GUIArgs.Name,256);
  77. m_AudioCH->Register("BitsPerSample",&m_GUIArgs.BitsPerSample,ChannelHandler::INPUT);
  78. m_AudioCH->Register("Stereo",&m_GUIArgs.Stereo,ChannelHandler::INPUT);
  79. m_AudioCH->Register("TimeRecorded",&m_GUIArgs.TimeRecorded,ChannelHandler::OUTPUT);
  80. m_AudioCH->Register("Recording",&m_GUIArgs.Recording,ChannelHandler::OUTPUT);
  81. }
  82. DiskWriterPlugin::~DiskWriterPlugin()
  83. {
  84. }
  85. PluginInfo &DiskWriterPlugin::Initialise(const HostInfo *Host)
  86. {
  87. PluginInfo& Info= SpiralPlugin::Initialise(Host);
  88. //host=Host;
  89. return Info;
  90. }
  91. SpiralGUIType *DiskWriterPlugin::CreateGUI()
  92. {
  93. return new DiskWriterPluginGUI(m_PluginInfo.Width,
  94. m_PluginInfo.Height,
  95. this,
  96. m_AudioCH,
  97. m_HostInfo);
  98. }
  99. void DiskWriterPlugin::Execute()
  100. {
  101. if(m_GUIArgs.Recording && m_Wav.IsOpen())
  102. {
  103. int on=0;
  104. float LeftBuffer[GetHostInfo()->BUFSIZE], RightBuffer[GetHostInfo()->BUFSIZE];
  105. for (int n=0; n<GetHostInfo()->BUFSIZE; n++)
  106. {
  107. // stereo channels - interleave
  108. LeftBuffer[n]=GetInput(0,n);
  109. RightBuffer[n]=GetInput(1,n);
  110. }
  111. m_Wav.Save(LeftBuffer, RightBuffer, GetHostInfo()->BUFSIZE);
  112. m_GUIArgs.TimeRecorded = m_Wav.GetSize()/m_Wav.GetSamplerate();
  113. }
  114. }
  115. void DiskWriterPlugin::ExecuteCommands()
  116. {
  117. if (m_AudioCH->IsCommandWaiting())
  118. {
  119. switch(m_AudioCH->GetCommand())
  120. {
  121. case OPENWAV :
  122. if (m_Wav.GetSamplerate() != GetHostInfo()->SAMPLERATE) {
  123. m_Wav.SetSamplerate(GetHostInfo()->SAMPLERATE);
  124. }
  125. if (m_Wav.GetBitsPerSample() != m_GUIArgs.BitsPerSample) {
  126. m_Wav.SetBitsPerSample(m_GUIArgs.BitsPerSample);
  127. }
  128. m_Wav.Open(m_GUIArgs.Name,WavFile::WRITE, (m_GUIArgs.Stereo)?(WavFile::STEREO):(WavFile::MONO));
  129. m_GUIArgs.TimeRecorded = 0;
  130. break;
  131. case CLOSEWAV : m_Wav.Close(); break;
  132. case RECORD : m_GUIArgs.Recording=true; break;
  133. case STOP : m_GUIArgs.Recording=false; break;
  134. default : break;
  135. }
  136. }
  137. }
  138. void DiskWriterPlugin::StreamOut (ostream &s)
  139. {
  140. s << m_Version << " " << m_GUIArgs.BitsPerSample << " " << m_GUIArgs.Stereo << " ";
  141. }
  142. void DiskWriterPlugin::StreamIn (istream &s)
  143. {
  144. char Test;
  145. int Version, BitsPerSample, Stereo;
  146. //originally DiskWriter had NO streaming code whatsover
  147. // so to test if this is an old patch we must
  148. // read ahead and find out what the first char
  149. // of the next line is
  150. s.seekg (2, ios_base::cur );//skip to next line
  151. Test = s.peek();//peek first char
  152. s.seekg (-2, ios_base::cur );//jump back to prior line
  153. //This test works because if the char
  154. // of the next line isn't a version number
  155. // it will only be 'D', ' ', #13, or '-'
  156. if ( (Test >= '0') && (Test <= '9') )
  157. {
  158. s >> Version;
  159. }
  160. else
  161. {
  162. //No Version, so use Version 1
  163. Version = 1;
  164. }
  165. switch (Version)
  166. {
  167. case 2:
  168. {
  169. s >> BitsPerSample >> Stereo;
  170. m_GUIArgs.BitsPerSample = BitsPerSample;
  171. m_GUIArgs.Stereo = Stereo;
  172. }
  173. break;
  174. case 1:
  175. {
  176. //use original fixed defaults
  177. m_GUIArgs.BitsPerSample = 16;
  178. m_GUIArgs.Stereo = true;
  179. }
  180. break;
  181. }
  182. }