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.

234 lines
9.4KB

  1. /*
  2. Copyright (C) 2013 Matt Flax <flatmax@>
  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::Attach() {
  29. //cout<<"JackIIODriver::Attach\n";
  30. JackAudioDriver::SetSampleRate((jack_nframes_t)IIO_DEFAULT_READ_FS);
  31. int ret;
  32. if ((ret=iio.enable(true))!=NO_ERROR) { // start the DMA
  33. iio.close();
  34. return ret;
  35. }
  36. return JackAudioDriver::Attach();
  37. }
  38. int JackIIODriver::Detach() {
  39. //cout<<"JackIIODriver::Detach\n";
  40. iio.enable(false); // stop the DMA
  41. return JackAudioDriver::Detach();
  42. }
  43. int JackIIODriver::Read() {
  44. //cout<<"JackIIODriver::Read\n";
  45. if (iio.getDeviceCnt()<1) {
  46. jack_error("JackIIODriver:: No IIO devices are present ");
  47. return -1;
  48. }
  49. uint devChCnt=iio[0].getChCnt(); // the number of channels per device
  50. jack_nframes_t nframes=data.rows()/devChCnt;
  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. int maxAvailChCnt=data.cols()*devChCnt;
  61. jack_default_audio_sample_t scaleFactor=1./32768.;
  62. if (fCaptureChannels>maxAvailChCnt)
  63. jack_error("JackIIODriver::Read warning : Jack period size = %ld IIO period size = %ld", fEngineControl->fBufferSize, nframes);
  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]=(float)j/(float)nframes;
  71. dest[j]=(jack_default_audio_sample_t)(data(j*devChCnt+rowOffset, col))*scaleFactor;
  72. // cout<<dest[j]<<'\t'<<data(j*devChCnt+rowOffset, col)<<"\t\t";
  73. }
  74. }
  75. }
  76. return 0;
  77. }
  78. int JackIIODriver::Write() {
  79. // cout<<"JackIIODriver::Write\n";
  80. JackDriver::CycleTakeEndTime(); // is this necessary ?
  81. return 0;
  82. }
  83. } // end namespace Jack
  84. #ifdef __cplusplus
  85. extern "C"
  86. {
  87. #endif
  88. SERVER_EXPORT const jack_driver_desc_t *
  89. driver_get_descriptor () {
  90. jack_driver_desc_t * desc;
  91. jack_driver_desc_filler_t filler;
  92. jack_driver_param_value_t value;
  93. desc = jack_driver_descriptor_construct("iio", JackDriverMaster, "Linux Industrial IO backend", &filler);
  94. strcpy(value.str, IIO_DEFAULT_CHIP);
  95. 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);
  96. value.ui = IIO_DEFAULT_CAPUTURE_PORT_COUNT;
  97. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'i', JackDriverParamUInt, &value, NULL, "Provide capture count (block size).", NULL);
  98. value.ui = IIO_DEFAULT_PERIOD_SIZE;
  99. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames (samples per channel) per period", NULL);
  100. value.ui = IIO_DEFAULT_PERIOD_COUNT;
  101. jack_driver_descriptor_add_parameter(desc, &filler, "nperiods", 'n', JackDriverParamUInt, &value, NULL, "Number of available periods (block count)", NULL);
  102. return desc;
  103. }
  104. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params) {
  105. // As of this implementation the IIO driver is only capture... to be expanded.
  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. delete iio_driver;
  138. return NULL;
  139. }
  140. if (iio_driver->iio.getDeviceCnt()<1) { // If there are no devices found by that chip name, then indicate.
  141. jack_error("\nThe iio driver found no devices by the name %s\n", chipName.c_str());
  142. delete iio_driver;
  143. return NULL;
  144. }
  145. iio_driver->iio.printInfo(); // print out detail about the devices which were found ...
  146. // if the available number of ports is less then the requested number, then restrict to the number of physical ports.
  147. if (iio_driver->iio.getChCnt()<inChCnt)
  148. inChCnt=iio_driver->iio.getChCnt();
  149. // resize the data buffer column count to match the device count
  150. int colCnt=(int)ceil((float)inChCnt/(float)iio_driver->iio[0].getChCnt()); // check whether we require less then the available number of channels
  151. int ret=iio_driver->iio.getReadArray(periodSize, iio_driver->data); // resize the array to be able to read enough memory
  152. if (ret!=NO_ERROR) {
  153. jack_error("iio::getReadArray couldn't create the data buffer, indicating the problem.");
  154. delete iio_driver;
  155. return NULL;
  156. }
  157. if (iio_driver->data.cols()>colCnt) // resize the data columns to match the specified number of columns (channels / channels per device)
  158. iio_driver->data.resize(iio_driver->data.rows(), colCnt);
  159. ret=iio_driver->iio.open(periodCount, periodSize); // try to open all IIO devices
  160. if (ret!=NO_ERROR)
  161. delete iio_driver;
  162. return NULL;
  163. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(iio_driver);
  164. if (threaded_driver) {
  165. bool capture=true, playback=false, monitor=false;
  166. int outChCnt=0;
  167. jack_nframes_t inputLatency = periodSize*periodCount, outputLatency=0;
  168. // Special open for OSS driver...
  169. if (iio_driver->Open(periodSize, periodCount, capture, playback, inChCnt, outChCnt, monitor, "iio:device", "iio:device", inputLatency, outputLatency)!=0) {
  170. delete threaded_driver;
  171. threaded_driver=NULL;
  172. }
  173. } else
  174. 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");
  175. if (!threaded_driver) { // handle the case that the threaded_driver was not created succ.
  176. delete iio_driver;
  177. iio_driver=NULL;
  178. }
  179. return threaded_driver;
  180. }
  181. #ifdef __cplusplus
  182. }
  183. #endif