Skip to contentSkip to conversation

troubles programming with the sequencer

DISCUSSION Brix 2 participants 4 messages Jan 23, 2004 – Jan 30, 2004

lore
Share & tools
lore

Brix

author
#0 lore raw

Hi, i'm developing an ear-trainer (an application to improve musical ears) and i must use the MIDI synth to play out chords, but i can't let the alsa sequencer work. My code doesn't return any error, i just can't hear anything. If i do it via the RawMIDI API, it works fine (but it's, bleahh, horrible to use). I thought it was because i didn't assign any path/instrument to the midi channels, so i tried to set them via RawMIDI, but it still doesn't work.

Here is the code:

#include <alsa/asoundlib.h>

snd_rawmidi_t *handle_out;

void set_chan_instr( int chan, int instrument ) { // sets patch/instrument number on a channel
int ch = 0xC0 + chan;
snd_rawmidi_write(handle_out, &ch, 1);
ch = instrument;
snd_rawmidi_write(handle_out, &ch, 1);
}

snd_seq_t *open_client() { // creates a client
snd_seq_t *handle;
int err;
err = snd_seq_open(&handle, "default", SND_SEQ_OPEN_DUPLEX, 0); if (err < 0)

    return NULL;

snd_seq_set_client_name(handle, "My Client");
return handle;
}

int main() {

snd_seq_t *seq;
snd_seq_queue_tempo_t *tempo;
snd_seq_event_t ev;
snd_seq_port_info_t *port_info, *wavetable;
int port;
int queue;

  // now we try to set up channel 0 via RawMIDI
  if ( snd_rawmidi_open( NULL, &handle_out, "hw:0,1", 0) ) {
    printf( "snd_rawmidi_open() failed\n" );
    return -1;
  }
  // ok, let's assign instrument #4
  set_chan_instr( 0, 4 );

  // we create a client...
  if ( ( seq = open_client() ) == NULL ) {
    printf( "Error in: snd_seq_open" );
    return -1;
  }
  // and a port
  port = snd_seq_create_simple_port(seq, "my port", SND_SEQ_PORT_CAP_DUPLEX, SND_SEQ_PORT_TYPE_APPLICATION);
  queue = snd_seq_alloc_queue(seq); // do allocate a queue
  snd_seq_queue_tempo_alloca(&tempo);
  snd_seq_queue_tempo_set_ppq(tempo, 96); // 96 pulses per quarter
  snd_seq_queue_tempo_set_tempo(tempo, 1000000); // 60 BPM
  snd_seq_set_queue_tempo(seq, queue, tempo);

  snd_seq_ev_clear(&ev); 
  snd_seq_ev_set_dest(&ev, 65, 0); // 65:0 works fine with pmidi!
  snd_seq_ev_set_source(&ev, port);
  snd_seq_ev_schedule_tick(&ev, queue, 0, 4); // starts at 4 ticks
  snd_seq_ev_set_note(&ev, 0, 60, 127, 8); // length 8 ticks
  snd_seq_event_output(seq, &ev);
  snd_seq_ev_schedule_tick(&ev, queue, 0, 12);
  snd_seq_ev_set_note(&ev, 0, 67, 127, 8);
  
  // let's play! (we hope..)
  snd_seq_event_output(seq, &ev);
  snd_seq_drain_output(seq);
  snd_seq_start_queue(seq, queue, NULL);
  snd_seq_drain_output(seq);

return 0;
}

Note that i can play midi files using pmidi on port 65:0. Hope you can help me,

Marco

Signature · 9 lines
	Hardware is never old.
	People just choose the wrong OS.


-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
#1 lore raw
Quoted 6 lines · Brix

My code doesn't return any error, i just can't hear anything. If i do it via the RawMIDI API, it works fine (but it's, bleahh, horrible to use).

I thought it was because i didn't assign any path/instrument to the midi channels,

The default instrument is 0 (grand piano).

Quoted 1 line

so i tried to set them via RawMIDI, but it still doesn't work.

You can send program change messages with the sequencer, too.

Quoted 3 lines

void set_chan_instr( int chan, int instrument ) {
int ch = 0xC0 + chan;
snd_rawmidi_write(handle_out, &ch, 1);

This does not work on big-endian machines, use char for ch.

Quoted 4 lines

// now we try to set up channel 0 via RawMIDI snd_rawmidi_open( NULL, &handle_out, "hw:0,1", 0) // ok, let's assign instrument #4 set_chan_instr( 0, 4 );

If the rawmidi device is already opened, it may not be possible for the sequencer to open it, too. Call snd_rawmidi_close, or leave this code out altogether.

Quoted 8 lines
  // let's play! (we hope..)
  snd_seq_event_output(seq, &ev);
  snd_seq_drain_output(seq);
  snd_seq_start_queue(seq, queue, NULL);
  snd_seq_drain_output(seq);

return 0;
}

Directly after starting the queue, the program exists, and the queue gets destroyed. Try snd_seq_sync_output.

HTH
Clemens

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn

author
#2 lore raw
Quoted 1 line · On Mon, 26 Jan 2004 14:52:59 +0100 (MET) Clemens Ladisch <[email protected]>…

The default instrument is 0 (grand piano).

Ok

Quoted 1 line

You can send program change messages with the sequencer, too.

This is what i do in the set_chan_instr() function, it works fine.

Quoted 1 line

This does not work on big-endian machines, use char for ch.

Ok

Quoted 3 lines

If the rawmidi device is already opened, it may not be possible for the sequencer to open it, too. Call snd_rawmidi_close, or leave this code out altogether.

Now i don't touch anything about instruments and channels, and i don't use any of the rawmidi functions, but it still doesn't work.

Quoted 2 lines

Directly after starting the queue, the program exists, and the queue gets destroyed. Try snd_seq_sync_output.

There is no such library function...
I tried calling sleep() for a few tenth of seconds, hoping the sequencer would flush out anything, but it did not help.

Signature · 9 lines
	Hardware is never old.
	People just choose the wrong OS.


-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
author
#3 lore raw
In reply to Clemens Ladisch
Quoted 1 line · On Mon, 26 Jan 2004 14:52:59 +0100 (MET) Clemens Ladisch <[email protected]>…

The default instrument is 0 (grand piano).

Ok

Quoted 1 line

You can send program change messages with the sequencer, too.

This is what i do in the set_chan_instr() function, it works fine.

Quoted 1 line

This does not work on big-endian machines, use char for ch.

Ok

Quoted 3 lines

If the rawmidi device is already opened, it may not be possible for the sequencer to open it, too. Call snd_rawmidi_close, or leave this code out altogether.

Now i don't touch anything about instruments and channels, and i don't use any of the rawmidi functions, but it still doesn't work.

Quoted 2 lines

Directly after starting the queue, the program exists, and the queue gets destroyed. Try snd_seq_sync_output.

There is no such library function...
I tried calling sleep() for a few tenth of seconds, hoping the sequencer would flush out anything, but it did not help.

Signature · 9 lines
	Hardware is never old.
	People just choose the wrong OS.


-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn