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.

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