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

Da Gambas-it.org - Wikipedia.
Riga 10: Riga 10:
 
  Private Extern snd_card_next(card As Pointer) As Integer
 
  Private Extern snd_card_next(card As Pointer) As Integer
 
   
 
   
  <FONT color=gray>' ''int   snd_ctl_open (snd_ctl_t **ctl, const char *name, int mode)
+
  <FONT color=gray>' ''int snd_ctl_open (snd_ctl_t **ctl, const char *name, int mode)
 
  ' ''Opens a CTL.''</font>
 
  ' ''Opens a CTL.''</font>
 
  Private Extern snd_ctl_open(card As Pointer, name As String, mode As Integer) As Integer
 
  Private Extern snd_ctl_open(card As Pointer, name As String, mode As Integer) As Integer
 
   
 
   
  <FONT color=gray>' ''int   snd_ctl_rawmidi_next_device (snd_ctl_t *ctl, int *device)''
+
  <FONT color=gray>' ''int snd_ctl_rawmidi_next_device (snd_ctl_t *ctl, int *device)''
 
  ' ''Get Next RawMidi device number.''</font>
 
  ' ''Get Next RawMidi device number.''</font>
 
  Private Extern snd_ctl_rawmidi_next_device(ctl As Pointer, device As Pointer) As Integer
 
  Private Extern snd_ctl_rawmidi_next_device(ctl As Pointer, device As Pointer) As Integer
 
   
 
   
  <FONT color=gray>' ''int   snd_rawmidi_info_malloc (snd_rawmidi_info_t **ptr)''
+
  <FONT color=gray>' ''int snd_rawmidi_info_malloc (snd_rawmidi_info_t **ptr)''
 
  ' ''Allocate a New Snd_rawmidi_info_t structure.''</font>
 
  ' ''Allocate a New Snd_rawmidi_info_t structure.''</font>
 
  Private Extern snd_rawmidi_info_malloc(ptr As Pointer) As Integer
 
  Private Extern snd_rawmidi_info_malloc(ptr As Pointer) As Integer

Versione delle 13:09, 28 mag 2014

Alcune funzioni esterne del API di Alsa consentono di conoscere i dispositivi Midi dell'hardware audio presenti nel proprio sistema, con i quali poter ricevere ed inviare dati Midi grezzi.


Mostriamo di seguito un esempio:

Library "libasound:2"


' int snd_card_next (int *card)
' Try To determine the Next card.
Private Extern snd_card_next(card As Pointer) As Integer

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

' int snd_ctl_rawmidi_next_device (snd_ctl_t *ctl, int *device)
' Get Next RawMidi device number.
Private Extern snd_ctl_rawmidi_next_device(ctl As Pointer, device As Pointer) As Integer

' int snd_rawmidi_info_malloc (snd_rawmidi_info_t **ptr)
' Allocate a New Snd_rawmidi_info_t structure.
Private Extern snd_rawmidi_info_malloc(ptr As Pointer) As Integer

' size_t snd_rawmidi_info_sizeof (void)
' Get size Of the 'snd_rawmidi_info_t' structure in bytes.
Private Extern snd_rawmidi_info_sizeof() As Integer

' void snd_rawmidi_info_set_device (snd_rawmidi_info_t *obj, unsigned int val)
' Set rawmidi device number.
Private Extern snd_rawmidi_info_set_device(obj As Pointer, ValInt As Integer)

' void snd_rawmidi_info_set_stream (snd_rawmidi_info_t *obj, snd_rawmidi_stream_t val)
' Set rawmidi stream identifier.
Private Extern snd_rawmidi_info_set_stream(obj As Pointer, ValInt As Integer)

' void snd_rawmidi_info_set_subdevice (snd_rawmidi_info_t *obj, unsigned int val)
' Set rawmidi subdevice number.
Private Extern snd_rawmidi_info_set_subdevice(obj As Pointer, ValInt As Integer)

' int snd_ctl_rawmidi_info (snd_ctl_t *ctl, snd_rawmidi_info_t *info)
' Get info about a RawMidi device.
Private Extern snd_ctl_rawmidi_info(ctl As Pointer, info As Pointer) As Integer

