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.

550 lines
12KB

  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. OutputPlugin::Mode OutputPlugin::m_Mode=NO_MODE;
  52. #define CHECK_AND_REPORT_ERROR if (result<0) \
  53. { \
  54. perror("Sound device did not accept settings"); \
  55. m_OutputOk=false; \
  56. return false; \
  57. }
  58. extern "C"
  59. {
  60. SpiralPlugin* CreateInstance()
  61. {
  62. return new OutputPlugin;
  63. }
  64. char** GetIcon()
  65. {
  66. return SpiralIcon_xpm;
  67. }
  68. int GetID()
  69. {
  70. return 0x0000;
  71. }
  72. }
  73. ///////////////////////////////////////////////////////
  74. OutputPlugin::OutputPlugin() :
  75. m_Volume(1.0f)
  76. {
  77. m_RefCount++;
  78. // we are an output.
  79. m_IsTerminal=true;
  80. m_PluginInfo.Name="OSS";
  81. m_PluginInfo.Width=100;
  82. m_PluginInfo.Height=100;
  83. m_PluginInfo.NumInputs=2;
  84. m_PluginInfo.NumOutputs=2;
  85. m_PluginInfo.PortTips.push_back("Left Out");
  86. m_PluginInfo.PortTips.push_back("Right Out");
  87. //m_PluginInfo.PortTips.push_back("Record Controller");
  88. m_PluginInfo.PortTips.push_back("Left In");
  89. m_PluginInfo.PortTips.push_back("Right In");
  90. m_AudioCH->Register("Volume",&m_Volume);
  91. }
  92. OutputPlugin::~OutputPlugin()
  93. {
  94. m_RefCount--;
  95. if (m_RefCount==0)
  96. {
  97. cb_Blocking(m_Parent,false);
  98. OSSOutput::PackUpAndGoHome();
  99. }
  100. }
  101. PluginInfo &OutputPlugin::Initialise(const HostInfo *Host)
  102. {
  103. PluginInfo& Info= SpiralPlugin::Initialise(Host);
  104. host=Host;
  105. OSSOutput::Get()->AllocateBuffer();
  106. return Info;
  107. }
  108. SpiralGUIType *OutputPlugin::CreateGUI()
  109. {
  110. return new OutputPluginGUI(m_PluginInfo.Width,
  111. m_PluginInfo.Height,
  112. this,
  113. m_AudioCH,
  114. m_HostInfo);
  115. }
  116. void OutputPlugin::Execute()
  117. {
  118. if (m_Mode==NO_MODE && m_RefCount==1)
  119. {
  120. if (OSSOutput::Get()->OpenWrite())
  121. {
  122. cb_Blocking(m_Parent,true);
  123. m_Mode=OUTPUT;
  124. }
  125. }
  126. //if (m_Mode==NO_MODE || m_Mode==CLOSED) cb_Blocking(m_Parent,false);
  127. //else cb_Blocking(m_Parent,true);
  128. if (m_Mode==OUTPUT || m_Mode==DUPLEX)
  129. {
  130. OSSOutput::Get()->SendStereo(GetInput(0),GetInput(1));
  131. // can't open GUI stuff here
  132. /* for (int n=0; n<m_HostInfo->BUFSIZE;n++)
  133. {
  134. // can't open GUI stuff here
  135. if (GetInput(2,n)!=0)
  136. {
  137. if (! m_CheckedAlready)
  138. {
  139. m_CheckedAlready=true;
  140. // an experimental line, should *theoretically* cut down on CPU time.
  141. n=m_HostInfo->BUFSIZE;
  142. if (! m_Recmode)
  143. {
  144. char *fn=fl_file_chooser("Pick a Wav file to save to", "*.wav", NULL);
  145. if (fn && fn!="")
  146. {
  147. OSSOutput::Get()->WavOpen(fn);
  148. }
  149. m_Recmode=true;
  150. }
  151. else
  152. {
  153. OSSOutput::Get()->WavClose();
  154. m_Recmode=false;
  155. }
  156. }
  157. }
  158. else
  159. m_CheckedAlready=false;
  160. }*/
  161. }
  162. if (m_Mode==INPUT || m_Mode==DUPLEX) OSSOutput::Get()->GetStereo(GetOutputBuf(0),GetOutputBuf(1));
  163. }
  164. void OutputPlugin::ExecuteCommands()
  165. {
  166. // Only Play() once per set of plugins
  167. cerr<<m_RefCount<<" "<<m_NoExecuted<<endl;
  168. m_NoExecuted--;
  169. if (m_NoExecuted<=0)
  170. {
  171. if (m_Mode==INPUT || m_Mode==DUPLEX) OSSOutput::Get()->Read();
  172. if (m_Mode==OUTPUT || m_Mode==DUPLEX) OSSOutput::Get()->Play();
  173. m_NoExecuted=m_RefCount;
  174. }
  175. if (m_AudioCH->IsCommandWaiting())
  176. {
  177. switch(m_AudioCH->GetCommand())
  178. {
  179. case OPENREAD :
  180. if (OSSOutput::Get()->OpenRead())
  181. {
  182. m_Mode=INPUT;
  183. //cb_Blocking(m_Parent,true);
  184. }
  185. break;
  186. case OPENWRITE :
  187. if (OSSOutput::Get()->OpenWrite())
  188. {
  189. m_Mode=OUTPUT;
  190. cb_Blocking(m_Parent,true);
  191. }
  192. break;
  193. case OPENDUPLEX :
  194. if (OSSOutput::Get()->OpenReadWrite())
  195. {
  196. m_Mode=DUPLEX;
  197. cb_Blocking(m_Parent,true);
  198. }
  199. break;
  200. case CLOSE :
  201. m_Mode=CLOSED;
  202. cb_Blocking(m_Parent,false);
  203. OSSOutput::Get()->Close();
  204. break;
  205. case SET_VOLUME : OSSOutput::Get()->SetVolume(m_Volume); break;
  206. default : break;
  207. }
  208. }
  209. }
  210. //////////////////////////////////////////////////////////////////////
  211. //////////////////////////////////////////////////////////////////////
  212. OSSOutput::OSSOutput() :
  213. m_Amp(0.5),
  214. m_Channels(2),
  215. m_ReadBufferNum(0),
  216. m_WriteBufferNum(0),
  217. m_OutputOk(false)
  218. {
  219. m_Buffer[0]=NULL;
  220. m_Buffer[1]=NULL;
  221. m_InBuffer[0]=NULL;
  222. m_InBuffer[1]=NULL;
  223. }
  224. //////////////////////////////////////////////////////////////////////
  225. OSSOutput::~OSSOutput()
  226. {
  227. Close();
  228. }
  229. //////////////////////////////////////////////////////////////////////
  230. void OSSOutput::AllocateBuffer()
  231. {
  232. if (m_Buffer[0]==NULL)
  233. {
  234. m_BufSizeBytes=host->BUFSIZE*m_Channels*2;
  235. // initialise for stereo
  236. m_Buffer[0] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  237. m_Buffer[1] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  238. m_InBuffer[0] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  239. m_InBuffer[1] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  240. }
  241. m_Wav.SetSamplerate(host->SAMPLERATE);
  242. }
  243. //////////////////////////////////////////////////////////////////////
  244. void OSSOutput::SendStereo(const Sample *ldata,const Sample *rdata)
  245. {
  246. if (m_Channels!=2) return;
  247. int on=0;
  248. float t;
  249. for (int n=0; n<host->BUFSIZE; n++)
  250. {
  251. // stereo channels - interleave
  252. if (ldata)
  253. {
  254. t=(*ldata)[n]*m_Amp;
  255. if (t>1) t=1;
  256. if (t<-1) t=-1;
  257. m_Buffer[m_WriteBufferNum][on]+=lrintf(t*SHRT_MAX);
  258. }
  259. on++;
  260. if (rdata)
  261. {
  262. t=(*rdata)[n]*m_Amp;
  263. if (t>1) t=1;
  264. if (t<-1) t=-1;
  265. m_Buffer[m_WriteBufferNum][on]+=lrintf(t*SHRT_MAX);
  266. }
  267. on++;
  268. }
  269. }
  270. //////////////////////////////////////////////////////////////////////
  271. void OSSOutput::Play()
  272. {
  273. int BufferToSend=!m_WriteBufferNum;
  274. #if __BYTE_ORDER == BIG_ENDIAN
  275. for (int n=0; n<host->BUFSIZE*m_Channels; n++)
  276. {
  277. m_Buffer[BufferToSend][n]=(short)((m_Buffer[BufferToSend][n]<<8)&0xff00)|
  278. ((m_Buffer[BufferToSend][n]>>8)&0xff);
  279. }
  280. #endif
  281. if (m_OutputOk)
  282. {
  283. write(m_Dspfd,m_Buffer[BufferToSend],m_BufSizeBytes);
  284. }
  285. if(m_Wav.Recording())
  286. {
  287. m_Wav.Save(m_Buffer[BufferToSend],m_BufSizeBytes);
  288. }
  289. memset(m_Buffer[BufferToSend],0,m_BufSizeBytes);
  290. m_WriteBufferNum=BufferToSend;
  291. }
  292. //////////////////////////////////////////////////////////////////////
  293. void OSSOutput::GetStereo(Sample *ldata,Sample *rdata)
  294. {
  295. if (m_Channels!=2) return;
  296. int on=0;
  297. for (int n=0; n<host->BUFSIZE; n++)
  298. {
  299. // stereo channels - interleave
  300. if (ldata) ldata->Set(n,(m_InBuffer[m_ReadBufferNum][on]*m_Amp)/(float)SHRT_MAX);
  301. on++;
  302. if (rdata) rdata->Set(n,(m_InBuffer[m_ReadBufferNum][on]*m_Amp)/(float)SHRT_MAX);
  303. on++;
  304. }
  305. }
  306. //////////////////////////////////////////////////////////////////////
  307. void OSSOutput::Read()
  308. {
  309. int BufferToRead=!m_ReadBufferNum;
  310. if (m_OutputOk)
  311. read(m_Dspfd,m_InBuffer[BufferToRead],m_BufSizeBytes);
  312. #if __BYTE_ORDER == BIG_ENDIAN
  313. for (int n=0; n<host->BUFSIZE*m_Channels; n++)
  314. {
  315. m_InBuffer[BufferToRead][n]=(short)((m_InBuffer[BufferToRead][n]<<8)&0xff00)|
  316. ((m_InBuffer[BufferToRead][n]>>8)&0xff);
  317. }
  318. #endif
  319. m_ReadBufferNum=BufferToRead;
  320. }
  321. //////////////////////////////////////////////////////////////////////
  322. bool OSSOutput::Close()
  323. {
  324. cerr<<"Closing dsp output"<<endl;
  325. close(m_Dspfd);
  326. return true;
  327. }
  328. //////////////////////////////////////////////////////////////////////
  329. bool OSSOutput::OpenWrite()
  330. {
  331. int result,val;
  332. cerr<<"Opening dsp output"<<endl;
  333. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_WRONLY);
  334. if(m_Dspfd<0)
  335. {
  336. fprintf(stderr,"Can't open audio driver for writing.\n");
  337. m_OutputOk=false;
  338. return false;
  339. }
  340. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  341. CHECK_AND_REPORT_ERROR;
  342. short fgmtsize=0;
  343. int numfgmts=host->FRAGCOUNT;
  344. if (host->FRAGCOUNT==-1) numfgmts=0x7fff;
  345. for (int i=0; i<32; i++)
  346. {
  347. if ((host->FRAGSIZE)==(1<<i))
  348. {
  349. fgmtsize=i;
  350. break;
  351. }
  352. }
  353. if (fgmtsize==0)
  354. {
  355. cerr<<"Fragment size ["<<host->FRAGSIZE<<"] must be power of two!"<<endl;
  356. fgmtsize=256;
  357. }
  358. numfgmts=numfgmts<<16;
  359. val=numfgmts|(int)fgmtsize;
  360. result=ioctl(m_Dspfd, SNDCTL_DSP_SETFRAGMENT, &val);
  361. CHECK_AND_REPORT_ERROR;
  362. val = 1;
  363. result = ioctl(m_Dspfd, SOUND_PCM_WRITE_CHANNELS, &val);
  364. CHECK_AND_REPORT_ERROR;
  365. val = AFMT_S16_LE;
  366. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  367. CHECK_AND_REPORT_ERROR;
  368. if (m_Channels==2) val=1;
  369. else val=0;
  370. result = ioctl(m_Dspfd,SNDCTL_DSP_STEREO,&val);
  371. CHECK_AND_REPORT_ERROR;
  372. val = host->SAMPLERATE;
  373. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  374. CHECK_AND_REPORT_ERROR;
  375. m_OutputOk=true;
  376. return true;
  377. }
  378. //////////////////////////////////////////////////////////////////////
  379. bool OSSOutput::OpenRead()
  380. {
  381. int result,val;
  382. cerr<<"Opening dsp input"<<endl;
  383. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_RDONLY);
  384. if(m_Dspfd<0)
  385. {
  386. fprintf(stderr,"Can't open audio driver for reading.\n");
  387. m_OutputOk=false;
  388. return false;
  389. }
  390. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  391. CHECK_AND_REPORT_ERROR;
  392. val = 1;
  393. result = ioctl(m_Dspfd, SOUND_PCM_READ_CHANNELS, &val);
  394. CHECK_AND_REPORT_ERROR;
  395. val = AFMT_S16_LE;
  396. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  397. CHECK_AND_REPORT_ERROR;
  398. val = host->SAMPLERATE;
  399. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  400. CHECK_AND_REPORT_ERROR;
  401. m_OutputOk=true;
  402. return true;
  403. }
  404. //////////////////////////////////////////////////////////////////////
  405. bool OSSOutput::OpenReadWrite()
  406. {
  407. int result,val;
  408. cerr<<"Opening dsp output (duplex)"<<endl;
  409. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_RDWR);
  410. if(m_Dspfd<0)
  411. {
  412. fprintf(stderr,"Can't open audio driver for writing.\n");
  413. m_OutputOk=false;
  414. return false;
  415. }
  416. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  417. CHECK_AND_REPORT_ERROR;
  418. short fgmtsize=0;
  419. int numfgmts=host->FRAGCOUNT;
  420. if (host->FRAGCOUNT==-1) numfgmts=0x7fff;
  421. for (int i=0; i<32; i++)
  422. {
  423. if ((host->FRAGSIZE)==(1<<i))
  424. {
  425. fgmtsize=i;
  426. break;
  427. }
  428. }
  429. if (fgmtsize==0)
  430. {
  431. cerr<<"Fragment size ["<<host->FRAGSIZE<<"] must be power of two!"<<endl;
  432. fgmtsize=256;
  433. }
  434. numfgmts=numfgmts<<16;
  435. val=numfgmts|(int)fgmtsize;
  436. result=ioctl(m_Dspfd, SNDCTL_DSP_SETFRAGMENT, &val);
  437. CHECK_AND_REPORT_ERROR;
  438. val = 1;
  439. result = ioctl(m_Dspfd, SOUND_PCM_WRITE_CHANNELS, &val);
  440. CHECK_AND_REPORT_ERROR;
  441. val = AFMT_S16_LE;
  442. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  443. CHECK_AND_REPORT_ERROR;
  444. if (m_Channels==2) val=1;
  445. else val=0;
  446. result = ioctl(m_Dspfd,SNDCTL_DSP_STEREO,&val);
  447. CHECK_AND_REPORT_ERROR;
  448. val = host->SAMPLERATE;
  449. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  450. CHECK_AND_REPORT_ERROR;
  451. m_OutputOk=true;
  452. return true;
  453. }