Impostare il volume principale del mixer di ALSA

Da Gambas-it.org - Wikipedia.

Utilizzando alcune risorse dell'interfaccia Mixer di ALSA, è possibile - fra l'altro - impostare e modificare il volume audio principale del mixer.

Sarà ovviamente necessario richiamare in Gambas la libreria dinamica condivisa di ALSA: "libasound.so.2.0.0"


Mostriamo un semplice esempio, nel quale bisognerà scrivere nell'apposito spazio al di sotto della console (oppure nel Terminale) del progetto Gambas un valore compreso tra 0 e 100 per impostare il volume audio del mixer.

Library "libasound:2.0.0"

' int snd_mixer_open (snd_mixer_t **mixer, int mode)
' Opens an empty mixer.
Private Extern snd_mixer_open(mixer As Pointer, mode As Integer) As Integer

' int snd_mixer_attach (snd_mixer_t *mixer, const char *name)
' Attach an HCTL specified with the CTL device name to an opened mixer.
Private Extern snd_mixer_attach(mixer As Pointer, name As String) As Integer

' int snd_mixer_selem_register (snd_mixer_t *mixer, struct snd_mixer_selem_regopt *options, snd_mixer_class_t **classp)
' Register mixer simple element class.
Private Extern snd_mixer_selem_register(mixer As Pointer, options As Pointer, classp As Pointer) As Integer

' int snd_mixer_load (snd_mixer_t *mixer)
' Load a mixer elements.
Private Extern snd_mixer_load(mixer As Pointer) As Integer

' int snd_mixer_selem_id_malloc (snd_mixer_selem_id_t **ptr)
' Allocate an invalid snd_mixer_selem_id_t using standard malloc.
Private Extern snd_mixer_selem_id_malloc(ptr As Pointer) As Integer

' void snd_mixer_selem_id_set_index (snd_mixer_selem_id_t *obj, unsigned int val)
' Set index part of a mixer simple element identifier.
Private Extern snd_mixer_selem_id_set_index(obj As Pointer, ival As Integer)

' void snd_mixer_selem_id_set_name (snd_mixer_selem_id_t *obj, const char *val)
' Set name part of a mixer simple element identifier.
Private Extern snd_mixer_selem_id_set_name(obj As Pointer, sval As String)

' snd_mixer_elem_t * snd_mixer_find_selem (snd_mixer_t *mixer, const snd_mixer_selem_id_t *id)
' Find a mixer simple element.
Private Extern snd_mixer_find_selem(mixer As Pointer, id As Pointer) As Pointer

' int snd_mixer_selem_get_playback_volume_range (snd_mixer_elem_t *elem, long *min, long *max)
' Get range for playback volume of a mixer simple element.
Private Extern snd_mixer_selem_get_playback_volume_range(elem As Pointer, lmin As Long, lmax As Long) As Integer

' int snd_mixer_selem_set_playback_volume_all (snd_mixer_elem_t *elem, long value)
' Set value of playback volume control for all channels of a mixer simple element.
Private Extern snd_mixer_selem_set_playback_volume_all(elem As Pointer, value As Long) As Integer

' int snd_mixer_close (snd_mixer_t *mixer)
' Close a mixer and free all related resources.
Private Extern snd_mixer_close(mixer As Pointer) As Integer


Public Sub Main()
 
 Dim handle, sid, el As Pointer
 Dim err As Integer
 Dim vol As String
 Dim volume, mini, maxi As Long
 
  err = snd_mixer_open(VarPtr(handle), 0)
  If err < 0 Then Error.Raise("Impossibile inizializzare il sub-sistema 'Mixer' di ALSA !")
 
  snd_mixer_attach(handle, "default")
  snd_mixer_selem_register(handle, 0, 0)
  snd_mixer_load(handle)
  
  snd_mixer_selem_id_malloc(VarPtr(sid))
  snd_mixer_selem_id_set_index(sid, 0)
  snd_mixer_selem_id_set_name(sid, "Master")
  el = snd_mixer_find_selem(handle, sid)
  
  Print "Immettere un valore (0 - 100) per impostare il volume."
  Input vol
  volume = CLong(Val(vol))
  
  snd_mixer_selem_get_playback_volume_range(el, VarPtr(mini), VarPtr(maxi))
  snd_mixer_selem_set_playback_volume_all(el, volume * maxi / 100)
  
  
' Va in chiusura:
  snd_mixer_close(handle)
  
End



Riferimenti