sound: rawmidi: Add framing mode
cover sound: rawmidi: Add framing mode
Hi,
When writing an application that records midi (and e g saves it to disk), ultra-low latency isn't really needed, all we need to know is exactly when the midi message came in. The application can then wake up once a second or so, to write the incoming data, including an accurate timestamp for every event, to disk.
As far as I can see, the rawmidi interface does not support such a feature at all. There is a snd_rawmidi_status syscall, but its timestamp field is not even filled by the kernel (!). But even if that was fixed, it would not fix the problem as there could be several midi events in the buffer with different timestamps.
You could use the seq interface, it does support timestamps, but I can see at least two potential problems with this:
First, the seq code runs in a work queue, not in the actual IRQ. This means that midi event is timestamped too late, especially so if the work is delayed for some reason.
Second, seq hard-codes the timestamp type to monotonic - there is no monotonic_raw, so the timestamp would be affected by NTP changes.
Also, the timespec uses 32-bit for sec and nsec, but I suspect this is less of a problem (unless people constantly record midi for sixty years or so...).
So here's a patch that adds a new "framing" mode that stuffs all MIDI data into 16 byte frames, 8 bytes of timestamp, one byte length, up to seven bytes of data. I'll follow up with an alsa-lib patch if this gets merged.
David Henningsson (1):
sound: rawmidi: Add framing mode
include/sound/rawmidi.h | 1 + include/uapi/sound/asound.h | 18 ++++++++++++++- sound/core/rawmidi.c | 45 ++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 2 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/1 sound: rawmidi: Add framing mode
This commit adds a new framing mode that frames all MIDI data into 16-byte frames with a timestamp from the monotonic_raw clock.
The main benefit is that we can get accurate timestamps even if userspace wakeup and processing is not immediate.
--- include/sound/rawmidi.h | 1 + include/uapi/sound/asound.h | 18 ++++++++++++++- sound/core/rawmidi.c | 45 ++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 2 deletions(-)
include/sound/rawmidi.h +1 -0
@@ -81,6 +81,7 @@ struct snd_rawmidi_substream {bool opened;/* open flag */ bool append;/* append flag (merge more streams) */ bool active_sensing;/* send active sensing when close */ +u8 framing;/* whether to frame data (for input) */ int use_count;/* use counter (for output) */ size_t bytes;struct snd_rawmidi *rmidi;
include/uapi/sound/asound.h +17 -1
@@ -736,12 +736,28 @@ struct snd_rawmidi_info {unsigned char reserved[64 ];/* reserved for future use */ }; +enum { +SNDRV_RAWMIDI_FRAMING_NONE =0 , +SNDRV_RAWMIDI_FRAMING_TSTAMP_MONOTONIC_RAW , +SNDRV_RAWMIDI_FRAMING_LAST =SNDRV_RAWMIDI_FRAMING_TSTAMP_MONOTONIC_RAW , +}; + +#define SND_RAWMIDI_FRAMING_DATA_LENGTH 7 + +struct snd_rawmidi_framing_tstamp { +unsigned int tv_sec;/* seconds */ +unsigned int tv_nsec;/* nanoseconds */ +unsigned char length; +unsigned char data[SND_RAWMIDI_FRAMING_DATA_LENGTH ]; +}; +struct snd_rawmidi_params {int stream;size_t buffer_size;/* queue size in bytes */ size_t avail_min;/* minimum avail bytes for wakeup */ unsigned int no_active_sensing:1 ;/* do not send active sensing byte in close() */ -unsigned char reserved[16 ];/* reserved for future use */ +unsigned char framing;/* For input data only, frame incoming data */ +unsigned char reserved[15 ];/* reserved for future use */ };#ifndef __KERNEL__
sound/core/rawmidi.c +44 -1
63 lines not on this page · raw message
Signature · 1 line
2.25.1
Jaroslav Kysela
maintainerDne 24. 03. 21 v 6:31 David Henningsson napsal(a):
Quoted 2 lines
This commit adds a new framing mode that frames all MIDI data into 16-byte frames with a timestamp from the
monotonic_rawclock.
I would add support for monotonic timestamps, too. The NTP drifts are usually small, so it may make sense to support those timestamps, too. It may be handy for the synchronization among multiple machines (timing sources).
The timestamp mode should be selected separately than the framing mode.
include/uapi/sound/asound.h struct snd_rawmidi_info { · line 752
Quoted 8 lines
include/uapi/sound/asound.h +8
-0+#define SND_RAWMIDI_FRAMING_DATA_LENGTH 7 + +struct snd_rawmidi_framing_tstamp { +unsigned int tv_sec;/* seconds */ +unsigned int tv_nsec;/* nanoseconds */ +unsigned char length; +unsigned char data[SND_RAWMIDI_FRAMING_DATA_LENGTH ]; +};
Perhaps, we should consider to have a fixed header and variable data length here. For MIDI, the standard messages have only few bytes usually. It would be better to use this space for the seconds field:
header {
unsigned long long tv_sec;
unsigned int tv_nsec;
unsigned int len;
unsigned char data[0];
};Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Quoted 8 lines · Jaroslav Kysela
Dne 24. 03. 21 v 6:31 David Henningsson napsal(a):
Quoted 2 lines
This commit adds a new framing mode that frames all MIDI data into 16-byte frames with a timestamp from the
monotonic_rawclock.I would add support for monotonic timestamps, too. The NTP drifts are usually small, so it may make sense to support those timestamps, too. It may be handy for the synchronization among multiple machines (timing sources).
The timestamp mode should be selected separately than the framing mode.
Okay, noted for v3.
Quoted 13 lines
include/uapi/sound/asound.h struct snd_rawmidi_info { · line 752
Quoted 9 lines
include/uapi/sound/asound.h +8
-0+#define SND_RAWMIDI_FRAMING_DATA_LENGTH 7 + +struct snd_rawmidi_framing_tstamp { +unsigned int tv_sec;/* seconds */ +unsigned int tv_nsec;/* nanoseconds */ +unsigned char length; +unsigned char data[SND_RAWMIDI_FRAMING_DATA_LENGTH ]; +};Perhaps, we should consider to have a fixed header and variable data length here. For MIDI, the standard messages have only few bytes usually. It would be better to use this space for the seconds field:
Quoted 6 lines
header {
unsigned long long tv_sec;
unsigned int tv_nsec;
unsigned int len;
unsigned char data[0];
};I considered that, but it has problems with alignment. If you have a normal midi message of 3 bytes, now your second tv_sec will end up starting on an odd byte, unless you add padding, and then that padding needs to be specified and so on. In addition, half of the header could end up in the end of the ring buffer and the other half in the beginning. So I found the 16 byte fixed version to be simpler and easier to implement correctly.
However if you like we could change the tv_sec to 64 bit and end up with:
#define SND_RAWMIDI_FRAMING_DATA_LENGTH 3
struct snd_rawmidi_framing_tstamp {
unsigned long long tv_sec; /* seconds */
unsigned int tv_nsec; /* nanoseconds */
unsigned char length;
unsigned char data[SND_RAWMIDI_FRAMING_DATA_LENGTH];
};We'll then have only three bytes for the actual data, but since that is what most midi messages are anyway, it would be okay, I assume.
// David
Jaroslav Kysela
maintainerDne 24. 03. 21 v 17:17 David Henningsson napsal(a):
Quoted 31 lines · Jaroslav Kysela
Quoted 8 lines
Dne 24. 03. 21 v 6:31 David Henningsson napsal(a):
Quoted 2 lines
This commit adds a new framing mode that frames all MIDI data into 16-byte frames with a timestamp from the
monotonic_rawclock.I would add support for monotonic timestamps, too. The NTP drifts are usually small, so it may make sense to support those timestamps, too. It may be handy for the synchronization among multiple machines (timing sources).
The timestamp mode should be selected separately than the framing mode.
Okay, noted for v3.
Quoted 13 lines
include/uapi/sound/asound.h struct snd_rawmidi_info { · line 752
Quoted 9 lines
include/uapi/sound/asound.h +8
-0+#define SND_RAWMIDI_FRAMING_DATA_LENGTH 7 + +struct snd_rawmidi_framing_tstamp { +unsigned int tv_sec;/* seconds */ +unsigned int tv_nsec;/* nanoseconds */ +unsigned char length; +unsigned char data[SND_RAWMIDI_FRAMING_DATA_LENGTH ]; +};Perhaps, we should consider to have a fixed header and variable data length here. For MIDI, the standard messages have only few bytes usually. It would be better to use this space for the seconds field:
Quoted 6 lines
header { unsigned long long tv_sec; unsigned int tv_nsec; unsigned int len; unsigned char data[0]; };
Quoted 7 lines
I considered that, but it has problems with alignment. If you have a normal midi message of 3 bytes, now your second
tv_secwill end up starting on an odd byte, unless you add padding, and then that padding needs to be specified and so on. In addition, half of the header could end up in the end of the ring buffer and the other half in the beginning. So I found the 16 byte fixed version to be simpler and easier to implement correctly.
I see. I agree that the fixed frame is easier to handle.
Quoted 4 lines
However if you like we could change the
tv_secto 64 bit and end up with:#define
SND_RAWMIDI_FRAMING_DATA_LENGTH3
Quoted 8 lines
struct snd_rawmidi_framing_tstamp { unsigned long long tv_sec; /* seconds */ unsigned int tv_nsec; /* nanoseconds */ unsigned char length; unsigned char data[SND_RAWMIDI_FRAMING_DATA_LENGTH]; };We'll then have only three bytes for the actual data, but since that is what most midi messages are anyway, it would be okay, I assume.
We can use the free bits in tv_nsec. It may be possible to carry 4 midi bytes with the 64-bit tv_sec field, too.
Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.