Skip to contentSkip to conversation

Rawmidi mystery

DISCUSSION Frank Neumann 4 participants 9 messages Feb 4, 2003 – Feb 7, 2003

lore
Share & tools
lore
#0 lore raw

Hi list,

I have written a small helper program (for myself so far) that should allow me to get a backup of all data from a Korg M3r expander through the ALSA rawmidi API. I have written a first version that opens the rawmidi device, tells the user to "start the dump on the M3r", and then waits for the beginning of the SysEx data and stores that into a file. This one works nicely.

Then, I modified it just a bit so that it does not passively wait but instead actively sends the SysEx sequence for "Get All Program/Combination/ Drum/Global data" and then records everything that comes back.

Normally, according to the M3r's SysEx implementation in its manual, this should give the same output, exactly 23963 bytes.
However, the second version loses about 130 bytes during the transfer. The strange thing is that the beginning of the data (SysEx header:
F0 42 30 24...) is fine, and starting around byte 700 the data is exactly the same as in the correct output, including the EOX (F7) at the very end of the output. But somewhere in this first part, data is lost.

Even worse: Starting the same program with the same arguments gives slightly differing number of output bytes each time:

franky@faramir:~/work/lakai/c/alsa/m3r> ./m3r2 a1 rawmidi device opened sucessfully.
Now sending ALL DATA DUMP REQUEST...
[Waiting for SysEx sequence to start]
[SysEx start code found, writing data]... [SysEx EOX code found, finishing - 23832 bytes written]

                                   ^^^^^

franky@faramir:~/work/lakai/c/alsa/m3r> ./m3r2 a2 rawmidi device opened sucessfully.
Now sending ALL DATA DUMP REQUEST...
[Waiting for SysEx sequence to start]
[SysEx start code found, writing data]... [SysEx EOX code found, finishing - 23833 bytes written]

                                   ^^^^^

franky@faramir:~/work/lakai/c/alsa/m3r> ./m3r2 a3 rawmidi device opened sucessfully.
Now sending ALL DATA DUMP REQUEST...
[Waiting for SysEx sequence to start]
[SysEx start code found, writing data]... [SysEx EOX code found, finishing - 23836 bytes written]

                                   ^^^^^

This number was observed to be in a range of +-20 bytes.
Could anyone explain this? Though the MIDI interface on the PC side is in a cheapo Soundblaster PCI512 card, I don't think it's responsible for data losses. To me this looks rather like a rawmidi problem. The program really doesn't do much more but snd_rawmidi_open, snd_rawmidi_write, snd_rawmidi_read, snd_rawmidi_drain and snd_rawmidi_close.

I tried this with 0.9.0rc6 and 0.9.0rc7, both give the same results.

If anyone needs more input or wants to look at the tiny source, let me know. All input is appreciated.

Thanks for reading,
Frank

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#1 lore raw
Quoted 6 lines · Frank Neumann

I have written a small helper program (for myself so far) that should allow me to get a backup of all data from a Korg M3r expander through the ALSA rawmidi API. I have written a first version that opens the rawmidi device, tells the user to "start the dump on the M3r", and then waits for the beginning of the SysEx data and stores that into a file. This one works nicely.

Something like
echo "Please start the dump on the M3r"
cat /dev/snd/midiC0D0 > some_file
? :-)

Quoted 11 lines

Then, I modified it just a bit so that it does not passively wait but instead actively sends the SysEx sequence for "Get All Program/Combination/ Drum/Global data" and then records everything that comes back.

Normally, according to the M3r's SysEx implementation in its manual, this should give the same output, exactly 23963 bytes.
However, the second version loses about 130 bytes during the transfer. The strange thing is that the beginning of the data (SysEx header:
F0 42 30 24...) is fine, and starting around byte 700 the data is exactly the same as in the correct output, including the EOX (F7) at the very end of the output. But somewhere in this first part, data is lost.

This looks like a buffer overrun, but then I don't understand why the first version of your program would work. Is there anything suspicious in /proc/asound/card0/midi0?

It may be possible that writing the data to disk between rawmidi_read() calls takes too much time. I'd like to see the source.

HTH
Clemens

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#2 lore raw
In reply to Frank Neumann

Hi Frank,

