summaryrefslogtreecommitdiff
path: root/audio_alsa.c
blob: 257497f44cbde4807b33b6754b0af474e6737813 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * libalsa output driver. This file is part of Shairport.
 * Copyright (c) Muffinman, Skaman 2013
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#define ALSA_PCM_NEW_HW_PARAMS_API

#include <stdio.h>
#include <unistd.h>
#include <memory.h>
#include <pthread.h>
#include <alsa/asoundlib.h>
#include "common.h"
#include "audio.h"

static void help(void);
static int init(int argc, char **argv);
static void deinit(void);
static void start(int sample_rate);
static void play(short buf[], int samples);
static void stop(void);
static void flush(void);
static uint32_t delay(void);
static void volume(double vol);
static void parameters(audio_parameters* info);
static int has_mute=0;
static int has_db_vol=0;
static double set_volume;

audio_output audio_alsa = {
    .name = "alsa",
    .help = &help,
    .init = &init,
    .deinit = &deinit,
    .start = &start,
    .stop = &stop,
    .flush = &flush,
    .delay = &delay,
    .play = &play,
    .volume = NULL, // to be set later on...
    .parameters = NULL // to be set later on...
};

static pthread_mutex_t alsa_mutex = PTHREAD_MUTEX_INITIALIZER;

static unsigned int desired_sample_rate;

static snd_pcm_t *alsa_handle = NULL;
static snd_pcm_hw_params_t *alsa_params = NULL;

static snd_mixer_t *alsa_mix_handle = NULL;
static snd_mixer_elem_t *alsa_mix_elem = NULL;
static snd_mixer_selem_id_t *alsa_mix_sid = NULL;
static long alsa_mix_minv, alsa_mix_maxv;
static long alsa_mix_mindb, alsa_mix_maxdb;

static char *alsa_out_dev = "default";
static char *alsa_mix_dev = NULL;
static char *alsa_mix_ctrl = "Master";
static int alsa_mix_index = 0;

static int play_number;
static int64_t accumulated_delay,accumulated_da_delay;

static void help(void) {
    printf("    -d output-device    set the output device [default*|...]\n"
           "    -t mixer-type       set the mixer type [software*|hardware]\n"
           "    -m mixer-device     set the mixer device ['output-device'*|...]\n"
           "    -c mixer-control    set the mixer control [Master*|...]\n"
           "    -i mixer-index      set the mixer index [0*|...]\n"
           "    *) default option\n"
          );
}

