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.

231 lines
9.7KB

  1. /*
  2. Copyright (C) 2013 Matt Flax <flatmax@flatmax.org>
  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 "JackIIODriver.h"
  16. #include "driver_interface.h"
  17. #include "JackEngineControl.h"
  18. #include "JackGraphManager.h"
  19. #include <values.h>
  20. #define IIO_DEFAULT_CHIP "AD7476A" ///< The default IIO recording chip to look for.
  21. #define IIO_DEFAULT_READ_FS 1.e6 ///< The default IIO sample rate for the default chip.
  22. #define IIO_DEFAULT_PERIOD_SIZE 2048 ///< The default period size is in the ms range
  23. #define IIO_DEFAULT_PERIOD_COUNT 2 ///< The default number of periods
  24. #define IIO_DEFAULT_CAPUTURE_PORT_COUNT MAXINT ///< The default number of capture ports is exceedingly big, trimmed down to a realistic size in driver_initialize
  25. //#define IIO_SAFETY_FACTOR 2./3. ///< The default safety factor, allow consumption of this fraction of the available DMA buffer before we don't allow the driver to continue.
  26. #define IIO_SAFETY_FACTOR 1. ///< The default safety factor, allow consumption of this fraction of the available DMA buffer before we don't allow the driver to continue.
  27. using namespace std;
  28. namespace Jack {
  29. int JackIIODriver::Open(jack_nframes_t buffer_size, jack_nframes_t samplerate, bool capturing, bool playing, int inchannels, int outchannels, bool monitor, const char* capture_driver_name, const char* playback_driver_name, jack_nframes_t capture_latency, jack_nframes_t playback_latency) {
  30. //cout<<"JackIIODriver::Open\n";
  31. int ret=JackAudioDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  32. if (ret!=NO_ERROR) // check whether the JackAudioDriver opened OK
  33. return ret;
  34. ret=iio.enable(true); // start the DMA
  35. return ret;
  36. }
  37. int JackIIODriver::Close() {
  38. //cout<<"JackIIODriver::Close\n";
  39. iio.enable(false); // stop the DMA
  40. return JackAudioDriver::Close();
  41. }
  42. int JackIIODriver::Read() {
  43. //cout<<"JackIIODriver::Read\n";
  44. if (iio.getDeviceCnt()<1) {
  45. jack_error("JackIIODriver:: No IIO devices are present ");
  46. return -1;
  47. }
  48. uint devChCnt=iio[0].getChCnt(); // the number of channels per device
  49. jack_nframes_t nframes=data.rows()/devChCnt;
  50. // This is left here for future debugging.
  51. // if (nframes != fEngineControl->fBufferSize)
  52. // jack_error("JackIIODriver::Read warning : Jack period size = %ld IIO period size = %ld", fEngineControl->fBufferSize, nframes);
  53. // cout<<"processing buffer size : "<<fEngineControl->fBufferSize<<endl;
  54. // cout<<"processing channel count : "<<fCaptureChannels<<endl;
  55. int ret=iio.read(nframes, data); // read the data from the IIO subsystem
  56. if (ret!=NO_ERROR)
  57. return -1;
  58. // Keep begin cycle time
  59. JackDriver::CycleTakeBeginTime(); // is this necessary ?
  60. jack_default_audio_sample_t scaleFactor=1./32768.;
  61. // This is left in for future debugging.
  62. //int maxAvailChCnt=data.cols()*devChCnt;
  63. // if (fCaptureChannels>maxAvailChCnt)
  64. // jack_error("JackIIODriver::Read warning : Jack capture ch. cnt = %ld IIO capture ch. cnt = %ld", fCaptureChannels, maxAvailChCnt);
  65. for (int i = 0; i < fCaptureChannels; i++) {
  66. int col=i/devChCnt; // find the column and offset to read from
  67. int rowOffset=i%devChCnt;
  68. if (fGraphManager->GetConnectionsNum(fCapturePortList[i]) > 0) {
  69. jack_default_audio_sample_t *dest=GetInputBuffer(i);
  70. for (jack_nframes_t j=0; j<nframes; j++)
  71. dest[j]=(jack_default_audio_sample_t)(data(j*devChCnt+rowOffset, col))*scaleFactor;
  72. }
  73. }
  74. return 0;
  75. }
  76. int JackIIODriver::Write() {
  77. // cout<<"JackIIODriver::Write\n";
  78. JackDriver::CycleTakeEndTime(); // is this necessary ?
  79. return 0;
  80. }
  81. } // end namespace Jack
  82. #ifdef __cplusplus
  83. extern "C"
  84. {
  85. #endif
  86. SERVER_EXPORT const jack_driver_desc_t *
  87. driver_get_descriptor () {
  88. jack_driver_desc_t * desc;
  89. jack_driver_desc_filler_t filler;
  90. jack_driver_param_value_t value;
  91. desc = jack_driver_descriptor_construct("iio", JackDriverMaster, "Linux Industrial IO backend", &filler);
  92. strcpy(value.str, IIO_DEFAULT_CHIP);
  93. jack_driver_descriptor_add_parameter(desc, &filler, "chip", 'C', JackDriverParamString, &value, NULL, "The name of the chip to search for in the IIO devices", NULL);
  94. value.ui = IIO_DEFAULT_CAPUTURE_PORT_COUNT;
  95. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'i', JackDriverParamUInt, &value, NULL, "Provide capture count (block size).", NULL);
  96. value.ui = IIO_DEFAULT_PERIOD_SIZE;
  97. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames (samples per channel) per period", NULL);
  98. value.ui = IIO_DEFAULT_PERIOD_COUNT;
  99. jack_driver_descriptor_add_parameter(desc, &filler, "nperiods", 'n', JackDriverParamUInt, &value, NULL, "Number of available periods (block count)", NULL);
  100. return desc;
  101. }
  102. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params) {
  103. // As of this implementation the IIO driver is only capture... to be expanded.
  104. int ret, colCnt;
  105. Jack::JackDriverClientInterface* threaded_driver=NULL;
  106. string chipName(IIO_DEFAULT_CHIP); // the default chip name to search for in the IIO devices.
  107. float fs = IIO_DEFAULT_READ_FS; // IIO sample rate is fixed.
  108. jack_nframes_t periodSize = IIO_DEFAULT_PERIOD_SIZE; // default block size
  109. jack_nframes_t periodCount = IIO_DEFAULT_PERIOD_COUNT; // default block count
  110. uint inChCnt = IIO_DEFAULT_CAPUTURE_PORT_COUNT; // The default number of physical input channels - a very large number, to be reduced.
  111. for (const JSList *node = params; node; node = jack_slist_next (node)) {
  112. jack_driver_param_t *param = (jack_driver_param_t *) node->data;
  113. switch (param->character) {
  114. case 'C': // we are specifying a new chip name
  115. chipName = param->value.str;
  116. break;
  117. case 'i': // we are specifying the number of capture channels
  118. inChCnt = param->value.ui;
  119. break;
  120. case 'p':
  121. periodSize = param->value.ui;
  122. break;
  123. case 'n':
  124. periodCount = param->value.ui;
  125. break;
  126. }
  127. }
  128. // create the driver which contains the IIO class
  129. Jack::JackIIODriver* iio_driver = new Jack::JackIIODriver("system", "iio_pcm", engine, table);
  130. if (!iio_driver) {
  131. jack_error("\nHave you run out of memory ? I tried to create the IIO driver in memory but failed!\n");
  132. return NULL;
  133. }
  134. // interrogate the available iio devices searching for the chip name
  135. if (iio_driver->iio.findDevicesByChipName(chipName)!=NO_ERROR) { // find all devices with a particular chip which are present.
  136. jack_error("\nThe iio driver found no devices by the name %s\n", chipName.c_str());
  137. goto initError;
  138. }
  139. if (iio_driver->iio.getDeviceCnt()<1) { // If there are no devices found by that chip name, then indicate.
  140. jack_error("\nThe iio driver found no devices by the name %s\n", chipName.c_str());
  141. goto initError;
  142. }
  143. iio_driver->iio.printInfo(); // print out detail about the devices which were found ...
  144. // if the available number of ports is less then the requested number, then restrict to the number of physical ports.
  145. if (iio_driver->iio.getChCnt()<inChCnt)
  146. inChCnt=iio_driver->iio.getChCnt();
  147. // resize the data buffer column count to match the device count
  148. colCnt=(int)ceil((float)inChCnt/(float)iio_driver->iio[0].getChCnt()); // check whether we require less then the available number of channels
  149. ret=iio_driver->iio.getReadArray(periodSize, iio_driver->data); // resize the array to be able to read enough memory
  150. if (ret!=NO_ERROR) {
  151. jack_error("iio::getReadArray couldn't create the data buffer, indicating the problem.");
  152. goto initError;
  153. }
  154. if (iio_driver->data.cols()>colCnt) // resize the data columns to match the specified number of columns (channels / channels per device)
  155. iio_driver->data.resize(iio_driver->data.rows(), colCnt);
  156. ret=iio_driver->iio.open(periodCount, periodSize); // try to open all IIO devices
  157. if (ret!=NO_ERROR)
  158. goto initError;
  159. threaded_driver = new Jack::JackThreadedDriver(iio_driver);
  160. if (threaded_driver) {
  161. bool capture=true, playback=false, monitor=false;
  162. int outChCnt=0;
  163. jack_nframes_t inputLatency = periodSize*periodCount, outputLatency=0;
  164. // Special open for OSS driver...
  165. if (iio_driver->Open(periodSize, (jack_nframes_t)fs, capture, playback, inChCnt, outChCnt, monitor, "iio:device", "iio:device", inputLatency, outputLatency)!=0) {
  166. delete threaded_driver;
  167. delete iio_driver;
  168. return NULL;
  169. }
  170. } else
  171. jack_error("\nHave you run out of memory ? I tried to create Jack's standard threaded driver in memory but failed! The good news is that you had enough memory to create the IIO driver.\n");
  172. return threaded_driver;
  173. initError: // error during initialisation, delete and return NULL
  174. delete iio_driver;
  175. return NULL;
  176. }
  177. #ifdef __cplusplus
  178. }
  179. #endif