Differenze tra le versioni di "Alsa e Gambas: Invio dati con l'uso di una Classe specifica"

Da Gambas-it.org - Wikipedia.
(Creata pagina con "===Preambolo=== Prenderemo ora in considerazione l'uso di una Classe specifica, nella quale inseriremo le variabili che saranno successivamente riempite dai valori relativi a...")
 
 
(13 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
===Preambolo===
+
===Introduzione===
 
+
Per definire e gestire gli Eventi Midi di ALSA si può usare una Classe specifica, fornita di Proprietà in "lettura" e in "scrittura" che potranno essere usate per memorizzare i valori necessari dell'Evento Midi, secondo la Struttura ''[https://www.alsa-project.org/alsa-doc/alsa-lib/structsnd__seq__event.html snd_seq_event_t]'' prevista dal protocollo di ALSA.
Prenderemo ora in considerazione l'uso di una Classe specifica, nella quale inseriremo le variabili che saranno successivamente riempite dai valori relativi ai dati Midi e necessari per la definizione dell'evento Midi particolare. Dichiarando poi una variabile del tipo di quella Classe per ciascun evento Midi si avrà un'area di memoria utilizzabile. L'uso di tale Classe specifica sostituisce egregiamente e più dinamicamente la [[Strutture:_dichiarazione_ed_uso|Struttura]]. |[[#Note|1]]|
+
<BR>Dichiarando poi una variabile del tipo di quella Classe per ciascun Evento Midi ALSA, si avrà un'area di memoria utilizzabile.
 
+
<BR>L'uso di tale Classe specifica sostituisce egregiamente e più dinamicamente la [[Strutture:_dichiarazione_ed_uso|Struttura]]. <SUP>&#091;[[#Note|nota 1]]&#093;</sup>
  
  
 
=Scrittura dei dati dei messaggi Midi nella Classe specifica dei dati Midi=
 
=Scrittura dei dati dei messaggi Midi nella Classe specifica dei dati Midi=
  
Viene innanzitutto creata ed impostata una Classe modello specifica (che qui nel nostro esempio sarà chiamata ''CMidi.class'') che contiene, come già detto, tutte le variabili necessarie per la definizione di qualunque evento Midi.
+
==Impostando e usando le Proprietà della Classe specifica==
 
+
La ''Classe'' modello specifica per la gestione delle risorse di ALSA, che chiameremo "CAlsa", va creata con tutte le Proprietà necessarie per la definizione di qualunque Evento Midi di ALSA e sarà così organizzata:
  <FONT color=#006400>' ''Gambas class file''
+
  <FONT Color=#006400>' ''Definisce le Proprietà della Classe specifica:''</font>
  ' ''Questa è la Classe specifica per i dati midi: CMidi.class''</font>
+
Property Type As Byte
 +
Property Flags As Byte
 +
  Property Tag As Byte
 +
Property Queue As Byte
 +
Property Tick_o_Tv_sec As Integer
 +
Property Tv_nsec As Integer
 +
Property Source_client As Byte
 +
Property Source_port As Byte
 +
Property Dest_client As Byte
 +
Property Dest_port As Byte
 +
Property Channel As Byte
 +
Property Note As Byte
 +
Property Velocity As Byte
 +
Property Off_velocity As Byte
 +
Property Param As Integer
 +
Property Value As Integer
 +
 +
<FONT Color=#006400>' ''Definisce i simboli associati alle suddette Proprietà:''</font>
 +
Private $Type As Byte
 +
Private $Flags As Byte
 +
Private $Tag As Byte
 +
Private $Queue As Byte
 +
Private $Tick_o_Tv_sec As Integer
 +
Private $Tv_nsec As Integer
 +
Private $Source_client As Byte
 +
Private $Source_port As Byte
 +
Private $Dest_client As Byte
 +
Private $Dest_port As Byte
 +
Private $Channel As Byte
 +
Private $Note As Byte
 +
Private $Velocity As Byte
 +
Private $Off_velocity As Byte
 +
Private $Param As Integer
 +
Private $Value As Integer
 +
 +
Private Function Type_Read() As Byte
 +
  Return $Type
 +
End
 +
 +
Private Sub Type_Write(Value As Byte)
 +
  $Type = Value
 +
End
 +
 +
Private Function Flags_Read() As Byte
 +
  Return $Flags
 +
End
 +
 +
Private Sub Flags_Write(Value As Byte)
 +
  $Flags = Value
 +
End
 +
 +
Private Function Tag_Read() As Byte
 +
  Return $Tag
 +
End
 +
 +
Private Sub Tag_Write(Value As Byte)
 +
  $Tag = Value
 +
End
 +
 +
Private Function Queue_Read() As Byte
 +
  Return $Queue
 +
End
 +
 +
Private Sub Queue_Write(Value As Byte)
 +
  $Queue = Value
 +
End
 +
 +
Private Function Tick_o_Tv_sec_Read() As Integer
 +
  Return $Tick_o_Tv_sec
 +
End
 +
 +
Private Sub Tick_o_Tv_sec_Write(Value As Integer)
 +
  $Tick_o_Tv_sec = Value
 +
End
 +
 +
Private Function Tv_nsec_Read() As Integer
 +
  Return $Tv_nsec
 +
End
 +
 +
Private Sub Tv_nsec_Write(Value As Integer)
 +
  $Tv_nsec = Value
 +
End
 +
 +
Private Function Source_client_Read() As Byte
 +
  Return $Source_client
 +
End
 +
 +
Private Sub Source_client_Write(Value As Byte)
 +
  $Source_client = Value
 +
End
 +
 +
Private Function Source_port_Read() As Byte
 +
  Return $Source_port
 +
End
 +
 +
Private Sub Source_port_Write(Value As Byte)
 +
  $Source_port = Value
 +
End
 +
 +
Private Function Dest_client_Read() As Byte
 +
  Return $Dest_client
 +
End
 +
 +
Private Sub Dest_client_Write(Value As Byte)
 +
  $Dest_client = Value
 +
End
 +
 +
Private Function Dest_port_Read() As Byte
 +
  Return $Dest_port
 +
End
 +
 +
Private Sub Dest_port_Write(Value As Byte)
 +
  $Dest_port = Value
 +
End
 +
 +
Private Function Channel_Read() As Byte
 +
  Return $Channel
 +
End
 +
 +
Private Sub Channel_Write(Value As Byte)
 +
  $Channel = Value
 +
End
 +
 +
Private Function Note_Read() As Byte
 +
  Return $Note
 +
End
 +
 +
Private Sub Note_Write(Value As Byte)
 +
  $Note = Value
 +
End
 +
 +
Private Function Velocity_Read() As Byte
 +
  Return $Velocity
 +
End
 +
 +
Private Sub Velocity_Write(Value As Byte)
 +
  $Velocity = Value
 +
End
 +
 +
Private Function Off_velocity_Read() As Byte
 +
  Return $Off_velocity
 +
End
 +
 +
Private Sub Off_velocity_Write(Value As Byte)
 +
  $Off_velocity = Value
 +
End
 +
 +
Private Function Param_Read() As Integer
 +
  Return $Param
 +
End
 +
 +
Private Sub Param_Write(Value As Integer)
 +
  $Param = Value
 +
End
 
   
 
   
 +
Private Function Value_Read() As Integer
 +
  Return $Value
 +
End
 
   
 
   
  Public type As Byte              <FONT color=#006400>' ''byte n° 0''</font>
+
Private Sub Value_Write(Value As Integer)
  Public flag As Byte
+
  $Value = Value
  Public tag As Byte
+
End
  Public queue As Byte
 
  Public timestamp1 As Integer
 
  Public timestamp2 As Integer
 
  Public source_id As Byte
 
  Public source_porta As Byte
 
  Public dest_id As Byte
 
  Public dest_porta As Byte        <FONT color=#006400>' ''byte n° 15''</font>
 
    Public channel As Byte            <FONT color=#006400>' ''byte n° 16''</font>
 
    Public note As Byte              <FONT color=#006400>' ''byte n° 17''</font>
 
    Public velocity As Byte          <FONT color=#006400>' ''byte n° 18''</font>
 
    Public off_velocity As Byte      <FONT color=#006400>' ''byte n° 19''</font>
 
      Public param As Integer          <FONT color=#006400>' ''byte n° 20''</font>
 
      Public value As Integer          <FONT color=#006400>' ''byte n° 24''</font>
 
Le variabili del tipo di questa Classe potranno essere dichiarate all'interno della Classe ''CAlsa.class''.
 
  
  
  
=I messaggi Midi specifici=
+
==Esempio di codice==
===Preambolo===
+
Mostriamo un codice in ambiente garfico che prevede tre Classi:
  
Organizzeremo nella classe secondaria CAlsa.class le routine per la gestione dei messaggi Midi. Poiché per i valori specifici di ciascun messaggio è prevista, come già sappiamo, una disposizione all'interno della zona di memoria comune con altri messaggi, prevederemo - per ciascun gruppo di messaggi dalla struttura simile - delle variabili del tipo della Classe specifica ''CMidi'' apposite che vadano bene per i messaggi appartenenti a quel gruppo.
+
- la prima è una Classe secondaria, che chiameremo '''''EventoMidi''''', ed è la Classe specifica per la gestione degli Eventi Midi di ALSA e sarà identica al modello di Classe specifica di un ''Evento Midi ALSA'' sopra descritto.
  
V'è da sottolineare che nella classe ''CAlsa.class'' la funzione ''snd_seq_event_output'' va modificata inserendo come secondo parametro nella sua dichiarazione con la funzione ''Extern'' il nome della Classe modello ''CMidi'':
+
- la seconda è un'altra Classe secondaria, che chiameremo '''''CAlsa''''', ed è la Classe per la gestione delle risorse di ALSA. In questa Classe saranno attribuiti alla variabile del tipo della Classe specifica della gestione degli ''Eventi Midi'' alcuni valori.
Private Extern snd_seq_event_output(handle As Pointer, vC As <FONT color=#B22222>Cmidi</font>) As Integer
 
  
mentre all'interno delle routine, quando essa viene chiamata, va inserito il nome della variabile-classe specifica. Ad esempio:
+
- La terza Classe è quella principale e in essa saranno attribuiti alla variabile del tipo della Classe specifica della gestione degli ''Eventi Midi'' la maggior parte dei valori per il particolare ''Evento Midi'' in quel momento gestito.
  err = snd_seq_event_output(handle, <FONT color=#FF7000>ev_OnOffPoly</font>)
+
<BR>Il suo codice, che sarà il seguente, prevede che la ''temporizzazione'' degli ''Eventi Midi'', ivi gestiti, sia impostata in modalità ''tick time'' Midi:
 
+
Private strum As String[] = ["Acustic Grand Piano", "Bright Acustic Piano", "Electric Grand Piano", "Honky-tonk",
 
+
  "Electric Piano 1", "Electric Piano 2", "Harpsichord", "Clavinet", "Celesta", "Glockenspiel", "Music Box", "Vibraphone",
==Gruppo messaggi: <FONT Color= "red">NoteON - NoteOFF</font> - <FONT Color= "red">Polyphonic Aftertouch</font>==
+
  "Marimba", "Xylophone", "Tubular Bells", "Dulcimer", "Hammond Organ", "Percussive Organ", "Rock Organ", "Church Organ",
Questi tre messaggi Midi hanno tra loro in comune tre medesimi specifici dati posti ai byte num. 16, 17 e 18 della Classe specifica modello, e che abbiamo chiamato: ''channel'', ''note'' e ''velocity''.
+
  "Reed Organ", "Accordion", "Harmonica", "Tango Accordion", "Acoustic Guitar (nylon)", "Acoustic Guitar (steel)",
 
+
  "Electric Guitar (jazz)", "Electric Guitar (clean)", "Electric Guitar(muted)", "Overdriven Guitar", "Distortion Guitar",
<P>Dichiareremo nella classe ''CAlsa.class'' una variabile ''ad hoc'' per questi tre messaggi Midi del tipo della Classe specifica modello ''CMidi'', della quale ultima assumerà così l'organizzazione interna delle particolari variabili ed il tipo dei valori:</p>
+
  "Guitar Harmonics", "Acoustic Bass", "Electric Bass (finger)", "Electric Bass (pick)", "Fretless Bass", "Slap Bass 1",
 
+
  "Slap Bass 2", "Synth Bass 1", "Synth Bass 2", "Violin", "Viola", "Cello", "Contrabass", "Tremolo Strings",
  ev_OnOffPoly As New CMidi
+
  "Pizzicato Strings", "Orchestral Harp", "Timpani", "String Ensemble 1", "String Ensemble 2", "SynthStrings 1",
 
+
  "SynthStrings 2", "Choir Aahs", "Voice Oohs", "Synth Voice", "Orchestra Hit", "Trumpet", "Trombone", "Tuba", "Muted Trumpet",
<P>Ora la nostra variabile ''ev_OnOffPoly'' è operativa e può essere utilizzata per la gestione ed il successivo invio dei messaggi Midi ad ALSA.</p>
+
  "French Horn", "Brass Section", "Synth Brass 1", "Synth Brass 2", "Soprano Sax", "Alto Sax", "Tenor Sax", "Baritone Sax",
<P>L'uso di tale variabile-Classe si ha quando si dovrà definire ''concretamente'' il messaggio Midi richiesto, fissando in ciascun elemento costituivo (le diverse variabili in essa contenute) i relativi valori. Si potrà evitare di richiamare le varibili particolari della Classe che eventualmente non servono.</p>
+
  "Oboe", "English Horn", "Basson", "Clarinet", "Piccolo", "Flute", "Recorder", "Pan Flute", "Bottle Blow", "Shakuhachi",
 
+
  "Whistle", "Ocarina", "Lead 1 (square)", "Lead 2 (sawtooth)", "Lead 3 (caliope lead)", "Lead 4 (chiff lead)",
<P>Quindi dalla classe principale chiameremo la routine specifica per i tre eventi qui considerati, passandogli anche tre valori necessari:</p>
+
  "Lead 5 (charang)", "Lead 6 (voice)", "Lead 7 (fifths)", "Lead 8(brass+lead)", "Pad 1 (new age)", "Pad 2 (warm)",
  Public Sub eventoOnOffPoly(def_Evento As Byte, nota as byte, velocitas As Byte)
+
  "Pad 3 (polysynth)", "Pad 4 (choir)", "Pad 5 (bowed)", "Pad 6 (metallic)", "Pad 7 (halo)", "Pad 8 (sweep)", "FX 1 (rain)",
 +
  "FX 2 (soundtrack)", "FX 3 (crystal)", "FX 4 (atmosphere)", "FX 5 (brightness)", "FX 6 (goblins)", "FX 7 (echoes)",
 +
  "FX 8 (sci-fi)", "Sitar", "Banjo", "Shamisen", "Koto", "Kalimba", "Bagpipe", "Fiddle", "Shanai", "Tinkle Bell", "Agogo",
 +
  "Steel Drums", "Woodblock", "Taiko Drum", "Melodic Tom", "Synth Drum", "Reverse Cymbal", "Guitar Fret Noise", "Breath Noise",
 +
  "Seashore", "Bird Tweet", "Telephone Ring", "Helicopter", "Applause", "Gunshot"]
 +
 +
Private Const id_dev As Integer = 128            <FONT Color=gray>' ''14 = midi out oppure  128 (solitamente) = softsynth''</font>
 +
Private Const p_dev As Integer = 0                <FONT Color=gray>' ''Porta del Client destinatario dei dati Midi: solitamente 0''</font>
 +
Private Const SND_SEQ_TIME_STAMP_TICK As Integer = 0
 +
Public alsa As CAlsa                              <FONT Color=gray>' ''Classe che incapsula le funzioni ALSA''</font>
 +
Public evmid As New Evento_Midi        <FONT Color=gray>' ''Classe delle Proprietà per gestire uno spcifico "Evento Midi" di ALSA''</font>
 +
 +
 +
'''Public''' Sub Form_Open()
 +
 +
<FONT Color=gray>' ''Crea ("istanzia") un Oggetto della Classe "CAlsa" per poterla usare:''</font>
 +
  With alsa = New CAlsa As "Alsa"
 +
<FONT Color=gray>' ''Apre ALSA e assegna un nome al Client:''</font>
 +
    Me.Title = "|| Client ALSA " & .AlsaIni("Programma di prova")
 +
  <FONT Color=gray>' ''Sceglie la periferica (Softsynth) su cui suonare:''</font>
 +
    .ImpostaDispositivo(id_dev, p_dev)
 +
End With
 +
 +
'''End'''
 +
 +
'''Public''' Sub ToggleButton1_Click()
 +
 +
  If ToggleButton1.Value Then
 +
<FONT Color=gray>' ''Avvia la "Coda" degli Eventi Midi ALSA:''</font>
 +
    alsa.AvvioCoda()
 +
<FONT Color=gray>' ''Imposta i valori specifici dell'Evento Midi "Control Change":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 0
 +
      .Channel = 0
 +
      .Param = 7
 +
      .Value = 50
 +
    End With
 +
    alsa.ControlChange(evmid)
 +
 +
<FONT Color=gray>' ''Imposta i valori specifici dell'Evento Midi "Program Change":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
    .Tick_o_Tv_sec = 0
 +
    .Channel = 0
 +
    .Value = strum.Find("Violin")
 +
    End With
 +
    alsa.ProgramChange(evmid)
 +
 +
<FONT Color=gray>' ''Imposta i valori specifici dell'Evento Midi "Midi-ON":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 0
 +
      .Channel = 0
 +
      .Note = 64
 +
      .Velocity = 100
 +
    End With
 +
    alsa.NoteON(evmid)
 +
 +
<FONT Color=gray>' ''Imposta i valori specifici dell'Evento Midi "Midi-OFF":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 96
 +
      .Channel = 0
 +
      .Note = 64
 +
      .Velocity = 100
 +
    End With
 +
    alsa.NoteOFF(evmid)
 +
 +
<FONT Color=gray>' ''Imposta il suono di un altro strumento musicale dopo 150 tick dall'inizio della "Coda" (quindi crea una pausa):''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 150
 +
      .Channel = 0
 +
      .Value = strum.Find("Reed Organ")
 +
    End With
 +
    alsa.ProgramChange(evmid)
 +
 +
<FONT Color=gray>' ''Imposta la nota Midi da eseguire dopo 182 tick dall'inizio della "Coda":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 182
 +
      .Channel = 0
 +
      .Note = 66
 +
      .Velocity = 100
 +
    End With
 +
    alsa.NoteON(evmid)
 +
 +
<FONT Color=gray>' ''Imposta la nota Midi da silenziare dopo 300 tick dall'inizio della "Coda":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 300
 +
      .Channel = 0
 +
      .Note = 66
 +
      .Velocity = 0
 +
    End With
 +
    alsa.NoteOFF(evmid)
 +
 +
<FONT Color=gray>' ''Imposta il suono di un altro strumento musicale dopo 300 tick dall'inizio della "Coda":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 300
 +
      .Channel = 0
 +
      .Value = strum.Find("Flute")
 +
    End With
 +
    alsa.ProgramChange(evmid)
 +
 +
<FONT Color=gray>' ''Imposta la nota Midi da eseguire dopo 300 tick dall'inizio della "Coda":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 300
 +
      .Channel = 0
 +
      .Note = 68
 +
      .Velocity = 100
 +
    End With
 +
    alsa.NoteON(evmid)
 +
 +
<FONT Color=gray>' ''Imposta la nota Midi da silenziare dopo 500 tick dall'inizio della "Coda":''</font>
 +
    With evmid
 +
      .Flags = SND_SEQ_TIME_STAMP_TICK
 +
      .Tick_o_Tv_sec = 500
 +
      .Channel = 0
 +
      .Note = 68
 +
      .Velocity = 0
 +
    End With
 +
    alsa.NoteOFF(evmid)
 +
 +
<FONT Color=gray>' ''Dispone infine l'invio di tutti gli Eventi Midi bufferizzati nella "Coda":''</font>
 +
    alsa.Flush()
 +
  Else
 +
    alsa.StopCoda()
 +
    Me.Close
 +
  Endif
 +
 +
'''End'''
 +
La Classe secondaria ''CAlsa'' avrà il seguente codice:
 +
Private handle As Pointer
 +
Private id As Integer
 +
Private s_port As Integer
 +
Private que As Integer
 +
Private dclient As Byte
 +
Private dport As Byte
 +
 +
 +
Library "libasound:2.0.0"
 +
 +
Private Const SND_SEQ_OPEN_OUTPUT As Integer = 1
 +
Private Const SND_SEQ_PORT_CAP_READ As Integer = 1
 +
Private Const SND_SEQ_PORT_TYPE_MIDI_GENERIC As Integer = 2
 +
Private Const SND_SEQ_PORT_TYPE_APPLICATION As Integer = 1048576
 +
Private Enum SND_SEQ_EVENT_NOTEON = 6, SND_SEQ_EVENT_NOTEOFF, SND_SEQ_EVENT_KEYPRESS,
 +
              SND_SEQ_EVENT_CONTROLLER = 10, SND_SEQ_EVENT_PGMCHANGE, SND_SEQ_EVENT_CHANPRESS,
 +
              SND_SEQ_EVENT_PITCHBEND, SND_SEQ_EVENT_START = 30, SND_SEQ_EVENT_STOP = 32
 +
 +
<FONT Color=gray>' ''int snd_seq_open (snd_seq_t **seqp, const char * name, int streams, int mode)''
 +
' ''Open the ALSA sequencer.''</font>
 +
Private Extern snd_seq_open(seqp As Pointer, name As String, streams As Integer, mode As Integer) As Integer
 +
 +
<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
 +
 +
<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
 +
 +
  <FONT Color=gray>' ''int snd_seq_create_simple_port (snd_seq_t* seq, const char* name, unsigned int caps, unsigned int type)''
 +
' ''Creates a port with the given capability and type bits.''</font>
 +
Private Extern snd_seq_create_simple_port(seq As Pointer, name As String, caps As Integer, type As Integer) As Integer
 +
 +
<FONT Color=gray>' ''int snd_seq_alloc_queue (snd_seq_t * seq)''
 +
' ''Allocate a queue.''</font>
 +
Private Extern snd_seq_alloc_queue(seq As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''int snd_seq_control_queue(snd_seq_t * seq, int q, int type, int value, snd_seq_event_t * ev)''
 +
' ''Queue controls - start/stop/continue.''</font>
 +
Private Extern snd_seq_control_queue(seq As Pointer, q As Integer, type As Integer, value As Integer, ev As Pointer) As Integer
 +
 
 +
<FONT Color=gray>' ''int snd_seq_event_output_buffer (snd_seq_t * seq, snd_seq_event_t *ev)''
 +
' ''Output an event.''</font>
 +
Private Extern snd_seq_event_output_buffer(seq As Pointer, ev As EventoMidi) As Integer
 +
 +
<FONT Color=gray>' ''int snd_seq_drain_output (snd_seq_t * seq)''
 +
' ''Drain output buffer to sequencer.''</font>
 +
Private Extern snd_seq_drain_output(seq As Pointer) As Integer
 +
 +
<FONT Color=gray>' ''const char * snd_strerror (int errnum)''
 +
' ''Returns the message for an error code.''</font>
 +
Private Extern snd_strerror(errnum As Integer) As String
 +
 +
<FONT Color=gray>' ''int snd_seq_close (snd_seq_t * seq)''
 +
' ''Close the sequencer.''</font>
 +
Private Extern snd_seq_close(seq As Pointer) As Integer
 +
 +
 +
'''Public''' Function AlsaIni(nome As String) As String
 +
 +
  Dim err As Integer
 +
 
 +
  err = snd_seq_open(VarPtr(handle), "default", SND_SEQ_OPEN_OUTPUT, 0)
 +
  Print "Apertura del subsistema 'seq' di ALSA = "; IIf(err == 0, "corretta !", "errata !")
 +
  If err < 0 Then error.RAISE("Errore: " & snd_strerror(err))
 +
 +
  snd_seq_set_client_name(handle, nome)
 +
  id = snd_seq_client_id(handle)
 +
  Print "Alsa ClientID = "; id
 +
 +
  s_port = snd_seq_create_simple_port(handle, "Uscita", SND_SEQ_PORT_CAP_READ, SND_SEQ_PORT_TYPE_MIDI_GENERIC + SND_SEQ_PORT_TYPE_APPLICATION)
 +
  Print "Porta del programma = "; s_port
 +
  If s_port < 0 Then error.Raise("Errore: " & snd_strerror(s_port))
 +
 +
  que = snd_seq_alloc_queue(handle)
 +
  If que < 0 Then error.Raise("Errore: " & snd_strerror(que))
 +
 +
  Return CStr(id) & ":" & CStr(s_port)
 +
 +
'''End'''
 +
 +
'''Public''' Procedure ImpostaDispositivo(client As Integer, port As Integer)
 +
 +
  dclient = client
 +
  dport = port
 +
 +
'''End'''
 +
 +
  '''Public''' Procedure AvvioCoda()
 +
 +
  Dim err As Integer
 +
 +
  err = snd_seq_control_queue(handle, que, SND_SEQ_EVENT_START, 0, 0)
 +
  If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))
 +
 
 +
'''End'''
 +
 +
'''Public''' Procedure ControlChange(mid As Evento_Midi)
 +
 +
  Dim err As Integer
 +
 +
  With mid
 +
    .Type = SND_SEQ_EVENT_CONTROLLER
 +
    .Queue = que
 +
    .Source_client = id
 +
    .Source_port = s_port
 +
    .Dest_client = dclient
 +
    .Dest_port = dport
 +
  End With
 +
 +
  err = snd_seq_event_output_buffer(handle, mid)
 +
  If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))
 +
 +
'''End'''
 +
 +
'''Public''' Procedure ProgramChange(mid As Evento_Midi)
 
   
 
   
Dim err As Integer
+
  Dim err As Integer
 
   
 
   
   With ev_OnOffPoly
+
   With mid
     .type = def_Evento          <FONT color=#006400>' ''= 6, 7 oppure 8''</font>
+
     .Type = SND_SEQ_EVENT_PGMCHANGE
     .queue = outq
+
     .Queue = que
     .source_id = id
+
     .Source_client = id
     .source_porta = outport
+
     .Source_port = s_port
    .dest_id = SND_SEQ_ADDRESS_SUBSCRIBERS
+
     .Dest_client = dclient
    .dest_porta = SND_SEQ_ADDRESS_UNKNOWN
+
     .Dest_port = dport
    .channel = 0
 
     .note = 60
 
     .velocity = velocitas
 
 
   End With
 
   End With
 
   
 
   
   err = snd_seq_event_output(handle, <FONT color=#FF7000>ev_OnOffPoly</font>)   <FONT color=#006400>' ''si passano ad ALSA i valori presenti nella variabile-Classe: ev_OnOffPoly''</font>
+
   err = snd_seq_event_output_buffer(handle, mid)
   printerr("OnOffPoly = ", err)
+
   If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))
 
   
 
   
  End
+
  '''End'''
 
+
 
+
  '''Public''' Procedure NoteON(mid As Evento_Midi)
==Gruppo messaggi: <FONT Color= "red">Program Change</font> - <FONT Color= "red">Channel Aftertouch (Key Pressure)</font>==
 
Questi due messaggi Midi hanno tra loro in comune due medesimi specifici dati posti ai byte num. 16 e 24 della Classe ''CMidi.class'' modello, e che abbiamo chiamato: ''channel'' e ''valorePrimo''.
 
 
 
<P>Dichiareremo una variabile ''ad hoc'' anche per questi due messaggi Midi del tipo della Classe ''CMidi.class'' modello, della quale ultima assumerà così l'organizzazione interna delle particolari variabili ed il tipo dei valori:</p>
 
 
 
  ev_PgmKeyPress As New CMidi
 
 
 
<P>Ora la nostra variabile ''ev_PgmKeyPress'' è operativa e può essere utilizzata per la gestione ed il successivo invio dei messaggi Midi ad ALSA.</p>
 
<P>L'uso di tale variabile-Classe specifica si ha quando si dovrà definire ''concretamente'' il messaggio Midi richiesto, fissando in ciascun elemento costituivo (le diverse variabili in essa contenute) i relativi valori. Si potrà evitare di richiamare le varibili particolari della Classe che eventualmente non servono.</p>
 
 
 
<P>Quindi dalla classe principale chiameremo la routine specifica per i due eventi qui considerati, passandogli anche due valori necessari:</p>
 
 
 
Public Sub eventoPgmKeyPress(def_Evento As Byte, sec_Valore As Integer)
 
 
   
 
   
Dim err As Integer
+
  Dim err As Integer
 
   
 
   
   With ev_PgmKeyPress
+
   With mid
  .type = def_Evento          <FONT color=#006400>' ''= 11 oppure 12''</font>
+
    .Type = SND_SEQ_EVENT_NOTEON
  .queue = outq
+
    .Queue = que
  .source_id = id
+
    .Source_client = id
  .source_porta = outport
+
    .Source_port = s_port
  .dest_id = SND_SEQ_ADDRESS_SUBSCRIBERS
+
    .Dest_client = dclient
  .dest_porta = SND_SEQ_ADDRESS_UNKNOWN
+
    .Dest_port = dport
  .channel = 0
 
  .value = sec_Valore        <FONT color=#006400>' ''impostato nella classe principale''</font>
 
 
   End With
 
   End With
 
   
 
   
<FONT color=#006400>' ''Si passano ad ALSA i valori presenti nella variabile-Classe: ev_PgmKeyPress:''</font>
+
   err = snd_seq_event_output_buffer(handle, mid)
   err = snd_seq_event_output(handle, <FONT color=#FF7000>ev_PgmKeyPress</font>)
+
   If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))
   printerr("PgmKeyPress = ", err)
 
 
   
 
   
  End
+
  '''End'''
 
+
 
+
  '''Public''' Procedure NoteOFF(mid As Evento_Midi)
==Gruppo messaggi: <FONT Color= "red">Control Change</font> - <FONT Color= "red">Pitch Bend (Pitch Wheel)</font>==
 
Questi due messaggi Midi hanno tra loro in comune tre medesimi specifici dati posti ai byte num. 16, 20 e 24 della Classe ''CMidi.class'' modello, e che abbiamo chiamato: ''channel'', ''primoValore'' e ''secondoValore''.
 
 
 
<P>Dichiareremo una variabile ''ad hoc'' anche per questi due messaggi Midi del tipo della Classe ''CMidi.class'' modello, della quale ultima assumerà così l'organizzazione interna delle particolari variabili ed il tipo dei valori:</p>
 
 
 
  ev_CtrlPitBnd As New CMidi
 
 
 
<P>Ora la nostra variabile-Classe ''ev_CtrlPitBnd'' è operativa e può essere utilizzata per la gestione ed il successivo invio dei messaggi Midi ad ALSA.</p>
 
<P>L'uso di tale variabile-Classe specifica si ha quando si dovrà definire ''concretamente'' il messaggio Midi richiesto, fissando in ciascun elemento costituivo (le diverse variabili in essa contenute) i relativi valori. Si potrà evitare di richiamare le varibili particolari della Classe che eventualmente non servono.</p>
 
 
 
<P>Quindi dalla classe principale chiameremo la routine specifica per i due eventi qui considerati, passandogli anche tre valori necessari:</p>
 
 
 
Public Sub eventoPgmKeyPress(def_Evento As Byte, prm_Valore As Integer, sec_Valore As Integer)
 
 
   
 
   
Dim err As Integer
+
  Dim err As Integer
 
   
 
   
   With ev_CtrlPitBnd
+
   With mid
     .type = def_Evento          <FONT color=#006400>' ''= 10 oppure 13''</font>
+
     .Type = SND_SEQ_EVENT_NOTEOFF
     .queue = outq
+
     .Queue = que
     .source_id = id
+
     .Source_client = id
     .source_porta = outport
+
     .Source_port = s_port
    .dest_id = SND_SEQ_ADDRESS_SUBSCRIBERS
+
     .Dest_client = dclient
    .dest_porta = SND_SEQ_ADDRESS_UNKNOWN
+
     .Dest_port = dport
    .channel = 0
 
     .param = prm_Valore        <FONT color=#006400>' ''impostato nella classe principale''</font>
 
     .value = sec_Valore        <FONT color=#006400>' ''impostato nella classe principale''</font>
 
 
   End With
 
   End With
 
   
 
   
   err = snd_seq_event_output(handle, <FONT color=#FF7000>ev_CtrlPitBnd</font>)   <FONT color=#006400>' ''si passano ad ALSA i valori presenti nella variabile-Classe: CtrlPitBnd''</font>
+
   err = snd_seq_event_output_buffer(handle, mid)
   printerr("CtrlPitBnd = ", err)
+
   If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))
 +
 +
