Audio plugin host https://kx.studio/carla
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.

OssEngine.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. OSSaudiooutput.C - Audio output for Open Sound System
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. */
  11. #include "OssEngine.h"
  12. #include "Compressor.h"
  13. #include "../Misc/Util.h"
  14. #include "../Misc/Config.h"
  15. #include "../globals.h"
  16. #include <cstring>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <errno.h>
  21. #include <sys/soundcard.h>
  22. #include <sys/stat.h>
  23. #include <sys/ioctl.h>
  24. #include <unistd.h>
  25. #include <iostream>
  26. #include <signal.h>
  27. #include "InMgr.h"
  28. using namespace std;
  29. namespace zyncarla {
  30. /*
  31. * The following statemachine converts MIDI commands to USB MIDI
  32. * packets, derived from Linux's usbmidi.c, which was written by
  33. * "Clemens Ladisch". It is used to figure out when a MIDI command is
  34. * complete, without having to read the first byte of the next MIDI
  35. * command. This is useful when connecting to so-called system PIPEs
  36. * and FIFOs. See "man mkfifo".
  37. *
  38. * Return values:
  39. * 0: No command
  40. * Else: Command is complete
  41. */
  42. static unsigned char
  43. OssMidiParse(struct OssMidiParse &midi_parse,
  44. unsigned char cn, unsigned char b)
  45. {
  46. unsigned char p0 = (cn << 4);
  47. if(b >= 0xf8) {
  48. midi_parse.temp_0[0] = p0 | 0x0f;
  49. midi_parse.temp_0[1] = b;
  50. midi_parse.temp_0[2] = 0;
  51. midi_parse.temp_0[3] = 0;
  52. midi_parse.temp_cmd = midi_parse.temp_0;
  53. return (1);
  54. } else if(b >= 0xf0) {
  55. switch (b) {
  56. case 0xf0: /* system exclusive begin */
  57. midi_parse.temp_1[1] = b;
  58. midi_parse.state = OSSMIDI_ST_SYSEX_1;
  59. break;
  60. case 0xf1: /* MIDI time code */
  61. case 0xf3: /* song select */
  62. midi_parse.temp_1[1] = b;
  63. midi_parse.state = OSSMIDI_ST_1PARAM;
  64. break;
  65. case 0xf2: /* song position pointer */
  66. midi_parse.temp_1[1] = b;
  67. midi_parse.state = OSSMIDI_ST_2PARAM_1;
  68. break;
  69. case 0xf4: /* unknown */
  70. case 0xf5: /* unknown */
  71. midi_parse.state = OSSMIDI_ST_UNKNOWN;
  72. break;
  73. case 0xf6: /* tune request */
  74. midi_parse.temp_1[0] = p0 | 0x05;
  75. midi_parse.temp_1[1] = 0xf6;
  76. midi_parse.temp_1[2] = 0;
  77. midi_parse.temp_1[3] = 0;
  78. midi_parse.temp_cmd = midi_parse.temp_1;
  79. midi_parse.state = OSSMIDI_ST_UNKNOWN;
  80. return (1);
  81. case 0xf7: /* system exclusive end */
  82. switch (midi_parse.state) {
  83. case OSSMIDI_ST_SYSEX_0:
  84. midi_parse.temp_1[0] = p0 | 0x05;
  85. midi_parse.temp_1[1] = 0xf7;
  86. midi_parse.temp_1[2] = 0;
  87. midi_parse.temp_1[3] = 0;
  88. midi_parse.temp_cmd = midi_parse.temp_1;
  89. midi_parse.state = OSSMIDI_ST_UNKNOWN;
  90. return (1);
  91. case OSSMIDI_ST_SYSEX_1:
  92. midi_parse.temp_1[0] = p0 | 0x06;
  93. midi_parse.temp_1[2] = 0xf7;
  94. midi_parse.temp_1[3] = 0;
  95. midi_parse.temp_cmd = midi_parse.temp_1;
  96. midi_parse.state = OSSMIDI_ST_UNKNOWN;
  97. return (1);
  98. case OSSMIDI_ST_SYSEX_2:
  99. midi_parse.temp_1[0] = p0 | 0x07;
  100. midi_parse.temp_1[3] = 0xf7;
  101. midi_parse.temp_cmd = midi_parse.temp_1;
  102. midi_parse.state = OSSMIDI_ST_UNKNOWN;
  103. return (1);
  104. }
  105. midi_parse.state = OSSMIDI_ST_UNKNOWN;
  106. break;
  107. }
  108. } else if(b >= 0x80) {
  109. midi_parse.temp_1[1] = b;
  110. if((b >= 0xc0) && (b <= 0xdf)) {
  111. midi_parse.state = OSSMIDI_ST_1PARAM;
  112. } else {
  113. midi_parse.state = OSSMIDI_ST_2PARAM_1;
  114. }
  115. } else { /* b < 0x80 */
  116. switch (midi_parse.state) {
  117. case OSSMIDI_ST_1PARAM:
  118. if(midi_parse.temp_1[1] < 0xf0) {
  119. p0 |= midi_parse.temp_1[1] >> 4;
  120. } else {
  121. p0 |= 0x02;
  122. midi_parse.state = OSSMIDI_ST_UNKNOWN;
  123. }
  124. midi_parse.temp_1[0] = p0;
  125. midi_parse.temp_1[2] = b;
  126. midi_parse.temp_1[3] = 0;
  127. midi_parse.temp_cmd = midi_parse.temp_1;
  128. return (1);
  129. case OSSMIDI_ST_2PARAM_1:
  130. midi_parse.temp_1[2] = b;
  131. midi_parse.state = OSSMIDI_ST_2PARAM_2;
  132. break;
  133. case OSSMIDI_ST_2PARAM_2:
  134. if(midi_parse.temp_1[1] < 0xf0) {
  135. p0 |= midi_parse.temp_1[1] >> 4;
  136. midi_parse.state = OSSMIDI_ST_2PARAM_1;
  137. } else {
  138. p0 |= 0x03;
  139. midi_parse.state = OSSMIDI_ST_UNKNOWN;
  140. }
  141. midi_parse.temp_1[0] = p0;
  142. midi_parse.temp_1[3] = b;
  143. midi_parse.temp_cmd = midi_parse.temp_1;
  144. return (1);
  145. case OSSMIDI_ST_SYSEX_0:
  146. midi_parse.temp_1[1] = b;
  147. midi_parse.state = OSSMIDI_ST_SYSEX_1;
  148. break;
  149. case OSSMIDI_ST_SYSEX_1:
  150. midi_parse.temp_1[2] = b;
  151. midi_parse.state = OSSMIDI_ST_SYSEX_2;
  152. break;
  153. case OSSMIDI_ST_SYSEX_2:
  154. midi_parse.temp_1[0] = p0 | 0x04;
  155. midi_parse.temp_1[3] = b;
  156. midi_parse.temp_cmd = midi_parse.temp_1;
  157. midi_parse.state = OSSMIDI_ST_SYSEX_0;
  158. return (1);
  159. default:
  160. break;
  161. }
  162. }
  163. return (0);
  164. }
  165. OssEngine::OssEngine(const SYNTH_T &synth,
  166. const oss_devs_t& oss_devs)
  167. :AudioOut(synth), audioThread(NULL), midiThread(NULL),
  168. linux_oss_wave_out_dev(oss_devs.linux_wave_out),
  169. linux_oss_seq_in_dev(oss_devs.linux_seq_in)
  170. {
  171. name = "OSS";
  172. midi.handle = -1;
  173. audio.handle = -1;
  174. /* allocate worst case audio buffer */
  175. audio.smps.ps32 = new int[synth.buffersize * 2];
  176. memset(audio.smps.ps32, 0, sizeof(int) * synth.buffersize * 2);
  177. memset(&midi.state, 0, sizeof(midi.state));
  178. audio.peaks[0] = 0;
  179. }
  180. OssEngine::~OssEngine()
  181. {
  182. Stop();
  183. delete [] audio.smps.ps32;
  184. }
  185. bool OssEngine::openAudio()
  186. {
  187. int x;
  188. if(audio.handle != -1)
  189. return true; //already open
  190. int snd_fragment;
  191. int snd_stereo = 1; //stereo;
  192. int snd_samplerate = synth.samplerate;
  193. const char *device = getenv("DSP_DEVICE");
  194. if(device == NULL)
  195. device = linux_oss_wave_out_dev;
  196. /* NOTE: PIPEs and FIFOs can block when opening them */
  197. audio.handle = open(device, O_WRONLY, O_NONBLOCK);
  198. if(audio.handle == -1) {
  199. cerr << "ERROR - I can't open the "
  200. << device << '.' << endl;
  201. return false;
  202. }
  203. ioctl(audio.handle, SNDCTL_DSP_RESET, NULL);
  204. /* Figure out the correct format first */
  205. int snd_format16 = AFMT_S16_NE;
  206. #ifdef AFMT_S32_NE
  207. int snd_format32 = AFMT_S32_NE;
  208. if (ioctl(audio.handle, SNDCTL_DSP_SETFMT, &snd_format32) == 0) {
  209. audio.is32bit = true;
  210. } else
  211. #endif
  212. if (ioctl(audio.handle, SNDCTL_DSP_SETFMT, &snd_format16) == 0) {
  213. audio.is32bit = false;
  214. } else {
  215. cerr << "ERROR - I cannot set DSP format for "
  216. << device << '.' << endl;
  217. goto error;
  218. }
  219. ioctl(audio.handle, SNDCTL_DSP_STEREO, &snd_stereo);
  220. ioctl(audio.handle, SNDCTL_DSP_SPEED, &snd_samplerate);
  221. if (snd_samplerate != (int)synth.samplerate) {
  222. cerr << "ERROR - Cannot set samplerate for "
  223. << device << ". " << snd_samplerate
  224. << " != " << synth.samplerate << endl;
  225. goto error;
  226. }
  227. /* compute buffer size for 16-bit stereo samples */
  228. audio.buffersize = 4 * synth.buffersize;
  229. if (audio.is32bit)
  230. audio.buffersize *= 2;
  231. for (x = 4; x < 20; x++) {
  232. if ((1 << x) >= audio.buffersize)
  233. break;
  234. }
  235. snd_fragment = 0x20000 | x; /* 2x buffer */
  236. ioctl(audio.handle, SNDCTL_DSP_SETFRAGMENT, &snd_fragment);
  237. pthread_attr_t attr;
  238. pthread_attr_init(&attr);
  239. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  240. audioThread = new pthread_t;
  241. pthread_create(audioThread, &attr, _audioThreadCb, this);
  242. return true;
  243. error:
  244. close(audio.handle);
  245. audio.handle = -1;
  246. return false;
  247. }
  248. void OssEngine::stopAudio()
  249. {
  250. int handle = audio.handle;
  251. if(handle == -1) //already closed
  252. return;
  253. audio.handle = -1;
  254. /* close handle first, so that write() exits */
  255. close(handle);
  256. pthread_join(*audioThread, NULL);
  257. delete audioThread;
  258. audioThread = NULL;
  259. }
  260. bool OssEngine::Start()
  261. {
  262. bool good = true;
  263. if(!openAudio()) {
  264. cerr << "Failed to open OSS audio" << endl;
  265. good = false;
  266. }
  267. if(!openMidi()) {
  268. cerr << "Failed to open OSS midi" << endl;
  269. good = false;
  270. }
  271. return good;
  272. }
  273. void OssEngine::Stop()
  274. {
  275. stopAudio();
  276. stopMidi();
  277. }
  278. void OssEngine::setMidiEn(bool nval)
  279. {
  280. if(nval)
  281. openMidi();
  282. else
  283. stopMidi();
  284. }
  285. bool OssEngine::getMidiEn() const
  286. {
  287. return midi.handle != -1;
  288. }
  289. void OssEngine::setAudioEn(bool nval)
  290. {
  291. if(nval)
  292. openAudio();
  293. else
  294. stopAudio();
  295. }
  296. bool OssEngine::getAudioEn() const
  297. {
  298. return audio.handle != -1;
  299. }
  300. bool OssEngine::openMidi()
  301. {
  302. int handle = midi.handle;
  303. if(handle != -1)
  304. return true; //already open
  305. const char *device = getenv("MIDI_DEVICE");
  306. if(device == NULL)
  307. device = linux_oss_seq_in_dev;
  308. /* NOTE: PIPEs and FIFOs can block when opening them */
  309. handle = open(device, O_RDONLY, O_NONBLOCK);
  310. if(-1 == handle)
  311. return false;
  312. midi.handle = handle;
  313. pthread_attr_t attr;
  314. pthread_attr_init(&attr);
  315. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  316. midiThread = new pthread_t;
  317. pthread_create(midiThread, &attr, _midiThreadCb, this);
  318. return true;
  319. }
  320. void OssEngine::stopMidi()
  321. {
  322. int handle = midi.handle;
  323. if(handle == -1) //already closed
  324. return;
  325. midi.handle = -1;
  326. /* close handle first, so that read() exits */
  327. close(handle);
  328. pthread_join(*midiThread, NULL);
  329. delete midiThread;
  330. midiThread = NULL;
  331. }
  332. void *OssEngine::_audioThreadCb(void *arg)
  333. {
  334. return (static_cast<OssEngine *>(arg))->audioThreadCb();
  335. }
  336. void *OssEngine::_midiThreadCb(void *arg)
  337. {
  338. return (static_cast<OssEngine *>(arg))->midiThreadCb();
  339. }
  340. void *OssEngine::audioThreadCb()
  341. {
  342. /*
  343. * In case the audio device is a PIPE/FIFO,
  344. * we need to ignore any PIPE signals:
  345. */
  346. signal(SIGPIPE, SIG_IGN);
  347. set_realtime();
  348. while(getAudioEn()) {
  349. const Stereo<float *> smps = getNext();
  350. for(int i = 0; i < synth.buffersize; ++i) {
  351. float l = smps.l[i];
  352. float r = smps.r[i];
  353. stereoCompressor(synth.samplerate, audio.peaks[0], l, r);
  354. if (audio.is32bit) {
  355. audio.smps.ps32[i * 2] = (int) (l * 2147483647.0f);
  356. audio.smps.ps32[i * 2 + 1] = (int) (r * 2147483647.0f);
  357. } else {/* 16bit */
  358. audio.smps.ps16[i * 2] = (short int) (l * 32767.0f);
  359. audio.smps.ps16[i * 2 + 1] = (short int) (r * 32767.0f);
  360. }
  361. }
  362. int error;
  363. do {
  364. /* make a copy of handle, in case of OSS audio disable */
  365. int handle = audio.handle;
  366. if(handle == -1)
  367. goto done;
  368. error = write(handle, audio.smps.ps32, audio.buffersize);
  369. } while (error == -1 && errno == EINTR);
  370. if(error == -1)
  371. goto done;
  372. }
  373. done:
  374. pthread_exit(NULL);
  375. return NULL;
  376. }
  377. void *OssEngine::midiThreadCb()
  378. {
  379. /*
  380. * In case the MIDI device is a PIPE/FIFO,
  381. * we need to ignore any PIPE signals:
  382. */
  383. signal(SIGPIPE, SIG_IGN);
  384. set_realtime();
  385. while(getMidiEn()) {
  386. unsigned char tmp;
  387. int error;
  388. do {
  389. /* make a copy of handle, in case of OSS MIDI disable */
  390. int handle = midi.handle;
  391. if(handle == -1)
  392. goto done;
  393. error = read(handle, &tmp, 1);
  394. } while (error == -1 && errno == EINTR);
  395. /* check that we got one byte */
  396. if(error != 1)
  397. goto done;
  398. /* feed MIDI byte into statemachine */
  399. if(OssMidiParse(midi.state, 0, tmp)) {
  400. /* we got a complete MIDI command */
  401. midiProcess(midi.state.temp_cmd[1],
  402. midi.state.temp_cmd[2],
  403. midi.state.temp_cmd[3]);
  404. }
  405. }
  406. done:
  407. pthread_exit(NULL);
  408. return NULL;
  409. }
  410. }