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.

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