summaryrefslogtreecommitdiff
path: root/src/mixer.cc
blob: 9450b661e8f83d21a9afd202367e0c791da1e0ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            mixer.cc
 *
 *  Tue Sep 23 14:38:54 CEST 2014
 *  Copyright 2014 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of LibAudioIO.
 *
 *  LibAudioIO is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *  
 *  LibAudioIO is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with LibAudioIO; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include "mixer.h"

#include <string>
#include <stdlib.h>

#define fERROR (strtof("-INF", NULL))

Mixer::Mixer(snd_mixer_t *handle, snd_mixer_elem_t *elem)
{
  this->handle = handle;
  this->elem = elem;
}

Mixer::~Mixer()
{
  if(handle) snd_mixer_close(handle);
}

snd_mixer_selem_channel_id_t Mixer::chanId(int idx)
{
  int num = 0;

  for(int ichn = 0; ichn <= (int)SND_MIXER_SCHN_LAST; ichn++) {
    snd_mixer_selem_channel_id_t chn = (snd_mixer_selem_channel_id_t)ichn;
    if(!snd_mixer_selem_has_capture_channel(elem, chn) &&
       !snd_mixer_selem_has_playback_channel(elem, chn)) continue;
    if(idx == num) return chn;
    num++;
  }

  return SND_MIXER_SCHN_UNKNOWN;
}

int Mixer::numberOfChannels()
{
  int num = 0;
  for(int ichn = 0; ichn <= (int)SND_MIXER_SCHN_LAST; ichn++) {
    snd_mixer_selem_channel_id_t chn = (snd_mixer_selem_channel_id_t)ichn;
    if(!snd_mixer_selem_has_capture_channel(elem, chn) &&
       !snd_mixer_selem_has_playback_channel(elem, chn)) continue;
    num++;
  }

  return num;
}

bool Mixer::isCapture()
{
  return snd_mixer_selem_has_capture_volume(elem) ||
    snd_mixer_selem_has_capture_switch(elem);
}

bool Mixer::isPlayback()
{
  return snd_mixer_selem_has_playback_volume(elem);
}

bool Mixer::isEnum()
{
  return snd_mixer_selem_is_enumerated(elem);
}

void Mixer::setEnumValue(std::string value)
{
  if(!isEnum()) return;

  int i, items;
  char itemname[40];

  // TODO: Always MONO for enums?
  snd_mixer_selem_channel_id_t channel_id =  SND_MIXER_SCHN_MONO;

  items = snd_mixer_selem_get_enum_items(elem);
  for (i = 0; i < items; i++) {
    snd_mixer_selem_get_enum_item_name(elem, i, sizeof(itemname) - 1, itemname);
    if(value == itemname) {
      snd_mixer_selem_set_enum_item(elem, channel_id, i);
      return;
    }
  }
}

std::string Mixer::enumValue()
{
  if(!isEnum()) return "";

  unsigned int idx;
  char itemname[40];

  // TODO: Always MONO for enums?
  snd_mixer_selem_channel_id_t channel_id = SND_MIXER_SCHN_MONO;

  if(!snd_mixer_selem_get_enum_item(elem, channel_id, &idx)) {
    snd_mixer_selem_get_enum_item_name(elem, idx,
                                       sizeof(itemname) - 1, itemname);
    return itemname;
  }

  return "";
}

std::vector<std::string> Mixer::enumValues()
{
  std::vector<std::string> values;
  if(!isEnum()) return values;

  int i, items;
  char itemname[40];
  items = snd_mixer_selem_get_enum_items(elem);
  for (i = 0; i < items; i++) {
    snd_mixer_selem_get_enum_item_name(elem, i, sizeof(itemname) - 1, itemname);
    values.push_back(itemname);
  }

  return values;
}