static int init(int argc, char **argv) {
  const char *str;
  int value;

  int hardware_mixer = 0;

  config.audio_backend_buffer_desired_length = 6615; // this is the default for ALSA
  config.audio_backend_latency_offset = 0; // this is the default for ALSA
  
  // get settings from settings file first, allow them to be over-ridden by command line options
  
  if (config.cfg!=NULL) {
      /* Get the desired buffer size setting. */
      if(config_lookup_int(config.cfg, "alsa.audio_backend_buffer_desired_length", &value)) {
        if ((value<0) || (value>66150))
          die("Invalid alsa audio backend buffer desired length \"%sd\". It should be between 0 and 66150, default is 6615",value);
        else
          config.audio_backend_buffer_desired_length=value;
      }
   
      /* Get the latency offset. */
      if(config_lookup_int(config.cfg, "alsa.audio_backend_latency_offset", &value)) {
        if ((value<-22050) || (value>22050))
          die("Invalid alsa audio backend buffer latency offset \"%sd\". It should be between -22050 and +22050, default is 0",value);
        else
          config.audio_backend_latency_offset=value;
      }

      /* Get the Output Device Name. */
      if(config_lookup_string(config.cfg, "alsa.output_device", &str)) {
        alsa_out_dev = (char*)str;
      }
      
      /* Get the Mixer Type setting. */
      if(config_lookup_string(config.cfg, "alsa.mixer_type", &str)) {
        if (strcasecmp(str,"software")==0)
          hardware_mixer=0;
        else if (strcasecmp(str,"hardware")==0)
          hardware_mixer=1;
        else
          die("Invalid alsa mixer option choice \"%s\". It should be \"software\" or \"hardware\"");
      }

      /* Get the Mixer Device Name. */
      if(config_lookup_string(config.cfg, "alsa.mixer_device", &str)) {
        alsa_mix_dev = (char*)str;
      }
      
      /* Get the Mixer Control Name. */
      if(config_lookup_string(config.cfg, "alsa.mixer_control_name", &str)) {
        alsa_mix_ctrl = (char*)str;
      }

  }

  optind = 1; // optind=0 is equivalent to optind=1 plus special behaviour
  argv--;     // so we shift the arguments to satisfy getopt()
  argc++;
  // some platforms apparently require optreset = 1; - which?
  int opt;
  while ((opt = getopt(argc, argv, "d:t:m:c:i:")) > 0) {
      switch (opt) {
          case 'd':
              alsa_out_dev = optarg;
              break;
          case 't':
              if (strcmp(optarg, "hardware") == 0)
                  hardware_mixer = 1;
              break;
          case 'm':
              alsa_mix_dev = optarg;
              break;
          case 'c':
              alsa_mix_ctrl = optarg;
              break;
          case 'i':
              alsa_mix_index = strtol(optarg, NULL, 10);
              break;
          default:
              help();
              die("Invalid audio option -%c specified", opt);
      }
  }

  if (optind < argc)
      die("Invalid audio argument: %s", argv[optind]);
  
  if (!hardware_mixer)
      return 0;

  if (alsa_mix_dev == NULL)
      alsa_mix_dev = alsa_out_dev;

  int ret = 0;

  snd_mixer_selem_id_alloca(&alsa_mix_sid);
  snd_mixer_selem_id_set_index(alsa_mix_sid, alsa_mix_index);
  snd_mixer_selem_id_set_name(alsa_mix_sid, alsa_mix_ctrl);

  if ((snd_mixer_open(&alsa_mix_handle, 0)) < 0)
      die ("Failed to open mixer");
  if ((snd_mixer_attach(alsa_mix_handle, alsa_mix_dev)) < 0)
      die ("Failed to attach mixer");
  if ((snd_mixer_selem_register(alsa_mix_handle, NULL, NULL)) < 0)
      die ("Failed to register mixer element");

  ret = snd_mixer_load(alsa_mix_handle);
  if (ret < 0)
      die ("Failed to load mixer element");
  alsa_mix_elem = snd_mixer_find_selem(alsa_mix_handle, alsa_mix_sid);
  if (!alsa_mix_elem)
      die ("Failed to find mixer element");
  if (snd_mixer_selem_get_playback_volume_range(alsa_mix_elem, &alsa_mix_minv, &alsa_mix_maxv)<0)
    debug(1,"Can't read mixer's [linear] min and max volumes.");
  else {
    if ((snd_mixer_selem_ask_playback_vol_dB(alsa_mix_elem,alsa_mix_minv,&alsa_mix_mindb)==0) &&
       (snd_mixer_selem_ask_playback_vol_dB(alsa_mix_elem,alsa_mix_maxv,&alsa_mix_maxdb)==0)) {
      has_db_vol=1;
      audio_alsa.volume = &volume; // insert the volume function now we know it can do dB stuff
      audio_alsa.parameters = &parameters; // likewise the parameters stuff
      if (alsa_mix_mindb==-9999999) {
        // trying to say that the lowest vol is mute, maybe? Raspberry Pi does this
        if (snd_mixer_selem_ask_playback_vol_dB(alsa_mix_elem,alsa_mix_minv+1,&alsa_mix_mindb)==0)
          debug(1,"Can't get dB value corresponding to a \"volume\" of 1.");
      }
      debug(1,"Hardware mixer has dB volume from %f to %f.",(1.0*alsa_mix_mindb)/100.0,(1.0*alsa_mix_maxdb)/100.0);
    } else {
      debug(1,"Hardware mixer does not have dB volume -- not used.");
    }
  }
  if (snd_mixer_selem_has_playback_switch(alsa_mix_elem)) {
    has_mute=1;
    debug(1,"Has mute ability.");
  }
  return 0;
}