At Wed, 5 Feb 2003 00:34:41 +0100,

Quoted 15 lines · Frank Neumann

Hi list,

I have written a small helper program (for myself so far) that should allow me to get a backup of all data from a Korg M3r expander through the ALSA rawmidi API. I have written a first version that opens the rawmidi device, tells the user to "start the dump on the M3r", and then waits for the beginning of the SysEx data and stores that into a file. This one works nicely.

Then, I modified it just a bit so that it does not passively wait but instead actively sends the SysEx sequence for "Get All Program/Combination/ Drum/Global data" and then records everything that comes back.

Quoted 7 lines

Normally, according to the M3r's SysEx implementation in its manual, this should give the same output, exactly 23963 bytes.
However, the second version loses about 130 bytes during the transfer. The strange thing is that the beginning of the data (SysEx header:
F0 42 30 24...) is fine, and starting around byte 700 the data is exactly the same as in the correct output, including the EOX (F7) at the very end of the output. But somewhere in this first part, data is lost.

just to be sure: any realtime events can come in?

Quoted 2 lines

Even worse: Starting the same program with the same arguments gives slightly differing number of output bytes each time:

...

Quoted 6 lines

This number was observed to be in a range of +-20 bytes.
Could anyone explain this? Though the MIDI interface on the PC side is in a cheapo Soundblaster PCI512 card, I don't think it's responsible for data losses. To me this looks rather like a rawmidi problem. The program really doesn't do much more but snd_rawmidi_open, snd_rawmidi_write, snd_rawmidi_read, snd_rawmidi_drain and snd_rawmidi_close.

as Clemens wrote, please check the buffer overruns.
the buffer overruns are shown in /proc/asound/card0/midiX proc file but only during the midi is opened for read.

ciao,

Takashi

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#3 lore raw

Hi list,
Takashi Iwai <[email protected]> wrote:

[..]

Quoted 9 lines
Quoted 7 lines

Normally, according to the M3r's SysEx implementation in its manual, this should give the same output, exactly 23963 bytes.
However, the second version loses about 130 bytes during the transfer. The strange thing is that the beginning of the data (SysEx header:
F0 42 30 24...) is fine, and starting around byte 700 the data is exactly the same as in the correct output, including the EOX (F7) at the very end of the output. But somewhere in this first part, data is lost.

just to be sure: any realtime events can come in?

No, I checked that. The M3r sends active sensing (0xfe), but I filtered that out. In the data I get the only MIDI bytes > 0x7f are the F0 at the start and the 0xF7 at the very end. Also, I think a SysEx message cannot be interrupted by any other message, right?

Quoted 10 lines
Quoted 6 lines

This number was observed to be in a range of +-20 bytes.
Could anyone explain this? Though the MIDI interface on the PC side is in a cheapo Soundblaster PCI512 card, I don't think it's responsible for data losses. To me this looks rather like a rawmidi problem. The program really doesn't do much more but snd_rawmidi_open, snd_rawmidi_write, snd_rawmidi_read, snd_rawmidi_drain and snd_rawmidi_close.

as Clemens wrote, please check the buffer overruns.
the buffer overruns are shown in /proc/asound/card0/midiX proc file but only during the midi is opened for read.

I checked that - no overruns during the whole transfer. At the end of the transfer (but before the device is closed) I see:
franky@faramir:/proc/asound/card0> cat midi0
EMU10K1 MPU-401 (UART)

Output 0

  Tx bytes     : 7
  Mode         : native
  Buffer size  : 4096
  Avail        : 4096
Input 0
  Rx bytes     : 23860
  Buffer size  : 4096
  Avail        : 24
  Overruns     : 0

The 24 avail input bytes are ActiveSensing bytes.

I have attached the small program to this mail for your studying pleasure :-}. If anything is wrong, I'd certainly love to hear about that. This is the first ALSA rawmidi program I've written.

I'll dive into the M3r's SysEx docs and the dump I get once more; maybe there is something I overlooked.

Thanks,
Frank

/*
* m3rx: Small ALSA-based (using rawmidi API) program to get a complete bulk dump from
* a Korg M3r expander module. No checksums, no flow control, nothing yet. *
* Copyright (c)2003 Frank Neumann
*
*/

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

