Differenze tra le versioni di "Alsa e Gambas: Client e porte in Ricezione"

Da Gambas-it.org - Wikipedia.
Riga 8: Riga 8:
 
   Me.Center
 
   Me.Center
 
   
 
   
   <Font Color= #006400>' ''creare ("istanziare") la classe per poterla usare''</font>
+
   <Font Color=gray>' ''creare ("istanziare") la classe per poterla usare''</font>
 
   alsa = New CAlsa As "alsa"
 
   alsa = New CAlsa As "alsa"
 
    
 
    
   <Font Color= #006400>' ''aprire alsa e assegnare un nome''</font>
+
   <Font Color=gray>' ''aprire alsa e assegnare un nome''</font>
 
   alsa.alsa_open("Applicativo in Ricezione dati")
 
   alsa.alsa_open("Applicativo in Ricezione dati")
 
    
 
    
Riga 18: Riga 18:
  
 
<P>Nella classe secondaria CAlsa.class richiameremo e porremo sostanzialmente tutte le funzioni, la libreria e le necessarie dichiarazioni di variabili che abbiamo conosciuto nel precedente progetto per l'invio dei dati Midi:</p>
 
<P>Nella classe secondaria CAlsa.class richiameremo e porremo sostanzialmente tutte le funzioni, la libreria e le necessarie dichiarazioni di variabili che abbiamo conosciuto nel precedente progetto per l'invio dei dati Midi:</p>
 
<Font Color= #006400>' ''Gambas-3 class file''</font>
 
 
 
 
  Public handle As Pointer
 
  Public handle As Pointer
 
  Private id As Integer
 
  Private id As Integer
Riga 28: Riga 24:
 
   Library "libasound:2"
 
   Library "libasound:2"
 
   
 
   
  Const SND_SEQ_OPEN_DUPLEX As Integer = 3
+
  Private Const SND_SEQ_OPEN_DUPLEX As Integer = 3
   
+
  Private Const SND_SEQ_PORT_CAP_WRITE As Integer = 2
<Font Color= #006400>' ''#define SND_SEQ_PORT_CAP_WRITE (1<<1)''</font>
+
  Private Const SND_SEQ_PORT_TYPE_MIDI_GENERIC As Integer = 2
Const SND_SEQ_PORT_CAP_WRITE As Integer = 2
+
  Private Const SND_SEQ_PORT_TYPE_APPLICATION As Integer = 1048576
   
+
 
<Font Color= #006400>' ''#define SND_SEQ_PORT_TYPE_MIDI_GENERIC  (1<<1)''</font>
+
  <Font Color=gray>' ''int snd_seq_open(snd_seq_t **seqp, const char * name, Int streams, Int mode)''
Const SND_SEQ_PORT_TYPE_MIDI_GENERIC As Integer = 2
+
' ''Open the ALSA sequencer.''</font>
   
 
<Font Color= #006400>' ''#define SND_SEQ_PORT_TYPE_MIDI_GENERIC  (1<<20)''</font>
 
Const SND_SEQ_PORT_TYPE_APPLICATION As Integer = 1048576
 
 
 
  <Font Color= #006400>' ''int snd_seq_open(snd_seq_t **seqp, const char * name, Int streams, Int mode)''</font>
 
 
  Private Extern snd_seq_open(Pseq As Pointer, name As String, streams As Integer, mode As Integer) As Integer
 
  Private Extern snd_seq_open(Pseq As Pointer, name As String, streams As Integer, mode As Integer) As Integer
 
   
 
   
  <Font Color= #006400>' ''int snd_seq_set_client_name(snd_seq_t* seq, const char* name)''</font>
+
  <Font Color=gray>' ''int snd_seq_set_client_name(snd_seq_t* seq, const char* name)''
 +
' ''Set client name.''</font>
 
  Private Extern snd_seq_set_client_name(seq As Pointer, name As String) As Integer
 
  Private Extern snd_seq_set_client_name(seq As Pointer, name As String) As Integer
 
   
 
   
  <Font Color= #006400>' ''int snd_seq_create_simple_port(snd_seq_t* seq, const char* name, unsigned int caps, unsigned int type)''</font>
+
  <Font Color=gray>' ''int snd_seq_create_simple_port(snd_seq_t* seq, const char* name, unsigned int caps, unsigned int type)''
 +
' ''Create a port - simple version.''</font>
 
  Private Extern snd_seq_create_simple_port(seq As Pointer, name As String, caps As Integer, type As Integer) As Integer
 
  Private Extern snd_seq_create_simple_port(seq As Pointer, name As String, caps As Integer, type As Integer) As Integer
 
   
 
   
  <Font Color= #006400>' ''int snd_seq_client_id(snd_seq_t * seq)''</font>
