jack1 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.

227 lines
7.0KB

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