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.

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