' int snd_ctl_close (snd_ctl_t *ctl)
' Closes CTL handle.
Private Extern snd_ctl_close(ctl As Pointer) As Integer

' int snd_config_update_free_global (void)
' Frees the global configuration tree In snd_config.
Private Extern snd_config_update_free_global() As Integer

' void *memset(void *str, int c, size_t n)
' Copies the character c (an unsigned char) to the first n characters of the string pointed to by the argument str.
Private Extern memset(StrP As Pointer, c As Integer, n As Integer) As Pointer In "libc:6"


Public Sub Main()

 Dim err, cardNum, devNum, subDevCount, i As Integer
 Dim cardHandle, rawMidiInfo As Pointer
 Dim $str As String
 Dim j, k As Byte
 Dim SND_RAWMIDI_STREAM_OUTPUT As Byte = 0
 

  Print "---- DISPOSITIVI HARDWARE MIDI PRESENTI ----\n"
 
  cardNum = -1
 
 
  While True

    err = snd_card_next(VarPtr(cardNum))
    If err < 0 Then Error.Raise("Impossibile ottenere il numero della scheda audio successiva !")
   
' Se non vi sono più schede, Alsa imposta "cardNum" a -1:
    If cardNum < 0 Then Exit
     
    $str = "hw:" & CStr(cardNum)

    err = snd_ctl_open(VarPtr(cardHandle), $Str, 0)
    If err < 0 Then Error.Raise("Impossibile aprire la connessione con la scheda audio !")
       
' Parte con il primo dispositivo Midi sulla scheda individuata:
    devNum = -1

     While True

' Ricava il numero del successivo dispositivo Midi sulla scheda attuale:
       err = snd_ctl_rawmidi_next_device(cardHandle, VarPtr(devNum))
       If err < 0 Then Error.Raise("Impossibile ottenere il numero del successivo dispositivo Midi !")

' Se non ci sono altri dispositivi Midi sulla scheda audio, Alsa imposta "devNum" a -1.
' NOTA: E' possibile che la scheda audio (ad esempio una scheda audio digitale) possa non avere dispositivi Midi:
       If devNum < 0 Then Exit
       
' Per ottenere informazioni sui sub-dispositivi del presente dispositivo Midi (sulla scheda),
' bisogna chiamare la funzione "snd_rawmidi_info_t", previa allocazione di memeoria sullo stack:
       snd_rawmidi_info_malloc(VarPtr(rawMidiInfo))
       memset(rawMidiInfo, 0, snd_rawmidi_info_sizeof())
         
' Si dice ad Alsa di quale numero di dispositivo vogliamo ottenere informazioni:
       snd_rawmidi_info_set_device(rawMidiInfo, devNum)
        
' Si ottengono le informazioni Midi del dispositivo:
       snd_rawmidi_info_set_stream(rawMidiInfo, SND_RAWMIDI_STREAM_OUTPUT)
       
       i = -1
       
       subDevCount = 1
       
       While (i < subDevCount - 1)
         Inc i
' Si dice ad Alsa di riempire la funzione "our snd_rawmidi_info_t" con le informazioni sul presente sub-dispositivo:
         snd_rawmidi_info_set_subdevice(rawMidiInfo, i)
         err = snd_ctl_rawmidi_info(cardHandle, rawMidiInfo)
         If err < 0 Then Error.Raise("Impossibile ottenere informazioni dal subdispositivo Midi output !")
             
' NOTA: Se v'è un solo subdevice, allora il numero del subdispositivo è virtuale,
' e può essere omesso quando viene specificato il nome dell'hardware Midi:
         If subDevCount > 1 Then
           Print "hw:"; cardNum; ","; devNum; ","; i; Chr(10)
         Else
           Print"hw:"; cardNum; ","; devNum; Chr(10)
         Endif
       Wend
       
       
     Wend

    snd_ctl_close(cardHandle)
   
  Wend

  snd_config_update_free_global()

End

Per vedere il funzionamento pratico di questo codice esemplificativo, lanciare il codice dopo aver collegato una tastiera Midi estern al proprio computer.