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.

367 lines
10KB

  1. /*
  2. AlsaEngine.cpp
  3. Copyright 2009, Alan Calvert
  4. 2010, Mark McCurry
  5. This file is part of ZynAddSubFX, which is free software: you can
  6. redistribute it and/or modify it under the terms of the GNU General
  7. Public License as published by the Free Software Foundation, either
  8. version 3 of the License, or (at your option) any later version.
  9. ZynAddSubFX 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. You should have received a copy of the GNU General Public License
  14. along with ZynAddSubFX. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <iostream>
  17. #include <cmath>
  18. using namespace std;
  19. #include "../Misc/Util.h"
  20. #include "../Misc/Config.h"
  21. #include "InMgr.h"
  22. #include "AlsaEngine.h"
  23. AlsaEngine::AlsaEngine()
  24. :AudioOut()
  25. {
  26. audio.buffer = new short[synth->buffersize * 2];
  27. name = "ALSA";
  28. audio.handle = NULL;
  29. midi.handle = NULL;
  30. midi.alsaId = -1;
  31. midi.pThread = 0;
  32. }
  33. AlsaEngine::~AlsaEngine()
  34. {
  35. Stop();
  36. delete[] audio.buffer;
  37. }
  38. void *AlsaEngine::_AudioThread(void *arg)
  39. {
  40. return (static_cast<AlsaEngine *>(arg))->AudioThread();
  41. }
  42. void *AlsaEngine::AudioThread()
  43. {
  44. set_realtime();
  45. return processAudio();
  46. }
  47. bool AlsaEngine::Start()
  48. {
  49. return openAudio() && openMidi();
  50. }
  51. void AlsaEngine::Stop()
  52. {
  53. if(getMidiEn())
  54. setMidiEn(false);
  55. if(getAudioEn())
  56. setAudioEn(false);
  57. snd_config_update_free_global();
  58. }
  59. void AlsaEngine::setMidiEn(bool nval)
  60. {
  61. if(nval)
  62. openMidi();
  63. else
  64. stopMidi();
  65. }
  66. bool AlsaEngine::getMidiEn() const
  67. {
  68. return midi.handle;
  69. }
  70. void AlsaEngine::setAudioEn(bool nval)
  71. {
  72. if(nval)
  73. openAudio();
  74. else
  75. stopAudio();
  76. }
  77. bool AlsaEngine::getAudioEn() const
  78. {
  79. return audio.handle;
  80. }
  81. void *AlsaEngine::_MidiThread(void *arg)
  82. {
  83. return static_cast<AlsaEngine *>(arg)->MidiThread();
  84. }
  85. void *AlsaEngine::MidiThread(void)
  86. {
  87. snd_seq_event_t *event;
  88. MidiEvent ev;
  89. set_realtime();
  90. while(snd_seq_event_input(midi.handle, &event) > 0) {
  91. //ensure ev is empty
  92. ev.channel = 0;
  93. ev.num = 0;
  94. ev.value = 0;
  95. ev.type = 0;
  96. if(!event)
  97. continue;
  98. switch(event->type) {
  99. case SND_SEQ_EVENT_NOTEON:
  100. if(event->data.note.note) {
  101. ev.type = M_NOTE;
  102. ev.channel = event->data.note.channel;
  103. ev.num = event->data.note.note;
  104. ev.value = event->data.note.velocity;
  105. InMgr::getInstance().putEvent(ev);
  106. }
  107. break;
  108. case SND_SEQ_EVENT_NOTEOFF:
  109. ev.type = M_NOTE;
  110. ev.channel = event->data.note.channel;
  111. ev.num = event->data.note.note;
  112. ev.value = 0;
  113. InMgr::getInstance().putEvent(ev);
  114. break;
  115. case SND_SEQ_EVENT_KEYPRESS:
  116. ev.type = M_PRESSURE;
  117. ev.channel = event->data.note.channel;
  118. ev.num = event->data.note.note;
  119. ev.value = event->data.note.velocity;
  120. InMgr::getInstance().putEvent(ev);
  121. break;
  122. case SND_SEQ_EVENT_PITCHBEND:
  123. ev.type = M_CONTROLLER;
  124. ev.channel = event->data.control.channel;
  125. ev.num = C_pitchwheel;
  126. ev.value = event->data.control.value;
  127. InMgr::getInstance().putEvent(ev);
  128. break;
  129. case SND_SEQ_EVENT_CONTROLLER:
  130. ev.type = M_CONTROLLER;
  131. ev.channel = event->data.control.channel;
  132. ev.num = event->data.control.param;
  133. ev.value = event->data.control.value;
  134. InMgr::getInstance().putEvent(ev);
  135. break;
  136. case SND_SEQ_EVENT_PGMCHANGE:
  137. ev.type = M_PGMCHANGE;
  138. ev.channel = event->data.control.channel;
  139. ev.num = event->data.control.value;
  140. InMgr::getInstance().putEvent(ev);
  141. break;
  142. case SND_SEQ_EVENT_RESET: // reset to power-on state
  143. ev.type = M_CONTROLLER;
  144. ev.channel = event->data.control.channel;
  145. ev.num = C_resetallcontrollers;
  146. ev.value = 0;
  147. InMgr::getInstance().putEvent(ev);
  148. break;
  149. case SND_SEQ_EVENT_PORT_SUBSCRIBED: // ports connected
  150. if(true)
  151. cout << "Info, alsa midi port connected" << endl;
  152. break;
  153. case SND_SEQ_EVENT_PORT_UNSUBSCRIBED: // ports disconnected
  154. if(true)
  155. cout << "Info, alsa midi port disconnected" << endl;
  156. break;
  157. case SND_SEQ_EVENT_SYSEX: // system exclusive
  158. case SND_SEQ_EVENT_SENSING: // midi device still there
  159. break;
  160. default:
  161. if(true)
  162. cout << "Info, other non-handled midi event, type: "
  163. << (int)event->type << endl;
  164. break;
  165. }
  166. snd_seq_free_event(event);
  167. }
  168. return NULL;
  169. }
  170. bool AlsaEngine::openMidi()
  171. {
  172. if(getMidiEn())
  173. return true;
  174. int alsaport;
  175. midi.handle = NULL;
  176. if(snd_seq_open(&midi.handle, "default", SND_SEQ_OPEN_INPUT, 0) != 0)
  177. return false;
  178. snd_seq_set_client_name(midi.handle, "ZynAddSubFX");
  179. alsaport = snd_seq_create_simple_port(
  180. midi.handle,
  181. "ZynAddSubFX",
  182. SND_SEQ_PORT_CAP_WRITE
  183. | SND_SEQ_PORT_CAP_SUBS_WRITE,
  184. SND_SEQ_PORT_TYPE_SYNTH);
  185. if(alsaport < 0)
  186. return false;
  187. pthread_attr_t attr;
  188. pthread_attr_init(&attr);
  189. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  190. pthread_create(&midi.pThread, &attr, _MidiThread, this);
  191. return true;
  192. }
  193. void AlsaEngine::stopMidi()
  194. {
  195. if(!getMidiEn())
  196. return;
  197. snd_seq_t *handle = midi.handle;
  198. if((NULL != midi.handle) && midi.pThread)
  199. pthread_cancel(midi.pThread);
  200. midi.handle = NULL;
  201. if(handle)
  202. snd_seq_close(handle);
  203. }
  204. short *AlsaEngine::interleave(const Stereo<float *> &smps)
  205. {
  206. /**\todo TODO fix repeated allocation*/
  207. short *shortInterleaved = audio.buffer;
  208. memset(shortInterleaved, 0, bufferSize * 2 * sizeof(short));
  209. int idx = 0; //possible off by one error here
  210. double scaled;
  211. for(int frame = 0; frame < bufferSize; ++frame) { // with a nod to libsamplerate ...
  212. scaled = smps.l[frame] * (8.0f * 0x10000000);
  213. shortInterleaved[idx++] = (short int)(lrint(scaled) >> 16);
  214. scaled = smps.r[frame] * (8.0f * 0x10000000);
  215. shortInterleaved[idx++] = (short int)(lrint(scaled) >> 16);
  216. }
  217. return shortInterleaved;
  218. }
  219. bool AlsaEngine::openAudio()
  220. {
  221. if(getAudioEn())
  222. return true;
  223. int rc = 0;
  224. /* Open PCM device for playback. */
  225. audio.handle = NULL;
  226. rc = snd_pcm_open(&audio.handle, "hw:0",
  227. SND_PCM_STREAM_PLAYBACK, 0);
  228. if(rc < 0) {
  229. fprintf(stderr,
  230. "unable to open pcm device: %s\n",
  231. snd_strerror(rc));
  232. return false;
  233. }
  234. /* Allocate a hardware parameters object. */
  235. snd_pcm_hw_params_alloca(&audio.params);
  236. /* Fill it in with default values. */
  237. snd_pcm_hw_params_any(audio.handle, audio.params);
  238. /* Set the desired hardware parameters. */
  239. /* Interleaved mode */
  240. snd_pcm_hw_params_set_access(audio.handle, audio.params,
  241. SND_PCM_ACCESS_RW_INTERLEAVED);
  242. /* Signed 16-bit little-endian format */
  243. snd_pcm_hw_params_set_format(audio.handle, audio.params,
  244. SND_PCM_FORMAT_S16_LE);
  245. /* Two channels (stereo) */
  246. snd_pcm_hw_params_set_channels(audio.handle, audio.params, 2);
  247. audio.sampleRate = synth->samplerate;
  248. snd_pcm_hw_params_set_rate_near(audio.handle, audio.params,
  249. &audio.sampleRate, NULL);
  250. audio.frames = 512;
  251. snd_pcm_hw_params_set_period_size_near(audio.handle,
  252. audio.params, &audio.frames, NULL);
  253. audio.periods = 4;
  254. snd_pcm_hw_params_set_periods_near(audio.handle,
  255. audio.params, &audio.periods, NULL);
  256. /* Write the parameters to the driver */
  257. rc = snd_pcm_hw_params(audio.handle, audio.params);
  258. if(rc < 0) {
  259. fprintf(stderr,
  260. "unable to set hw parameters: %s\n",
  261. snd_strerror(rc));
  262. return false;
  263. }
  264. /* Set buffer size (in frames). The resulting latency is given by */
  265. /* latency = periodsize * periods / (rate * bytes_per_frame) */
  266. snd_pcm_hw_params_set_buffer_size(audio.handle,
  267. audio.params,
  268. synth->buffersize);
  269. //snd_pcm_hw_params_get_period_size(audio.params, &audio.frames, NULL);
  270. //snd_pcm_hw_params_get_period_time(audio.params, &val, NULL);
  271. pthread_attr_t attr;
  272. pthread_attr_init(&attr);
  273. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  274. pthread_create(&audio.pThread, &attr, _AudioThread, this);
  275. return true;
  276. }
  277. void AlsaEngine::stopAudio()
  278. {
  279. if(!getAudioEn())
  280. return;
  281. snd_pcm_t *handle = audio.handle;
  282. audio.handle = NULL;
  283. pthread_join(audio.pThread, NULL);
  284. snd_pcm_drain(handle);
  285. if(snd_pcm_close(handle))
  286. cout << "Error: in snd_pcm_close " << __LINE__ << ' ' << __FILE__
  287. << endl;
  288. }
  289. void *AlsaEngine::processAudio()
  290. {
  291. while(audio.handle) {
  292. audio.buffer = interleave(getNext());
  293. snd_pcm_t *handle = audio.handle;
  294. int rc = snd_pcm_writei(handle, audio.buffer, synth->buffersize);
  295. if(rc == -EPIPE) {
  296. /* EPIPE means underrun */
  297. cerr << "underrun occurred" << endl;
  298. snd_pcm_prepare(handle);
  299. }
  300. else
  301. if(rc < 0)
  302. cerr << "error from writei: " << snd_strerror(rc) << endl;
  303. }
  304. return NULL;
  305. }