Skip to contentSkip to conversation

sound: rawmidi: Add framing mode

PATCH In review David Henningsson 3 participants 3 messages Apr 19, 2021 – Apr 20, 2021 Sound

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 sound: rawmidi: Add framing mode

This commit adds a new framing mode that frames all MIDI data into 32-byte frames with a timestamp.

The main benefit is that we can get accurate timestamps even if userspace wakeup and processing is not immediate.

Testing on a Celeron N3150 with this mode has a max jitter of 2.8 ms, compared to the in-kernel seq implementation which has a max jitter of 5 ms during idle and much worse when running scheduler stress tests in parallel.

---

Changes since v5: Added realtime clock and changed params struct according to Jaroslav's wishes.

This version of the patch has been compile tested only.

 include/sound/rawmidi.h     |  2 +
 include/uapi/sound/asound.h | 30 ++++++++++++-
 sound/core/rawmidi.c        | 86 ++++++++++++++++++++++++++++++++++++-
 sound/core/rawmidi_compat.c |  4 +-
 4 files changed, 118 insertions(+), 4 deletions(-)
include/sound/rawmidi.h +2 -0
index 334842daa904..989e1517332d 100644
@@ -81,6 +81,8 @@ struct snd_rawmidi_substream {
 	bool opened;			/* open flag */
 	bool append;			/* append flag (merge more streams) */
 	bool active_sensing;		/* send active sensing when close */
+	unsigned int framing;		/* whether to frame input data */
+	unsigned int clock_type;	/* clock source to use for input framing */
 	int use_count;			/* use counter (for output) */
 	size_t bytes;
 	struct snd_rawmidi *rmidi;
include/uapi/sound/asound.h +28 -2
index 535a7229e1d9..773a00c0a1d8 100644
@@ -710,7 +710,7 @@ enum {
  *  Raw MIDI section - /dev/snd/midi??
  */
 
-#define SNDRV_RAWMIDI_VERSION		SNDRV_PROTOCOL_VERSION(2, 0, 1)
+#define SNDRV_RAWMIDI_VERSION		SNDRV_PROTOCOL_VERSION(2, 0, 2)
 
 enum {
 	SNDRV_RAWMIDI_STREAM_OUTPUT = 0,
@@ -736,12 +736,38 @@ struct snd_rawmidi_info {
 	unsigned char reserved[64];	/* reserved for future use */
 };
 
+#define SNDRV_RAWMIDI_MODE_FRAMING_MASK		(7<<0)
+#define SNDRV_RAWMIDI_MODE_FRAMING_SHIFT	0
+#define SNDRV_RAWMIDI_MODE_FRAMING_NONE		(0<<0)
+#define SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP	(1<<0)
+#define SNDRV_RAWMIDI_MODE_CLOCK_MASK		(7<<3)
+#define SNDRV_RAWMIDI_MODE_CLOCK_SHIFT		3
+#define SNDRV_RAWMIDI_MODE_CLOCK_NONE		(0<<3)
+#define SNDRV_RAWMIDI_MODE_CLOCK_REALTIME	(1<<3)
+#define SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC	(2<<3)
+#define SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW	(3<<3)
+
+#define SNDRV_RAWMIDI_FRAMING_DATA_LENGTH 16
+
+struct snd_rawmidi_framing_tstamp {
+	/* For now, frame_type is always 0. Midi 2.0 is expected to add new
+	 * types here. Applications are expected to skip unknown frame types.
+	 */
+	u8 frame_type;
+	u8 length; /* number of valid bytes in data field */
+	u8 reserved[2];
+	u32 tv_nsec;		/* nanoseconds */
+	u64 tv_sec;		/* seconds */
+	u8 data[SNDRV_RAWMIDI_FRAMING_DATA_LENGTH];
+} __attribute__((packed));
+
 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 int mode;		/* For input data only, frame incoming data */
+	unsigned char reserved[12];	/* reserved for future use */
 };
 
 #ifndef __KERNEL__
sound/core/rawmidi.c +85 -1

139 lines not on this page · raw message

sound/core/rawmidi_compat.c +3 -1
index 7397130976d0..68a93443583c 100644
@@ -13,7 +13,8 @@ struct snd_rawmidi_params32 {
 	u32 buffer_size;
 	u32 avail_min;
 	unsigned int no_active_sensing; /* avoid bit-field */
-	unsigned char reserved[16];
+	unsigned int mode;
+	unsigned char reserved[12];
 } __attribute__((packed));
 
 static int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile,
@@ -25,6 +26,7 @@ static int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile,
 	if (get_user(params.stream, &src->stream) ||
 	    get_user(params.buffer_size, &src->buffer_size) ||
 	    get_user(params.avail_min, &src->avail_min) ||
+	    get_user(params.mode, &src->mode) ||
 	    get_user(val, &src->no_active_sensing))
 		return -EFAULT;
 	params.no_active_sensing = val;
Signature · 1 line
2.25.1
maintainer Reviewed-by on v6 1/1
#1 lore raw

Dne 19. 04. 21 v 18:40 David Henningsson napsal(a):

Quoted 6 lines

This commit adds a new framing mode that frames all MIDI data into 32-byte frames with a timestamp.

The main benefit is that we can get accurate timestamps even if userspace wakeup and processing is not immediate.

Quoted 6 lines

Testing on a Celeron N3150 with this mode has a max jitter of 2.8 ms, compared to the in-kernel seq implementation which has a max jitter of 5 ms during idle and much worse when running scheduler stress tests in parallel.

Nice. Thank you.

sound/core/rawmidi.c static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait) · line 1613

Quoted 8 lines
sound/core/rawmidi.c +1 -0
@@ -1534,6 +1607,7 @@ static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
 /*
  */
 
+
 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
 				       struct snd_info_buffer *buffer)
 {

This is probably a typo chunk?

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#2 lore raw
In reply to David Henningsson
Quoted 12 lines · David Henningsson

This commit adds a new framing mode that frames all MIDI data into 32-byte frames with a timestamp.

The main benefit is that we can get accurate timestamps even if userspace wakeup and processing is not immediate.

Testing on a Celeron N3150 with this mode has a max jitter of 2.8 ms, compared to the in-kernel seq implementation which has a max jitter of 5 ms during idle and much worse when running scheduler stress tests in parallel.

Quoted 7 lines

---

Changes since v5: Added realtime clock and changed params struct according to Jaroslav's wishes.

This version of the patch has been compile tested only.

Thanks for the updated patch. It looks almost good, just some minor issues below:

include/uapi/sound/asound.h struct snd_rawmidi_info { · line 762

Quoted 11 lines
include/uapi/sound/asound.h +11 -0
+struct snd_rawmidi_framing_tstamp {
+	/* For now, frame_type is always 0. Midi 2.0 is expected to add new
+	 * types here. Applications are expected to skip unknown frame types.
+	 */
+	u8 frame_type;
+	u8 length; /* number of valid bytes in data field */
+	u8 reserved[2];
+	u32 tv_nsec;		/* nanoseconds */
+	u64 tv_sec;		/* seconds */
+	u8 data[SNDRV_RAWMIDI_FRAMING_DATA_LENGTH];
+} __attribute__((packed));

Use __packed instead.

Quoted 7 lines
sound/core/rawmidi.c +3 -0
@@ -720,7 +723,18 @@ EXPORT_SYMBOL(snd_rawmidi_output_params);
 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
 			     struct snd_rawmidi_params *params)
 {
+	unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
+	unsigned int clock_type = params->mode & SNDRV_RAWMIDI_MODE_CLOCK_MASK;
+

sound/core/rawmidi.c EXPORT_SYMBOL(snd_rawmidi_output_params); · line 738

Quoted 10 lines
sound/core/rawmidi.c +8 -0
+	if (framing == SNDRV_RAWMIDI_MODE_FRAMING_NONE && clock_type != SNDRV_RAWMIDI_MODE_CLOCK_NONE)
+		return -EINVAL;
+	else if (clock_type > SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW)
+		return -EINVAL;
+	if (framing > SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP)
+		return -EINVAL;
 	snd_rawmidi_drain_input(substream);
+	substream->framing = framing;
+	substream->clock_type = clock_type;
 	return resize_runtime_buffer(substream->runtime, params, true);

The stale framing and clock_type are set in the case of errors, which will confuse the later operations.

Quoted 3 lines
sound/core/rawmidi.c +3 -0
+static int receive_with_tstamp_framing(struct snd_rawmidi_substream *substream,
+			const unsigned char *buffer, int src_count, const struct timespec64 *tstamp)
+{

....

sound/core/rawmidi.c static int snd_rawmidi_control_ioctl(struct snd_card *card, · line 1014

Quoted 1 line
sound/core/rawmidi.c +1 -0
+	return dest_frames * frame_size;

The snd_rawmidi_receive() function is supposed to return the processed read size, hence this would become incompatible. Though, I don't find any code that actually checks the return value from snd_rawmidi_receive(), but a definition is a definition...

thanks,

Takashi