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.

187 lines
4.9KB

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