Skip to contentSkip to conversation

Development Question

DISCUSSION M. Gagnon 4 participants 9 messages Mar 17, 2007 – Mar 20, 2007 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

M. Gagnon

author
#1 lore raw

Hi, i am now trying to port an application to linux using Alsa. At some point, i have midi bytes as char* and i have to play them. This is exactly the same data you'd find in a file, expect that it's in memory instead. How is that possible?

If this is not possible, i could always take any help on playing midi using sequencers I looked at the docs, i found a few samples, but they seemed to be outdated and the one that could interest me segfaulted. After i debugged it and fixed the segfault, it played nothing... (and i'm sure my midi is configured correctly BTW, pmidi works and i also succeeded in getting single notes to play)

Thanks

_________________________________________________________________
Ne perdez pas de temps dans les files d’attente… magasinez en ligne.
http://magasiner.sympatico.msn.ca

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#2 lore raw
Quoted 11 lines · M. Gagnon

Hi, i am now trying to port an application to linux using Alsa. At some point, i have midi bytes as char* and i have to play them. This is exactly the same data you'd find in a file, expect that it's in memory instead. How is that possible?

If this is not possible, i could always take any help on playing midi using sequencers I looked at the docs, i found a few samples, but they seemed to be outdated and the one that could interest me segfaulted. After i debugged it and fixed the segfault, it played nothing... (and i'm sure my midi is configured correctly BTW, pmidi works and i also succeeded in getting single notes to play)

This is how I do playback in the freebob backend for jack:

for (s=0;s<samples_read;s++) {
signed int *byte=(buff+s);
snd_seq_event_t ev;
if ((snd_midi_event_encode_byte(

          port->parser,(*byte) & 0xFF, &ev)
        ) > 0) {
       // a midi message is complete, send it out to ALSA
       snd_seq_ev_set_subs(&ev);
       snd_seq_ev_set_direct(&ev);
       snd_seq_ev_set_source(&ev, port->seq_port_nr);
       snd_seq_event_output_direct(port->seq_handle, &ev);			   }
}

you can check the freebob_driver_midi_* functions in the freebob jack backend for more detail.

Pieter

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

author
#3 lore raw

This is interesting, Pieter, however my app already has the knowledge of notes and events, the only reason i had midi bytes was because i converted the events into bytes because then i could make them play using a single function on mac.

Apparently on Linux/Alsa it's not this way, so i should probably learn how to put a serie of events in a sequence and then play it. The most recent sample of this i found was dated '2002' and didn't work, and my own attempts with the documentation didn't either.

I am already able to send events directly. Where can i find recent infomation on how to schedule them in a queue and play them after?

thanks

Quoted 33 lines

From: Pieter Palmers <[email protected]>
To: "M. Gagnon" <[email protected]>
CC: [email protected]
Subject: Re: [Alsa-devel] Development Question
Date: Sat, 17 Mar 2007 22:36:31 +0100

Quoted 11 lines · M. Gagnon

Hi, i am now trying to port an application to linux using Alsa. At some point, i have midi bytes as char* and i have to play them. This is exactly the same data you'd find in a file, expect that it's in memory instead. How is that possible?

If this is not possible, i could always take any help on playing midi using sequencers I looked at the docs, i found a few samples, but they seemed to be outdated and the one that could interest me segfaulted. After i debugged it and fixed the segfault, it played nothing... (and i'm sure my midi is configured correctly BTW, pmidi works and i also succeeded in getting single notes to play)

This is how I do playback in the freebob backend for jack:

for (s=0;s<samples_read;s++) {
signed int *byte=(buff+s);
snd_seq_event_t ev;
if ((snd_midi_event_encode_byte(

         port->parser,(*byte) & 0xFF, &ev)
       ) > 0) {
      // a midi message is complete, send it out to ALSA
      snd_seq_ev_set_subs(&ev);
      snd_seq_ev_set_direct(&ev);
      snd_seq_ev_set_source(&ev, port->seq_port_nr);
      snd_seq_event_output_direct(port->seq_handle, &ev);			   }
}
Quoted 5 lines

you can check the freebob_driver_midi_* functions in the freebob jack backend for more detail.

Pieter

_________________________________________________________________
Windows Live Spaces: partagez vos photos avec vos amis!
http://spaces.live.com/?mkt=fr-ca

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#4 lore raw
Quoted 5 lines · M. Gagnon

This is interesting, Pieter, however my app already has the knowledge of notes and events, the only reason i had midi bytes was because i converted the events into bytes because then i could make them play using a single function on mac.

Quoted 7 lines

Apparently on Linux/Alsa it's not this way, so i should probably learn how to put a serie of events in a sequence and then play it. The most recent sample of this i found was dated '2002' and didn't work, and my own attempts with the documentation didn't either.

I am already able to send events directly. Where can i find recent infomation on how to schedule them in a queue and play them after?

I found the doxygen docs that come with alsa-devel sufficient.

What you have to do is convert your own 'events' into ALSA seq events, and then use the snd_seq_event_output function. Things as timestamps etc... are properties of ALSA seq events, and can be set using snd_seq_event_* functions.

Pieter

PS: One tricky thing if you use multiple ports is that the port an event should be sent to is also a property of the event.

Quoted 48 lines

thanks

Quoted 33 lines

From: Pieter Palmers <[email protected]>
To: "M. Gagnon" <[email protected]>
CC: [email protected]
Subject: Re: [Alsa-devel] Development Question
Date: Sat, 17 Mar 2007 22:36:31 +0100

Quoted 11 lines · M. Gagnon

Hi, i am now trying to port an application to linux using Alsa. At some point, i have midi bytes as char* and i have to play them. This is exactly the same data you'd find in a file, expect that it's in memory instead. How is that possible?

If this is not possible, i could always take any help on playing midi using sequencers I looked at the docs, i found a few samples, but they seemed to be outdated and the one that could interest me segfaulted. After i debugged it and fixed the segfault, it played nothing... (and i'm sure my midi is configured correctly BTW, pmidi works and i also succeeded in getting single notes to play)

This is how I do playback in the freebob backend for jack:

for (s=0;s<samples_read;s++) {
signed int *byte=(buff+s);
snd_seq_event_t ev;
if ((snd_midi_event_encode_byte(

         port->parser,(*byte) & 0xFF, &ev)
       ) > 0) {
      // a midi message is complete, send it out to ALSA
      snd_seq_ev_set_subs(&ev);
      snd_seq_ev_set_direct(&ev);
      snd_seq_ev_set_source(&ev, port->seq_port_nr);
      snd_seq_event_output_direct(port->seq_handle, &ev);			   }
}
Quoted 5 lines

you can check the freebob_driver_midi_* functions in the freebob jack backend for more detail.

Pieter

_________________________________________________________________
Windows Live Spaces: partagez vos photos avec vos amis!
http://spaces.live.com/?mkt=fr-ca

Quoted 10 lines

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

author
#5 lore raw

Okay, i tried with the docs. This is what i came up with, however i can't seem to be able to play anything more than the first event, like time was not moving, Perhaps i've missed something simple to make the time start?

I have pretty much followed everything that's there
http://www.alsa-project.org/alsa-doc/alsa-lib/seq.html
but no luck

What i am missing here? I guess it's something simple but i can't find

Thanks a lot for the support!

/*
all:
g++ midi_receiver.cpp -o AlsaNoteTest -lasound
*/

#include <stdio.h>
#include <alsa/asoundlib.h>
#include <vector>
#include <iostream>
#include <unistd.h>

// --------- declaration -------------
namespace AlsaNotePlayer
{

void  findDevices ();
void  openDevice (int client, int port);
void  closeDevice ();

void noteOn_seq(int channel, int note, int velocity, int tick); void noteOff_seq(int channel, int note, int velocity, int tick); void play();
void setTempo(int tempo);
}

// -------- definition ----------------

int main()
{
AlsaNotePlayer::findDevices(); AlsaNotePlayer::openDevice(128, 0); // replace by your ports as needed

AlsaNotePlayer::setTempo(60);

int time=0;
int duration = 200;

AlsaNotePlayer::noteOn_seq(1, 60, 127, time);
time += duration;
AlsaNotePlayer::noteOff_seq(1, 60, 0, time);

AlsaNotePlayer::noteOn_seq(1, 62, 127, time);
time += duration;
AlsaNotePlayer::noteOff_seq(1, 62, 0, time);

AlsaNotePlayer::noteOn_seq(1, 60, 127, time);
time += duration;
AlsaNotePlayer::noteOff_seq(1, 60, 0, time);

AlsaNotePlayer::noteOn_seq(1, 59, 127, time);
time += duration;
AlsaNotePlayer::noteOff_seq(1, 59, 0, time);

AlsaNotePlayer::play();
usleep(999999999);

}

namespace AlsaNotePlayer
{

class MidiDevice
{

public:

int client, port;
char* name;

MidiDevice(int client_arg, int port_arg, const char* name_arg) {
client = client_arg;
port = port_arg;
name = (char*)name_arg;
}

};

std::vector<MidiDevice> devices;

struct MidiContext

{
		snd_seq_t* sequencer;
		int queue;
		snd_seq_addr_t address;
		int port;
		snd_seq_port_subscribe_t *subs;
};

struct MidiContext MidiContext;
snd_seq_addr_t address;

void noteOn_seq(int channel, int note, int velocity, int tick)

{
	snd_seq_event_t event;

	snd_seq_ev_clear(&event);

	event.queue  = MidiContext.queue;
	event.source = MidiContext.address;

	snd_seq_ev_set_subs(&event);
	snd_seq_ev_set_source(&event, MidiContext.port);
	//snd_seq_ev_set_direct(&event);
	snd_seq_ev_set_noteon(&event, channel, note, velocity);
	snd_seq_ev_schedule_tick(&event, MidiContext.queue, 0, tick );

	snd_seq_event_output(MidiContext.sequencer, &event);

       std::cout << "pending events size: " << 

snd_seq_event_output_pending(MidiContext.sequencer) << std::endl; }

void noteOff_seq(int channel, int note, int velocity, int tick)

{
	snd_seq_event_t event;
	snd_seq_ev_clear(&event);

	event.queue  = MidiContext.queue;
	event.source = MidiContext.address;

	snd_seq_ev_set_subs(&event);
	//snd_seq_ev_set_direct(&event);
	snd_seq_ev_set_noteoff(&event, channel, note, velocity);
        snd_seq_ev_schedule_tick(&event, channel, 0 /*absolute*/, tick );

	snd_seq_event_output(MidiContext.sequencer, &event);

       std::cout << "pending events size: " << 

snd_seq_event_output_pending(MidiContext.sequencer) << std::endl; }

void play()

{
        snd_seq_start_queue(MidiContext.sequencer, MidiContext.queue, 0);

        int output = snd_seq_drain_output(MidiContext.sequencer);

	if(output == 0) std::cout << "all events drained" << std::endl;
	else if(output > 0) std::cout << output << "bytes remaining" << std::endl;
	else if(output < 0)
	{
		 fprintf (stderr, "Error: (%s)\n", snd_strerror (output));
	}
}

void findDevices()

{
	snd_seq_client_info_t* clientInfo;

	if (snd_seq_open(&MidiContext.sequencer, "default", SND_SEQ_OPEN_OUTPUT, 0) 
< 0)
	{
		return;
	}

	snd_seq_client_info_alloca(&clientInfo);
	snd_seq_client_info_set_client(clientInfo, -1);

	// iterate through clients
	while (snd_seq_query_next_client(MidiContext.sequencer, clientInfo) >= 0)
	{
		snd_seq_port_info_t* pinfo;

		snd_seq_port_info_alloca(&pinfo);
		snd_seq_port_info_set_client(pinfo, 
snd_seq_client_info_get_client(clientInfo));
		snd_seq_port_info_set_port(pinfo, -1);

		// and now through ports
		while (snd_seq_query_next_port(MidiContext.sequencer, pinfo) >= 0)
		{
			unsigned int capability = snd_seq_port_info_get_capability(pinfo);

			if ((capability & SND_SEQ_PORT_CAP_SUBS_WRITE) == 0)
			{
				continue;
			}

			int client  = (snd_seq_port_info_get_addr(pinfo))->client;
			int port    = (snd_seq_port_info_get_addr(pinfo))->port;

			std::cout << client << ":" << port << " --> " << 

snd_seq_port_info_get_name(pinfo) << std::endl;
devices.push_back( MidiDevice(client, port,
snd_seq_port_info_get_name(pinfo)) );

		}

	}

	if (snd_seq_set_client_name(MidiContext.sequencer, "Aria") < 0)
	{
		return;
	}

	MidiContext.address.port = 
snd_seq_create_simple_port(MidiContext.sequencer, "Aria Port 0", 
SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_APPLICATION);
	MidiContext.address.client = snd_seq_client_id (MidiContext.sequencer);
	MidiContext.queue = snd_seq_alloc_queue (MidiContext.sequencer);

	snd_seq_set_client_pool_output (MidiContext.sequencer, 1024);
}

void setTempo(int tempo_arg)
{
// argument is not yet considered

        snd_seq_queue_tempo_t *tempo;
        snd_seq_queue_tempo_alloca(&tempo);
        snd_seq_queue_tempo_set_tempo(tempo, 1000000); // 60 BPM
        snd_seq_queue_tempo_set_ppq(tempo, 48); // 48 PPQ
        snd_seq_set_queue_tempo(MidiContext.sequencer, MidiContext.queue, 

tempo);
}

void openDevice(int client, int port)

{
	address.client = client;
	address.port = port;

	snd_seq_port_subscribe_alloca(&MidiContext.subs);

	snd_seq_port_subscribe_set_sender(MidiContext.subs, &MidiContext.address);
	snd_seq_port_subscribe_set_dest(MidiContext.subs, &address);

	snd_seq_subscribe_port(MidiContext.sequencer, MidiContext.subs);
}

void closeDevice()

{
	snd_seq_port_subscribe_alloca(&MidiContext.subs);
	snd_seq_port_subscribe_set_sender(MidiContext.subs, &MidiContext.address);
	snd_seq_port_subscribe_set_dest(MidiContext.subs, &address);

	if (snd_seq_unsubscribe_port(MidiContext.sequencer, MidiContext.subs) < 0)
	{
		return;
	}
	//snd_seq_port_subscribe_free(MidiContext.subs);
}

}

Quoted 27 lines

From: Pieter Palmers <[email protected]>
To: "M. Gagnon" <[email protected]>
CC: [email protected]
Subject: Re: [Alsa-devel] Development Question
Date: Sun, 18 Mar 2007 11:29:32 +0100

Quoted 5 lines · M. Gagnon

This is interesting, Pieter, however my app already has the knowledge of notes and events, the only reason i had midi bytes was because i converted the events into bytes because then i could make them play using a single function on mac.

Quoted 7 lines

Apparently on Linux/Alsa it's not this way, so i should probably learn how to put a serie of events in a sequence and then play it. The most recent sample of this i found was dated '2002' and didn't work, and my own attempts with the documentation didn't either.

I am already able to send events directly. Where can i find recent infomation on how to schedule them in a queue and play them after?

I found the doxygen docs that come with alsa-devel sufficient.

What you have to do is convert your own 'events' into ALSA seq events, and then use the snd_seq_event_output function. Things as timestamps etc... are properties of ALSA seq events, and can be set using snd_seq_event_* functions.

Quoted 5 lines

Pieter

PS: One tricky thing if you use multiple ports is that the port an event should be sent to is also a property of the event.

_________________________________________________________________
Windows Live Spaces: partagez vos photos avec vos amis!
http://spaces.live.com/?mkt=fr-ca

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#6 lore raw
Quoted 3 lines · M. Gagnon

Okay, i tried with the docs. This is what i came up with, however i can't seem to be able to play anything more than the first event, like time was not moving, Perhaps i've missed something simple to make the time start?

What is the output of the program?
What are the contents of /proc/asound/seq/queues and .../timers while it's trying to play?

Regards,
Clemens

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

author
#7 lore raw
Quoted 2 lines

What is the output of the program?

14:0 --> Midi Through Port-0
128:0 --> TiMidity port 0
128:1 --> TiMidity port 1
128:2 --> TiMidity port 2
128:3 --> TiMidity port 3
pending events size: 28
pending events size: 56
pending events size: 84
pending events size: 112
pending events size: 140
pending events size: 168
pending events size: 196
pending events size: 224
pending events size: 252
pending events size: 280
pending events size: 308
pending events size: 336
pending events size: 364
pending events size: 392
pending events size: 420
pending events size: 448
BEFORE DRAIN: pending events size: 476
AFTER DRAIN: pending events size: 0
all events drained

everything seems okay to me (i may not be adding the same number of notes as in the sample i posted, and i have more console prints now, so small differences could appear from the code i posted)

Quoted 2 lines

What are the contents of /proc/asound/seq/queues and .../timers while it's trying to play?

i'm sorry, but despite some googling this still sounds esoteric to me. Any reference i could look at to know how to do this?

thanks

_________________________________________________________________
Participez au concours Tournée Live Mobile dès maintenant!
http://www.concertmobilelive.ca

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#8 lore raw

unsubscribe

Quoted 45 lines · M. Gagnon
Quoted 2 lines

What is the output of the program?

14:0 --> Midi Through Port-0
128:0 --> TiMidity port 0
128:1 --> TiMidity port 1
128:2 --> TiMidity port 2
128:3 --> TiMidity port 3
pending events size: 28
pending events size: 56
pending events size: 84
pending events size: 112
pending events size: 140
pending events size: 168
pending events size: 196
pending events size: 224
pending events size: 252
pending events size: 280
pending events size: 308
pending events size: 336
pending events size: 364
pending events size: 392
pending events size: 420
pending events size: 448
BEFORE DRAIN: pending events size: 476
AFTER DRAIN: pending events size: 0
all events drained

everything seems okay to me (i may not be adding the same number of notes as in the sample i posted, and i have more console prints now, so small differences could appear from the code i posted)

Quoted 2 lines

What are the contents of /proc/asound/seq/queues and .../timers while it's trying to play?

i'm sorry, but despite some googling this still sounds esoteric to me. Any reference i could look at to know how to do this?

thanks

_________________________________________________________________
Participez au concours Tournée Live Mobile dès maintenant!
http://www.concertmobilelive.ca

Quoted 10 lines

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#9 lore raw
In reply to M. Gagnon
Quoted 8 lines · M. Gagnon
Quoted 1 line

What is the output of the program?

pending events size: 28
pending events size: 56
pending events size: 84
...
BEFORE DRAIN: pending events size: 476
AFTER DRAIN: pending events size: 0

Looks OK. In theory, all events should be sent somewhere.

Please run aseqdump and send the events to its port. What does it output?

Quoted 4 lines
Quoted 2 lines

What are the contents of /proc/asound/seq/queues and .../timers while it's trying to play?

i'm sorry, but despite some googling this still sounds esoteric to me.

These are just file names. Run "cat /proc/asound/seq/queues" and "cat /proc/asound/seq/timer".

Regards,
Clemens

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV