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.

195 lines
5.2KB

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