Differenze tra le versioni di "Conoscere con le funzioni esterne di ALSA i dispositivi audio presenti nel sistema"

Da Gambas-it.org - Wikipedia.
Riga 2: Riga 2:
  
  
Mostriamo di segtuito qualche esempio:
+
Mostriamo di seguito un esempio:
 
  Private Const SND_PCM_STREAM_PLAYBACK As Byte = 0  <FONT color=gray>' ''Playback stream''</font>
 
  Private Const SND_PCM_STREAM_PLAYBACK As Byte = 0  <FONT color=gray>' ''Playback stream''</font>
 
  Private Const ENOENT As Byte = 2
 
  Private Const ENOENT As Byte = 2

Versione delle 09:33, 28 mag 2014

Alcune funzioni esterne del API di ALSA consentono di conoscere quali sono le porte di entrata e di uscita dell'audio digitale delle schede audio presenti nel proprio sistema.


Mostriamo di seguito un esempio:

Private Const SND_PCM_STREAM_PLAYBACK As Byte = 0   ' Playback stream
Private Const ENOENT As Byte = 2
Private handlCtl As Pointer


Library "libasound:2"

' int snd_card_next(int *card)
' Try to determine the next card.
Private Extern snd_card_next(cardI As Pointer) As Integer

' const char* snd_pcm_stream_name(snd_pcm_stream_t stream)
' Get name of PCM stream type
Private Extern snd_pcm_stream_name(streamI As Byte) As String

' int snd_ctl_open(snd_ctl_t **ctl, const char *name, int mode)
' Opens a CTL.
Private Extern snd_ctl_open(ctlP As Pointer, name As String, mode As Integer) As Integer

' snd_ctl_card_info_malloc(ptr)
' Allocate an invalid snd_ctl_card_info_t using standard malloc
Private Extern snd_ctl_card_info_malloc(ptr As Pointer)

' int snd_ctl_card_info(snd_ctl_t *ctl, snd_ctl_card_info_t *info)
' Get card related information.
Private Extern snd_ctl_card_info(ctlP As Pointer, infoP As Pointer) As Integer

' void snd_ctl_close(snd_ctl_t *ctl)
' Closes a ctl.
Private Extern snd_ctl_close(ctlP As Pointer)

' int snd_ctl_pcm_next_device(snd_ctl_t *ctl, int *device)
' Get Next PCM device number.
Private Extern snd_ctl_pcm_next_device(ctlP As Pointer, device As Pointer) As Integer

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

' void snd_pcm_info_set_device (snd_pcm_info_t *obj, unsigned int val)
' Set wanted device inside a PCM info container.
Private Extern snd_pcm_info_set_device(pcminfoP As Pointer, ValI As Integer)

' void snd_pcm_info_set_subdevice(snd_pcm_info_t *obj, unsigned int val)
' Set wanted subdevice inside a PCM info container.
Private Extern snd_pcm_info_set_subdevice(pcminfoP As Pointer, ValI As Integer)

' void snd_pcm_info_set_stream(snd_pcm_info_t *obj, snd_pcm_stream_t val)
' Set wanted stream inside a PCM info container.
Private Extern snd_pcm_info_set_stream(pcminfoP As Pointer, valInt As Integer)

' int snd_ctl_pcm_info (snd_ctl_t *ctl, snd_pcm_info_t *info)
' Get info about a PCM device.
Private Extern snd_ctl_pcm_info(ctlP As Pointer, pcminfoP As Pointer) As Integer

' const char * snd_ctl_card_info_get_id (const snd_ctl_card_info_t *obj)
' Get card identifier From a CTL card info.
Private Extern snd_ctl_card_info_get_id(infoP As Pointer) As String

' const char * snd_ctl_card_info_get_name (const snd_ctl_card_info_t *obj)
' Get card name From a CTL card info.
Private Extern snd_ctl_card_info_get_name(infoP As Pointer) As String

' const char* snd_pcm_info_get_id(const snd_pcm_info_t * obj)
' Get id from a PCM info container.
Private Extern snd_pcm_info_get_id(pcminfoP As Pointer) As String

' const char* snd_pcm_info_get_name (const snd_pcm_info_t * obj)
' Get name From a PCM info container.
Private Extern snd_pcm_info_get_name(pcminfoP As Pointer) As String

