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.

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