+
  <Font Color=gray>' ''int snd_seq_client_id(snd_seq_t * seq)''
 +
' ''Get the client id.''</font>
 
  Private Extern snd_seq_client_id(seq As Pointer) As Integer
 
  Private Extern snd_seq_client_id(seq As Pointer) As Integer
 
   
 
   
  <Font Color= #006400>' ''int snd_seq_connect_from(seq as pointer, myport as integer, src_client as integer, src_port as integer)''</font>
+
  <Font Color=gray>' ''int snd_seq_connect_from(seq as pointer, myport as integer, src_client as integer, src_port as integer)''
 +
' ''Simple subscription (w/o exclusive & time conversion).''</font>
 
  Private Extern snd_seq_connect_from(seq As Pointer, myport As Integer, src_client As Integer, src_port As Integer) As Integer
 
  Private Extern snd_seq_connect_from(seq As Pointer, myport As Integer, src_client As Integer, src_port As Integer) As Integer
 
   
 
   
 
   
 
   
  '''Public''' Sub alsa_open(myname As String)
+
  '''Public''' Sub alsa_open(nome As String)
 
   Dim err As Integer
 
   Dim err As Integer
 
    
 
    
Riga 63: Riga 57:
 
   If err < 0 Then error.RAISE("Errore nell'apertura di ALSA !")      <Font Color= #006400>' ''gestione dell'errore''</font>
 
   If err < 0 Then error.RAISE("Errore nell'apertura di ALSA !")      <Font Color= #006400>' ''gestione dell'errore''</font>
 
      
 
      
   snd_seq_set_client_name(handle, myname)
+
   snd_seq_set_client_name(handle, nome)
 
   id = snd_seq_client_id(handle)
 
   id = snd_seq_client_id(handle)
 
   Print "Alsa Client-ID = "; id
 
   Print "Alsa Client-ID = "; id
 
    
 
    
   <Font Color= #006400>' ''per poter leggere la propria porta, essa viene posta con capacità "Write", ossia "scrivibile" da parte dell'Altro dispositivo''</font>
+
   <Font Color=gray>' ''per poter leggere la propria porta, essa viene posta con capacità "Write", ossia "scrivibile" da parte dell'Altro dispositivo''</font>
 
   err = snd_seq_create_simple_port(handle, "Porta applicativo", SND_SEQ_PORT_CAP_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC + SND_SEQ_PORT_TYPE_APPLICATION)
 
   err = snd_seq_create_simple_port(handle, "Porta applicativo", SND_SEQ_PORT_CAP_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC + SND_SEQ_PORT_TYPE_APPLICATION)
 
   Print "Numero della porta input dell'applicazione = "; err
 
   Print "Numero della porta input dell'applicazione = "; err
Riga 73: Riga 67:
 
   inport = err
 
   inport = err
 
      
 
      
  <Font Color= #006400>' ''si pongono: 14 (id del sistema ALSA) e 0 ( num. della sua porta)''</font>
+
  <Font Color=gray>' ''si pongono: 14 (id del sistema ALSA) e 0 ( num. della sua porta)''</font>
  <Font Color= #006400>' '''''per connettere il nostro client ad Alsa e "ricevere" dati direttamente da essa.'''''</font>
+
  <Font Color=gray>' '''''per connettere il nostro client ad Alsa e "ricevere" dati direttamente da essa.'''''</font>
 
   err = snd_seq_connect_from(handle, inport, 14, 0)       
 
   err = snd_seq_connect_from(handle, inport, 14, 0)       
 
   printerr("Subscribe inport", err)
 
   printerr("Subscribe inport", err)
Riga 84: Riga 78:
 
<P>Quindi inseriremo le funzioni per la gestione degli errori:</p>
 
<P>Quindi inseriremo le funzioni per la gestione degli errori:</p>
  
  <Font Color= #006400>''' ###  GESTIONE DELL'ERRORE  ### ''</font>
