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.

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