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.

548 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. // we are an output.
  78. m_IsTerminal=true;
  79. m_PluginInfo.Name="OSS";
  80. m_PluginInfo.Width=100;
  81. m_PluginInfo.Height=100;
  82. m_PluginInfo.NumInputs=2;
  83. m_PluginInfo.NumOutputs=2;
  84. m_PluginInfo.PortTips.push_back("Left Out");
  85. m_PluginInfo.PortTips.push_back("Right Out");
  86. //m_PluginInfo.PortTips.push_back("Record Controller");
  87. m_PluginInfo.PortTips.push_back("Left In");
  88. m_PluginInfo.PortTips.push_back("Right In");
  89. m_AudioCH->Register("Volume",&m_Volume);
  90. m_Mode=NO_MODE;
  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)
  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. m_NoExecuted++;
  168. if (m_NoExecuted==m_RefCount)
  169. {
  170. if (m_Mode==INPUT || m_Mode==DUPLEX) OSSOutput::Get()->Read();
  171. if (m_Mode==OUTPUT || m_Mode==DUPLEX) OSSOutput::Get()->Play();
  172. m_NoExecuted=0;
  173. }
  174. if (m_AudioCH->IsCommandWaiting())
  175. {
  176. switch(m_AudioCH->GetCommand())
  177. {
  178. case OPENREAD :
  179. if (OSSOutput::Get()->OpenRead())
  180. {
  181. m_Mode=INPUT;
  182. //cb_Blocking(m_Parent,true);
  183. }
  184. break;
  185. case OPENWRITE :
  186. if (OSSOutput::Get()->OpenWrite())
  187. {
  188. m_Mode=OUTPUT;
  189. cb_Blocking(m_Parent,true);
  190. }
  191. break;
  192. case OPENDUPLEX :
  193. if (OSSOutput::Get()->OpenReadWrite())
  194. {
  195. m_Mode=DUPLEX;
  196. cb_Blocking(m_Parent,true);
  197. }
  198. break;
  199. case CLOSE :
  200. m_Mode=CLOSED;
  201. cb_Blocking(m_Parent,false);
  202. OSSOutput::Get()->Close();
  203. break;
  204. case SET_VOLUME : OSSOutput::Get()->SetVolume(m_Volume); break;
  205. default : break;
  206. }
  207. }
  208. }
  209. //////////////////////////////////////////////////////////////////////
  210. //////////////////////////////////////////////////////////////////////
  211. OSSOutput::OSSOutput() :
  212. m_Amp(0.5),
  213. m_Channels(2),
  214. m_ReadBufferNum(0),
  215. m_WriteBufferNum(0),
  216. m_OutputOk(false)
  217. {
  218. m_Buffer[0]=NULL;
  219. m_Buffer[1]=NULL;
  220. m_InBuffer[0]=NULL;
  221. m_InBuffer[1]=NULL;
  222. }
  223. //////////////////////////////////////////////////////////////////////
  224. OSSOutput::~OSSOutput()
  225. {
  226. Close();
  227. }
  228. //////////////////////////////////////////////////////////////////////
  229. void OSSOutput::AllocateBuffer()
  230. {
  231. if (m_Buffer[0]==NULL)
  232. {
  233. m_BufSizeBytes=host->BUFSIZE*m_Channels*2;
  234. // initialise for stereo
  235. m_Buffer[0] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  236. m_Buffer[1] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  237. m_InBuffer[0] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  238. m_InBuffer[1] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  239. }
  240. m_Wav.SetSamplerate(host->SAMPLERATE);
  241. }
  242. //////////////////////////////////////////////////////////////////////
  243. void OSSOutput::SendStereo(const Sample *ldata,const Sample *rdata)
  244. {
  245. if (m_Channels!=2) return;
  246. int on=0;
  247. float t;
  248. for (int n=0; n<host->BUFSIZE; n++)
  249. {
  250. // stereo channels - interleave
  251. if (ldata)
  252. {
  253. t=(*ldata)[n]*m_Amp;
  254. if (t>1) t=1;
  255. if (t<-1) t=-1;
  256. m_Buffer[m_WriteBufferNum][on]+=lrintf(t*SHRT_MAX);
  257. }
  258. on++;
  259. if (rdata)
  260. {
  261. t=(*rdata)[n]*m_Amp;
  262. if (t>1) t=1;
  263. if (t<-1) t=-1;
  264. m_Buffer[m_WriteBufferNum][on]+=lrintf(t*SHRT_MAX);
  265. }
  266. on++;
  267. }
  268. }
  269. //////////////////////////////////////////////////////////////////////
  270. void OSSOutput::Play()
  271. {
  272. int BufferToSend=!m_WriteBufferNum;
  273. #if __BYTE_ORDER == BIG_ENDIAN
  274. for (int n=0; n<host->BUFSIZE*m_Channels; n++)
  275. {
  276. m_Buffer[BufferToSend][n]=(short)((m_Buffer[BufferToSend][n]<<8)&0xff00)|
  277. ((m_Buffer[BufferToSend][n]>>8)&0xff);
  278. }
  279. #endif
  280. if (m_OutputOk)
  281. {
  282. write(m_Dspfd,m_Buffer[BufferToSend],m_BufSizeBytes);
  283. }
  284. if(m_Wav.Recording())
  285. {
  286. m_Wav.Save(m_Buffer[BufferToSend],m_BufSizeBytes);
  287. }
  288. memset(m_Buffer[BufferToSend],0,m_BufSizeBytes);
  289. m_WriteBufferNum=BufferToSend;
  290. }
  291. //////////////////////////////////////////////////////////////////////
  292. void OSSOutput::GetStereo(Sample *ldata,Sample *rdata)
  293. {
  294. if (m_Channels!=2) return;
  295. int on=0;
  296. for (int n=0; n<host->BUFSIZE; n++)
  297. {
  298. // stereo channels - interleave
  299. if (ldata) ldata->Set(n,(m_InBuffer[m_ReadBufferNum][on]*m_Amp)/(float)SHRT_MAX);
  300. on++;
  301. if (rdata) rdata->Set(n,(m_InBuffer[m_ReadBufferNum][on]*m_Amp)/(float)SHRT_MAX);
  302. on++;
  303. }
  304. }
  305. //////////////////////////////////////////////////////////////////////
  306. void OSSOutput::Read()
  307. {
  308. int BufferToRead=!m_ReadBufferNum;
  309. if (m_OutputOk)
  310. read(m_Dspfd,m_InBuffer[BufferToRead],m_BufSizeBytes);
  311. #if __BYTE_ORDER == BIG_ENDIAN
  312. for (int n=0; n<host->BUFSIZE*m_Channels; n++)
  313. {
  314. m_InBuffer[BufferToRead][n]=(short)((m_InBuffer[BufferToRead][n]<<8)&0xff00)|
  315. ((m_InBuffer[BufferToRead][n]>>8)&0xff);
  316. }
  317. #endif
  318. m_ReadBufferNum=BufferToRead;
  319. }
  320. //////////////////////////////////////////////////////////////////////
  321. bool OSSOutput::Close()
  322. {
  323. cerr<<"Closing dsp output"<<endl;
  324. close(m_Dspfd);
  325. return true;
  326. }
  327. //////////////////////////////////////////////////////////////////////
  328. bool OSSOutput::OpenWrite()
  329. {
  330. int result,val;
  331. cerr<<"Opening dsp output"<<endl;
  332. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_WRONLY);
  333. if(m_Dspfd<0)
  334. {
  335. fprintf(stderr,"Can't open audio driver for writing.\n");
  336. m_OutputOk=false;
  337. return false;
  338. }
  339. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  340. CHECK_AND_REPORT_ERROR;
  341. short fgmtsize=0;
  342. int numfgmts=host->FRAGCOUNT;
  343. if (host->FRAGCOUNT==-1) numfgmts=0x7fff;
  344. for (int i=0; i<32; i++)
  345. {
  346. if ((host->FRAGSIZE)==(1<<i))
  347. {
  348. fgmtsize=i;
  349. break;
  350. }
  351. }
  352. if (fgmtsize==0)
  353. {
  354. cerr<<"Fragment size ["<<host->FRAGSIZE<<"] must be power of two!"<<endl;
  355. fgmtsize=256;
  356. }
  357. numfgmts=numfgmts<<16;
  358. val=numfgmts|(int)fgmtsize;
  359. result=ioctl(m_Dspfd, SNDCTL_DSP_SETFRAGMENT, &val);
  360. CHECK_AND_REPORT_ERROR;
  361. val = 1;
  362. result = ioctl(m_Dspfd, SOUND_PCM_WRITE_CHANNELS, &val);
  363. CHECK_AND_REPORT_ERROR;
  364. val = AFMT_S16_LE;
  365. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  366. CHECK_AND_REPORT_ERROR;
  367. if (m_Channels==2) val=1;
  368. else val=0;
  369. result = ioctl(m_Dspfd,SNDCTL_DSP_STEREO,&val);
  370. CHECK_AND_REPORT_ERROR;
  371. val = host->SAMPLERATE;
  372. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  373. CHECK_AND_REPORT_ERROR;
  374. m_OutputOk=true;
  375. return true;
  376. }
  377. //////////////////////////////////////////////////////////////////////
  378. bool OSSOutput::OpenRead()
  379. {
  380. int result,val;
  381. cerr<<"Opening dsp input"<<endl;
  382. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_RDONLY);
  383. if(m_Dspfd<0)
  384. {
  385. fprintf(stderr,"Can't open audio driver for reading.\n");
  386. m_OutputOk=false;
  387. return false;
  388. }
  389. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  390. CHECK_AND_REPORT_ERROR;
  391. val = 1;
  392. result = ioctl(m_Dspfd, SOUND_PCM_READ_CHANNELS, &val);
  393. CHECK_AND_REPORT_ERROR;
  394. val = AFMT_S16_LE;
  395. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  396. CHECK_AND_REPORT_ERROR;
  397. val = host->SAMPLERATE;
  398. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  399. CHECK_AND_REPORT_ERROR;
  400. m_OutputOk=true;
  401. return true;
  402. }
  403. //////////////////////////////////////////////////////////////////////
  404. bool OSSOutput::OpenReadWrite()
  405. {
  406. int result,val;
  407. cerr<<"Opening dsp output (duplex)"<<endl;
  408. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_RDWR);
  409. if(m_Dspfd<0)
  410. {
  411. fprintf(stderr,"Can't open audio driver for writing.\n");
  412. m_OutputOk=false;
  413. return false;
  414. }
  415. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  416. CHECK_AND_REPORT_ERROR;
  417. short fgmtsize=0;
  418. int numfgmts=host->FRAGCOUNT;
  419. if (host->FRAGCOUNT==-1) numfgmts=0x7fff;
  420. for (int i=0; i<32; i++)
  421. {
  422. if ((host->FRAGSIZE)==(1<<i))
  423. {
  424. fgmtsize=i;
  425. break;
  426. }
  427. }
  428. if (fgmtsize==0)
  429. {
  430. cerr<<"Fragment size ["<<host->FRAGSIZE<<"] must be power of two!"<<endl;
  431. fgmtsize=256;
  432. }
  433. numfgmts=numfgmts<<16;
  434. val=numfgmts|(int)fgmtsize;
  435. result=ioctl(m_Dspfd, SNDCTL_DSP_SETFRAGMENT, &val);
  436. CHECK_AND_REPORT_ERROR;
  437. val = 1;
  438. result = ioctl(m_Dspfd, SOUND_PCM_WRITE_CHANNELS, &val);
  439. CHECK_AND_REPORT_ERROR;
  440. val = AFMT_S16_LE;
  441. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  442. CHECK_AND_REPORT_ERROR;
  443. if (m_Channels==2) val=1;
  444. else val=0;
  445. result = ioctl(m_Dspfd,SNDCTL_DSP_STEREO,&val);
  446. CHECK_AND_REPORT_ERROR;
  447. val = host->SAMPLERATE;
  448. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  449. CHECK_AND_REPORT_ERROR;
  450. m_OutputOk=true;
  451. return true;
  452. }