Skip to contentSkip to conversation

Questions on writing a sequencer

DISCUSSION [email protected] 1 participant 1 message Jan 11, 2006

lore
Share & tools
lore
#0 lore raw

Hello,
I tried to write a simple sequencer.
As i read throught the documentation of alsa-lib and the tutorial linked I got to know how to open a seq handle, set the tempo, src and dest. As I read throughtout the tutorials the queues handle their speed themeselves.
But they don't do in my application.
Does someone know why?
I had to use usleep for correct timing oO.
I hope you can help me with this, because it is very important for me.
If you have any really low-feature example or another tutorial not given on www.alsa-project.org please send it. Here's the source code of my program(which is also attached):
[SOURCE BEGIN]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <alsa/asoundlib.h>

snd_seq_t *open_seq();
void midi_action(snd_seq_t *seq_handle);

snd_seq_t *open_seq() {

snd_seq_t *seq_handle;
int portid;

  if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0) {
    fprintf(stderr, "Error opening ALSA sequencer.\n");
    exit(1);
  }
  snd_seq_set_client_name(seq_handle, "Aseq Output");
  if ((portid = snd_seq_create_simple_port(seq_handle, "Aseq Output",
            SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
            SND_SEQ_PORT_TYPE_MIDI_GENERIC)) < 0) {
    fprintf(stderr, "Error creating sequencer port.\n");
    exit(1);

}
return(seq_handle);
}

void midi_action(snd_seq_t *seq_handle) {

snd_seq_event_t *ev;

  do {
    snd_seq_event_input(seq_handle, &ev);
    switch (ev->type) {
      case SND_SEQ_EVENT_CONTROLLER: 
        fprintf(stderr, "Control event on Channel %2d: %5d       \r",
                ev->data.control.channel, ev->data.control.value);
        break;
      case SND_SEQ_EVENT_PITCHBEND:
        fprintf(stderr, "Pitchbender event on Channel %2d: %5d   \r", 
                ev->data.control.channel, ev->data.control.value);
        break;
      case SND_SEQ_EVENT_NOTEON:
        fprintf(stderr, "Note On event on Channel %2d: %5d       \r",
                ev->data.control.channel, ev->data.note.note);
        break;        
      case SND_SEQ_EVENT_NOTEOFF: 
        fprintf(stderr, "Note Off event on Channel %2d: %5d      \r",         
                ev->data.control.channel, ev->data.note.note);           
        break;        
    }
    snd_seq_free_event(ev);

} while (snd_seq_event_input_pending(seq_handle, 0) > 0);
}

int main(int argc, char *argv[]) {

snd_seq_t *seq_handle;
int npfd;
struct pollfd *pfd;

  seq_handle = open_seq();
  
  int myqueue = snd_seq_alloc_named_queue(seq_handle, "my queue");
 
  snd_seq_queue_tempo_t *tempo;
  snd_seq_queue_tempo_alloca(&tempo);
  int iTempo = (int)(6e7 / ((double)140*(double)96*(double)96);
  snd_seq_queue_tempo_set_tempo(tempo, iTempo); // 60 BPM
  snd_seq_queue_tempo_set_ppq(tempo, 96); // 48 PPQ
  snd_seq_set_queue_tempo(seq_handle, myqueue, tempo);
  snd_seq_queue_tempo_free(tempo);
  
  snd_seq_addr_t sender, dest;
  snd_seq_port_subscribe_t *subs;
  sender.client = 131;
  sender.port = 0;
  dest.client = 130;
  dest.port = 1;
  snd_seq_port_subscribe_alloca(&subs);
  snd_seq_port_subscribe_set_sender(subs, &sender);
  snd_seq_port_subscribe_set_dest(subs, &dest);
  snd_seq_subscribe_port(seq_handle, subs);

  bool on=false;
  
  while(true)
  {      
  	snd_seq_event_t ev;
        snd_seq_ev_clear(&ev);
        snd_seq_ev_set_source(&ev, sender.port);
        snd_seq_ev_set_subs(&ev);
        snd_seq_ev_schedule_tick(&ev, myqueue, 0, 0);

	if(!on) {
		snd_seq_ev_set_noteon(&ev, 10, 38, 127);
		on=true;
	}
	else {
		snd_seq_ev_set_noteoff(&ev, 10, 38, 127);
		on=false;
	}

        snd_seq_event_output(seq_handle, &ev);
        snd_seq_drain_output(seq_handle);  // if necessary
	unsigned long fbpm = 1000000.0f * ( 60.0f / 140.0f / 2.0f );
	usleep(fbpm);

}
}

[CODE END]

Signature · 1 line
Science may someday discover what faith has always known.

/* seqdemo.c by Matthias Nagorni */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <alsa/asoundlib.h>

snd_seq_t *open_seq();
void midi_action(snd_seq_t *seq_handle);

snd_seq_t *open_seq() {

snd_seq_t *seq_handle;
int portid;

  if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0) {
    fprintf(stderr, "Error opening ALSA sequencer.\n");
    exit(1);
  }
  snd_seq_set_client_name(seq_handle, "Aseq Output");
  if ((portid = snd_seq_create_simple_port(seq_handle, "Aseq Output",
            SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
            SND_SEQ_PORT_TYPE_MIDI_GENERIC)) < 0) {
    fprintf(stderr, "Error creating sequencer port.\n");
    exit(1);

}
return(seq_handle);
}