'''End'''
 
   
 
   
  End
+
  '''Public''' Procedure Flush()
<BR>Da notare che per il Pitch Bend il dato ''primoValore'' è ininfluente, e può essere quindi fissato a 0.
+
 +
  Dim err As Integer
 +
 +
  err = snd_seq_drain_output(handle)
 +
  If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))
 +
 +
'''End'''
 +
 +
'''Public''' Procedure StopCoda()
 +
 +
  Dim err As Integer
 +
 
 +
  err = snd_seq_control_queue(handle, que, SND_SEQ_EVENT_STOP, 0, 0)
 +
  If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))
 +
 +
  snd_seq_close(handle)
 +
 +
'''End'''
  
  
==Il codice completo==
+
==Impostando e usando solo variabili ''Globali'' per ciascun dato della Classe specifica==
Per vedere in ordine il nostro codice definitivo sin qui descritto della classe specifica modello ''CMidi'' e classe ''CAlsa.class'', cliccare sul collegamento alla sua pagina: [[Alsa_e_Gambas_Codice_5|5° CODICE]].
+
La Classe specifica della gestione degli Eventi Midi di ALSA può anche solo contenere singole variabili di tipo ''Globale'', ciascuna delle quali rappresenta un membro:
 +
Public type As Byte
 +
Public flag As Byte
 +
Public tag As Byte
 +
Public queue As Byte
 +
Public tick_o_tv_sec As Integer
 +
Public tv_nsec As Integer
 +
Public source_client As Byte
 +
Public source_port As Byte
 +
Public dest_client As Byte
 +
Public dest_port As Byte
 +
Public channel As Byte
 +
Public note As Byte
 +
Public velocity As Byte
 +
Public off_velocity As Byte
 +
Public param As Integer
 +
Public value As Integer
 +
Ovviamente nel codice pricipale di Gambas si creerà un Oggetto (istanza) della Classe specifica degli Eventi Midi ALSA, così costituita, e le predette variabili ''globali'' saranno richiamate nel modo consueto attraverso la variabile identificatrice della Classe specifica.
  
  

Versione attuale delle 09:37, 4 feb 2022

Introduzione

Per definire e gestire gli Eventi Midi di ALSA si può usare una Classe specifica, fornita di Proprietà in "lettura" e in "scrittura" che potranno essere usate per memorizzare i valori necessari dell'Evento Midi, secondo la Struttura snd_seq_event_t prevista dal protocollo di ALSA.
Dichiarando poi una variabile del tipo di quella Classe per ciascun Evento Midi ALSA, si avrà un'area di memoria utilizzabile.
L'uso di tale Classe specifica sostituisce egregiamente e più dinamicamente la Struttura. [nota 1]