int Mixer::setLevel(int channel, float level)
{
  int err;
  long int value = level * 100;
  // dir: -1 := exact or first below
  //       0 := exact (error if not?)
  //       1 := exact or first above
  int dir = 1;

  if(isCapture()) {
    err = snd_mixer_selem_set_capture_dB(elem, chanId(channel), value, dir);	
    if(err) {
      printf("snd_mixer_selem_set_capture_dB: %s, %d\n",
             snd_strerror(err), err);
      return 1;
    }
  }

  if(isPlayback()) {
    err = snd_mixer_selem_set_playback_dB(elem, chanId(channel), value, dir);	
    if(err) {
      printf("snd_mixer_selem_set_playback_dB: %s, %d\n",
             snd_strerror(err), err);
      return 1;
    }
  }

  return 0;
}

float Mixer::level(int channel)
{
  int err;
  long int value;

  if(isCapture()) {
    err = snd_mixer_selem_get_capture_dB(elem, chanId(channel), &value);	
    if(err) {
      printf("snd_mixer_selem_get_capture_dB: %s, %d\n",
             snd_strerror(err), err);
      return fERROR;
    }
  } else if(isPlayback()) {
    err = snd_mixer_selem_get_playback_dB(elem, chanId(channel), &value);	
    if(err) {
      printf("snd_mixer_selem_get_playback_dB: %s, %d\n",
             snd_strerror(err), err);
      return fERROR;
    }
  } else {
    return fERROR;
  }

  return (float)value / 100.0;
}

Mixer::range_t Mixer::range()
{
  Mixer::range_t range;

  int err;
  long int lvl_min, lvl_max;

  if(isCapture()) {
    err = snd_mixer_selem_get_capture_dB_range(elem, &lvl_min, &lvl_max);

    if(err) {
      printf("snd_mixer_selem_get_capture_dB_range: %s, %d\n",
             snd_strerror(err), err);
    }
  } else if(isPlayback()) {
    err = snd_mixer_selem_get_playback_dB_range(elem, &lvl_min, &lvl_max);

    if(err) {
      printf("snd_mixer_selem_get_playback_dB_range: %s, %d\n",
             snd_strerror(err), err);
    }
  } else {
    range.min = fERROR;
    range.max = fERROR;
    return range;
  }

  range.min = (float)lvl_min / 100.0;
  range.max = (float)lvl_max / 100.0;

  return range;
}

void Mixer::setCapture(bool capture)
{
  if(!isCapture()) return;
  int c = capture?1:0;
  int err;
  err = snd_mixer_selem_set_capture_switch_all(elem, c);
  if(err) {
    printf("snd_mixer_selem_set_capture_switch_all: %s, %d\n",
           snd_strerror(err), err);
  }
}

bool Mixer::capture()
{
  if(!isCapture()) return false;

  int err;
  
  int num = numberOfChannels();
  for(int idx = 0; idx < num; idx++) {
    int value;
    err = snd_mixer_selem_get_capture_switch(elem, chanId(idx), &value);
    if(err) {
      printf(" snd_mixer_selem_get_capture_switch: %s, %d\n",
             snd_strerror(err), err);
      return false;
    }
    if(value == 0) return false;
 }

  return true;
}

void Mixer::setMuted(bool muted)
{
  if(!isPlayback()) return;
  int p = muted?0:1;
  int err;
  err = snd_mixer_selem_set_playback_switch_all(elem, p);
  if(err) {
    printf("snd_mixer_selem_set_playback_switch_all: %s, %d\n",
           snd_strerror(err), err);
  }
}

bool Mixer::muted()
{
  if(!isPlayback()) return false;

  int err;
  
  int num = numberOfChannels();
  for(int idx = 0; idx < num; idx++) {
    int value;
    err = snd_mixer_selem_get_playback_switch(elem, chanId(idx), &value);
    if(err) {
      printf(" snd_mixer_selem_get_playback_switch: %s, %d\n",
             snd_strerror(err), err);
      return true;
    }
    if(value == 0) return true;
 }

  return false;
}