void midi_action(snd_seq_t *seq_handle) {

snd_seq_event_t *ev;

  do {
    snd_seq_event_input(seq_handle, &ev);
    switch (ev->type) {
      case SND_SEQ_EVENT_CONTROLLER: 
        fprintf(stderr, "Control event on Channel %2d: %5d       \r",
                ev->data.control.channel, ev->data.control.value);
        break;
      case SND_SEQ_EVENT_PITCHBEND:
        fprintf(stderr, "Pitchbender event on Channel %2d: %5d   \r", 
                ev->data.control.channel, ev->data.control.value);
        break;
      case SND_SEQ_EVENT_NOTEON:
        fprintf(stderr, "Note On event on Channel %2d: %5d       \r",
                ev->data.control.channel, ev->data.note.note);
        break;        
      case SND_SEQ_EVENT_NOTEOFF: 
        fprintf(stderr, "Note Off event on Channel %2d: %5d      \r",         
                ev->data.control.channel, ev->data.note.note);           
        break;        
    }
    snd_seq_free_event(ev);

} while (snd_seq_event_input_pending(seq_handle, 0) > 0);
}

int main(int argc, char *argv[]) {

snd_seq_t *seq_handle;
int npfd;
struct pollfd *pfd;

  seq_handle = open_seq();
  
  int myqueue = snd_seq_alloc_named_queue(seq_handle, "my queue");
 
  snd_seq_queue_tempo_t *tempo;
  snd_seq_queue_tempo_alloca(&tempo);
  int iTempo = (int)(6e7 / ((double)140*(double)96*(double)96);
  snd_seq_queue_tempo_set_tempo(tempo, iTempo); // 60 BPM
  snd_seq_queue_tempo_set_ppq(tempo, 96); // 48 PPQ
  snd_seq_set_queue_tempo(seq_handle, myqueue, tempo);
  snd_seq_queue_tempo_free(tempo);
  
  snd_seq_addr_t sender, dest;
  snd_seq_port_subscribe_t *subs;
  sender.client = 131;
  sender.port = 0;
  dest.client = 130;
  dest.port = 1;
  snd_seq_port_subscribe_alloca(&subs);
  snd_seq_port_subscribe_set_sender(subs, &sender);
  snd_seq_port_subscribe_set_dest(subs, &dest);
  snd_seq_subscribe_port(seq_handle, subs);

  bool on=false;
  
  while(true)
  {      
  	snd_seq_event_t ev;
        snd_seq_ev_clear(&ev);
        snd_seq_ev_set_source(&ev, sender.port);
        snd_seq_ev_set_subs(&ev);
        snd_seq_ev_schedule_tick(&ev, myqueue, 0, 0);

	if(!on) {
		snd_seq_ev_set_noteon(&ev, 10, 38, 127);
		on=true;
	}
	else {
		snd_seq_ev_set_noteoff(&ev, 10, 38, 127);
		on=false;
	}

        snd_seq_event_output(seq_handle, &ev);
        snd_seq_drain_output(seq_handle);  // if necessary
	unsigned long fbpm = 1000000.0f * ( 60.0f / 140.0f / 2.0f );
	usleep(fbpm);

}
}