Scrittura dei dati dei messaggi Midi nella Classe specifica dei dati Midi

Impostando e usando le Proprietà della Classe specifica

La Classe modello specifica per la gestione delle risorse di ALSA, che chiameremo "CAlsa", va creata con tutte le Proprietà necessarie per la definizione di qualunque Evento Midi di ALSA e sarà così organizzata:

' Definisce le Proprietà della Classe specifica:
Property Type As Byte
Property Flags As Byte
Property Tag As Byte
Property Queue As Byte
Property Tick_o_Tv_sec As Integer
Property Tv_nsec As Integer
Property Source_client As Byte
Property Source_port As Byte
Property Dest_client As Byte
Property Dest_port As Byte
Property Channel As Byte
Property Note As Byte
Property Velocity As Byte
Property Off_velocity As Byte
Property Param As Integer
Property Value As Integer

' Definisce i simboli associati alle suddette Proprietà:
Private $Type As Byte
Private $Flags As Byte
Private $Tag As Byte
Private $Queue As Byte
Private $Tick_o_Tv_sec As Integer
Private $Tv_nsec As Integer
Private $Source_client As Byte
Private $Source_port As Byte
Private $Dest_client As Byte
Private $Dest_port As Byte
Private $Channel As Byte
Private $Note As Byte
Private $Velocity As Byte
Private $Off_velocity As Byte
Private $Param As Integer
Private $Value As Integer

Private Function Type_Read() As Byte
  Return $Type
End

Private Sub Type_Write(Value As Byte)
  $Type = Value
End

Private Function Flags_Read() As Byte
  Return $Flags
End

Private Sub Flags_Write(Value As Byte)
  $Flags = Value
End

Private Function Tag_Read() As Byte
  Return $Tag
End

Private Sub Tag_Write(Value As Byte)
  $Tag = Value
End

Private Function Queue_Read() As Byte
  Return $Queue
End

Private Sub Queue_Write(Value As Byte)
  $Queue = Value
End

Private Function Tick_o_Tv_sec_Read() As Integer
  Return $Tick_o_Tv_sec
End

Private Sub Tick_o_Tv_sec_Write(Value As Integer)
  $Tick_o_Tv_sec = Value
End

Private Function Tv_nsec_Read() As Integer
  Return $Tv_nsec
End

Private Sub Tv_nsec_Write(Value As Integer)
  $Tv_nsec = Value
End

Private Function Source_client_Read() As Byte
  Return $Source_client
End

Private Sub Source_client_Write(Value As Byte)
  $Source_client = Value
End

Private Function Source_port_Read() As Byte
  Return $Source_port
End

Private Sub Source_port_Write(Value As Byte)
  $Source_port = Value
End

Private Function Dest_client_Read() As Byte
  Return $Dest_client
End

Private Sub Dest_client_Write(Value As Byte)
  $Dest_client = Value
End

Private Function Dest_port_Read() As Byte
  Return $Dest_port
End

Private Sub Dest_port_Write(Value As Byte)
  $Dest_port = Value
End

Private Function Channel_Read() As Byte
  Return $Channel
End

Private Sub Channel_Write(Value As Byte)
  $Channel = Value
End

Private Function Note_Read() As Byte
  Return $Note
End

Private Sub Note_Write(Value As Byte)
  $Note = Value
End

Private Function Velocity_Read() As Byte
  Return $Velocity
End

Private Sub Velocity_Write(Value As Byte)
  $Velocity = Value
End

Private Function Off_velocity_Read() As Byte
  Return $Off_velocity
End

Private Sub Off_velocity_Write(Value As Byte)
  $Off_velocity = Value
End

Private Function Param_Read() As Integer
  Return $Param
End

Private Sub Param_Write(Value As Integer)
  $Param = Value
End

Private Function Value_Read() As Integer
  Return $Value
End

Private Sub Value_Write(Value As Integer)
  $Value = Value
End


Esempio di codice

Mostriamo un codice in ambiente garfico che prevede tre Classi:

- la prima è una Classe secondaria, che chiameremo EventoMidi, ed è la Classe specifica per la gestione degli Eventi Midi di ALSA e sarà identica al modello di Classe specifica di un Evento Midi ALSA sopra descritto.

- la seconda è un'altra Classe secondaria, che chiameremo CAlsa, ed è la Classe per la gestione delle risorse di ALSA. In questa Classe saranno attribuiti alla variabile del tipo della Classe specifica della gestione degli Eventi Midi alcuni valori.

- La terza Classe è quella principale e in essa saranno attribuiti alla variabile del tipo della Classe specifica della gestione degli Eventi Midi la maggior parte dei valori per il particolare Evento Midi in quel momento gestito.
Il suo codice, che sarà il seguente, prevede che la temporizzazione degli Eventi Midi, ivi gestiti, sia impostata in modalità tick time Midi:

Private strum As String[] = ["Acustic Grand Piano", "Bright Acustic Piano", "Electric Grand Piano", "Honky-tonk",
 "Electric Piano 1", "Electric Piano 2", "Harpsichord", "Clavinet", "Celesta", "Glockenspiel", "Music Box", "Vibraphone",
 "Marimba", "Xylophone", "Tubular Bells", "Dulcimer", "Hammond Organ", "Percussive Organ", "Rock Organ", "Church Organ",
 "Reed Organ", "Accordion", "Harmonica", "Tango Accordion", "Acoustic Guitar (nylon)", "Acoustic Guitar (steel)",
 "Electric Guitar (jazz)", "Electric Guitar (clean)", "Electric Guitar(muted)", "Overdriven Guitar", "Distortion Guitar",
 "Guitar Harmonics", "Acoustic Bass", "Electric Bass (finger)", "Electric Bass (pick)", "Fretless Bass", "Slap Bass 1",
 "Slap Bass 2", "Synth Bass 1", "Synth Bass 2", "Violin", "Viola", "Cello", "Contrabass", "Tremolo Strings",
 "Pizzicato Strings", "Orchestral Harp", "Timpani", "String Ensemble 1", "String Ensemble 2", "SynthStrings 1",
 "SynthStrings 2", "Choir Aahs", "Voice Oohs", "Synth Voice", "Orchestra Hit", "Trumpet", "Trombone", "Tuba", "Muted Trumpet",
 "French Horn", "Brass Section", "Synth Brass 1", "Synth Brass 2", "Soprano Sax", "Alto Sax", "Tenor Sax", "Baritone Sax",
 "Oboe", "English Horn", "Basson", "Clarinet", "Piccolo", "Flute", "Recorder", "Pan Flute", "Bottle Blow", "Shakuhachi",
 "Whistle", "Ocarina", "Lead 1 (square)", "Lead 2 (sawtooth)", "Lead 3 (caliope lead)", "Lead 4 (chiff lead)",
 "Lead 5 (charang)", "Lead 6 (voice)", "Lead 7 (fifths)", "Lead 8(brass+lead)", "Pad 1 (new age)", "Pad 2 (warm)",
 "Pad 3 (polysynth)", "Pad 4 (choir)", "Pad 5 (bowed)", "Pad 6 (metallic)", "Pad 7 (halo)", "Pad 8 (sweep)", "FX 1 (rain)",
 "FX 2 (soundtrack)", "FX 3 (crystal)", "FX 4 (atmosphere)", "FX 5 (brightness)", "FX 6 (goblins)", "FX 7 (echoes)",
 "FX 8 (sci-fi)", "Sitar", "Banjo", "Shamisen", "Koto", "Kalimba", "Bagpipe", "Fiddle", "Shanai", "Tinkle Bell", "Agogo",
 "Steel Drums", "Woodblock", "Taiko Drum", "Melodic Tom", "Synth Drum", "Reverse Cymbal", "Guitar Fret Noise", "Breath Noise",
 "Seashore", "Bird Tweet", "Telephone Ring", "Helicopter", "Applause", "Gunshot"]

Private Const id_dev As Integer = 128             ' 14 = midi out oppure  128 (solitamente) = softsynth
Private Const p_dev As Integer = 0                ' Porta del Client destinatario dei dati Midi: solitamente 0
Private Const SND_SEQ_TIME_STAMP_TICK As Integer = 0
Public alsa As CAlsa                              ' Classe che incapsula le funzioni ALSA
Public evmid As New Evento_Midi         ' Classe delle Proprietà per gestire uno spcifico "Evento Midi" di ALSA


Public Sub Form_Open()

' Crea ("istanzia") un Oggetto della Classe "CAlsa" per poterla usare:
 With alsa = New CAlsa As "Alsa"
' Apre ALSA e assegna un nome al Client:
   Me.Title = "|| Client ALSA " & .AlsaIni("Programma di prova")
' Sceglie la periferica (Softsynth) su cui suonare:
   .ImpostaDispositivo(id_dev, p_dev)
End With

End

Public Sub ToggleButton1_Click()

 If ToggleButton1.Value Then
' Avvia la "Coda" degli Eventi Midi ALSA:
   alsa.AvvioCoda()
' Imposta i valori specifici dell'Evento Midi "Control Change":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 0
     .Channel = 0
     .Param = 7
     .Value = 50
   End With
   alsa.ControlChange(evmid)

' Imposta i valori specifici dell'Evento Midi "Program Change":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
   .Tick_o_Tv_sec = 0
   .Channel = 0
   .Value = strum.Find("Violin")
   End With
   alsa.ProgramChange(evmid)

' Imposta i valori specifici dell'Evento Midi "Midi-ON":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 0
     .Channel = 0
     .Note = 64
     .Velocity = 100
   End With
   alsa.NoteON(evmid)

' Imposta i valori specifici dell'Evento Midi "Midi-OFF":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 96
     .Channel = 0
     .Note = 64
     .Velocity = 100
   End With
   alsa.NoteOFF(evmid)

' Imposta il suono di un altro strumento musicale dopo 150 tick dall'inizio della "Coda" (quindi crea una pausa):
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 150
     .Channel = 0
     .Value = strum.Find("Reed Organ")
   End With
   alsa.ProgramChange(evmid)

' Imposta la nota Midi da eseguire dopo 182 tick dall'inizio della "Coda":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 182
     .Channel = 0
     .Note = 66
     .Velocity = 100
   End With
   alsa.NoteON(evmid)

' Imposta la nota Midi da silenziare dopo 300 tick dall'inizio della "Coda":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 300
     .Channel = 0
     .Note = 66
     .Velocity = 0
   End With
   alsa.NoteOFF(evmid)

' Imposta il suono di un altro strumento musicale dopo 300 tick dall'inizio della "Coda":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 300
     .Channel = 0
     .Value = strum.Find("Flute")
   End With
   alsa.ProgramChange(evmid)

' Imposta la nota Midi da eseguire dopo 300 tick dall'inizio della "Coda":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 300
     .Channel = 0
     .Note = 68
     .Velocity = 100
   End With
   alsa.NoteON(evmid)

' Imposta la nota Midi da silenziare dopo 500 tick dall'inizio della "Coda":
   With evmid
     .Flags = SND_SEQ_TIME_STAMP_TICK 
     .Tick_o_Tv_sec = 500
     .Channel = 0
     .Note = 68
     .Velocity = 0
   End With
   alsa.NoteOFF(evmid)

' Dispone infine l'invio di tutti gli Eventi Midi bufferizzati nella "Coda":
   alsa.Flush()
 Else
   alsa.StopCoda()
   Me.Close
 Endif

End

La Classe secondaria CAlsa avrà il seguente codice:

Private handle As Pointer
Private id As Integer
Private s_port As Integer
Private que As Integer
Private dclient As Byte
Private dport As Byte


Library "libasound:2.0.0"

Private Const SND_SEQ_OPEN_OUTPUT As Integer = 1
Private Const SND_SEQ_PORT_CAP_READ As Integer = 1
Private Const SND_SEQ_PORT_TYPE_MIDI_GENERIC As Integer = 2
Private Const SND_SEQ_PORT_TYPE_APPLICATION As Integer = 1048576
Private Enum SND_SEQ_EVENT_NOTEON = 6, SND_SEQ_EVENT_NOTEOFF, SND_SEQ_EVENT_KEYPRESS,
             SND_SEQ_EVENT_CONTROLLER = 10, SND_SEQ_EVENT_PGMCHANGE, SND_SEQ_EVENT_CHANPRESS,
             SND_SEQ_EVENT_PITCHBEND, SND_SEQ_EVENT_START = 30, SND_SEQ_EVENT_STOP = 32

' int snd_seq_open (snd_seq_t **seqp, const char * name, int streams, int mode)
' Open the ALSA sequencer.
Private Extern snd_seq_open(seqp 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_client_id (snd_seq_t * seq)
' Get the client id.
Private Extern snd_seq_client_id(seq As Pointer) As Integer

' int snd_seq_create_simple_port (snd_seq_t* seq, const char* name, unsigned int caps, unsigned int type)
' Creates a port with the given capability and type bits.
Private Extern snd_seq_create_simple_port(seq As Pointer, name As String, caps As Integer, type As Integer) As Integer

' int snd_seq_alloc_queue (snd_seq_t * seq)
' Allocate a queue.
Private Extern snd_seq_alloc_queue(seq As Pointer) As Integer

' int snd_seq_control_queue(snd_seq_t * seq, int q, int type, int value, snd_seq_event_t * ev)
' Queue controls - start/stop/continue.
Private Extern snd_seq_control_queue(seq As Pointer, q As Integer, type As Integer, value As Integer, ev As Pointer) As Integer
 
' int snd_seq_event_output_buffer (snd_seq_t * seq, snd_seq_event_t *ev)
' Output an event.
Private Extern snd_seq_event_output_buffer(seq As Pointer, ev As EventoMidi) As Integer

' int snd_seq_drain_output (snd_seq_t * seq)
' Drain output buffer to sequencer.
Private Extern snd_seq_drain_output(seq As Pointer) As Integer

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

' int snd_seq_close (snd_seq_t * seq)
' Close the sequencer.
Private Extern snd_seq_close(seq As Pointer) As Integer


Public Function AlsaIni(nome As String) As String

 Dim err As Integer
 
 err = snd_seq_open(VarPtr(handle), "default", SND_SEQ_OPEN_OUTPUT, 0)
 Print "Apertura del subsistema 'seq' di ALSA = "; IIf(err == 0, "corretta !", "errata !")
 If err < 0 Then error.RAISE("Errore: " & snd_strerror(err))

 snd_seq_set_client_name(handle, nome)
 id = snd_seq_client_id(handle)
 Print "Alsa ClientID = "; id

 s_port = snd_seq_create_simple_port(handle, "Uscita", SND_SEQ_PORT_CAP_READ, SND_SEQ_PORT_TYPE_MIDI_GENERIC + SND_SEQ_PORT_TYPE_APPLICATION) 
 Print "Porta del programma = "; s_port
 If s_port < 0 Then error.Raise("Errore: " & snd_strerror(s_port))

 que = snd_seq_alloc_queue(handle)
 If que < 0 Then error.Raise("Errore: " & snd_strerror(que))

 Return CStr(id) & ":" & CStr(s_port)

End

Public Procedure ImpostaDispositivo(client As Integer, port As Integer)

 dclient = client
 dport = port

End

Public Procedure AvvioCoda()

 Dim err As Integer

 err = snd_seq_control_queue(handle, que, SND_SEQ_EVENT_START, 0, 0)
 If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))
 
End

Public Procedure ControlChange(mid As Evento_Midi)

 Dim err As Integer

 With mid
   .Type = SND_SEQ_EVENT_CONTROLLER
   .Queue = que
   .Source_client = id
   .Source_port = s_port
   .Dest_client = dclient
   .Dest_port = dport
 End With

 err = snd_seq_event_output_buffer(handle, mid)
 If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))

End

Public Procedure ProgramChange(mid As Evento_Midi)

 Dim err As Integer

 With mid
   .Type = SND_SEQ_EVENT_PGMCHANGE
   .Queue = que
   .Source_client = id
   .Source_port = s_port
   .Dest_client = dclient
   .Dest_port = dport
 End With

 err = snd_seq_event_output_buffer(handle, mid)
 If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))

End

Public Procedure NoteON(mid As Evento_Midi)

 Dim err As Integer

 With mid
   .Type = SND_SEQ_EVENT_NOTEON
   .Queue = que
   .Source_client = id
   .Source_port = s_port
   .Dest_client = dclient
   .Dest_port = dport
 End With

 err = snd_seq_event_output_buffer(handle, mid)
 If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))

End

Public Procedure NoteOFF(mid As Evento_Midi)

 Dim err As Integer

 With mid
   .Type = SND_SEQ_EVENT_NOTEOFF
   .Queue = que
   .Source_client = id
   .Source_port = s_port
   .Dest_client = dclient
   .Dest_port = dport
 End With

 err = snd_seq_event_output_buffer(handle, mid)
 If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))

End

Public Procedure Flush()

 Dim err As Integer

 err = snd_seq_drain_output(handle)
 If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))

End

Public Procedure StopCoda()

 Dim err As Integer
 
 err = snd_seq_control_queue(handle, que, SND_SEQ_EVENT_STOP, 0, 0)
 If err < 0 Then Error.Raise("Errore: " & snd_strerror(err))

 snd_seq_close(handle) 

End


Impostando e usando solo variabili Globali per ciascun dato della Classe specifica

La Classe specifica della gestione degli Eventi Midi di ALSA può anche solo contenere singole variabili di tipo Globale, ciascuna delle quali rappresenta un membro:

Public type As Byte
Public flag As Byte
Public tag As Byte
Public queue As Byte
Public tick_o_tv_sec As Integer
Public tv_nsec As Integer
Public source_client As Byte
Public source_port As Byte
Public dest_client As Byte
Public dest_port As Byte
Public channel As Byte
Public note As Byte
Public velocity As Byte
Public off_velocity As Byte
Public param As Integer
Public value As Integer

Ovviamente nel codice pricipale di Gambas si creerà un Oggetto (istanza) della Classe specifica degli Eventi Midi ALSA, così costituita, e le predette variabili globali saranno richiamate nel modo consueto attraverso la variabile identificatrice della Classe specifica.


Note

[1] In Gambas una "Struttura " può anche essere sostituita con una Classe avente solo Proprietà (dunque senza Metodi né Eventi).