jack2 codebase
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.

519 lines
20KB

  1. /*
  2. Copyright (C) 2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __JackAlsaIOAdapter__
  16. #define __JackAlsaIOAdapter__
  17. #include <math.h>
  18. #include <limits.h>
  19. #include <alsa/asoundlib.h>
  20. #include "JackIOAdapter.h"
  21. #include "JackPlatformThread.h"
  22. #include "jack.h"
  23. #include "JackError.h"
  24. namespace Jack
  25. {
  26. inline void* aligned_calloc(size_t nmemb, size_t size) { return (void*)((size_t)(calloc((nmemb*size) + 15, sizeof(char))) + 15 & ~15); }
  27. #define max(x,y) (((x)>(y)) ? (x) : (y))
  28. #define min(x,y) (((x)<(y)) ? (x) : (y))
  29. #define check_error(err) if (err) { jack_error("%s:%d, alsa error %d : %s", __FILE__, __LINE__, err, snd_strerror(err)); return err; }
  30. #define check_error_msg(err,msg) if (err) { jack_error("%s:%d, %s : %s(%d)", __FILE__, __LINE__, msg, snd_strerror(err), err); return err; }
  31. #define display_error_msg(err,msg) if (err) { jack_error("%s:%d, %s : %s(%d)", __FILE__, __LINE__, msg, snd_strerror(err), err); }
  32. /**
  33. * A convenient class to pass parameters to AudioInterface
  34. */
  35. class AudioParam
  36. {
  37. public:
  38. const char* fCardName;
  39. unsigned int fFrequency;
  40. int fBuffering;
  41. unsigned int fSoftInputs;
  42. unsigned int fSoftOutputs;
  43. public:
  44. AudioParam() :
  45. fCardName("hw:0"),
  46. fFrequency(44100),
  47. fBuffering(512),
  48. fSoftInputs(2),
  49. fSoftOutputs(2)
  50. {}
  51. AudioParam(int input, int output, jack_nframes_t buffer_size, jack_nframes_t sample_rate) :
  52. fCardName("hw:0"),
  53. fFrequency(sample_rate),
  54. fBuffering(buffer_size),
  55. fSoftInputs(input),
  56. fSoftOutputs(output)
  57. {}
  58. AudioParam& cardName(const char* n) { fCardName = n; return *this; }
  59. AudioParam& frequency(int f) { fFrequency = f; return *this; }
  60. AudioParam& buffering(int fpb) { fBuffering = fpb; return *this; }
  61. AudioParam& inputs(int n) { fSoftInputs = n; return *this; }
  62. AudioParam& outputs(int n) { fSoftOutputs = n; return *this; }
  63. };
  64. /**
  65. * An ALSA audio interface
  66. */
  67. class AudioInterface : public AudioParam
  68. {
  69. public:
  70. snd_pcm_t* fOutputDevice;
  71. snd_pcm_t* fInputDevice;
  72. snd_pcm_hw_params_t* fInputParams;
  73. snd_pcm_hw_params_t* fOutputParams;
  74. snd_pcm_format_t fSampleFormat;
  75. snd_pcm_access_t fSampleAccess;
  76. unsigned int fCardInputs;
  77. unsigned int fCardOutputs;
  78. unsigned int fChanInputs;
  79. unsigned int fChanOutputs;
  80. // interleaved mode audiocard buffers
  81. void* fInputCardBuffer;
  82. void* fOutputCardBuffer;
  83. // non interleaved mode audiocard buffers
  84. void* fInputCardChannels[256];
  85. void* fOutputCardChannels[256];
  86. // non interleaved mod, floating point software buffers
  87. float* fInputSoftChannels[256];
  88. float* fOutputSoftChannels[256];
  89. public:
  90. const char* cardName() { return fCardName; }
  91. int frequency() { return fFrequency; }
  92. int buffering() { return fBuffering; }
  93. float** inputSoftChannels() { return fInputSoftChannels; }
  94. float** outputSoftChannels() { return fOutputSoftChannels; }
  95. AudioInterface(const AudioParam& ap = AudioParam()) : AudioParam(ap)
  96. {
  97. fInputDevice = 0;
  98. fOutputDevice = 0;
  99. fInputParams = 0;
  100. fOutputParams = 0;
  101. }
  102. AudioInterface(int input, int output, jack_nframes_t buffer_size, jack_nframes_t sample_rate) :
  103. AudioParam(input, output, buffer_size, sample_rate)
  104. {}
  105. /**
  106. * Open the audio interface
  107. */
  108. int open()
  109. {
  110. int err;
  111. jack_log("open %s", fCardName);
  112. // allocation d'un stream d'entree et d'un stream de sortie
  113. err = snd_pcm_open(&fInputDevice, fCardName, SND_PCM_STREAM_CAPTURE, 0); check_error(err)
  114. jack_log("open 1");
  115. err = snd_pcm_open(&fOutputDevice, fCardName, SND_PCM_STREAM_PLAYBACK, 0); check_error(err)
  116. jack_log("open 2");
  117. // recherche des parametres d'entree
  118. err = snd_pcm_hw_params_malloc(&fInputParams); check_error(err);
  119. setAudioParams(fInputDevice, fInputParams);
  120. snd_pcm_hw_params_get_channels(fInputParams, &fCardInputs);
  121. // recherche des parametres de sortie
  122. err = snd_pcm_hw_params_malloc( &fOutputParams ); check_error(err)
  123. setAudioParams(fOutputDevice, fOutputParams);
  124. snd_pcm_hw_params_get_channels(fOutputParams, &fCardOutputs);
  125. jack_info("inputs : %ud, outputs : %ud", fCardInputs, fCardOutputs);
  126. // enregistrement des parametres d'entree-sortie
  127. err = snd_pcm_hw_params(fInputDevice, fInputParams); check_error (err);
  128. err = snd_pcm_hw_params(fOutputDevice, fOutputParams); check_error (err);
  129. //assert(snd_pcm_hw_params_get_period_size(fInputParams,NULL) == snd_pcm_hw_params_get_period_size(fOutputParams,NULL));
  130. // allocation of alsa buffers
  131. if (fSampleAccess == SND_PCM_ACCESS_RW_INTERLEAVED) {
  132. fInputCardBuffer = aligned_calloc(interleavedBufferSize(fInputParams), 1);
  133. fOutputCardBuffer = aligned_calloc(interleavedBufferSize(fOutputParams), 1);
  134. } else {
  135. for (unsigned int i = 0; i < fCardInputs; i++) {
  136. fInputCardChannels[i] = aligned_calloc(noninterleavedBufferSize(fInputParams), 1);
  137. }
  138. for (unsigned int i = 0; i < fCardOutputs; i++) {
  139. fOutputCardChannels[i] = aligned_calloc(noninterleavedBufferSize(fOutputParams), 1);
  140. }
  141. }
  142. // allocation of floating point buffers needed by the dsp code
  143. fChanInputs = max(fSoftInputs, fCardInputs); assert (fChanInputs < 256);
  144. fChanOutputs = max(fSoftOutputs, fCardOutputs); assert (fChanOutputs < 256);
  145. for (unsigned int i = 0; i < fChanInputs; i++) {
  146. fInputSoftChannels[i] = (float*) aligned_calloc (fBuffering, sizeof(float));
  147. for (int j = 0; j < fBuffering; j++) {
  148. fInputSoftChannels[i][j] = 0.0;
  149. }
  150. }
  151. for (unsigned int i = 0; i < fChanOutputs; i++) {
  152. fOutputSoftChannels[i] = (float*) aligned_calloc (fBuffering, sizeof(float));
  153. for (int j = 0; j < fBuffering; j++) {
  154. fOutputSoftChannels[i][j] = 0.0;
  155. }
  156. }
  157. jack_log("opren ok");
  158. return 0;
  159. }
  160. int setAudioParams(snd_pcm_t* stream, snd_pcm_hw_params_t* params)
  161. {
  162. int err;
  163. // set params record with initial values
  164. err = snd_pcm_hw_params_any ( stream, params );
  165. check_error_msg(err, "unable to init parameters")
  166. // set alsa access mode (and fSampleAccess field) either to non interleaved or interleaved
  167. err = snd_pcm_hw_params_set_access (stream, params, SND_PCM_ACCESS_RW_NONINTERLEAVED );
  168. if (err) {
  169. err = snd_pcm_hw_params_set_access (stream, params, SND_PCM_ACCESS_RW_INTERLEAVED );
  170. check_error_msg(err, "unable to set access mode neither to non-interleaved or to interleaved");
  171. }
  172. snd_pcm_hw_params_get_access(params, &fSampleAccess);
  173. // search for 32-bits or 16-bits format
  174. err = snd_pcm_hw_params_set_format (stream, params, SND_PCM_FORMAT_S32);
  175. if (err) {
  176. err = snd_pcm_hw_params_set_format (stream, params, SND_PCM_FORMAT_S16);
  177. check_error_msg(err, "unable to set format to either 32-bits or 16-bits");
  178. }
  179. snd_pcm_hw_params_get_format(params, &fSampleFormat);
  180. // set sample frequency
  181. snd_pcm_hw_params_set_rate_near (stream, params, &fFrequency, 0);
  182. // set period and period size (buffering)
  183. err = snd_pcm_hw_params_set_period_size (stream, params, fBuffering, 0);
  184. check_error_msg(err, "period size not available");
  185. err = snd_pcm_hw_params_set_periods (stream, params, 2, 0);
  186. check_error_msg(err, "number of periods not available");
  187. return 0;
  188. }
  189. ssize_t interleavedBufferSize(snd_pcm_hw_params_t* params)
  190. {
  191. _snd_pcm_format format; snd_pcm_hw_params_get_format(params, &format);
  192. snd_pcm_uframes_t psize; snd_pcm_hw_params_get_period_size(params, &psize, NULL);
  193. unsigned int channels; snd_pcm_hw_params_get_channels(params, &channels);
  194. ssize_t bsize = snd_pcm_format_size (format, psize * channels);
  195. return bsize;
  196. }
  197. ssize_t noninterleavedBufferSize(snd_pcm_hw_params_t* params)
  198. {
  199. _snd_pcm_format format; snd_pcm_hw_params_get_format(params, &format);
  200. snd_pcm_uframes_t psize; snd_pcm_hw_params_get_period_size(params, &psize, NULL);
  201. ssize_t bsize = snd_pcm_format_size (format, psize);
  202. return bsize;
  203. }
  204. int close()
  205. {
  206. // TODO
  207. return 0;
  208. }
  209. /**
  210. * Read audio samples from the audio card. Convert samples to floats and take
  211. * care of interleaved buffers
  212. */
  213. int read()
  214. {
  215. if (fSampleAccess == SND_PCM_ACCESS_RW_INTERLEAVED) {
  216. int count = snd_pcm_readi(fInputDevice, fInputCardBuffer, fBuffering);
  217. if (count<0) {
  218. display_error_msg(count, "reading samples");
  219. int err = snd_pcm_prepare(fInputDevice);
  220. check_error_msg(err, "preparing input stream");
  221. }
  222. if (fSampleFormat == SND_PCM_FORMAT_S16) {
  223. short* buffer16b = (short*) fInputCardBuffer;
  224. for (int s = 0; s < fBuffering; s++) {
  225. for (unsigned int c = 0; c < fCardInputs; c++) {
  226. fInputSoftChannels[c][s] = float(buffer16b[c + s*fCardInputs])*(1.0/float(SHRT_MAX));
  227. }
  228. }
  229. } else { // SND_PCM_FORMAT_S32
  230. long* buffer32b = (long*) fInputCardBuffer;
  231. for (int s = 0; s < fBuffering; s++) {
  232. for (unsigned int c = 0; c < fCardInputs; c++) {
  233. fInputSoftChannels[c][s] = float(buffer32b[c + s*fCardInputs])*(1.0/float(LONG_MAX));
  234. }
  235. }
  236. }
  237. } else if (fSampleAccess == SND_PCM_ACCESS_RW_NONINTERLEAVED) {
  238. int count = snd_pcm_readn(fInputDevice, fInputCardChannels, fBuffering);
  239. if (count<0) {
  240. display_error_msg(count, "reading samples");
  241. int err = snd_pcm_prepare(fInputDevice);
  242. check_error_msg(err, "preparing input stream");
  243. }
  244. if (fSampleFormat == SND_PCM_FORMAT_S16) {
  245. for (unsigned int c = 0; c < fCardInputs; c++) {
  246. short* chan16b = (short*) fInputCardChannels[c];
  247. for (int s = 0; s < fBuffering; s++) {
  248. fInputSoftChannels[c][s] = float(chan16b[s])*(1.0/float(SHRT_MAX));
  249. }
  250. }
  251. } else { // SND_PCM_FORMAT_S32
  252. for (unsigned int c = 0; c < fCardInputs; c++) {
  253. long* chan32b = (long*) fInputCardChannels[c];
  254. for (int s = 0; s < fBuffering; s++) {
  255. fInputSoftChannels[c][s] = float(chan32b[s])*(1.0/float(LONG_MAX));
  256. }
  257. }
  258. }
  259. } else {
  260. check_error_msg(-10000, "unknow access mode");
  261. }
  262. return 0;
  263. }
  264. /**
  265. * write the output soft channels to the audio card. Convert sample
  266. * format and interleaves buffers when needed
  267. */
  268. int write()
  269. {
  270. recovery:
  271. if (fSampleAccess == SND_PCM_ACCESS_RW_INTERLEAVED) {
  272. if (fSampleFormat == SND_PCM_FORMAT_S16) {
  273. short* buffer16b = (short*) fOutputCardBuffer;
  274. for (int f = 0; f < fBuffering; f++) {
  275. for (unsigned int c = 0; c < fCardOutputs; c++) {
  276. float x = fOutputSoftChannels[c][f];
  277. buffer16b[c + f * fCardOutputs] = short(max(min(x, 1.0), -1.0) * float(SHRT_MAX));
  278. }
  279. }
  280. } else { // SND_PCM_FORMAT_S32
  281. long* buffer32b = (long*) fOutputCardBuffer;
  282. for (int f = 0; f < fBuffering; f++) {
  283. for (unsigned int c = 0; c < fCardOutputs; c++) {
  284. float x = fOutputSoftChannels[c][f];
  285. buffer32b[c + f * fCardOutputs] = long( max(min(x, 1.0), -1.0) * float(LONG_MAX));
  286. }
  287. }
  288. }
  289. int count = snd_pcm_writei(fOutputDevice, fOutputCardBuffer, fBuffering);
  290. if (count < 0) {
  291. display_error_msg(count, "w3");
  292. int err = snd_pcm_prepare(fOutputDevice);
  293. check_error_msg(err, "preparing output stream");
  294. goto recovery;
  295. }
  296. } else if (fSampleAccess == SND_PCM_ACCESS_RW_NONINTERLEAVED) {
  297. if (fSampleFormat == SND_PCM_FORMAT_S16) {
  298. for (unsigned int c = 0; c < fCardOutputs; c++) {
  299. short* chan16b = (short*) fOutputCardChannels[c];
  300. for (int f = 0; f < fBuffering; f++) {
  301. float x = fOutputSoftChannels[c][f];
  302. chan16b[f] = short(max(min(x,1.0), -1.0) * float(SHRT_MAX)) ;
  303. }
  304. }
  305. } else { // SND_PCM_FORMAT_S32
  306. for (unsigned int c = 0; c < fCardOutputs; c++) {
  307. long* chan32b = (long*) fOutputCardChannels[c];
  308. for (int f = 0; f < fBuffering; f++) {
  309. float x = fOutputSoftChannels[c][f];
  310. chan32b[f] = long(max(min(x,1.0),-1.0) * float(LONG_MAX)) ;
  311. }
  312. }
  313. }
  314. int count = snd_pcm_writen(fOutputDevice, fOutputCardChannels, fBuffering);
  315. if (count<0) {
  316. display_error_msg(count, "w3");
  317. int err = snd_pcm_prepare(fOutputDevice);
  318. check_error_msg(err, "preparing output stream");
  319. goto recovery;
  320. }
  321. } else {
  322. check_error_msg(-10000, "unknow access mode");
  323. }
  324. return 0;
  325. }
  326. /**
  327. * print short information on the audio device
  328. */
  329. int shortinfo()
  330. {
  331. int err;
  332. snd_ctl_card_info_t* card_info;
  333. snd_ctl_t* ctl_handle;
  334. err = snd_ctl_open(&ctl_handle, fCardName, 0); check_error(err);
  335. snd_ctl_card_info_alloca(&card_info);
  336. err = snd_ctl_card_info(ctl_handle, card_info); check_error(err);
  337. printf("%s|%d|%d|%d|%d|%s\n",
  338. snd_ctl_card_info_get_driver(card_info),
  339. fCardInputs, fCardOutputs,
  340. fFrequency, fBuffering,
  341. snd_pcm_format_name((_snd_pcm_format)fSampleFormat));
  342. }
  343. /**
  344. * print more detailled information on the audio device
  345. */
  346. int longinfo()
  347. {
  348. int err;
  349. snd_ctl_card_info_t* card_info;
  350. snd_ctl_t* ctl_handle;
  351. printf("Audio Interface Description :\n");
  352. printf("Sampling Frequency : %d, Sample Format : %s, buffering : %d\n",
  353. fFrequency, snd_pcm_format_name((_snd_pcm_format)fSampleFormat), fBuffering);
  354. printf("Software inputs : %2d, Software outputs : %2d\n", fSoftInputs, fSoftOutputs);
  355. printf("Hardware inputs : %2d, Hardware outputs : %2d\n", fCardInputs, fCardOutputs);
  356. printf("Channel inputs : %2d, Channel outputs : %2d\n", fChanInputs, fChanOutputs);
  357. // affichage des infos de la carte
  358. err = snd_ctl_open (&ctl_handle, fCardName, 0); check_error(err);
  359. snd_ctl_card_info_alloca (&card_info);
  360. err = snd_ctl_card_info(ctl_handle, card_info); check_error(err);
  361. printCardInfo(card_info);
  362. // affichage des infos liees aux streams d'entree-sortie
  363. if (fSoftInputs > 0) printHWParams(fInputParams);
  364. if (fSoftOutputs > 0) printHWParams(fOutputParams);
  365. return 0;
  366. }
  367. void printCardInfo(snd_ctl_card_info_t* ci)
  368. {
  369. printf("Card info (address : %p)\n", ci);
  370. printf("\tID = %s\n", snd_ctl_card_info_get_id(ci));
  371. printf("\tDriver = %s\n", snd_ctl_card_info_get_driver(ci));
  372. printf("\tName = %s\n", snd_ctl_card_info_get_name(ci));
  373. printf("\tLongName = %s\n", snd_ctl_card_info_get_longname(ci));
  374. printf("\tMixerName = %s\n", snd_ctl_card_info_get_mixername(ci));
  375. printf("\tComponents = %s\n", snd_ctl_card_info_get_components(ci));
  376. printf("--------------\n");
  377. }
  378. void printHWParams(snd_pcm_hw_params_t* params)
  379. {
  380. printf("HW Params info (address : %p)\n", params);
  381. #if 0
  382. printf("\tChannels = %d\n", snd_pcm_hw_params_get_channels(params));
  383. printf("\tFormat = %s\n", snd_pcm_format_name((_snd_pcm_format)snd_pcm_hw_params_get_format(params)));
  384. printf("\tAccess = %s\n", snd_pcm_access_name((_snd_pcm_access)snd_pcm_hw_params_get_access(params)));
  385. printf("\tRate = %d\n", snd_pcm_hw_params_get_rate(params, NULL));
  386. printf("\tPeriods = %d\n", snd_pcm_hw_params_get_periods(params, NULL));
  387. printf("\tPeriod size = %d\n", (int)snd_pcm_hw_params_get_period_size(params, NULL));
  388. printf("\tPeriod time = %d\n", snd_pcm_hw_params_get_period_time(params, NULL));
  389. printf("\tBuffer size = %d\n", (int)snd_pcm_hw_params_get_buffer_size(params));
  390. printf("\tBuffer time = %d\n", snd_pcm_hw_params_get_buffer_time(params, NULL));
  391. #endif
  392. printf("--------------\n");
  393. }
  394. };
  395. class JackAlsaIOAdapter : public JackIOAdapterInterface, public JackRunnableInterface
  396. {
  397. private:
  398. JackThread fThread;
  399. AudioInterface fAudioInterface;
  400. public:
  401. JackAlsaIOAdapter(int input, int output, jack_nframes_t buffer_size, jack_nframes_t sample_rate)
  402. :JackIOAdapterInterface(input, output, buffer_size, sample_rate)
  403. ,fThread(this), fAudioInterface(input, output, buffer_size, sample_rate)
  404. {}
  405. ~JackAlsaIOAdapter()
  406. {}
  407. virtual int Open();
  408. virtual int Close();
  409. virtual int SetBufferSize(jack_nframes_t buffer_size);
  410. virtual bool Execute();
  411. };
  412. }
  413. #endif