#define ST_START 0x00
#define ST_IN_SYSEX 0x01
#define ST_DONE 0x02

/*** START OF MAIN FUNCTION ***/
int main(int argc, char* argv[])

{
	int err, state, numbytes;
	unsigned char ch;
	unsigned char sysex_get_all[] = { 0xf0, 0x42, 0x30, 0x24, 0x0f, 0x00, 0xf7 };

	FILE *fp1;
	snd_rawmidi_t *raw_in = 0, *raw_out = 0;

	if (argc != 2)
	{
		fprintf(stderr, "Usage: %s <outputsysexfilename>\n", argv[0]);
		exit(5);
	}

	fp1 = fopen(argv[1], "w");
	if (fp1 == NULL)
	{
		fprintf(stderr, "Unable to open output file '%s'\n", argv[1]);
		exit(5);
	}
			
	err = snd_rawmidi_open(&raw_in, NULL, "default", 0);	
	if (err)
	{
		fprintf(stderr,"snd_rawmidi_open 'default' failed: %d\n", err);
		exit(5);
	}

	err = snd_rawmidi_open(NULL, &raw_out, "default", 0);	
	if (err)
	{
		fprintf(stderr,"snd_rawmidi_open(2) 'default' failed: %d\n", err);
		exit(5);
	}

	fprintf(stderr, "rawmidi device opened sucessfully.\n");
	fprintf(stderr, " Now sending ALL DATA DUMP REQUEST...\n");
	snd_rawmidi_write(raw_out, &sysex_get_all, sizeof(sysex_get_all));
	snd_rawmidi_drain(raw_out); 

	fprintf(stderr, "[Waiting for SysEx sequence to start]\n");
	
	state = ST_START;
	numbytes = 0;
	while (state != ST_DONE)
	{
		snd_rawmidi_read(raw_in,&ch,1);
		switch(state)
		{
			case ST_START:
					if (ch == 0xf0)
					{
						fprintf(stderr, "[SysEx start code found, writing data]...\n");
						state = ST_IN_SYSEX;
						fputc(ch, fp1);
						numbytes++;
//						fprintf(stderr, "\r%06d bytes written...   ", numbytes);
					}
					break;
			case ST_IN_SYSEX:
					if (ch < 0x80)
					{
						fputc(ch, fp1);
						numbytes++;
//						fprintf(stderr, "\r%06d bytes written...   ", numbytes);
					}
					else if (ch == 0xf7) /* EOX */
					{
						fputc(ch, fp1);
						numbytes++;
//						fprintf(stderr, "\r%06d bytes written...   ", numbytes);
						fprintf(stderr, "\n[SysEx EOX code found, finishing - %d bytes written]\n", 
								numbytes);
						state = ST_DONE;
					}
					break;

			default:
				break;
		}
	}

	snd_rawmidi_drain(raw_in); 
	snd_rawmidi_close(raw_in);	
	snd_rawmidi_close(raw_out);	
	fclose(fp1);
}
#4 lore raw
Quoted 7 lines · Frank Neumann
Quoted 1 line · Takashi Iwai

just to be sure: any realtime events can come in?

No, I checked that. The M3r sends active sensing (0xfe), but I filtered that out. In the data I get the only MIDI bytes > 0x7f are the F0 at the start and the 0xF7 at the very end. Also, I think a SysEx message cannot be interrupted by any other message, right?

Realtime messages can interrupt any other message, but it's unlikely that your device would send one.

Quoted 1 line

I checked that - no overruns during the whole transfer.

Maybe some bytes have been ignored. Please try the following patch:

m3r2.c renamed from m3r2.c.orig +4 -0
@@ -75,6 +75,8 @@
 						numbytes++;
 //						fprintf(stderr, "\r%06d bytes written...   ", numbytes);
 					}
+					else if (ch != 0xfe)
+						fprintf(stderr, "dropped %02x\n", ch);
 					break;
 			case ST_IN_SYSEX:
 					if (ch < 0x80)
@@ -92,6 +94,8 @@
 								numbytes);
 						state = ST_DONE;
 					}
+					else if (ch != 0xfe)
+						fprintf(stderr, "dropped %02x\n", ch);
 					break;
 
 			default:

