pnmixer
Volume mixer for the system tray
Data Structures | Macros | Typedefs | Functions
alsa.c File Reference

Alsa audio subsystem. More...

#include <math.h>
#include <glib.h>
#include <alsa/asoundlib.h>
#include "support-log.h"
#include "alsa.h"
Include dependency graph for alsa.c:

Go to the source code of this file.

Data Structures

struct  alsa_card_iter
 
struct  alsa_card
 

Macros

#define _GNU_SOURCE   /* exp10() */
 
#define ALSA_DEFAULT_CARD   "(default)"
 
#define ALSA_DEFAULT_HCTL   "default"
 
#define ALSA_ERR(err, ...)   alsa_log_msg(LOG_ERROR, NULL, snd_strerror(err), __VA_ARGS__)
 
#define ALSA_CARD_DEBUG(card, ...)   alsa_log_msg(LOG_DEBUG, card, NULL, __VA_ARGS__)
 
#define ALSA_CARD_WARN(card, ...)   alsa_log_msg(LOG_WARN, card, NULL, __VA_ARGS__)
 
#define ALSA_CARD_ERR(card, err, ...)   alsa_log_msg(LOG_ERROR, card, snd_strerror(err), __VA_ARGS__)
 
#define MAX_LINEAR_DB_SCALE   24
 

Typedefs

typedef struct alsa_card_iter AlsaCardIter
 

Functions

static void alsa_log_msg (enum log_level level, const char *card, const char *strerr, const char *format,...)
 
static gboolean use_linear_dB_scale (long db_min, long db_max)
 
static long lrint_dir (double x, int dir)
 
static const char * elem_get_name (snd_mixer_elem_t *elem)
 
static double elem_get_volume (const char *hctl, snd_mixer_elem_t *elem, double *volume)
 
static gboolean elem_set_volume (const char *hctl, snd_mixer_elem_t *elem, double volume, int dir)
 
static gboolean elem_get_volume_normalized (const char *hctl, snd_mixer_elem_t *elem, double *volume)
 
static gboolean elem_set_volume_normalized (const char *hctl, snd_mixer_elem_t *elem, double volume, int dir)
 
static gboolean elem_has_mute (G_GNUC_UNUSED const char *hctl, snd_mixer_elem_t *elem)
 
static gboolean elem_get_mute (const char *hctl, snd_mixer_elem_t *elem, gboolean *muted)
 
static gboolean elem_set_mute (const char *hctl, snd_mixer_elem_t *elem, gboolean mute)
 
static struct pollfd * mixer_get_poll_descriptors (const char *hctl, snd_mixer_t *mixer)
 
static GSList * mixer_list_playable (G_GNUC_UNUSED const char *hctl, snd_mixer_t *mixer)
 
static gboolean mixer_is_playable (const char *hctl, snd_mixer_t *mixer)
 
static snd_mixer_elem_t * mixer_get_playable_elem (const char *hctl, snd_mixer_t *mixer, const char *channel)
 
static snd_mixer_elem_t * mixer_get_first_playable_elem (const char *hctl, snd_mixer_t *mixer)
 
static void mixer_close (const char *hctl, snd_mixer_t *mixer)
 
static snd_mixer_t * mixer_open (const char *hctl)
 
static void alsa_card_iter_free (AlsaCardIter *iter)
 
static AlsaCardIteralsa_card_iter_new (void)
 
static gboolean alsa_card_iter_loop (AlsaCardIter *iter)
 
static guint * watch_poll_descriptors (const char *hctl, struct pollfd *pollfds, GIOFunc func, gpointer data)
 
static void unwatch_poll_descriptors (guint *watch_ids)
 
static gboolean poll_watch_cb (GIOChannel *source, GIOCondition condition, AlsaCard *card)
 
const char * alsa_card_get_name (AlsaCard *card)
 
const char * alsa_card_get_channel (AlsaCard *card)
 
gboolean alsa_card_has_mute (AlsaCard *card)
 
gboolean alsa_card_is_muted (AlsaCard *card)
 
void alsa_card_toggle_mute (AlsaCard *card)
 
gdouble alsa_card_get_volume (AlsaCard *card)
 
void alsa_card_set_volume (AlsaCard *card, gdouble value, int dir)
 
void alsa_card_install_callback (AlsaCard *card, AlsaCb callback, gpointer user_data)
 
void alsa_card_free (AlsaCard *card)
 
AlsaCardalsa_card_new (const char *card_name, const char *channel, gboolean normalize)
 
GSList * alsa_list_cards (void)
 
GSList * alsa_list_channels (const char *card_name)
 

Detailed Description

Alsa audio subsystem.

Alsa audio subsystem. All the alsa-related code is enclosed in here, and this is the only file that uses the alsa library. This is the lowest-level part of PNMixer, that's why it doesn't include any other local headers. If you do so, it probably means that you're starting messing up the code, so think twice.

Definition in file alsa.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE   /* exp10() */

Definition at line 26 of file alsa.c.

◆ ALSA_CARD_DEBUG

