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.

213 lines
5.8KB

  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. #ifndef __JackAudioAdapterInterface__
  16. #define __JackAudioAdapterInterface__
  17. #if defined(HAVE_CONFIG_H)
  18. #include "config.h"
  19. #endif
  20. #include "ringbuffer.h"
  21. #include "jack.h"
  22. #include "JackError.h"
  23. #include "JackResampler.h"
  24. #include "JackFilters.h"
  25. #include <samplerate.h>
  26. namespace Jack
  27. {
  28. #ifdef JACK_MONITOR
  29. #define TABLE_MAX 100000
  30. struct Measure
  31. {
  32. int delta;
  33. int time1;
  34. int time2;
  35. float r1;
  36. float r2;
  37. int pos1;
  38. int pos2;
  39. };
  40. struct MeasureTable
  41. {
  42. Measure fTable[TABLE_MAX];
  43. int fCount;
  44. MeasureTable() :fCount ( 0 )
  45. {}
  46. void Write ( int time1, int time2, float r1, float r2, int pos1, int pos2 );
  47. void Save();
  48. };
  49. #endif
  50. /*!
  51. \brief Base class for audio adapters.
  52. */
  53. class JackAudioAdapterInterface
  54. {
  55. protected:
  56. #ifdef JACK_MONITOR
  57. MeasureTable fTable;
  58. #endif
  59. int fCaptureChannels;
  60. int fPlaybackChannels;
  61. jack_nframes_t fBufferSize;
  62. jack_nframes_t fSampleRate;
  63. // DLL
  64. JackAtomicDelayLockedLoop fProducerDLL;
  65. JackAtomicDelayLockedLoop fConsumerDLL;
  66. JackResampler** fCaptureRingBuffer;
  67. JackResampler** fPlaybackRingBuffer;
  68. bool fRunning;
  69. public:
  70. JackAudioAdapterInterface ( jack_nframes_t buffer_size, jack_nframes_t sample_rate )
  71. :fCaptureChannels ( 0 ),
  72. fPlaybackChannels ( 0 ),
  73. fBufferSize ( buffer_size ),
  74. fSampleRate ( sample_rate ),
  75. fProducerDLL ( buffer_size, sample_rate ),
  76. fConsumerDLL ( buffer_size, sample_rate ),
  77. fRunning ( false )
  78. {}
  79. virtual ~JackAudioAdapterInterface()
  80. {}
  81. void SetRingBuffers ( JackResampler** input, JackResampler** output )
  82. {
  83. fCaptureRingBuffer = input;
  84. fPlaybackRingBuffer = output;
  85. }
  86. bool IsRunning() {return fRunning;}
  87. virtual void Reset() {fRunning = false;}
  88. void ResetRingBuffers();
  89. virtual int Open();
  90. virtual int Close();
  91. virtual int SetBufferSize ( jack_nframes_t buffer_size )
  92. {
  93. fBufferSize = buffer_size;
  94. fConsumerDLL.Init ( fBufferSize, fSampleRate );
  95. fProducerDLL.Init ( fBufferSize, fSampleRate );
  96. return 0;
  97. }
  98. virtual int SetHostBufferSize ( jack_nframes_t buffer_size )
  99. {
  100. fBufferSize = buffer_size;
  101. fConsumerDLL.Init ( fBufferSize, fSampleRate );
  102. return 0;
  103. }
  104. virtual int SetAdaptedBufferSize ( jack_nframes_t buffer_size )
  105. {
  106. fProducerDLL.Init ( buffer_size, fSampleRate );
  107. return 0;
  108. }
  109. //TODO : switch the two next methods to SetHost/AdaptedBufferSize in adapters
  110. virtual int SetSampleRate ( jack_nframes_t sample_rate )
  111. {
  112. fSampleRate = sample_rate;
  113. fConsumerDLL.Init ( fBufferSize, fSampleRate );
  114. // Producer (Audio) keeps the same SR
  115. return 0;
  116. }
  117. virtual int SetAudioSampleRate ( jack_nframes_t sample_rate )
  118. {
  119. fSampleRate = sample_rate;
  120. // Consumer keeps the same SR
  121. fProducerDLL.Init ( fBufferSize, fSampleRate );
  122. return 0;
  123. }
  124. //host = driver that hosts the adapter
  125. virtual int SetHostSampleRate ( jack_nframes_t sample_rate )
  126. {
  127. fSampleRate = sample_rate;
  128. fConsumerDLL.Init ( fBufferSize, fSampleRate );
  129. return 0;
  130. }
  131. //adapted = driver hosted by the adapter
  132. virtual int SetAdaptedSampleRate ( jack_nframes_t sample_rate )
  133. {
  134. fProducerDLL.Init ( fBufferSize, fSampleRate );
  135. return 0;
  136. }
  137. virtual void SetCallbackTime ( jack_time_t callback_usec )
  138. {
  139. fConsumerDLL.IncFrame ( callback_usec );
  140. }
  141. void ResampleFactor ( jack_nframes_t& frame1, jack_nframes_t& frame2 );
  142. void SetInputs ( int inputs )
  143. {
  144. jack_log ( "JackAudioAdapterInterface::SetInputs %d", inputs );
  145. fCaptureChannels = inputs;
  146. }
  147. void SetOutputs ( int outputs )
  148. {
  149. jack_log ( "JackAudioAdapterInterface::SetOutputs %d", outputs );
  150. fPlaybackChannels = outputs;
  151. }
  152. int GetInputs()
  153. {
  154. jack_log ( "JackAudioAdapterInterface::GetInputs %d", fCaptureChannels );
  155. return fCaptureChannels;
  156. }
  157. int GetOutputs()
  158. {
  159. jack_log ( "JackAudioAdapterInterface::GetOutputs %d", fPlaybackChannels );
  160. return fPlaybackChannels;
  161. }
  162. };
  163. }
  164. #endif