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.

206 lines
5.6KB

  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. #include "JackAudioAdapter.h"
  16. #include "JackError.h"
  17. #include "JackExports.h"
  18. #include "JackTools.h"
  19. #include "jslist.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <assert.h>
  23. using namespace std;
  24. namespace Jack
  25. {
  26. JackAudioAdapter::JackAudioAdapter(jack_client_t* jack_client,
  27. JackAudioAdapterInterface* audio_io,
  28. int input,
  29. int output)
  30. {
  31. int i;
  32. char name[32];
  33. fJackClient = jack_client;
  34. fCaptureChannels = input;
  35. fPlaybackChannels = output;
  36. fAudioAdapter = audio_io;
  37. fCapturePortList = new jack_port_t* [fCaptureChannels];
  38. fPlaybackPortList = new jack_port_t* [fPlaybackChannels];
  39. for (i = 0; i < fCaptureChannels; i++) {
  40. sprintf(name, "capture_%d", i+1);
  41. if ((fCapturePortList[i] = jack_port_register(fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == NULL)
  42. goto fail;
  43. }
  44. for (i = 0; i < fPlaybackChannels; i++) {
  45. sprintf(name, "playback_%d", i+1);
  46. if ((fPlaybackPortList[i] = jack_port_register(fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == NULL)
  47. goto fail;
  48. }
  49. return;
  50. fail:
  51. FreePorts();
  52. }
  53. JackAudioAdapter::~JackAudioAdapter()
  54. {
  55. // When called, Close has already been used for the client, thus ports are already unregistered.
  56. delete fAudioAdapter;
  57. }
  58. void JackAudioAdapter::FreePorts()
  59. {
  60. int i;
  61. for (i = 0; i < fCaptureChannels; i++) {
  62. if (fCapturePortList[i])
  63. jack_port_unregister(fJackClient, fCapturePortList[i]);
  64. }
  65. for (i = 0; i < fCaptureChannels; i++) {
  66. if (fPlaybackPortList[i])
  67. jack_port_unregister(fJackClient, fPlaybackPortList[i]);
  68. }
  69. delete[] fCapturePortList;
  70. delete[] fPlaybackPortList;
  71. }
  72. int JackAudioAdapter::Open()
  73. {
  74. return fAudioAdapter->Open();
  75. }
  76. int JackAudioAdapter::Close()
  77. {
  78. return fAudioAdapter->Close();
  79. }
  80. } //namespace
  81. #ifdef __cplusplus
  82. extern "C"
  83. {
  84. #endif
  85. #include "JackCallbackAudioAdapter.h"
  86. #include "driver_interface.h"
  87. #ifdef __linux__
  88. #include "JackAlsaAdapter.h"
  89. #endif
  90. #ifdef __APPLE__
  91. #include "JackCoreAudioAdapter.h"
  92. #endif
  93. #ifdef WIN32
  94. #include "JackPortAudioAdapter.h"
  95. #endif
  96. #define max(x,y) (((x)>(y)) ? (x) : (y))
  97. #define min(x,y) (((x)<(y)) ? (x) : (y))
  98. using namespace Jack;
  99. EXPORT int jack_internal_initialize(jack_client_t* jack_client, const JSList* params)
  100. {
  101. Jack::JackAudioAdapter* adapter;
  102. const char** ports;
  103. jack_log("Loading audioadapter");
  104. // Find out input and output ports numbers
  105. int input = 0;
  106. if ((ports = jack_get_ports(jack_client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) != NULL) {
  107. while (ports[input]) input++;
  108. }
  109. if (ports)
  110. free(ports);
  111. int output = 0;
  112. if ((ports = jack_get_ports(jack_client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) != NULL) {
  113. while (ports[output]) output++;
  114. }
  115. if (ports)
  116. free(ports);
  117. input = max(2, input);
  118. output = max(2, output);
  119. #ifdef __linux__
  120. adapter = new Jack::JackCallbackAudioAdapter(jack_client,
  121. new Jack::JackAlsaAdapter(input, output, jack_get_buffer_size(jack_client), jack_get_sample_rate(jack_client), params), input, output);
  122. #endif
  123. #ifdef WIN32
  124. adapter = new Jack::JackCallbackAudioAdapter(jack_client,
  125. new Jack::JackPortAudioAdapter(input, output, jack_get_buffer_size(jack_client), jack_get_sample_rate(jack_client), params), input, output);
  126. #endif
  127. #ifdef __APPLE__
  128. adapter = new Jack::JackCallbackAudioAdapter(jack_client,
  129. new Jack::JackCoreAudioAdapter(input, output, jack_get_buffer_size(jack_client), jack_get_sample_rate(jack_client), params), input, output);
  130. #endif
  131. assert(adapter);
  132. if (adapter->Open() == 0) {
  133. return 0;
  134. } else {
  135. delete adapter;
  136. return 1;
  137. }
  138. }
  139. EXPORT int jack_initialize(jack_client_t* jack_client, const char* load_init)
  140. {
  141. JSList* params = NULL;
  142. jack_driver_desc_t *desc = jack_get_descriptor();
  143. JackArgParser parser(load_init);
  144. if (parser.GetArgc() > 0) {
  145. if (jack_parse_driver_params(desc, parser.GetArgc(), (char**)parser.GetArgv(), &params) != 0)
  146. jack_error("Internal client jack_parse_driver_params error");
  147. }
  148. return jack_internal_initialize(jack_client, params);
  149. }
  150. EXPORT void jack_finish(void* arg)
  151. {
  152. Jack::JackCallbackAudioAdapter* adapter = static_cast<Jack::JackCallbackAudioAdapter*>(arg);
  153. if (adapter) {
  154. jack_log("Unloading audioadapter");
  155. adapter->Close();
  156. delete adapter;
  157. }
  158. }
  159. #ifdef __cplusplus
  160. }
  161. #endif