#define ALSA_CARD_DEBUG (   card,
  ... 
)    alsa_log_msg(LOG_DEBUG, card, NULL, __VA_ARGS__)

Definition at line 65 of file alsa.c.

◆ ALSA_CARD_ERR

#define ALSA_CARD_ERR (   card,
  err,
  ... 
)    alsa_log_msg(LOG_ERROR, card, snd_strerror(err), __VA_ARGS__)

Definition at line 71 of file alsa.c.

◆ ALSA_CARD_WARN

#define ALSA_CARD_WARN (   card,
  ... 
)    alsa_log_msg(LOG_WARN, card, NULL, __VA_ARGS__)

Definition at line 68 of file alsa.c.

◆ ALSA_DEFAULT_CARD

#define ALSA_DEFAULT_CARD   "(default)"

Definition at line 34 of file alsa.c.

◆ ALSA_DEFAULT_HCTL

#define ALSA_DEFAULT_HCTL   "default"

Definition at line 35 of file alsa.c.

◆ ALSA_ERR

#define ALSA_ERR (   err,
  ... 
)    alsa_log_msg(LOG_ERROR, NULL, snd_strerror(err), __VA_ARGS__)

Definition at line 62 of file alsa.c.

◆ MAX_LINEAR_DB_SCALE

#define MAX_LINEAR_DB_SCALE   24

Definition at line 86 of file alsa.c.

Typedef Documentation

◆ AlsaCardIter

typedef struct alsa_card_iter AlsaCardIter

Definition at line 508 of file alsa.c.

Function Documentation

◆ alsa_card_free()

void alsa_card_free ( AlsaCard card)

Free a card instance, therefore closing mixer and freeing any allocated ressources.

Parameters
carda Card instance.

Definition at line 862 of file alsa.c.

◆ alsa_card_get_channel()

const char* alsa_card_get_channel ( AlsaCard card)

Get the name of the channel. This is an internal string that shouldn't be modified.

Parameters
carda Card instance.
Returns
the name of the channel.

Definition at line 750 of file alsa.c.

◆ alsa_card_get_name()

const char* alsa_card_get_name ( AlsaCard card)

Get the name of the card. This is an internal string that shouldn't be modified.

Parameters
carda Card instance.
Returns
the name of the card.

Definition at line 737 of file alsa.c.

◆ alsa_card_get_volume()

gdouble alsa_card_get_volume ( AlsaCard card)

Get the volume in percent (value between 0 and 100).

Parameters
carda Card instance.
Returns
the volume in percent.

Definition at line 804 of file alsa.c.

◆ alsa_card_has_mute()

gboolean alsa_card_has_mute ( AlsaCard card)

Whether the card has mute capabilities.

Parameters
carda Card instance.
Returns
TRUE if the card can be muted, FALSE otherwise.

Definition at line 762 of file alsa.c.

◆ alsa_card_install_callback()

void alsa_card_install_callback ( AlsaCard card,
AlsaCb  callback,
gpointer  user_data 
)

Set a callback invoked on volume/mute changes.

Parameters
carda Card instance.
callbackthe callback to be invoked.
user_datathe user data passed to the callback.

Definition at line 850 of file alsa.c.

◆ alsa_card_is_muted()

gboolean alsa_card_is_muted ( AlsaCard card)

Get the mute state, either TRUE or FALSE.

Parameters
carda Card instance.
Returns
TRUE if the card is muted, FALSE otherwise.

Definition at line 774 of file alsa.c.

◆ alsa_card_iter_free()

static void alsa_card_iter_free ( AlsaCardIter iter)
static

Definition at line 512 of file alsa.c.

◆ alsa_card_iter_loop()

static gboolean alsa_card_iter_loop ( AlsaCardIter iter)
static

Definition at line 540 of file alsa.c.

◆ alsa_card_iter_new()

static AlsaCardIter* alsa_card_iter_new ( void  )
static

Definition at line 524 of file alsa.c.

◆ alsa_card_new()

AlsaCard* alsa_card_new ( const char *  card_name,
const char *  channel,
gboolean  normalize 
)

Create a new Card instance. Look for the selected card among the available cards. If found, open it, and look for the selected channel. If found, all is well, the card is ready to be used. Otherwise, NULL is returned.

Parameters
card_namethe name of the card, or NULL to use the default card.
channelthe name of the channel, or NULL to use the first playable channel.
normalizewhether we use normalized volume or not.
Returns
a newly allocated Card instance, or NULL on failure.

Definition at line 891 of file alsa.c.

◆ alsa_card_set_volume()

void alsa_card_set_volume ( AlsaCard card,
gdouble  value,
int  dir 
)

Set the volume in percent (value between 0 and 100).

Parameters
carda Card instance.
valuethe volume in percent.
dirthe direction of the volume change (-1: lowering, +1: raising, 0: setting).

Definition at line 827 of file alsa.c.

◆ alsa_card_toggle_mute()

void alsa_card_toggle_mute ( AlsaCard card)

Toggle the mute state.

