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.

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