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.

586 lines
13KB

  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. OSSClient* OSSClient::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. OSSClient::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. OSSClient::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. bool OutputPlugin::Kill()
  121. {
  122. m_IsDead=true;
  123. OSSClient::Get()->Kill();
  124. m_Mode=CLOSED;
  125. cb_Blocking(m_Parent,false);
  126. return true;
  127. }
  128. void OutputPlugin::Reset()
  129. {
  130. if (m_IsDead) return;
  131. m_IsDead=true;
  132. OSSClient::Get()->Kill();
  133. cb_Blocking(m_Parent,false);
  134. ResetPorts();
  135. OSSClient::Get()->AllocateBuffer();
  136. switch (m_Mode)
  137. {
  138. case INPUT :
  139. OSSClient::Get()->OpenRead();
  140. cb_Blocking(m_Parent,true);
  141. break;
  142. case OUTPUT :
  143. OSSClient::Get()->OpenWrite();
  144. cb_Blocking(m_Parent,true);
  145. break;
  146. case DUPLEX :
  147. OSSClient::Get()->OpenReadWrite();
  148. cb_Blocking(m_Parent,true);
  149. break;
  150. default:{}
  151. }
  152. m_IsDead=false;
  153. }
  154. void OutputPlugin::Execute()
  155. {
  156. if (m_IsDead)
  157. return;
  158. if (m_Mode==NO_MODE && m_RefCount==1)
  159. {
  160. if (OSSClient::Get()->OpenWrite())
  161. {
  162. cb_Blocking(m_Parent,true);
  163. m_Mode=OUTPUT;
  164. m_NotifyOpenOut=true;
  165. }
  166. }
  167. if (m_Mode==OUTPUT || m_Mode==DUPLEX)
  168. {
  169. OSSClient::Get()->SendStereo(GetInput(0),GetInput(1));
  170. }
  171. if (m_Mode==INPUT || m_Mode==DUPLEX)
  172. OSSClient::Get()->GetStereo(GetOutputBuf(0),GetOutputBuf(1));
  173. }
  174. void OutputPlugin::ExecuteCommands()
  175. {
  176. if (m_IsDead)
  177. return;
  178. if (m_AudioCH->IsCommandWaiting())
  179. {
  180. switch(m_AudioCH->GetCommand())
  181. {
  182. case OPENREAD :
  183. if (OSSClient::Get()->OpenRead())
  184. {
  185. m_Mode=INPUT;
  186. //cb_Blocking(m_Parent,true);
  187. }
  188. break;
  189. case OPENWRITE :
  190. if (OSSClient::Get()->OpenWrite())
  191. {
  192. m_Mode=OUTPUT;
  193. cb_Blocking(m_Parent,true);
  194. }
  195. break;
  196. case OPENDUPLEX :
  197. if (OSSClient::Get()->OpenReadWrite())
  198. {
  199. m_Mode=DUPLEX;
  200. cb_Blocking(m_Parent,true);
  201. }
  202. break;
  203. case CLOSE :
  204. m_Mode=CLOSED;
  205. cb_Blocking(m_Parent,false);
  206. OSSClient::Get()->Close();
  207. break;
  208. case SET_VOLUME :
  209. OSSClient::Get()->SetVolume(m_Volume);
  210. break;
  211. case CLEAR_NOTIFY:
  212. m_NotifyOpenOut=false;
  213. break;
  214. default: break;
  215. }
  216. }
  217. }
  218. void OutputPlugin::ProcessAudio()
  219. {
  220. if (m_IsDead)
  221. return;
  222. // Only Play() once per set of plugins
  223. m_NoExecuted--;
  224. if (m_NoExecuted<=0)
  225. {
  226. if (m_Mode==INPUT || m_Mode==DUPLEX) OSSClient::Get()->Read();
  227. if (m_Mode==OUTPUT || m_Mode==DUPLEX) OSSClient::Get()->Play();
  228. m_NoExecuted=m_RefCount;
  229. }
  230. }
  231. //////////////////////////////////////////////////////////////////////
  232. //////////////////////////////////////////////////////////////////////
  233. OSSClient::OSSClient() :
  234. m_Amp(0.5),
  235. m_Channels(2),
  236. m_ReadBufferNum(0),
  237. m_WriteBufferNum(0),
  238. m_OutputOk(false),
  239. m_IsDead(false)
  240. {
  241. m_Buffer[0]=NULL;
  242. m_Buffer[1]=NULL;
  243. m_InBuffer[0]=NULL;
  244. m_InBuffer[1]=NULL;
  245. }
  246. //////////////////////////////////////////////////////////////////////
  247. OSSClient::~OSSClient()
  248. {
  249. Close();
  250. DeallocateBuffer();
  251. }
  252. //////////////////////////////////////////////////////////////////////
  253. void OSSClient::AllocateBuffer()
  254. {
  255. if (m_Buffer[0]==NULL)
  256. {
  257. m_BufSizeBytes=host->BUFSIZE*m_Channels*2;
  258. // initialise for stereo
  259. m_Buffer[0] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  260. m_Buffer[1] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  261. m_InBuffer[0] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  262. m_InBuffer[1] = (short*) calloc(m_BufSizeBytes/2,m_BufSizeBytes);
  263. }
  264. }
  265. void OSSClient::DeallocateBuffer()
  266. {
  267. if (m_Buffer[0]!=NULL)
  268. {
  269. m_BufSizeBytes=0;
  270. // initialise for stereo
  271. free(m_Buffer[0]);
  272. free(m_Buffer[1]);
  273. free(m_InBuffer[0]);
  274. free(m_InBuffer[1]);
  275. }
  276. }
  277. //////////////////////////////////////////////////////////////////////
  278. void OSSClient::SendStereo(const Sample *ldata,const Sample *rdata)
  279. {
  280. if (m_Channels!=2) return;
  281. int on=0;
  282. float t;
  283. for (int n=0; n<host->BUFSIZE; n++)
  284. {
  285. if (m_IsDead) return;
  286. // stereo channels - interleave
  287. if (ldata)
  288. {
  289. t=(*ldata)[n]*m_Amp;
  290. if (t>1) t=1;
  291. if (t<-1) t=-1;
  292. m_Buffer[m_WriteBufferNum][on]+=lrintf(t*SHRT_MAX);
  293. }
  294. on++;
  295. if (rdata)
  296. {
  297. t=(*rdata)[n]*m_Amp;
  298. if (t>1) t=1;
  299. if (t<-1) t=-1;
  300. m_Buffer[m_WriteBufferNum][on]+=lrintf(t*SHRT_MAX);
  301. }
  302. on++;
  303. }
  304. }
  305. //////////////////////////////////////////////////////////////////////
  306. void OSSClient::Play()
  307. {
  308. int BufferToSend=!m_WriteBufferNum;
  309. #if __BYTE_ORDER == BIG_ENDIAN
  310. for (int n=0; n<host->BUFSIZE*m_Channels; n++)
  311. {
  312. m_Buffer[BufferToSend][n]=(short)((m_Buffer[BufferToSend][n]<<8)&0xff00)|
  313. ((m_Buffer[BufferToSend][n]>>8)&0xff);
  314. }
  315. #endif
  316. if (m_OutputOk)
  317. {
  318. write(m_Dspfd,m_Buffer[BufferToSend],m_BufSizeBytes);
  319. }
  320. memset(m_Buffer[BufferToSend],0,m_BufSizeBytes);
  321. m_WriteBufferNum=BufferToSend;
  322. }
  323. //////////////////////////////////////////////////////////////////////
  324. void OSSClient::GetStereo(Sample *ldata,Sample *rdata)
  325. {
  326. if (m_Channels!=2) return;
  327. int on=0;
  328. for (int n=0; n<host->BUFSIZE; n++)
  329. {
  330. if (m_IsDead) return;
  331. // stereo channels - interleave
  332. if (ldata) ldata->Set(n,(m_InBuffer[m_ReadBufferNum][on]*m_Amp)/(float)SHRT_MAX);
  333. on++;
  334. if (rdata) rdata->Set(n,(m_InBuffer[m_ReadBufferNum][on]*m_Amp)/(float)SHRT_MAX);
  335. on++;
  336. }
  337. }
  338. //////////////////////////////////////////////////////////////////////
  339. void OSSClient::Read()
  340. {
  341. int BufferToRead=!m_ReadBufferNum;
  342. if (m_OutputOk)
  343. read(m_Dspfd,m_InBuffer[BufferToRead],m_BufSizeBytes);
  344. #if __BYTE_ORDER == BIG_ENDIAN
  345. for (int n=0; n<host->BUFSIZE*m_Channels; n++)
  346. {
  347. m_InBuffer[BufferToRead][n]=(short)((m_InBuffer[BufferToRead][n]<<8)&0xff00)|
  348. ((m_InBuffer[BufferToRead][n]>>8)&0xff);
  349. }
  350. #endif
  351. m_ReadBufferNum=BufferToRead;
  352. }
  353. //////////////////////////////////////////////////////////////////////
  354. bool OSSClient::Close()
  355. {
  356. cerr<<"Closing dsp output"<<endl;
  357. close(m_Dspfd);
  358. return true;
  359. }
  360. //////////////////////////////////////////////////////////////////////
  361. bool OSSClient::OpenWrite()
  362. {
  363. int result,val;
  364. cerr<<"Opening dsp output"<<endl;
  365. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_WRONLY);
  366. if(m_Dspfd<0)
  367. {
  368. fprintf(stderr,"Can't open audio driver for writing.\n");
  369. m_OutputOk=false;
  370. return false;
  371. }
  372. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  373. CHECK_AND_REPORT_ERROR;
  374. short fgmtsize=0;
  375. int numfgmts=host->FRAGCOUNT;
  376. if (host->FRAGCOUNT==-1) numfgmts=0x7fff;
  377. for (int i=0; i<32; i++)
  378. {
  379. if ((host->FRAGSIZE)==(1<<i))
  380. {
  381. fgmtsize=i;
  382. break;
  383. }
  384. }
  385. if (fgmtsize==0)
  386. {
  387. cerr<<"Fragment size ["<<host->FRAGSIZE<<"] must be power of two!"<<endl;
  388. fgmtsize=256;
  389. }
  390. numfgmts=numfgmts<<16;
  391. val=numfgmts|(int)fgmtsize;
  392. result=ioctl(m_Dspfd, SNDCTL_DSP_SETFRAGMENT, &val);
  393. CHECK_AND_REPORT_ERROR;
  394. val = 1;
  395. result = ioctl(m_Dspfd, SOUND_PCM_WRITE_CHANNELS, &val);
  396. CHECK_AND_REPORT_ERROR;
  397. val = AFMT_S16_LE;
  398. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  399. CHECK_AND_REPORT_ERROR;
  400. if (m_Channels==2) val=1;
  401. else val=0;
  402. result = ioctl(m_Dspfd,SNDCTL_DSP_STEREO,&val);
  403. CHECK_AND_REPORT_ERROR;
  404. val = host->SAMPLERATE;
  405. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  406. CHECK_AND_REPORT_ERROR;
  407. m_OutputOk=true;
  408. return true;
  409. }
  410. //////////////////////////////////////////////////////////////////////
  411. bool OSSClient::OpenRead()
  412. {
  413. int result,val;
  414. cerr<<"Opening dsp input"<<endl;
  415. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_RDONLY);
  416. if(m_Dspfd<0)
  417. {
  418. fprintf(stderr,"Can't open audio driver for reading.\n");
  419. m_OutputOk=false;
  420. return false;
  421. }
  422. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  423. CHECK_AND_REPORT_ERROR;
  424. val = 1;
  425. result = ioctl(m_Dspfd, SOUND_PCM_READ_CHANNELS, &val);
  426. CHECK_AND_REPORT_ERROR;
  427. val = AFMT_S16_LE;
  428. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  429. CHECK_AND_REPORT_ERROR;
  430. val = host->SAMPLERATE;
  431. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  432. CHECK_AND_REPORT_ERROR;
  433. m_OutputOk=true;
  434. return true;
  435. }
  436. //////////////////////////////////////////////////////////////////////
  437. bool OSSClient::OpenReadWrite()
  438. {
  439. int result,val;
  440. cerr<<"Opening dsp output (duplex)"<<endl;
  441. m_Dspfd = open(host->OUTPUTFILE.c_str(),O_RDWR);
  442. if(m_Dspfd<0)
  443. {
  444. fprintf(stderr,"Can't open audio driver for writing.\n");
  445. m_OutputOk=false;
  446. return false;
  447. }
  448. result = ioctl(m_Dspfd,SNDCTL_DSP_RESET,NULL);
  449. CHECK_AND_REPORT_ERROR;
  450. short fgmtsize=0;
  451. int numfgmts=host->FRAGCOUNT;
  452. if (host->FRAGCOUNT==-1) numfgmts=0x7fff;
  453. for (int i=0; i<32; i++)
  454. {
  455. if ((host->FRAGSIZE)==(1<<i))
  456. {
  457. fgmtsize=i;
  458. break;
  459. }
  460. }
  461. if (fgmtsize==0)
  462. {
  463. cerr<<"Fragment size ["<<host->FRAGSIZE<<"] must be power of two!"<<endl;
  464. fgmtsize=256;
  465. }
  466. numfgmts=numfgmts<<16;
  467. val=numfgmts|(int)fgmtsize;
  468. result=ioctl(m_Dspfd, SNDCTL_DSP_SETFRAGMENT, &val);
  469. CHECK_AND_REPORT_ERROR;
  470. val = 1;
  471. result = ioctl(m_Dspfd, SOUND_PCM_WRITE_CHANNELS, &val);
  472. CHECK_AND_REPORT_ERROR;
  473. val = AFMT_S16_LE;
  474. result = ioctl(m_Dspfd,SNDCTL_DSP_SETFMT,&val);
  475. CHECK_AND_REPORT_ERROR;
  476. if (m_Channels==2) val=1;
  477. else val=0;
  478. result = ioctl(m_Dspfd,SNDCTL_DSP_STEREO,&val);
  479. CHECK_AND_REPORT_ERROR;
  480. val = host->SAMPLERATE;
  481. result = ioctl(m_Dspfd,SNDCTL_DSP_SPEED,&val);
  482. CHECK_AND_REPORT_ERROR;
  483. m_OutputOk=true;
  484. return true;
  485. }