+
  <Font Color=gray>''' ###  GESTIONE DELL'ERRORE  ### ''</font>
 
   
 
   
 
  Private Extern snd_strerror(err As Integer) As Pointer
 
  Private Extern snd_strerror(err As Integer) As Pointer

Versione delle 10:36, 1 mar 2016

Per la creazione del nostro Client e delle sue porte e per consentirne il collegamento ad ALSA, nella classe principale FMain.class sciveremo le seguenti righe:

Public alsa As CAlsa               ' classe che incapsula le funzioni ALSA


Public Sub Form_Open()

 Me.Center

 ' creare ("istanziare") la classe per poterla usare
 alsa = New CAlsa As "alsa"
 
 ' aprire alsa e assegnare un nome
 alsa.alsa_open("Applicativo in Ricezione dati")
 
End


Nella classe secondaria CAlsa.class richiameremo e porremo sostanzialmente tutte le funzioni, la libreria e le necessarie dichiarazioni di variabili che abbiamo conosciuto nel precedente progetto per l'invio dei dati Midi:

Public handle As Pointer
Private id As Integer
Private inport As Integer
 
 Library "libasound:2"

Private Const SND_SEQ_OPEN_DUPLEX As Integer = 3
Private Const SND_SEQ_PORT_CAP_WRITE As Integer = 2
Private Const SND_SEQ_PORT_TYPE_MIDI_GENERIC As Integer = 2
Private Const SND_SEQ_PORT_TYPE_APPLICATION As Integer = 1048576
 
' int snd_seq_open(snd_seq_t **seqp, const char * name, Int streams, Int mode)
' Open the ALSA sequencer.
Private Extern snd_seq_open(Pseq As Pointer, name As String, streams As Integer, mode As Integer) As Integer

' int snd_seq_set_client_name(snd_seq_t* seq, const char* name)
' Set client name.
Private Extern snd_seq_set_client_name(seq As Pointer, name As String) As Integer

' int snd_seq_create_simple_port(snd_seq_t* seq, const char* name, unsigned int caps, unsigned int type)
' Create a port - simple version.
Private Extern snd_seq_create_simple_port(seq As Pointer, name As String, caps As Integer, type As Integer) As Integer

' int snd_seq_client_id(snd_seq_t * seq)
' Get the client id.
Private Extern snd_seq_client_id(seq As Pointer) As Integer

' int snd_seq_connect_from(seq as pointer, myport as integer, src_client as integer, src_port as integer)
' Simple subscription (w/o exclusive & time conversion).
Private Extern snd_seq_connect_from(seq As Pointer, myport As Integer, src_client As Integer, src_port As Integer) As Integer


Public Sub alsa_open(nome As String)
 Dim err As Integer
 
 err = snd_seq_open(VarPtr(handle), "default", SND_SEQ_OPEN_DUPLEX, 0)
 printerr("Apertura di Alsa regolare !", err)
 If err < 0 Then error.RAISE("Errore nell'apertura di ALSA !")      ' gestione dell'errore
   
 snd_seq_set_client_name(handle, nome)
 id = snd_seq_client_id(handle)
 Print "Alsa Client-ID = "; id
 
 ' per poter leggere la propria porta, essa viene posta con capacità "Write", ossia "scrivibile" da parte dell'Altro dispositivo
 err = snd_seq_create_simple_port(handle, "Porta applicativo", SND_SEQ_PORT_CAP_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC + SND_SEQ_PORT_TYPE_APPLICATION)
 Print "Numero della porta input dell'applicazione = "; err
 If err < 0 Then error.Raise("Errore nella creazione della porta !")      ' gestione dell'errore
 inport = err
   
' si pongono: 14 (id del sistema ALSA) e 0 ( num. della sua porta)
' per connettere il nostro client ad Alsa e "ricevere" dati direttamente da essa.
 err = snd_seq_connect_from(handle, inport, 14, 0)      
 printerr("Subscribe inport", err)
 If err < 0 Then error.Raise("Error subscribe input device")      ' gestione dell'errore

End


Quindi inseriremo le funzioni per la gestione degli errori:

' ###   GESTIONE DELL'ERRORE   ### 

Private Extern snd_strerror(err As Integer) As Pointer

Public Sub errmsg(err As Integer) As String

   Return String@(snd_strerror(err))

End


Private Sub printerr(operation As String, err As Integer)
   If err < 0 Then Print operation; ": err="; err; " ("; errmsg(err); ")"
End


Ricevere messaggi Midi da una tastiera o altro dispositivo Midi esterno

Se si intende, invece, ricevere messaggi Midi da una tastiera o altro dispositivo Midi esterno, dopo averlo collegato alla porta USB, si verificherà in console mediante il comando cat /proc/asound/seq/clients il suo numero identificativo e la sua porta. Quindi, se, per esempio, il numero identificativo della nostra tastiera esterna è 24 ed il numero della la sua porta è 0, bisognerà sostituire i parametri relativi al dispositivo sorgente nell'apposita funzione:

err = snd_seq_connect_from(handle, inport, 24, 0)