' unsigned int  snd_pcm_info_get_subdevices_count (const snd_pcm_info_t *obj)
' Get subdevices count From a PCM info container.
Private Extern snd_pcm_info_get_subdevices_count(pcminfoP As Pointer) As Integer

' unsigned int snd_pcm_info_get_subdevices_avail (const snd_pcm_info_t *obj)
' Get available subdevices count From a PCM info container.
Private Extern snd_pcm_info_get_subdevices_avail(pcminfoP As Pointer) As Integer

' const char * snd_pcm_info_get_subdevice_name (const snd_pcm_info_t *obj)
' Get subdevice name From a PCM info container.
Private Extern snd_pcm_info_get_subdevice_name(pcminfoP As Pointer) As String

' const char * snd_strerror (int errnum)
' Returns the message For an Error code.
Private Extern snd_strerror(errore As Integer) As String


Public Sub Main()
 
 Dim info, pcminfo As Pointer
 Dim card, dev, count, avail, idx, err As Integer
 Dim name, card_id, card_name, pcm_id, pcm_name, subdevice_name As String

 
  snd_ctl_card_info_malloc(VarPtr(info))
  snd_pcm_info_malloc(VarPtr(pcminfo))
   
  card = -1

  If (snd_card_next(VarPtr(card)) < 0) Or (card < 0) Then Error.Raise("Nessuna scheda audio trovata !")
    
    Print "******* LISTA DEI DISPOSITIVI HARDWARE DI RIPRODUZIONE AUDIO *******"
   
  snd_pcm_stream_name(SND_PCM_STREAM_PLAYBACK)

   
  While card >= 0

    name = "hw:" & card

    err = snd_ctl_open(VarPtr(handlCtl), name, 0)
    If err < 0 Then
      Error("Errore nell'apertura del Controllo: " & card & ":" & snd_strerror(err))
      Goto next_card
    Endif
     
    err = snd_ctl_card_info(handlCtl, info)
    If err < 0 Then
      Error("Informazione sull'hardware del controllo: " & card & ":" & snd_strerror(err))
      snd_ctl_close(handlCtl)
      Goto next_card
    Endif
     
    dev = -1
     
    While True

      If snd_ctl_pcm_next_device(handlCtl, VarPtr(dev)) < 0 Then Error.Raise("Errore alla funzione 'snd_ctl_pcm_next_device' !")

      If dev < 0 Then Break
       
      snd_pcm_info_set_device(pcminfo, dev)
       
      snd_pcm_info_set_subdevice(pcminfo, 0)
       
      snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK)

      
      err = snd_ctl_pcm_info(handlCtl, pcminfo)

      If err < 0 Then
        If err <> - ENOENT Then Error("Informazione sull'audio digitale del controllo: " & card & " " & snd_strerror(err))
        Continue
      Endif
       
''''''''

      card_id = snd_ctl_card_info_get_id(info)
       
      card_name = snd_ctl_card_info_get_name(info)
       
      pcm_id = snd_pcm_info_get_id(pcminfo)
       
      pcm_name = snd_pcm_info_get_name(pcminfo)
       
      Print "_______________________________________________________________________________________\n"
      Print "DISPOSITIVO AUDIO: "; name; "  "; card_id; " ["; card_name; "]    Device: "; dev; "  "; pcm_id; " ["; pcm_name; "]\n" 
       
      count = snd_pcm_info_get_subdevices_count(pcminfo)
       
      avail = snd_pcm_info_get_subdevices_avail(pcminfo)
       
      Print "Subdevices: "; avail; "/"; count; Chr(10)

      For idx = 0 To count - 1
        snd_pcm_info_set_subdevice(pcminfo, idx)
        err = snd_ctl_pcm_info(handlCtl, pcminfo)
        If err < 0 Then
          Error.Raise("Errore nella Riproduzione audio digitale del controllo: " & card & " " & snd_strerror(err))
        Else
          subdevice_name = snd_pcm_info_get_subdevice_name(pcminfo)
          Print "Subdevice: #"; idx; ":  "; subdevice_name
        Endif
      Next
        
        
    Wend
      snd_ctl_close(handlCtl)
      next_card:
      If snd_card_next(VarPtr(card)) < 0 Then
        Error.Raise("Errore in 'next_card' !")
        Break
      Endif
  Wend

End