quarta-feira, 14 de março de 2018

ALSA MIDI

Instalando as dependências
sudo apt-get install libasound2 libasound2-dev


#include
#include
#include
#include
#include

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);

  while (keep_walking) {

    
   // Note on 
   snd_seq_ev_set_noteon(&ev, 0, 60, 127);
   snd_seq_event_output(seq_handle, &ev);
   snd_seq_drain_output(seq_handle);
    // Duration
   usleep(note_time);

   // Note off
   snd_seq_ev_set_noteoff(&ev, 0, 60, 0);
   snd_seq_event_output(seq_handle, &ev);
   snd_seq_drain_output(seq_handle);
    // Duration
   usleep(note_time / 6);

  }

   snd_seq_close(seq_handle);
   return 0;
}

Para compilar:


gcc alsamidi.c -o alsamidi -lasound