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.

233 lines
7.1KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2002 Dave LaRose
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. $Id: hdsp.c,v 1.3 2005/09/29 14:51:59 letz Exp $
  16. */
  17. #include "hardware.h"
  18. #include "alsa_driver.h"
  19. #include "hdsp.h"
  20. /* Constants to make working with the hdsp matrix mixer easier */
  21. static const int HDSP_MINUS_INFINITY_GAIN = 0;
  22. static const int HDSP_UNITY_GAIN = 32768;
  23. static const int HDSP_MAX_GAIN = 65535;
  24. /*
  25. * Use these two arrays to choose the value of the input_channel
  26. * argument to hsdp_set_mixer_gain(). hdsp_physical_input_index[n]
  27. * selects the nth optical/analog input. audio_stream_index[n]
  28. * selects the nth channel being received from the host via pci/pccard.
  29. */
  30. static const int hdsp_num_input_channels = 52;
  31. static const int hdsp_physical_input_index[] = {
  32. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  33. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
  34. static const int hdsp_audio_stream_index[] = {
  35. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
  36. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
  37. /*
  38. * Use this array to choose the value of the output_channel
  39. * argument to hsdp_set_mixer_gain(). hdsp_physical_output_index[26]
  40. * and hdsp_physical_output_index[27] refer to the two "line out"
  41. * channels (1/4" phone jack on the front of digiface/multiface).
  42. */
  43. static const int hdsp_num_output_channels = 28;
  44. static const int hdsp_physical_output_index[] = {
  45. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  46. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
  47. /* Function for checking argument values */
  48. static int clamp_int(int value, int lower_bound, int upper_bound)
  49. {
  50. if(value < lower_bound) {
  51. return lower_bound;
  52. }
  53. if(value > upper_bound) {
  54. return upper_bound;
  55. }
  56. return value;
  57. }
  58. /* Note(XXX): Maybe should share this code with hammerfall.c? */
  59. static void
  60. set_control_id (snd_ctl_elem_id_t *ctl, const char *name)
  61. {
  62. snd_ctl_elem_id_set_name (ctl, name);
  63. snd_ctl_elem_id_set_numid (ctl, 0);
  64. snd_ctl_elem_id_set_interface (ctl, SND_CTL_ELEM_IFACE_HWDEP);
  65. snd_ctl_elem_id_set_device (ctl, 0);
  66. snd_ctl_elem_id_set_subdevice (ctl, 0);
  67. snd_ctl_elem_id_set_index (ctl, 0);
  68. }
  69. /* The hdsp matrix mixer lets you connect pretty much any input to */
  70. /* any output with gain from -inf to about +2dB. Pretty slick. */
  71. /* This routine makes a convenient way to set the gain from */
  72. /* input_channel to output_channel (see hdsp_physical_input_index */
  73. /* etc. above. */
  74. /* gain is an int from 0 to 65535, with 0 being -inf gain, and */
  75. /* 65535 being about +2dB. */
  76. static int hdsp_set_mixer_gain(jack_hardware_t *hw, int input_channel,
  77. int output_channel, int gain)
  78. {
  79. hdsp_t *h = (hdsp_t *) hw->private;
  80. snd_ctl_elem_value_t *ctl;
  81. snd_ctl_elem_id_t *ctl_id;
  82. int err;
  83. /* Check args */
  84. input_channel = clamp_int(input_channel, 0, hdsp_num_input_channels);
  85. output_channel = clamp_int(output_channel, 0, hdsp_num_output_channels);
  86. gain = clamp_int(gain, HDSP_MINUS_INFINITY_GAIN, HDSP_MAX_GAIN);
  87. /* Allocate control element and select "Mixer" control */
  88. snd_ctl_elem_value_alloca (&ctl);
  89. snd_ctl_elem_id_alloca (&ctl_id);
  90. set_control_id (ctl_id, "Mixer");
  91. snd_ctl_elem_value_set_id (ctl, ctl_id);
  92. /* Apparently non-standard and unstable interface for the */
  93. /* mixer control. */
  94. snd_ctl_elem_value_set_integer (ctl, 0, input_channel);
  95. snd_ctl_elem_value_set_integer (ctl, 1, output_channel);
  96. snd_ctl_elem_value_set_integer (ctl, 2, gain);
  97. /* Commit the mixer value and check for errors */
  98. if ((err = snd_ctl_elem_write (h->driver->ctl_handle, ctl)) != 0) {
  99. jack_error ("ALSA/HDSP: cannot set mixer gain (%s)", snd_strerror (err));
  100. return -1;
  101. }
  102. /* Note (XXX): Perhaps we should maintain a cache of the current */
  103. /* mixer values, since it's not clear how to query them from the */
  104. /* hdsp hardware. We'll leave this out until a little later. */
  105. return 0;
  106. }
  107. static int hdsp_set_input_monitor_mask (jack_hardware_t *hw, unsigned long mask)
  108. {
  109. int i;
  110. /* For each input channel */
  111. for (i = 0; i < 26; i++) {
  112. /* Monitoring requested for this channel? */
  113. if(mask & (1<<i)) {
  114. /* Yes. Connect physical input to output */
  115. if(hdsp_set_mixer_gain (hw, hdsp_physical_input_index[i],
  116. hdsp_physical_output_index[i],
  117. HDSP_UNITY_GAIN) != 0) {
  118. return -1;
  119. }
  120. #ifdef CANNOT_HEAR_SOFTWARE_STREAM_WHEN_MONITORING
  121. /* ...and disconnect the corresponding software */
  122. /* channel */
  123. if(hdsp_set_mixer_gain (hw, hdsp_audio_stream_index[i],
  124. hdsp_physical_output_index[i],
  125. HDSP_MINUS_INFINITY_GAIN) != 0) {
  126. return -1;
  127. }
  128. #endif
  129. } else {
  130. /* No. Disconnect physical input from output */
  131. if(hdsp_set_mixer_gain (hw, hdsp_physical_input_index[i],
  132. hdsp_physical_output_index[i],
  133. HDSP_MINUS_INFINITY_GAIN) != 0) {
  134. return -1;
  135. }
  136. #ifdef CANNOT_HEAR_SOFTWARE_STREAM_WHEN_MONITORING
  137. /* ...and connect the corresponding software */
  138. /* channel */
  139. if(hdsp_set_mixer_gain (hw, hdsp_audio_stream_index[i],
  140. hdsp_physical_output_index[i],
  141. HDSP_UNITY_GAIN) != 0) {
  142. return -1;
  143. }
  144. #endif
  145. }
  146. }
  147. /* Cache the monitor mask */
  148. hw->input_monitor_mask = mask;
  149. return 0;
  150. }
  151. static int hdsp_change_sample_clock (jack_hardware_t *hw, SampleClockMode mode)
  152. {
  153. // Empty for now, until Dave understands more about clock sync so
  154. // he can test.
  155. return -1;
  156. }
  157. static double hdsp_get_hardware_peak (jack_port_t *port, jack_nframes_t frame)
  158. {
  159. return 0;
  160. }
  161. static double hdsp_get_hardware_power (jack_port_t *port, jack_nframes_t frame)
  162. {
  163. return 0;
  164. }
  165. static void
  166. hdsp_release (jack_hardware_t *hw)
  167. {
  168. hdsp_t *h = (hdsp_t *) hw->private;
  169. if (h != 0) {
  170. free (h);
  171. }
  172. }
  173. /* Mostly copied directly from hammerfall.c */
  174. jack_hardware_t *
  175. jack_alsa_hdsp_hw_new (alsa_driver_t *driver)
  176. {
  177. jack_hardware_t *hw;
  178. hdsp_t *h;
  179. hw = (jack_hardware_t *) malloc (sizeof (jack_hardware_t));
  180. /* Not using clock lock-sync-whatever in home hardware setup */
  181. /* yet. Will write this code when can test it. */
  182. /* hw->capabilities = Cap_HardwareMonitoring|Cap_AutoSync|Cap_WordClock|Cap_ClockMaster|Cap_ClockLockReporting; */
  183. hw->capabilities = Cap_HardwareMonitoring | Cap_HardwareMetering;
  184. hw->input_monitor_mask = 0;
  185. hw->private = 0;
  186. hw->set_input_monitor_mask = hdsp_set_input_monitor_mask;
  187. hw->change_sample_clock = hdsp_change_sample_clock;
  188. hw->release = hdsp_release;
  189. hw->get_hardware_peak = hdsp_get_hardware_peak;
  190. hw->get_hardware_power = hdsp_get_hardware_power;
  191. h = (hdsp_t *) malloc (sizeof (hdsp_t));
  192. h->driver = driver;
  193. hw->private = h;
  194. return hw;
  195. }