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.

193 lines
5.0KB

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