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 seguito un esempio:
+
Mostriamo un esempio, nel quale sul ''Form'' dell'applicativo porremo una ''ListBox''.
 +
 
 +
Il codice può essere il seguente:
 
  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
Riga 89: Riga 91:
 
  ' ''Returns the message For an Error code.''</font>
 
  ' ''Returns the message For an Error code.''</font>
 
  Private Extern snd_strerror(errore As Integer) As String
 
  Private Extern snd_strerror(errore As Integer) As String
 +
 
 
   
 
   
 +
'''Public''' Sub Form_Open()
 
   
 
   
'''Public''' Sub Main()
+
  Me.show
 
    
 
    
 +
  Do
 +
    Wait 0.1
 +
  Loop Until Me.Id <> 0
 +
 +
  Lista_Dispositivi()
 +
 +
'''End'''
 +
 +
 +
'''Private''' Procedure Lista_Dispositivi()
 +
 
   Dim info, pcminfo As Pointer
 
   Dim info, pcminfo As Pointer
 
   Dim card, dev, count, avail, idx, err As Integer
 
   Dim card, dev, count, avail, idx, err As Integer
Riga 103: Riga 118:
 
   card = -1
 
   card = -1
 
   
 
   
   If (snd_card_next(VarPtr(card)) < 0) Or (card < 0) Then Error.Raise("Nessuna scheda audio trovata !")
+
   If (snd_card_next(VarPtr(card)) < 0) Or (card < 0) Then
      
+
    Error("Nessuna scheda audio trovata !")
    Print "******* LISTA DEI DISPOSITIVI HARDWARE DI RIPRODUZIONE AUDIO *******"
+
     Return
 +
  Endif
 +
 +
  ListBox1.Add("******* LISTA DEI DISPOSITIVI HARDWARE DI RIPRODUZIONE AUDIO *******")
 
      
 
      
 
   snd_pcm_stream_name(SND_PCM_STREAM_PLAYBACK)
 
   snd_pcm_stream_name(SND_PCM_STREAM_PLAYBACK)
 
   
 
   
   
 
 
   While card >= 0
 
   While card >= 0
 
   
 
   
Riga 116: Riga 133:
 
     err = snd_ctl_open(VarPtr(handlCtl), name, 0)
 
     err = snd_ctl_open(VarPtr(handlCtl), name, 0)
 
     If err < 0 Then
 
     If err < 0 Then
       Error("Errore nell'apertura del Controllo: " & card & ":" & snd_strerror(err))
+
       Error("Apertura del Controllo: " & card & ":" & snd_strerror(err))
 
       Goto next_card
 
       Goto next_card
 
     Endif
 
     Endif
     
+
   
 
     err = snd_ctl_card_info(handlCtl, info)
 
     err = snd_ctl_card_info(handlCtl, info)
 
     If err < 0 Then
 
     If err < 0 Then
Riga 131: Riga 148:
 
     While True
 
     While True
 
   
 
   
       If snd_ctl_pcm_next_device(handlCtl, VarPtr(dev)) < 0 Then Error.Raise("Errore alla funzione 'snd_ctl_pcm_next_device' !")
+
       If snd_ctl_pcm_next_device(handlCtl, VarPtr(dev)) < 0 Then Error("Errore alla funzione 'snd_ctl_pcm_next_device' !")
 
   
 
   
 
       If dev < 0 Then Break
 
       If dev < 0 Then Break
       
+
     
 
       snd_pcm_info_set_device(pcminfo, dev)
 
       snd_pcm_info_set_device(pcminfo, dev)
 
          
 
          
Riga 141: Riga 158:
 
       snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK)
 
       snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK)
 
   
 
   
     
 
 
       err = snd_ctl_pcm_info(handlCtl, pcminfo)
 
       err = snd_ctl_pcm_info(handlCtl, pcminfo)
 
   
 
   
 
       If err < 0 Then
 
       If err < 0 Then
         If err <> - ENOENT Then Error("Informazione sull'audio digitale del controllo: " & card & " " & snd_strerror(err))
+
         If err <> - ENOENT Then Error.Raise("Informazione sull'audio digitale del controllo: " & card & " " & snd_strerror(err))
 
         Continue
 
         Continue
 
       Endif
 
       Endif
 
          
 
          
<FONT color=gray>'''''''''''''</font>
 
 
 
       card_id = snd_ctl_card_info_get_id(info)
 
       card_id = snd_ctl_card_info_get_id(info)
 
          
 
          
