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.

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