static void deinit(void) {
  stop();
  if (alsa_mix_handle) {
      snd_mixer_close(alsa_mix_handle);
  }
}

int open_alsa_device(void) { 

  int ret, dir = 0;
  unsigned int my_sample_rate = desired_sample_rate;
  snd_pcm_uframes_t frames = 441*10;
  snd_pcm_uframes_t buffer_size = frames*4;
  ret = snd_pcm_open(&alsa_handle, alsa_out_dev, SND_PCM_STREAM_PLAYBACK, 0);
  if (ret < 0)
    return(ret);
    // die("Alsa initialization failed: unable to open pcm device: %s.", snd_strerror(ret));

  snd_pcm_hw_params_alloca(&alsa_params);
  snd_pcm_hw_params_any(alsa_handle, alsa_params);
  snd_pcm_hw_params_set_access(alsa_handle, alsa_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  snd_pcm_hw_params_set_format(alsa_handle, alsa_params, SND_PCM_FORMAT_S16);
  snd_pcm_hw_params_set_channels(alsa_handle, alsa_params, 2);
  snd_pcm_hw_params_set_rate_near(alsa_handle, alsa_params, &my_sample_rate, &dir);
  // snd_pcm_hw_params_set_period_size_near(alsa_handle, alsa_params, &frames, &dir);
  // snd_pcm_hw_params_set_buffer_size_near(alsa_handle, alsa_params, &buffer_size);
  ret = snd_pcm_hw_params(alsa_handle, alsa_params);
  if (ret < 0) {
    die("unable to set hw parameters: %s.", snd_strerror(ret));
  }
  if (my_sample_rate!=desired_sample_rate) {
    die("Can't set the D/A converter to %d -- set to %d instead./n",desired_sample_rate,my_sample_rate);
  }
  return(0);
}

static void start(int sample_rate) {
  if (sample_rate != 44100)
    die("Unexpected sample rate %d -- only 44,100 supported!",sample_rate);
  desired_sample_rate = sample_rate; // must be a variable
}

static uint32_t delay() {
	if (alsa_handle==NULL) {
		return 0;
	} else {
		pthread_mutex_lock(&alsa_mutex);
		snd_pcm_sframes_t current_avail,current_delay = 0;
		int derr,ignore;
		if (snd_pcm_state(alsa_handle)==SND_PCM_STATE_RUNNING) {
			derr = snd_pcm_avail_delay(alsa_handle,&current_avail,&current_delay);
			// current_avail not used
			if (derr != 0) {
				ignore = snd_pcm_recover(alsa_handle, derr, 0);
				debug(1,"Error %d in delay(): %s. Delay reported is %d frames.", derr, snd_strerror(derr),current_delay);
				current_delay=-1;
			} 
		} else if (snd_pcm_state(alsa_handle)==SND_PCM_STATE_PREPARED) {
			current_delay=0;
		} else {
			if (snd_pcm_state(alsa_handle)==SND_PCM_STATE_XRUN)
				current_delay=0;
			else {
				current_delay=-1;
				debug(1,"Error -- ALSA delay(): bad state: %d.",snd_pcm_state(alsa_handle));
			}
			if (derr = snd_pcm_prepare(alsa_handle)) {
				ignore = snd_pcm_recover(alsa_handle, derr, 0);
				debug(1,"Error preparing after delay error: %s.", snd_strerror(derr));
				current_delay = -1;
			}
		}
		pthread_mutex_unlock(&alsa_mutex);
		return current_delay;
  }
}

static void play(short buf[], int samples) {
  int ret = 0;
	if (alsa_handle==NULL) {
		ret = open_alsa_device();
		if ((ret==0) && (audio_alsa.volume))
		  volume(set_volume);
	}
  if (ret==0) {
    pthread_mutex_lock(&alsa_mutex);
    snd_pcm_sframes_t current_delay = 0;
    int err,ignore;
    if ((snd_pcm_state(alsa_handle)==SND_PCM_STATE_PREPARED) || (snd_pcm_state(alsa_handle)==SND_PCM_STATE_RUNNING)) {
      err = snd_pcm_writei(alsa_handle, (char*)buf, samples);
      if (err < 0) {
        ignore = snd_pcm_recover(alsa_handle, err, 0);
        debug(1,"Error %d writing %d samples in play() %s.",err,samples, snd_strerror(err));
      }
    } else {
      debug(1,"Error -- ALSA device in incorrect state (%d) for play.",snd_pcm_state(alsa_handle));
      if (err = snd_pcm_prepare(alsa_handle)) {
        ignore = snd_pcm_recover(alsa_handle, err, 0);
        debug(1,"Error preparing after play error: %s.", snd_strerror(err));
      }
    }
    pthread_mutex_unlock(&alsa_mutex);
  }
}

static void flush(void) {
  int derr;
  if (alsa_handle) {
    // debug(1,"Dropping frames for flush...");
    if (derr = snd_pcm_drop(alsa_handle))
      debug(1,"Error dropping frames: %s.", snd_strerror(derr));
    // debug(1,"Dropped frames ok. State is %d.",snd_pcm_state(alsa_handle));
    if (derr = snd_pcm_prepare(alsa_handle))
      debug(1,"Error preparing after flush: %s.", snd_strerror(derr));
    // debug(1,"Frames successfully dropped.");
    /*
    if (snd_pcm_state(alsa_handle)==SND_PCM_STATE_PREPARED)
      debug(1,"Flush returns to SND_PCM_STATE_PREPARED state.");      
    if (snd_pcm_state(alsa_handle)==SND_PCM_STATE_RUNNING)
      debug(1,"Flush returns to SND_PCM_STATE_RUNNING state.");
    */    
    if (!((snd_pcm_state(alsa_handle)==SND_PCM_STATE_PREPARED) || (snd_pcm_state(alsa_handle)==SND_PCM_STATE_RUNNING)))
      debug(1,"Flush returning unexpected state -- %d.",snd_pcm_state(alsa_handle));
    
    // flush also closes the device
    snd_pcm_close(alsa_handle);
    alsa_handle = NULL;
  }
}

static void stop(void) {
	if (alsa_handle!=0)
	  // when we want to stop, we want the alsa device
	  // to be closed immediately -- we may even be killing the thread, so we don't wish to wait
	  // so we should flush first
	  flush(); // flush will also close the device
	  // close_alsa_device();
}

static void parameters(audio_parameters* info) {
  info->has_true_mute=has_mute;
  info->is_muted = ((has_mute) && (set_volume == -144.0));
  info->minimum_volume_dB=alsa_mix_mindb;
  info->maximum_volume_dB=alsa_mix_maxdb;
  info->airplay_volume=set_volume;
  info->current_volume_dB=vol2attn(set_volume,alsa_mix_maxdb,alsa_mix_mindb);
}

static void volume(double vol) {
  set_volume=vol;
  double vol_setting = vol2attn(vol,alsa_mix_maxdb,alsa_mix_mindb);
  // debug(1,"Setting volume db to %f, for volume input of %f.",vol_setting/100,vol);
  if (snd_mixer_selem_set_playback_dB_all(alsa_mix_elem, vol_setting, -1) != 0)
    die ("Failed to set playback dB volume"); 
  if (has_mute) 
    snd_mixer_selem_set_playback_switch_all(alsa_mix_elem, (vol!=-144.0));
}