exemplos-compmus/_examples/c/alsamidi3.c

67 lines
1.5 KiB
C

---
author: "Flávio Schiavoni"
description: Comunicador Alsa MIDI
---
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <alsa/asoundlib.h>
int keep_walking;
static void sigint_handler(int i){
keep_walking = 0;
}
int main(int argc, char *argv[]) {
int note_time = 1000000; // 1 segundo (em microsegundos)
// Pega o Ctrl+C ou signal de Kill para a aplicação.
signal(SIGINT, &sigint_handler);
signal(SIGTERM, &sigint_handler);
keep_walking = 1;
// Crio o dispositivo MIDI
snd_seq_t *seq_handle;
int portid;
if (snd_seq_open(&seq_handle, "hw", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
fprintf(stderr, "Error opening ALSA sequencer.\n");
exit(1);
}
snd_seq_set_client_name(seq_handle, "My App Midi");
if ((portid = snd_seq_create_simple_port(seq_handle, "My Midi Port",
SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {
fprintf(stderr, "Error creating sequencer port.\n");
exit(1);
}
snd_seq_event_t ev;
snd_seq_ev_clear(&ev);
snd_seq_ev_set_source(&ev, 0);
snd_seq_ev_set_subs(&ev);
snd_seq_ev_set_direct(&ev);
int value = 0;
while (keep_walking) {
// Note on
snd_seq_ev_set_controller(&ev, 0, 0, value);
snd_seq_event_output(seq_handle, &ev);
snd_seq_drain_output(seq_handle);
// Duration
usleep(note_time);
// value = (value < 127) ? value + 1 : 0;
}
snd_seq_close(seq_handle);
return 0;
}