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.

181 lines
4.7KB

  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. {
  29. int i;
  30. char name[32];
  31. fJackClient = jack_client;
  32. fCaptureChannels = audio_io->GetInputs();
  33. fPlaybackChannels = audio_io->GetOutputs();
  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. #include "driver_interface.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. using namespace Jack;
  95. EXPORT int jack_internal_initialize(jack_client_t* jack_client, const JSList* params)
  96. {
  97. Jack::JackAudioAdapter* adapter;
  98. jack_log("Loading audioadapter");
  99. #ifdef __linux__
  100. adapter = new Jack::JackCallbackAudioAdapter(jack_client,
  101. new Jack::JackAlsaAdapter(jack_get_buffer_size(jack_client), jack_get_sample_rate(jack_client), params));
  102. #endif
  103. #ifdef WIN32
  104. adapter = new Jack::JackCallbackAudioAdapter(jack_client,
  105. new Jack::JackPortAudioAdapter(jack_get_buffer_size(jack_client), jack_get_sample_rate(jack_client), params));
  106. #endif
  107. #ifdef __APPLE__
  108. adapter = new Jack::JackCallbackAudioAdapter(jack_client,
  109. new Jack::JackCoreAudioAdapter(jack_get_buffer_size(jack_client), jack_get_sample_rate(jack_client), params));
  110. #endif
  111. assert(adapter);
  112. if (adapter->Open() == 0) {
  113. return 0;
  114. } else {
  115. delete adapter;
  116. return 1;
  117. }
  118. }
  119. EXPORT int jack_initialize(jack_client_t* jack_client, const char* load_init)
  120. {
  121. JSList* params = NULL;
  122. jack_driver_desc_t *desc = jack_get_descriptor();
  123. JackArgParser parser(load_init);
  124. if (parser.GetArgc() > 0) {
  125. if (jack_parse_internal_client_params(desc, parser.GetNumArgv(), (char**)parser.GetArgv(), &params) != 0)
  126. jack_error("Internal client jack_parse_driver_params error");
  127. }
  128. return jack_internal_initialize(jack_client, params);
  129. }
  130. EXPORT void jack_finish(void* arg)
  131. {
  132. Jack::JackCallbackAudioAdapter* adapter = static_cast<Jack::JackCallbackAudioAdapter*>(arg);
  133. if (adapter) {
  134. jack_log("Unloading audioadapter");
  135. adapter->Close();
  136. delete adapter;
  137. }
  138. }
  139. #ifdef __cplusplus
  140. }
  141. #endif