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.

154 lines
3.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 "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 "JackCoreAudioIOAdapter.h"
  84. #include "JackPortAudioIOAdapter.h"
  85. #include "JackCallbackNetIOAdapter.h"
  86. EXPORT int jack_initialize(jack_client_t* jack_client, const char* load_init)
  87. {
  88. if (adapter) {
  89. jack_error("NetAudio Adapter already loaded");
  90. return 1;
  91. } else {
  92. jack_log("Loading NetAudio Adapter");
  93. /*
  94. adapter = new Jack::JackCallbackNetIOAdapter(jack_client,
  95. new Jack::JackPortAudioIOAdapter(2, 2, jack_get_buffer_size(jack_client), jack_get_sample_rate(jack_client)), 2, 2);
  96. */
  97. #ifdef __APPLE__
  98. adapter = new Jack::JackCallbackNetIOAdapter(jack_client,
  99. new Jack::JackCoreAudioIOAdapter(2, 2, jack_get_buffer_size(jack_client), jack_get_sample_rate(jack_client)), 2, 2);
  100. #endif
  101. assert(adapter);
  102. if (adapter->Open() == 0) {
  103. return 0;
  104. } else {
  105. delete adapter;
  106. adapter = NULL;
  107. return 1;
  108. }
  109. }
  110. }
  111. EXPORT void jack_finish(void* arg)
  112. {
  113. if (adapter) {
  114. jack_log("Unloading NetAudio Adapter");
  115. adapter->Close();
  116. delete adapter;
  117. adapter = NULL;
  118. }
  119. }
  120. #ifdef __cplusplus
  121. }
  122. #endif