It may be possible that your program waits too long before starting to read data. (If some bytes come in before your first call to rawmidi_read(), they're simply ignored.)
Please try removing the drain() in line 58.

HTH
Clemens

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#5 lore raw

Hi again list,
Clemens Ladisch <[email protected]> wrote:

[..]

Quoted 7 lines
Quoted 4 lines

No, I checked that. The M3r sends active sensing (0xfe), but I filtered that out. In the data I get the only MIDI bytes > 0x7f are the F0 at the start and the 0xF7 at the very end. Also, I think a SysEx message cannot be interrupted by any other message, right?

Realtime messages can interrupt any other message, but it's unlikely that your device would send one.

Oh..yes, you're right. I just looked it up in the MIDI spec. Wow. I wonder how many MIDI parsers are out there that do not handle this correctly :-).

Quoted 3 lines
Quoted 1 line

I checked that - no overruns during the whole transfer.

Maybe some bytes have been ignored. Please try the following patch:

[patch removed]

No, that's not it. The resulting number of bytes was _smaller_ than expected, so if there had been active sensing bytes, they would have enlarged the file size. Also, I checked the stream and found no bytes > 0x7f between F0 and F7.

Quoted 4 lines

It may be possible that your program waits too long before starting to read data. (If some bytes come in before your first call to rawmidi_read(), they're simply ignored.)
Please try removing the drain() in line 58.

And YES, that did the trick! Thanks a lot, Clemens. Looks like the drain() takes a lot longer than I'd have expected (my thought was that it would return right after the last "command" bytes have been sent out, and that the incoming stream would be held by the buffer (4096 bytes, right) for a little while, so I didn't expect any losses there.

I find this a little odd - especially since it was not the _beginning_ of the stream that is missing, but a couple of bytes a little later. But, what the heck - it works now, I learnt something - thanks! :-)

Greetings,
Frank

PS: I checked with gettimeofday(): The snd_rawmidi_drain() takes about 47-49ms on my system..quite long, huh? I mean, the command sequence is just 7 bytes, that's about 2.24ms of "raw" MIDI transfer time..

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#6 lore raw
Quoted 10 lines · Frank Neumann

And YES, that did the trick! Thanks a lot, Clemens. Looks like the drain() takes a lot longer than I'd have expected (my thought was that it would return right after the last "command" bytes have been sent out, and that the incoming stream would be held by the buffer (4096 bytes, right) for a little while, so I didn't expect any losses there.

I find this a little odd - especially since it was not the _beginning_ of the stream that is missing, but a couple of bytes a little later. But, what the heck - it works now, I learnt something - thanks! :-)

Quoted 6 lines

Greetings,
Frank

PS: I checked with gettimeofday(): The snd_rawmidi_drain() takes about 47-49ms on my system..quite long, huh? I mean, the command sequence is just 7 bytes, that's about 2.24ms of "raw" MIDI transfer time..

It looks like a serious bug in the driver. Could you resend me privately your utility again? Thanks.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#7 lore raw
In reply to Frank Neumann
Quoted 3 lines · Frank Neumann

PS: I checked with gettimeofday(): The snd_rawmidi_drain() takes about 47-49ms on my system..quite long, huh? I mean, the command sequence is just 7 bytes, that's about 2.24ms of "raw" MIDI transfer time..

There are two possibilities how the _drain() can be implemented:
1) The driver knows when the FIFO is empty, and waits until then, or
2) Nothing is known, so ALSA simply waits for 50 ms.

Currently, there isn't any driver which implements 1). :-(

Clemens

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#8 lore raw
Quoted 10 lines · Clemens Ladisch
Quoted 3 lines · Frank Neumann

PS: I checked with gettimeofday(): The snd_rawmidi_drain() takes about 47-49ms on my system..quite long, huh? I mean, the command sequence is just 7 bytes, that's about 2.24ms of "raw" MIDI transfer time..

There are two possibilities how the _drain() can be implemented:
1) The driver knows when the FIFO is empty, and waits until then, or
2) Nothing is known, so ALSA simply waits for 50 ms.

Currently, there isn't any driver which implements 1). :-(

Yes, you got it. I forgot this algorithm. Unfortunately, the most of cards don't allow us to read the FIFO size / state, so it's a bit problematic to determine the real time when all data are flushed to device.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com