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.

478 lines
11KB

  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. #if defined (__FreeBSD__)
  28. #include <machine/soundcard.h>
  29. #else
  30. #if defined (__NetBSD__) || defined (__OpenBSD__)
  31. #include <soundcard.h> /* OSS emulation */
  32. #undef ioctl
  33. #else /* BSDI, Linux, Solaris */
  34. #include <sys/soundcard.h>
  35. #endif /* __NetBSD__ or __OpenBSD__ */
  36. #endif /* __FreeBSD__ */
  37. #include <sys/ioctl.h>
  38. #include <limits.h>
  39. #include "OutputPlugin.h"
  40. #include "OutputPluginGUI.h"
  41. #include <FL/fl_file_chooser.H>
  42. #include "SpiralIcon.xpm"
  43. static const int IN_FREQ = 0;
  44. static const int IN_PW = 1;
  45. static const int IN_SHLEN = 2;
  46. static const int OUT_MAIN = 0;
  47. static const HostInfo* host;
  48. OSSOutput* OSSOutput::m_Singleton = NULL;
  49. int OutputPlugin::m_RefCount=0;
  50. int OutputPlugin::m_NoExecuted=0;
  51. #define CHECK_AND_REPORT_ERROR if (result<0) \
  52. { \
  53. perror("Sound device did not accept settings"); \
  54. m_OutputOk=false; \
  55. return; \
  56. }
  57. extern "C" {
  58. SpiralPlugin* CreateInstance()
  59. {
  60. return new OutputPlugin;
  61. }
  62. char** GetIcon()
  63. {
  64. return SpiralIcon_xpm;
  65. }
  66. int GetID()
  67. {
  68. return 0x0000;
  69. }
  70. }
  71. ///////////////////////////////////////////////////////
  72. OutputPlugin::OutputPlugin()
  73. {
  74. m_RefCount++;
  75. m_PluginInfo.Name="OSS";
  76. m_PluginInfo.Width=100;
  77. m_PluginInfo.Height=130;
  78. m_PluginInfo.NumInputs=3;
  79. m_PluginInfo.NumOutputs=2;
  80. m_PluginInfo.PortTips.push_back("Left Out");
  81. m_PluginInfo.PortTips.push_back("Right Out");
  82. m_PluginInfo.PortTips.push_back("Record Controller");
  83. m_PluginInfo.PortTips.push_back("Left In");
  84. m_PluginInfo.PortTips.push_back("Right In");
  85. m_Mode=OUTPUT;
  86. }
  87. OutputPlugin::~OutputPlugin()
  88. {
  89. m_RefCount--;
  90. if (m_RefCount==0)
  91. {
  92. OSSOutput::PackUpAndGoHome();
  93. }
  94. }
  95. PluginInfo &OutputPlugin::Initialise(const HostInfo *Host)
  96. {
  97. PluginInfo& Info= SpiralPlugin::Initialise(Host);
  98. host=Host;
  99. OSSOutput::Get()->AllocateBuffer();
  100. return Info;
  101. }
  102. SpiralGUIType *OutputPlugin::CreateGUI()
  103. {
  104. m_GUI = new OutputPluginGUI(m_PluginInfo.Width,
  105. m_PluginInfo.Height,
  106. this,m_HostInfo);
  107. m_GUI->hide();
  108. return m_GUI;
  109. }
  110. void OutputPlugin::Execute()
  111. {
  112. // Only Play() once per set of plugins
  113. m_NoExecuted++;
  114. if (m_NoExecuted==m_RefCount)
  115. {
  116. if (m_Mode==INPUT || m_Mode==DUPLEX) OSSOutput::Get()->Read();
  117. if (m_Mode==OUTPUT || m_Mode==DUPLEX) OSSOutput::Get()->Play();
  118. m_NoExecuted=0;
  119. }
  120. if (m_Mode==OUTPUT || m_Mode==DUPLEX)
  121. {
  122. OSSOutput::Get()->SendStereo(GetInput(0),GetInput(1));
  123. for (int n=0; n<m_HostInfo->BUFSIZE;n++)
  124. {
  125. if (GetInput(2,n)!=0)
  126. {
  127. if (! m_CheckedAlready)
  128. {
  129. m_CheckedAlready=true;
  130. // an experimental line, should *theoretically* cut down on CPU time.
  131. n=m_HostInfo->BUFSIZE;
  132. if (! m_Recmode)
  133. {
  134. char *fn=fl_file_chooser("Pick a Wav file to save to", "*.wav", NULL);
  135. if (fn && fn!="")
  136. {
  137. OSSOutput::Get()->WavOpen(fn);
  138. }
  139. m_Recmode=true;
  140. }
  141. else
  142. {
  143. OSSOutput::Get()->WavClose();
  144. m_Recmode=false;
  145. }
  146. }
  147. }
  148. else
  149. m_CheckedAlready=false;
  150. }
  151. }
  152. if (m_Mode==INPUT || m_Mode==DUPLEX) OSSOutput::Get()->GetStereo(GetOutputBuf(0),GetOutputBuf(1));
  153. }
  154. //////////////////////////////////////////////////////////////////////
  155. //////////////////////////////////////////////////////////////////////
  156. OSSOutput::OSSOutput() :
  157. m_Amp(0.5),
  158. m_Channels(2),
  159. m_ReadBufferNum(0),
  160. m_WriteBufferNum(0),
  161. m_OutputOk(true)
  162. {
  163. m_Buffer[0]=NULL;
  164. m_Buffer[1]=NULL;
  165. m_InBuffer[0]=NULL;
  166. m_InBuffer[1]=NULL;
  167. OpenWrite();
  168. }
  169. //////////////////////////////////////////////////////////////////////
  170. OSSOutput::~OSSOutput()
  171. {
  172. Close();
  173. }
  174. //////////////////////////////////////////////////////////////////////
  175. void OSSOutput::AllocateBuffer()
  176. {
  177. if (m_Buffer[0]==NULL)
  178. {
  179. m_BufSizeBytes=host->BUFSIZE*m_Channels*2;
  180. // initialise for stereo
  181. m_Buffer[0] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  182. m_Buffer[1] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  183. m_InBuffer[0] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  184. m_InBuffer[1] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  185. }
  186. m_Wav.SetSamplerate(host->SAMPLERATE);
  187. }
  188. //////////////////////////////////////////////////////////////////////
  189. void OSSOutput::SendStereo(const Sample *ldata,const Sample *rdata)
  190. {
  191. if (m_Channels!=2) return;
  192. int on=0;
  193. float t;
  194. for (int n=0; n<host->BUFSIZE; n++)
  195. {
  196. // stereo channels - interleave
  197. if (ldata)
  198. {
  199. t=(*ldata)[n]*m_Amp;
  200. if (t>1) t=1;
  201. if (t<-1) t=-1;
  202. m_Buffer[m_WriteBufferNum][on]+=lrintf(t*SHRT_MAX);
  203. }
  204. on++;
  205. if (rdata)
  206. {
  207. t=(*rdata)[n]*m_Amp;
  208. if (t>1) t=1;
  209. if (t<-1) t=-1;
  210. m_Buffer[m_WriteBufferNum][on]+=lrintf(t*SHRT_MAX);
  211. }
  212. on++;
  213. }
  214. }
  215. //////////////////////////////////////////////////////////////////////
  216. void OSSOutput::Play()
  217. {
  218. int BufferToSend=!m_WriteBufferNum;
  219. #if __BYTE_ORDER == BIG_ENDIAN
  220. for (int n=0; n<host->BUFSIZE*m_Channels; n++)
  221. {
  222. m_Buffer[BufferToSend][n]=(short)((m_Buffer[BufferToSend][n]<<8)&0xff00)|
  223. ((m_Buffer[BufferToSend][n]>>8)&0xff);
  224. }
  225. #endif
  226. if (m_OutputOk)
  227. write(m_Dspfd,m_Buffer[BufferToSend],m_BufSizeBytes);
  228. if(m_Wav.Recording())
  229. {
  230. m_Wav.Save(m_Buffer[BufferToSend],m_BufSizeBytes);
  231. }
  232. memset(m_Buffer[BufferToSend],0,m_BufSizeBytes);
  233. m_WriteBufferNum=BufferToSend;
  234. }
  235. //////////////////////////////////////////////////////////////////////
  236. void OSSOutput::GetStereo(Sample *ldata,Sample *rdata)
  237. {
  238. if (m_Channels!=2) return;
  239. int on=0;
  240. for (int n=0; n<host->BUFSIZE; n++)
  241. {
  242. // stereo channels - interleave
  243. if (ldata) ldata->Set(n,(m_InBuffer[m_ReadBufferNum][on]*m_Amp)/(float)SHRT_MAX);
  244. on++;
  245. if (rdata) rdata->Set(n,(m_InBuffer[m_ReadBufferNum][on]*m_Amp)/(float)SHRT_MAX);
  246. on++;
  247. }
  248. }
  249. //////////////////////////////////////////////////////////////////////
  250. void OSSOutput::Read()
  251. {
  252. int BufferToRead=!m_ReadBufferNum;
  253. if (m_OutputOk)
  254. read(m_Dspfd,m_InBuffer[BufferToRead],m_BufSizeBytes);
  255. #if __BYTE_ORDER == BIG_ENDIAN
  256. for (int n=0; n<host->BUFSIZE*m_Channels; n++)
  257. {
  258. m_InBuffer[BufferToRead][n]=(short)((m_InBuffer[BufferToRead][n]<<8)&0xff00)|
  259. ((m_InBuffer[BufferToRead][n]>>8)&0xff);
  260. }
  261. #endif
  262. m_ReadBufferNum=BufferToRead;
  263. }
  264. //////////////////////////////////////////////////////////////////////
  265. void OSSOutput::Close()
  266. {
  267. cerr<<"Closing dsp output"<<endl;
  268. close(m_Dspfd);
  269. }
  270. //////////////////////////////////////////////////////////////////////
  271. void OSSOutput::OpenWrite()
  272. {
  273. int result,val;
  274. cerr<<"Opening dsp output"<<endl;
  275. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_WRONLY);
  276. if(m_Dspfd<0)
  277. {
  278. fprintf(stderr,"Can't open audio driver for writing.\n");
  279. m_OutputOk=false;
  280. return;
  281. }
  282. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  283. CHECK_AND_REPORT_ERROR;
  284. short fgmtsize=0;
  285. int numfgmts=host->FRAGCOUNT;
  286. if (host->FRAGCOUNT==-1) numfgmts=0x7fff;
  287. for (int i=0; i<32; i++)
  288. {
  289. if ((host->FRAGSIZE)==(1<<i))
  290. {
  291. fgmtsize=i;
  292. break;
  293. }
  294. }
  295. if (fgmtsize==0)
  296. {
  297. cerr<<"Fragment size ["<<host->FRAGSIZE<<"] must be power of two!"<<endl;
  298. fgmtsize=256;
  299. }
  300. numfgmts=numfgmts<<16;
  301. val=numfgmts|(int)fgmtsize;
  302. result=ioctl(m_Dspfd, SNDCTL_DSP_SETFRAGMENT, &val);
  303. CHECK_AND_REPORT_ERROR;
  304. val = 1;
  305. result = ioctl(m_Dspfd, SOUND_PCM_WRITE_CHANNELS, &val);
  306. CHECK_AND_REPORT_ERROR;
  307. val = AFMT_S16_LE;
  308. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  309. CHECK_AND_REPORT_ERROR;
  310. if (m_Channels==2) val=1;
  311. else val=0;
  312. result = ioctl(m_Dspfd,SNDCTL_DSP_STEREO,&val);
  313. CHECK_AND_REPORT_ERROR;
  314. val = host->SAMPLERATE;
  315. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  316. CHECK_AND_REPORT_ERROR;
  317. }
  318. //////////////////////////////////////////////////////////////////////
  319. void OSSOutput::OpenRead()
  320. {
  321. int result,val;
  322. cerr<<"Opening dsp input"<<endl;
  323. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_RDONLY);
  324. if(m_Dspfd<0)
  325. {
  326. fprintf(stderr,"Can't open audio driver for reading.\n");
  327. m_OutputOk=false;
  328. return;
  329. }
  330. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  331. CHECK_AND_REPORT_ERROR;
  332. val = 1;
  333. result = ioctl(m_Dspfd, SOUND_PCM_READ_CHANNELS, &val);
  334. CHECK_AND_REPORT_ERROR;
  335. val = AFMT_S16_LE;
  336. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  337. CHECK_AND_REPORT_ERROR;
  338. val = host->SAMPLERATE;
  339. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  340. CHECK_AND_REPORT_ERROR;
  341. }
  342. //////////////////////////////////////////////////////////////////////
  343. void OSSOutput::OpenReadWrite()
  344. {
  345. int result,val;
  346. cerr<<"Opening dsp output (duplex)"<<endl;
  347. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_RDWR);
  348. if(m_Dspfd<0)
  349. {
  350. fprintf(stderr,"Can't open audio driver for writing.\n");
  351. m_OutputOk=false;
  352. return;
  353. }
  354. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  355. CHECK_AND_REPORT_ERROR;
  356. short fgmtsize=0;
  357. int numfgmts=host->FRAGCOUNT;
  358. if (host->FRAGCOUNT==-1) numfgmts=0x7fff;
  359. for (int i=0; i<32; i++)
  360. {
  361. if ((host->FRAGSIZE)==(1<<i))
  362. {
  363. fgmtsize=i;
  364. break;
  365. }
  366. }
  367. if (fgmtsize==0)
  368. {
  369. cerr<<"Fragment size ["<<host->FRAGSIZE<<"] must be power of two!"<<endl;
  370. fgmtsize=256;
  371. }
  372. numfgmts=numfgmts<<16;
  373. val=numfgmts|(int)fgmtsize;
  374. result=ioctl(m_Dspfd, SNDCTL_DSP_SETFRAGMENT, &val);
  375. CHECK_AND_REPORT_ERROR;
  376. val = 1;
  377. result = ioctl(m_Dspfd, SOUND_PCM_WRITE_CHANNELS, &val);
  378. CHECK_AND_REPORT_ERROR;
  379. val = AFMT_S16_LE;
  380. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  381. CHECK_AND_REPORT_ERROR;
  382. if (m_Channels==2) val=1;
  383. else val=0;
  384. result = ioctl(m_Dspfd,SNDCTL_DSP_STEREO,&val);
  385. CHECK_AND_REPORT_ERROR;
  386. val = host->SAMPLERATE;
  387. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  388. CHECK_AND_REPORT_ERROR;
  389. }