Riga 159: Riga 173:
 
       pcm_name = snd_pcm_info_get_name(pcminfo)
 
       pcm_name = snd_pcm_info_get_name(pcminfo)
 
          
 
          
       Print "_______________________________________________________________________________________\n"
+
       With ListBox1
      Print "DISPOSITIVO AUDIO: "; name; "  "; card_id; " ["; card_name; "]    Device: "; dev; "  "; pcm_id; " ["; pcm_name; "]\n"  
+
        .Add("_______________________________________________________________________________________")
       
+
        .Add(" ")
 +
        .Add("DISPOSITIVO AUDIO: " & name & "  " & card_id & " [" & card_name & "]    Device: " & dev & "  " & pcm_id & " [" & pcm_name & "]")
 +
        .Add(" ")
 +
      End With
 +
 
       count = snd_pcm_info_get_subdevices_count(pcminfo)
 
       count = snd_pcm_info_get_subdevices_count(pcminfo)
 
          
 
          
 
       avail = snd_pcm_info_get_subdevices_avail(pcminfo)
 
       avail = snd_pcm_info_get_subdevices_avail(pcminfo)
 
          
 
          
       Print "Subdevices: "; avail; "/"; count; Chr(10)
+
       With ListBox1
+
        .Add("Subdevices: " & avail & "/" & count)
 +
        .Add(" ")
 +
      End With
 +
       
 
       For idx = 0 To count - 1
 
       For idx = 0 To count - 1
 
         snd_pcm_info_set_subdevice(pcminfo, idx)
 
         snd_pcm_info_set_subdevice(pcminfo, idx)
 
         err = snd_ctl_pcm_info(handlCtl, pcminfo)
 
         err = snd_ctl_pcm_info(handlCtl, pcminfo)
 
         If err < 0 Then
 
         If err < 0 Then
           Error.Raise("Errore nella Riproduzione audio digitale del controllo: " & card & " " & snd_strerror(err))
+
           Error("Informazione sulla Riproduzione audio digitale del controllo: " & card & " " & snd_strerror(err))
 
         Else
 
         Else
 
           subdevice_name = snd_pcm_info_get_subdevice_name(pcminfo)
 
           subdevice_name = snd_pcm_info_get_subdevice_name(pcminfo)
           Print "Subdevice: #"; idx; ":  "; subdevice_name
+
           ListBox1.Add("Subdevice: #" & idx & ":  " & subdevice_name)
 
         Endif
 
         Endif
 
       Next
 
       Next
       
 
 
          
 
          
 
     Wend
 
     Wend
      snd_ctl_close(handlCtl)
+
      next_card:
+
    snd_ctl_close(handlCtl)
      If snd_card_next(VarPtr(card)) < 0 Then
+
    next_card:
        Error.Raise("Errore in 'next_card' !")
+
    If snd_card_next(VarPtr(card)) < 0 Then
        Break
+
      Error("Errore in 'next_card' !")
      Endif
+
      Break
 +
    Endif
 +
 
 
   Wend
 
   Wend
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione delle 19:20, 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 un esempio, nel quale sul Form dell'applicativo porremo una ListBox.

Il codice può essere il seguente:

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 Form_Open()

 Me.show
 
 Do
   Wait 0.1
 Loop Until Me.Id <> 0

 Lista_Dispositivi()

End


Private Procedure Lista_Dispositivi()

 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("Nessuna scheda audio trovata !")
    Return
  Endif

  ListBox1.Add("******* 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("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("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.Raise("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)
       
      With ListBox1
        .Add("_______________________________________________________________________________________")
        .Add(" ")
        .Add("DISPOSITIVO AUDIO: " & name & "  " & card_id & " [" & card_name & "]    Device: " & dev & "  " & pcm_id & " [" & pcm_name & "]")
        .Add(" ")
      End With

      count = snd_pcm_info_get_subdevices_count(pcminfo)
       
      avail = snd_pcm_info_get_subdevices_avail(pcminfo)
       
      With ListBox1
        .Add("Subdevices: " & avail & "/" & count)
        .Add(" ")
      End With
       
      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("Informazione sulla Riproduzione audio digitale del controllo: " & card & " " & snd_strerror(err))
        Else
          subdevice_name = snd_pcm_info_get_subdevice_name(pcminfo)
          ListBox1.Add("Subdevice: #" & idx & ":  " & subdevice_name)
        Endif
      Next
        
    Wend

    snd_ctl_close(handlCtl)
    next_card:
    If snd_card_next(VarPtr(card)) < 0 Then
      Error("Errore in 'next_card' !")
      Break
    Endif
 
  Wend

End