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.

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