2 * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2013 Paul Davis
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.
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.
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.
20 #include <alsa/asoundlib.h>
21 #include "pbd/convert.h"
22 #include "ardouralsautil/devicelist.h"
27 ARDOUR::get_alsa_audio_device_names (std::map<std::string, std::string>& devices)
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);
38 while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
41 devname += PBD::to_string (cardnum, std::dec);
43 if (snd_ctl_open (&handle, devname.c_str(), 0) >= 0 && snd_ctl_card_info (handle, info) >= 0) {
45 if (snd_ctl_card_info (handle, info) < 0) {
49 string card_name = snd_ctl_card_info_get_name (info);
51 /* change devname to use ID, not number */
54 devname += snd_ctl_card_info_get_id (info);
56 while (snd_ctl_pcm_next_device (handle, &device) >= 0 && device >= 0) {
58 /* only detect duplex devices here. more
59 * complex arrangements are beyond our scope
62 snd_pcm_info_set_device (pcminfo, device);
63 snd_pcm_info_set_subdevice (pcminfo, 0);
64 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_CAPTURE);
66 if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
70 snd_pcm_info_set_device (pcminfo, device);
71 snd_pcm_info_set_subdevice (pcminfo, 0);
72 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_PLAYBACK);
74 if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
78 devname += PBD::to_string (device, std::dec);
79 devices.insert (std::make_pair (card_name, devname));
82 snd_ctl_close(handle);
88 ARDOUR::get_alsa_rawmidi_device_names (std::map<std::string, std::string>& devices)
91 snd_ctl_card_info_t *cinfo;
92 snd_ctl_card_info_alloca (&cinfo);
93 while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
95 std::string devname = "hw:";
96 devname += PBD::to_string (cardnum, std::dec);
97 if (snd_ctl_open (&handle, devname.c_str (), 0) >= 0 && snd_ctl_card_info (handle, cinfo) >= 0) {
99 while (snd_ctl_rawmidi_next_device (handle, &device) >= 0 && device >= 0) {
100 snd_rawmidi_info_t *info;
101 snd_rawmidi_info_alloca (&info);
102 snd_rawmidi_info_set_device (info, device);
104 int subs_in, subs_out;
106 snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_INPUT);
107 if (snd_ctl_rawmidi_info (handle, info) >= 0) {
108 subs_in = snd_rawmidi_info_get_subdevices_count (info);
113 snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_OUTPUT);
114 if (snd_ctl_rawmidi_info (handle, info) >= 0) {
115 subs_out = snd_rawmidi_info_get_subdevices_count (info);
120 const int subs = subs_in > subs_out ? subs_in : subs_out;
125 for (int sub = 0; sub < subs; ++sub) {
126 snd_rawmidi_info_set_stream (info, sub < subs_in ?
127 SND_RAWMIDI_STREAM_INPUT :
128 SND_RAWMIDI_STREAM_OUTPUT);
130 snd_rawmidi_info_set_subdevice (info, sub);
131 if (snd_ctl_rawmidi_info (handle, info) < 0) {
135 const char *sub_name = snd_rawmidi_info_get_subdevice_name (info);
136 if (sub == 0 && sub_name[0] == '\0') {
138 devname += snd_ctl_card_info_get_id (cinfo);
140 devname += PBD::to_string (device, std::dec);
142 std::string card_name;
143 card_name = snd_rawmidi_info_get_name (info);
145 if (sub < subs_in) card_name += "I";
146 if (sub < subs_out) card_name += "O";
149 devices.insert (std::make_pair (card_name, devname));
153 devname += snd_ctl_card_info_get_id (cinfo);
155 devname += PBD::to_string (device, std::dec);
157 devname += PBD::to_string (sub, std::dec);
159 std::string card_name = sub_name;
161 if (sub < subs_in) card_name += "I";
162 if (sub < subs_out) card_name += "O";
164 devices.insert (std::make_pair (card_name, devname));
168 snd_ctl_close (handle);
174 ARDOUR::get_alsa_sequencer_names (std::map<std::string, std::string>& devices)
176 snd_seq_t *seq= NULL;
177 snd_seq_client_info_t *cinfo;
178 snd_seq_port_info_t *pinfo;
180 snd_seq_client_info_alloca (&cinfo);
181 snd_seq_port_info_alloca (&pinfo);
183 if (snd_seq_open (&seq, "hw", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
187 snd_seq_client_info_set_client(cinfo, -1);
188 while (snd_seq_query_next_client (seq, cinfo) >= 0) {
189 int client = snd_seq_client_info_get_client (cinfo);
190 if (client == SND_SEQ_CLIENT_SYSTEM) {
193 if (!strcmp (snd_seq_client_info_get_name(cinfo), "Midi Through")) {
196 snd_seq_port_info_set_client (pinfo, client);
197 snd_seq_port_info_set_port (pinfo, -1);
199 while (snd_seq_query_next_port (seq, pinfo) >= 0) {
200 int caps = snd_seq_port_info_get_capability(pinfo);
201 if (0 == (caps & (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE))) {
204 if (caps & SND_SEQ_PORT_CAP_NO_EXPORT) {
207 std::string card_name;
208 card_name = snd_seq_port_info_get_name (pinfo);
211 if (caps & SND_SEQ_PORT_CAP_READ) card_name += "I";
212 if (caps & SND_SEQ_PORT_CAP_WRITE) card_name += "O";
216 devname = PBD::to_string(snd_seq_port_info_get_client (pinfo), std::dec);
218 devname += PBD::to_string(snd_seq_port_info_get_port (pinfo), std::dec);
219 devices.insert (std::make_pair (card_name, devname));
226 ARDOUR::card_to_num(const char* device_name)
230 snd_ctl_t* ctl_handle;
233 if (strncasecmp(device_name, "plughw:", 7) == 0) {
236 if (!(comma = strchr(device_name, ','))) {
237 ctl_name = strdup(device_name);
239 ctl_name = strndup(device_name, comma - device_name);
242 if (snd_ctl_open (&ctl_handle, ctl_name, 0) >= 0) {
243 snd_ctl_card_info_t *card_info;
244 snd_ctl_card_info_alloca (&card_info);
245 if (snd_ctl_card_info(ctl_handle, card_info) >= 0) {
246 i = snd_ctl_card_info_get_card(card_info);
248 snd_ctl_close(ctl_handle);