when calculating average slave/master delta, use absolute value.
[ardour.git] / libs / ardouralsautil / devicelist.cc
1 /*
2  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <alsa/asoundlib.h>
21 #include <glib.h>
22
23 #include "pbd/string_convert.h"
24 #include "ardouralsautil/devicelist.h"
25
26 using namespace std;
27
28 void
29 ARDOUR::get_alsa_audio_device_names (std::map<std::string, std::string>& devices, AlsaDuplex duplex)
30 {
31         snd_ctl_t *handle;
32         snd_ctl_card_info_t *info;
33         snd_pcm_info_t *pcminfo;
34         snd_ctl_card_info_alloca(&info);
35         snd_pcm_info_alloca(&pcminfo);
36         string devname;
37         int cardnum = -1;
38         int device = -1;
39         const char* fixed_name;
40
41         if ((fixed_name = g_getenv ("ARDOUR_ALSA_DEVICE"))) {
42                 devices.insert (make_pair<string,string> (fixed_name, fixed_name));
43                 return;
44         }
45
46         assert (duplex > 0);
47
48         while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
49
50                 devname = "hw:";
51                 devname += PBD::to_string (cardnum);
52
53                 if (snd_ctl_open (&handle, devname.c_str(), 0) >= 0 && snd_ctl_card_info (handle, info) >= 0) {
54
55                         if (snd_ctl_card_info (handle, info) < 0) {
56                                 continue;
57                         }
58
59                         string card_name = snd_ctl_card_info_get_name (info);
60
61                         /* change devname to use ID, not number */
62
63                         devname = "hw:";
64                         devname += snd_ctl_card_info_get_id (info);
65
66                         while (snd_ctl_pcm_next_device (handle, &device) >= 0 && device >= 0) {
67
68                                 /* only detect duplex devices here. more
69                                  * complex arrangements are beyond our scope
70                                  */
71
72                                 snd_pcm_info_set_device (pcminfo, device);
73                                 snd_pcm_info_set_subdevice (pcminfo, 0);
74                                 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_CAPTURE);
75
76                                 if (snd_ctl_pcm_info (handle, pcminfo) < 0 && (duplex & HalfDuplexIn)) {
77                                         continue;
78                                 }
79
80                                 snd_pcm_info_set_device (pcminfo, device);
81                                 snd_pcm_info_set_subdevice (pcminfo, 0);
82                                 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_PLAYBACK);
83
84                                 if (snd_ctl_pcm_info (handle, pcminfo) < 0 && (duplex & HalfDuplexOut)) {
85                                         continue;
86                                 }
87                                 devname += ',';
88                                 devname += PBD::to_string (device);
89                                 devices.insert (std::make_pair (card_name, devname));
90                         }
91
92                         snd_ctl_close(handle);
93                 }
94         }
95 }
96
97 void
98 ARDOUR::get_alsa_rawmidi_device_names (std::map<std::string, std::string>& devices)
99 {
100         int cardnum = -1;
101         snd_ctl_card_info_t *cinfo;
102         snd_ctl_card_info_alloca (&cinfo);
103         while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
104                 snd_ctl_t *handle;
105                 std::string devname = "hw:";
106                 devname += PBD::to_string (cardnum);
107                 if (snd_ctl_open (&handle, devname.c_str (), 0) >= 0 && snd_ctl_card_info (handle, cinfo) >= 0) {
108                         int device = -1;
109                         while (snd_ctl_rawmidi_next_device (handle, &device) >= 0 && device >= 0) {
110                                 snd_rawmidi_info_t *info;
111                                 snd_rawmidi_info_alloca (&info);
112                                 snd_rawmidi_info_set_device (info, device);
113
114                                 int subs_in, subs_out;
115
116                                 snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_INPUT);
117                                 if (snd_ctl_rawmidi_info (handle, info) >= 0) {
118                                         subs_in = snd_rawmidi_info_get_subdevices_count (info);
119                                 } else {
120                                         subs_in = 0;
121                                 }
122
123                                 snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_OUTPUT);
124                                 if (snd_ctl_rawmidi_info (handle, info) >= 0) {
125                                         subs_out = snd_rawmidi_info_get_subdevices_count (info);
126                                 } else {
127                                         subs_out = 0;
128                                 }
129
130                                 const int subs = subs_in > subs_out ? subs_in : subs_out;
131                                 if (!subs) {
132                                         continue;
133                                 }
134
135                                 for (int sub = 0; sub < subs; ++sub) {
136                                         snd_rawmidi_info_set_stream (info, sub < subs_in ?
137                                                         SND_RAWMIDI_STREAM_INPUT :
138                                                         SND_RAWMIDI_STREAM_OUTPUT);
139
140                                         snd_rawmidi_info_set_subdevice (info, sub);
141                                         if (snd_ctl_rawmidi_info (handle, info) < 0) {
142                                                 continue;
143                                         }
144
145                                         const char *sub_name = snd_rawmidi_info_get_subdevice_name (info);
146                                         if (sub == 0 && sub_name[0] == '\0') {
147                                                 devname = "hw:";
148                                                 devname += snd_ctl_card_info_get_id (cinfo);
149                                                 devname += ",";
150                                                 devname += PBD::to_string (device);
151
152                                                 std::string card_name;
153                                                 card_name = snd_rawmidi_info_get_name (info);
154                                                 card_name += " (";
155                                                 if (sub < subs_in) card_name += "I";
156                                                 if (sub < subs_out) card_name += "O";
157                                                 card_name += ")";
158
159                                                 devices.insert (std::make_pair (card_name, devname));
160                                                 break;
161                                         } else {
162                                                 devname = "hw:";
163                                                 devname += snd_ctl_card_info_get_id (cinfo);
164                                                 devname += ",";
165                                                 devname += PBD::to_string (device);
166                                                 devname += ",";
167                                                 devname += PBD::to_string (sub);
168
169                                                 std::string card_name = sub_name;
170                                                 card_name += " (";
171                                                 if (sub < subs_in) card_name += "I";
172                                                 if (sub < subs_out) card_name += "O";
173                                                 card_name += ")";
174                                                 devices.insert (std::make_pair (card_name, devname));
175                                         }
176                                 }
177                         }
178                         snd_ctl_close (handle);
179                 }
180         }
181 }
182
183 void
184 ARDOUR::get_alsa_sequencer_names (std::map<std::string, std::string>& devices)
185 {
186         snd_seq_t *seq= NULL;
187         snd_seq_client_info_t *cinfo;
188         snd_seq_port_info_t *pinfo;
189
190         snd_seq_client_info_alloca (&cinfo);
191         snd_seq_port_info_alloca (&pinfo);
192
193         if (snd_seq_open (&seq, "hw", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
194                 return;
195         }
196
197         snd_seq_client_info_set_client(cinfo, -1);
198         while (snd_seq_query_next_client (seq, cinfo) >= 0) {
199                 int client = snd_seq_client_info_get_client (cinfo);
200                 if (client == SND_SEQ_CLIENT_SYSTEM) {
201                         continue;
202                 }
203                 if (!strcmp (snd_seq_client_info_get_name(cinfo), "Midi Through")) {
204                         continue;
205                 }
206                 snd_seq_port_info_set_client (pinfo, client);
207                 snd_seq_port_info_set_port (pinfo, -1);
208
209                 while (snd_seq_query_next_port (seq, pinfo) >= 0) {
210                         int caps = snd_seq_port_info_get_capability(pinfo);
211                         if (0 == (caps & (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE))) {
212                                 continue;
213                         }
214                         if (caps & SND_SEQ_PORT_CAP_NO_EXPORT) {
215                                 continue;
216                         }
217                         std::string card_name;
218                         card_name = snd_seq_port_info_get_name (pinfo);
219
220                         card_name += " (";
221                         if (caps & SND_SEQ_PORT_CAP_READ) card_name += "I";
222                         if (caps & SND_SEQ_PORT_CAP_WRITE) card_name += "O";
223                         card_name += ")";
224
225                         std::string devname;
226                         devname = PBD::to_string(snd_seq_port_info_get_client (pinfo));
227                         devname += ":";
228                         devname += PBD::to_string(snd_seq_port_info_get_port (pinfo));
229                         devices.insert (std::make_pair (card_name, devname));
230                 }
231         }
232         snd_seq_close (seq);
233 }
234
235 int
236 ARDOUR::card_to_num(const char* device_name)
237 {
238         char* ctl_name;
239         const char * comma;
240         snd_ctl_t* ctl_handle;
241         int i = -1;
242
243         if (strncasecmp(device_name, "plughw:", 7) == 0) {
244                 device_name += 4;
245         }
246         if (!(comma = strchr(device_name, ','))) {
247                 ctl_name = strdup(device_name);
248         } else {
249                 ctl_name = strndup(device_name, comma - device_name);
250         }
251
252         if (snd_ctl_open (&ctl_handle, ctl_name, 0) >= 0) {
253                 snd_ctl_card_info_t *card_info;
254                 snd_ctl_card_info_alloca (&card_info);
255                 if (snd_ctl_card_info(ctl_handle, card_info) >= 0) {
256                         i = snd_ctl_card_info_get_card(card_info);
257                 }
258                 snd_ctl_close(ctl_handle);
259         }
260         free(ctl_name);
261         return i;
262 }