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.

238 lines
6.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. #ifndef __JackAudioAdapterInterface__
  16. #define __JackAudioAdapterInterface__
  17. #include "JackResampler.h"
  18. #include "JackFilters.h"
  19. #include "JackConstants.h"
  20. #include <stdio.h>
  21. namespace Jack
  22. {
  23. #ifdef JACK_MONITOR
  24. #define TABLE_MAX 100000
  25. struct Measure
  26. {
  27. int delta;
  28. int time1;
  29. int time2;
  30. float r1;
  31. float r2;
  32. int pos1;
  33. int pos2;
  34. };
  35. struct MeasureTable
  36. {
  37. Measure fTable[TABLE_MAX];
  38. int fCount;
  39. MeasureTable() :fCount(0)
  40. {}
  41. void Write(int time1, int time2, float r1, float r2, int pos1, int pos2);
  42. void Save(unsigned int fHostBufferSize, unsigned int fHostSampleRate, unsigned int fAdaptedSampleRate, unsigned int fAdaptedBufferSize);
  43. };
  44. #endif
  45. /*!
  46. \brief Base class for audio adapters.
  47. */
  48. class JackAudioAdapterInterface
  49. {
  50. protected:
  51. #ifdef JACK_MONITOR
  52. MeasureTable fTable;
  53. #endif
  54. //channels
  55. int fCaptureChannels;
  56. int fPlaybackChannels;
  57. //host parameters
  58. jack_nframes_t fHostBufferSize;
  59. jack_nframes_t fHostSampleRate;
  60. //adapted parameters
  61. jack_nframes_t fAdaptedBufferSize;
  62. jack_nframes_t fAdaptedSampleRate;
  63. //PI controler
  64. JackPIControler fPIControler;
  65. JackResampler** fCaptureRingBuffer;
  66. JackResampler** fPlaybackRingBuffer;
  67. unsigned int fQuality;
  68. unsigned int fRingbufferCurSize;
  69. jack_time_t fPullAndPushTime;
  70. bool fRunning;
  71. bool fAdaptative;
  72. void ResetRingBuffers();
  73. void AdaptRingBufferSize();
  74. void GrowRingBufferSize();
  75. public:
  76. JackAudioAdapterInterface(jack_nframes_t buffer_size, jack_nframes_t sample_rate, jack_nframes_t ring_buffer_size = DEFAULT_ADAPTATIVE_SIZE):
  77. fCaptureChannels(0),
  78. fPlaybackChannels(0),
  79. fHostBufferSize(buffer_size),
  80. fHostSampleRate(sample_rate),
  81. fAdaptedBufferSize(buffer_size),
  82. fAdaptedSampleRate(sample_rate),
  83. fPIControler(sample_rate / sample_rate, 256),
  84. fCaptureRingBuffer(NULL), fPlaybackRingBuffer(NULL),
  85. fQuality(0),
  86. fRingbufferCurSize(ring_buffer_size),
  87. fPullAndPushTime(0),
  88. fRunning(false),
  89. fAdaptative(true)
  90. {}
  91. JackAudioAdapterInterface(jack_nframes_t host_buffer_size,
  92. jack_nframes_t host_sample_rate,
  93. jack_nframes_t adapted_buffer_size,
  94. jack_nframes_t adapted_sample_rate,
  95. jack_nframes_t ring_buffer_size = DEFAULT_ADAPTATIVE_SIZE) :
  96. fCaptureChannels(0),
  97. fPlaybackChannels(0),
  98. fHostBufferSize(host_buffer_size),
  99. fHostSampleRate(host_sample_rate),
  100. fAdaptedBufferSize(adapted_buffer_size),
  101. fAdaptedSampleRate(adapted_sample_rate),
  102. fPIControler(host_sample_rate / host_sample_rate, 256),
  103. fQuality(0),
  104. fRingbufferCurSize(ring_buffer_size),
  105. fPullAndPushTime(0),
  106. fRunning(false),
  107. fAdaptative(true)
  108. {}
  109. virtual ~JackAudioAdapterInterface()
  110. {}
  111. virtual void Reset();
  112. virtual void Create();
  113. virtual void Destroy();
  114. virtual int Open()
  115. {
  116. return 0;
  117. }
  118. virtual int Close()
  119. {
  120. return 0;
  121. }
  122. virtual int SetHostBufferSize(jack_nframes_t buffer_size)
  123. {
  124. fHostBufferSize = buffer_size;
  125. if (fAdaptative) {
  126. AdaptRingBufferSize();
  127. }
  128. return 0;
  129. }
  130. virtual int SetAdaptedBufferSize(jack_nframes_t buffer_size)
  131. {
  132. fAdaptedBufferSize = buffer_size;
  133. if (fAdaptative) {
  134. AdaptRingBufferSize();
  135. }
  136. return 0;
  137. }
  138. virtual int SetBufferSize(jack_nframes_t buffer_size)
  139. {
  140. SetHostBufferSize(buffer_size);
  141. SetAdaptedBufferSize(buffer_size);
  142. return 0;
  143. }
  144. virtual int SetHostSampleRate(jack_nframes_t sample_rate)
  145. {
  146. fHostSampleRate = sample_rate;
  147. fPIControler.Init(double(fHostSampleRate) / double(fAdaptedSampleRate));
  148. return 0;
  149. }
  150. virtual int SetAdaptedSampleRate(jack_nframes_t sample_rate)
  151. {
  152. fAdaptedSampleRate = sample_rate;
  153. fPIControler.Init(double(fHostSampleRate) / double(fAdaptedSampleRate));
  154. return 0;
  155. }
  156. virtual int SetSampleRate(jack_nframes_t sample_rate)
  157. {
  158. SetHostSampleRate(sample_rate);
  159. SetAdaptedSampleRate(sample_rate);
  160. return 0;
  161. }
  162. void SetInputs(int inputs)
  163. {
  164. jack_log("JackAudioAdapterInterface::SetInputs %d", inputs);
  165. fCaptureChannels = inputs;
  166. }
  167. void SetOutputs(int outputs)
  168. {
  169. jack_log("JackAudioAdapterInterface::SetOutputs %d", outputs);
  170. fPlaybackChannels = outputs;
  171. }
  172. int GetInputs()
  173. {
  174. //jack_log("JackAudioAdapterInterface::GetInputs %d", fCaptureChannels);
  175. return fCaptureChannels;
  176. }
  177. int GetOutputs()
  178. {
  179. //jack_log ("JackAudioAdapterInterface::GetOutputs %d", fPlaybackChannels);
  180. return fPlaybackChannels;
  181. }
  182. virtual int GetInputLatency(int port_index) { return 0; }
  183. virtual int GetOutputLatency(int port_index) { return 0; }
  184. int PushAndPull(jack_default_audio_sample_t** inputBuffer, jack_default_audio_sample_t** outputBuffer, unsigned int frames);
  185. int PullAndPush(jack_default_audio_sample_t** inputBuffer, jack_default_audio_sample_t** outputBuffer, unsigned int frames);
  186. };
  187. }
  188. #endif