package portaudio w/WASAPI for Vista or later
[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 "pbd/convert.h"
22 #include "ardouralsautil/devicelist.h"
23
24 using namespace std;
25
26 void
27 ARDOUR::get_alsa_audio_device_names (std::map<std::string, std::string>& devices, AlsaDuplex duplex)
28 {
29         snd_ctl_t *handle;
30         snd_ctl_card_info_t *info;
31         snd_pcm_info_t *pcminfo;
32         snd_ctl_card_info_alloca(&info);
33         snd_pcm_info_alloca(&pcminfo);
34         string devname;
35         int cardnum = -1;
36         int device = -1;
37
38         assert (duplex > 0);
39
40         while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
41
42                 devname = "hw:";
43                 devname += PBD::to_string (cardnum, std::dec);
44
45                 if (snd_ctl_open (&handle, devname.c_str(), 0) >= 0 && snd_ctl_card_info (handle, info) >= 0) {
46
47                         if (snd_ctl_card_info (handle, info) < 0) {
48                                 continue;
49                         }
50
51                         string card_name = snd_ctl_card_info_get_name (info);
52
53                         /* change devname to use ID, not number */
54
55                         devname = "hw:";
56                         devname += snd_ctl_card_info_get_id (info);
57
58                         while (snd_ctl_pcm_next_device (handle, &device) >= 0 && device >= 0) {
59
60                                 /* only detect duplex devices here. more
61                                  * complex arrangements are beyond our scope
62                                  */
63
64                                 snd_pcm_info_set_device (pcminfo, device);
65                                 snd_pcm_info_set_subdevice (pcminfo, 0);
66                                 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_CAPTURE);
67
68                                 if (snd_ctl_pcm_info (handle, pcminfo) < 0 && (duplex & HalfDuplexIn)) {
69                                         continue;
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_PLAYBACK);
75
76                                 if (snd_ctl_pcm_info (handle, pcminfo) < 0 && (duplex & HalfDuplexOut)) {
77                                         continue;
78                                 }
79                                 devname += ',';
80                                 devname += PBD::to_string (device, std::dec);
81                                 devices.insert (std::make_pair (card_name, devname));
82                         }
83
84                         snd_ctl_close(handle);
85                 }
86         }
87 }
88
89 void
90 ARDOUR::get_alsa_rawmidi_device_names (std::map<std::string, std::string>& devices)
91 {
92         int cardnum = -1;
93         snd_ctl_card_info_t *cinfo;
94         snd_ctl_card_info_alloca (&cinfo);
95         while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
96                 snd_ctl_t *handle;
97                 std::string devname = "hw:";
98                 devname += PBD::to_string (cardnum, std::dec);
99                 if (snd_ctl_open (&handle, devname.c_str (), 0) >= 0 && snd_ctl_card_info (handle, cinfo) >= 0) {
100                         int device = -1;
101                         while (snd_ctl_rawmidi_next_device (handle, &device) >= 0 && device >= 0) {
102                                 snd_rawmidi_info_t *info;
103                                 snd_rawmidi_info_alloca (&info);
104                                 snd_rawmidi_info_set_device (info, device);
105
106                                 int subs_in, subs_out;
107
108                                 snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_INPUT);
109                                 if (snd_ctl_rawmidi_info (handle, info) >= 0) {
110                                         subs_in = snd_rawmidi_info_get_subdevices_count (info);
111                                 } else {
112                                         subs_in = 0;
113                                 }
114
115                                 snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_OUTPUT);
116                                 if (snd_ctl_rawmidi_info (handle, info) >= 0) {
117                                         subs_out = snd_rawmidi_info_get_subdevices_count (info);
118                                 } else {
119                                         subs_out = 0;
120                                 }
121
122                                 const int subs = subs_in > subs_out ? subs_in : subs_out;
123                                 if (!subs) {
124                                         continue;
125                                 }
126
127                                 for (int sub = 0; sub < subs; ++sub) {
128                                         snd_rawmidi_info_set_stream (info, sub < subs_in ?
129                                                         SND_RAWMIDI_STREAM_INPUT :
130                                                         SND_RAWMIDI_STREAM_OUTPUT);
131
132                                         snd_rawmidi_info_set_subdevice (info, sub);
133                                         if (snd_ctl_rawmidi_info (handle, info) < 0) {
134                                                 continue;
135                                         }
136
137                                         const char *sub_name = snd_rawmidi_info_get_subdevice_name (info);
138                                         if (sub == 0 && sub_name[0] == '\0') {
139                                                 devname = "hw:";
140                                                 devname += snd_ctl_card_info_get_id (cinfo);
141                                                 devname += ",";
142                                                 devname += PBD::to_string (device, std::dec);
143
144                                                 std::string card_name;
145                                                 card_name = snd_rawmidi_info_get_name (info);
146                                                 card_name += " (";
147                                                 if (sub < subs_in) card_name += "I";
148                                                 if (sub < subs_out) card_name += "O";
149                                                 card_name += ")";
150
151                                                 devices.insert (std::make_pair (card_name, devname));
152                                                 break;
153                                         } else {
154                                                 devname = "hw:";
155                                                 devname += snd_ctl_card_info_get_id (cinfo);
156                                                 devname += ",";
157                                                 devname += PBD::to_string (device, std::dec);
158                                                 devname += ",";
159                                                 devname += PBD::to_string (sub, std::dec);
160
161                                                 std::string card_name = sub_name;
162                                                 card_name += " (";
163                                                 if (sub < subs_in) card_name += "I";
164                                                 if (sub < subs_out) card_name += "O";
165                                                 card_name += ")";
166                                                 devices.insert (std::make_pair (card_name, devname));
167                                         }
168                                 }
169                         }
170                         snd_ctl_close (handle);
171                 }
172         }
173 }
174
175 void
176 ARDOUR::get_alsa_sequencer_names (std::map<std::string, std::string>& devices)
177 {
178         snd_seq_t *seq= NULL;
179         snd_seq_client_info_t *cinfo;
180         snd_seq_port_info_t *pinfo;
181
182         snd_seq_client_info_alloca (&cinfo);
183         snd_seq_port_info_alloca (&pinfo);
184
185         if (snd_seq_open (&seq, "hw", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
186                 return;
187         }
188
189         snd_seq_client_info_set_client(cinfo, -1);
190         while (snd_seq_query_next_client (seq, cinfo) >= 0) {
191                 int client = snd_seq_client_info_get_client (cinfo);
192                 if (client == SND_SEQ_CLIENT_SYSTEM) {
193                         continue;
194                 }
195                 if (!strcmp (snd_seq_client_info_get_name(cinfo), "Midi Through")) {
196                         continue;
197                 }
198                 snd_seq_port_info_set_client (pinfo, client);
199                 snd_seq_port_info_set_port (pinfo, -1);
200
201                 while (snd_seq_query_next_port (seq, pinfo) >= 0) {
202                         int caps = snd_seq_port_info_get_capability(pinfo);
203                         if (0 == (caps & (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE))) {
204                                 continue;
205                         }
206                         if (caps & SND_SEQ_PORT_CAP_NO_EXPORT) {
207                                 continue;
208                         }
209                         std::string card_name;
210                         card_name = snd_seq_port_info_get_name (pinfo);
211
212                         card_name += " (";
213                         if (caps & SND_SEQ_PORT_CAP_READ) card_name += "I";
214                         if (caps & SND_SEQ_PORT_CAP_WRITE) card_name += "O";
215                         card_name += ")";
216
217                         std::string devname;
218                         devname = PBD::to_string(snd_seq_port_info_get_client (pinfo), std::dec);
219                         devname += ":";
220                         devname += PBD::to_string(snd_seq_port_info_get_port (pinfo), std::dec);
221                         devices.insert (std::make_pair (card_name, devname));
222                 }
223         }
224         snd_seq_close (seq);
225 }
226
227 int
228 ARDOUR::card_to_num(const char* device_name)
229 {
230         char* ctl_name;
231         const char * comma;
232         snd_ctl_t* ctl_handle;
233         int i = -1;
234
235         if (strncasecmp(device_name, "plughw:", 7) == 0) {
236                 device_name += 4;
237         }
238         if (!(comma = strchr(device_name, ','))) {
239                 ctl_name = strdup(device_name);
240         } else {
241                 ctl_name = strndup(device_name, comma - device_name);
242         }
243
244         if (snd_ctl_open (&ctl_handle, ctl_name, 0) >= 0) {
245                 snd_ctl_card_info_t *card_info;
246                 snd_ctl_card_info_alloca (&card_info);
247                 if (snd_ctl_card_info(ctl_handle, card_info) >= 0) {
248                         i = snd_ctl_card_info_get_card(card_info);
249                 }
250                 snd_ctl_close(ctl_handle);
251         }
252         free(ctl_name);
253         return i;
254 }