Parameters
carda Card instance.

Definition at line 788 of file alsa.c.

◆ alsa_list_cards()

GSList* alsa_list_cards ( void  )

Return the list of playable cards as a GSList. Must be freed using g_slist_free_full() and g_free().

Returns
a list of playable cards.

Definition at line 966 of file alsa.c.

◆ alsa_list_channels()

GSList* alsa_list_channels ( const char *  card_name)

For a given card name, return the list of playable channels as a GSList. Must be freed using g_slist_free_full() and g_free().

Parameters
card_namethe name of the card for which we list the channels
Returns
a list of playable channels.

Definition at line 1002 of file alsa.c.

◆ alsa_log_msg()

static void alsa_log_msg ( enum log_level  level,
const char *  card,
const char *  strerr,
const char *  format,
  ... 
)
static

Definition at line 42 of file alsa.c.

◆ elem_get_mute()

static gboolean elem_get_mute ( const char *  hctl,
snd_mixer_elem_t *  elem,
gboolean *  muted 
)
static

Definition at line 266 of file alsa.c.

◆ elem_get_name()

static const char* elem_get_name ( snd_mixer_elem_t *  elem)
static

Definition at line 107 of file alsa.c.

◆ elem_get_volume()

static double elem_get_volume ( const char *  hctl,
snd_mixer_elem_t *  elem,
double *  volume 
)
static

Definition at line 114 of file alsa.c.

◆ elem_get_volume_normalized()

static gboolean elem_get_volume_normalized ( const char *  hctl,
snd_mixer_elem_t *  elem,
double *  volume 
)
static

Definition at line 175 of file alsa.c.

◆ elem_has_mute()

static gboolean elem_has_mute ( G_GNUC_UNUSED const char *  hctl,
snd_mixer_elem_t *  elem 
)
static

Definition at line 259 of file alsa.c.

◆ elem_set_mute()

static gboolean elem_set_mute ( const char *  hctl,
snd_mixer_elem_t *  elem,
gboolean  mute 
)
static

Definition at line 295 of file alsa.c.

◆ elem_set_volume()

static gboolean elem_set_volume ( const char *  hctl,
snd_mixer_elem_t *  elem,
double  volume,
int  dir 
)
static

Definition at line 146 of file alsa.c.

◆ elem_set_volume_normalized()

static gboolean elem_set_volume_normalized ( const char *  hctl,
snd_mixer_elem_t *  elem,
double  volume,
int  dir 
)
static

Definition at line 220 of file alsa.c.

◆ lrint_dir()

static long lrint_dir ( double  x,
int  dir 
)
static

Definition at line 95 of file alsa.c.

◆ mixer_close()

static void mixer_close ( const char *  hctl,
snd_mixer_t *  mixer 
)
static

Definition at line 438 of file alsa.c.

◆ mixer_get_first_playable_elem()

static snd_mixer_elem_t* mixer_get_first_playable_elem ( const char *  hctl,
snd_mixer_t *  mixer 
)
static

Definition at line 414 of file alsa.c.

◆ mixer_get_playable_elem()

static snd_mixer_elem_t* mixer_get_playable_elem ( const char *  hctl,
snd_mixer_t *  mixer,
const char *  channel 
)
static

Definition at line 384 of file alsa.c.

◆ mixer_get_poll_descriptors()

static struct pollfd* mixer_get_poll_descriptors ( const char *  hctl,
snd_mixer_t *  mixer 
)
static

Definition at line 326 of file alsa.c.

◆ mixer_is_playable()

static gboolean mixer_is_playable ( const char *  hctl,
snd_mixer_t *  mixer 
)
static

Definition at line 369 of file alsa.c.

◆ mixer_list_playable()

static GSList* mixer_list_playable ( G_GNUC_UNUSED const char *  hctl,
snd_mixer_t *  mixer 
)
static

Definition at line 350 of file alsa.c.

◆ mixer_open()

static snd_mixer_t* mixer_open ( const char *  hctl)
static

Definition at line 457 of file alsa.c.

◆ poll_watch_cb()

static gboolean poll_watch_cb ( GIOChannel *  source,
GIOCondition  condition,
AlsaCard card 
)
static

Callback function for volume changes. We forward changes to higher level, through a callback mechanism again.

Parameters
sourcethe GIOChannel event source.
conditionthe condition which has been satisfied.
carddata set in g_io_add_watch().
Returns
FALSE if the event source should be removed.

Definition at line 660 of file alsa.c.

◆ unwatch_poll_descriptors()

static void unwatch_poll_descriptors ( guint *  watch_ids)
static

Definition at line 621 of file alsa.c.

◆ use_linear_dB_scale()

static gboolean use_linear_dB_scale ( long  db_min,
long  db_max 
)
inlinestatic

Definition at line 89 of file alsa.c.

◆ watch_poll_descriptors()

static guint* watch_poll_descriptors ( const char *  hctl,
struct pollfd *  pollfds,
GIOFunc  func,
gpointer  data 
)
static

Definition at line 591 of file alsa.c.