ALSA: seq: ump: Fix Note On/Off and key pressure conversion
Subsystems (4)
cover ALSA: seq: ump: Fix Note On/Off and key pressure conversion
Hello Takashi,
While extending seq-ump-test, I found three conversion bugs in seq_ump_convert.c. In each case the UMP core or the other sequencer path already does the right thing, and the fix follows it.
Patches 1-3 are the fixes. They also apply to for-linus. Patch 4 adds the tests on top of seq-ump-test.c in for-next.
Without patches 1-3, the new tests midi1_key_pressure, ump_midi1_to_midi2_note and ump_midi2_to_midi1_note fail and the other 25 pass. With them, all 28 pass.
Prepared with Claude Code (Claude Opus 5.5). Tested on x86_64 under qemu, on sound/for-next.
Best regards,
HyeongJun An
HyeongJun An (4):
ALSA: seq: ump: Don't turn a zero key pressure into a Note Off
ALSA: seq: ump: Convert a zero-velocity Note On to Note Off for MIDI
2.0
ALSA: seq: ump: Keep a soft MIDI 2.0 Note On from becoming a Note Off selftests/alsa: Test the sequencer UMP Note On/Off conversion
sound/core/seq/seq_ump_convert.c | 10 +- tools/testing/selftests/alsa/seq-ump-test.c | 187 +++++++++++++++++++- 2 files changed, 188 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/4 ALSA: seq: ump: Don't turn a zero key pressure into a Note Off
The conversion of legacy events to UMP MIDI 1.0 uses note_ev_to_ump_midi1() for KEYPRESS events as well as for note events. The function turns any event with zero velocity into a Note Off. So a Polyphonic Key Pressure of 0 reaches a UMP MIDI 1.0 client as a Note Off and ends the note.
Only a Note On with zero velocity means Note Off. To fix this, check the status, too.
The conversion to MIDI 2.0 already sends a zero key pressure as it is.
--- sound/core/seq/seq_ump_convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/seq/seq_ump_convert.c +1 -1
@@ -630,7 +630,7 @@ static int note_ev_to_ump_midi1(const struct snd_seq_event *event,union snd_ump_midi1_msg *data, unsigned char status) { -if (!event->data.note.velocity) + if (status == UMP_MSG_STATUS_NOTE_ON && !event->data.note.velocity) status =UMP_MSG_STATUS_NOTE_OFF ; data->note.status = status; data->note.channel = event->data.note.channel &0x0f ;
Signature · 1 line
2.43.0
PATCH 2/4 ALSA: seq: ump: Convert a zero-velocity Note On to Note Off for MIDI 2.0
In reply to HyeongJun AnWhen a UMP MIDI 1.0 client sends to a MIDI 2.0 client, cvt_ump_midi1_to_midi2() passes a Note On with zero velocity as a MIDI 2.0 Note On with zero velocity. The MIDI 1.0 sender meant a Note Off, but the MIDI 2.0 receiver gets a Note On.
The other MIDI 1.0 to MIDI 2.0 conversions send a Note Off here:note_ev_to_ump_midi2() for legacy sequencer events, and cvt_legacy_cmd_to_ump() in the UMP core. Do the same.
--- sound/core/seq/seq_ump_convert.c | 4 ++++ 1 file changed, 4 insertions(+)
sound/core/seq/seq_ump_convert.c +4 -0
@@ -380,6 +380,10 @@ static int cvt_ump_midi1_to_midi2(struct snd_seq_client *dest,switch (midi1->note.status) {case UMP_MSG_STATUS_NOTE_ON :case UMP_MSG_STATUS_NOTE_OFF : +/* MIDI 2.0 doesn't treat a note-on with 0 velocity as note-off */ +if (midi1->note.status ==UMP_MSG_STATUS_NOTE_ON && + !midi1->note.velocity) + midi2->note.status =UMP_MSG_STATUS_NOTE_OFF ; midi2->note.note = midi1->note.note; midi2->note.velocity =upscale_7_to_16bit (midi1->note.velocity);break ;
Signature · 1 line
2.43.0
PATCH 3/4 ALSA: seq: ump: Keep a soft MIDI 2.0 Note On from becoming a Note Off
In reply to HyeongJun AnThe cvt_ump_midi2_to_midi1() scales the 16-bit MIDI 2.0 velocity down to 7 bits. A Note On with a velocity below 0x200 becomes a MIDI 1.0 Note On with zero velocity, which a MIDI 1.0 receiver takes as a Note Off.
Send velocity 1 then, as ump_midi2_to_note_ev() does for legacy clients and cvt_ump_midi2_to_legacy() does in the UMP core.
--- sound/core/seq/seq_ump_convert.c | 4 ++++ 1 file changed, 4 insertions(+)
sound/core/seq/seq_ump_convert.c +4 -0
@@ -457,6 +457,10 @@ static int cvt_ump_midi2_to_midi1(struct snd_seq_client *dest,case UMP_MSG_STATUS_NOTE_OFF : midi1->note.note = midi2->note.note; midi1->note.velocity =downscale_16_to_7bit (midi2->note.velocity); +/* a note-on with 0 velocity would be a note-off in MIDI 1.0 */ +if (midi2->note.status ==UMP_MSG_STATUS_NOTE_ON && + !midi1->note.velocity) + midi1->note.velocity =1 ;break ;case UMP_MSG_STATUS_POLY_PRESSURE : midi1->paf.note = midi2->paf.note;
Signature · 1 line
2.43.0
PATCH 4/4 selftests/alsa: Test the sequencer UMP Note On/Off conversion
In reply to HyeongJun Anseq-ump-test only covers the RPN/NRPN conversion. Check that a Note On with zero velocity becomes a Note Off and that a key pressure of zero stays a key pressure, from a legacy client to UMP MIDI 1.0 and MIDI 2.0 clients. Also check Note On and Note Off between UMP MIDI 1.0 and MIDI 2.0 clients.
Without the three previous fixes, midi1_key_pressure, ump_midi1_to_midi2_note and ump_midi2_to_midi1_note fail.
--- tools/testing/selftests/alsa/seq-ump-test.c | 187 +++++++++++++++++++- 1 file changed, 179 insertions(+), 8 deletions(-)
tools/testing/selftests/alsa/seq-ump-test.c +179 -8
@@ -1,11 +1,11 @@// SPDX-License-Identifier: GPL-2.0 /* -* Test the conversion of MIDI 1.0 sequencer events to UMP. +* Test the sequencer conversion of MIDI 1.0 events to UMP, and of UMP +* packets between MIDI 1.0 and MIDI 2.0 clients. * -* A legacy client sends CC and CONTROL14 events to a UMP client, and the -* test checks the packets that the UMP client receives. The converter keeps -* its RPN/NRPN state per destination port and channel, so each test uses -* new clients. +* A client sends events to a UMP client, and the test checks the packets +* that the UMP client receives. The converter keeps its RPN/NRPN state +* per destination port and channel, so each test uses new clients. */ #include <errno.h> #include <fcntl.h>
@@ -124,6 +124,48 @@ static int send_event(struct seq_client *src, struct seq_client *dst,return 0 ; } +static int send_note (struct seq_client *src,struct seq_client *dst, +int type,int ch,int note,int velocity) +{ +struct snd_seq_event ev; + +memset (&ev,0 ,sizeof (ev)); + ev.type = type; + ev.flags =SNDRV_SEQ_TIME_STAMP_TICK |SNDRV_SEQ_EVENT_LENGTH_FIXED ; + ev.queue =SNDRV_SEQ_QUEUE_DIRECT ; + ev.source.client = src->id; + ev.source.port = src->port; + ev.dest.client = dst->id; + ev.dest.port = dst->port; + ev.data.note.channel = ch; + ev.data.note.note = note; + ev.data.note.velocity = velocity; +if (write (src->fd, &ev,sizeof (ev)) !=sizeof (ev)) +return -errno; +return 0 ; +} + +/* send a UMP packet from a UMP client */ +static int send_ump (struct seq_client *src,struct seq_client *dst, +unsigned int w0,unsigned int w1) +{ +struct snd_seq_ump_event ev; + +memset (&ev,0 ,sizeof (ev)); + ev.flags =SNDRV_SEQ_TIME_STAMP_TICK |SNDRV_SEQ_EVENT_LENGTH_FIXED | +SNDRV_SEQ_EVENT_UMP ; + ev.queue =SNDRV_SEQ_QUEUE_DIRECT ; + ev.source.client = src->id; + ev.source.port = src->port; + ev.dest.client = dst->id; + ev.dest.port = dst->port; + ev.ump[0 ] = w0; + ev.ump[1 ] = w1; +if (write (src->fd, &ev,sizeof (ev)) !=sizeof (ev)) +return -errno; +return 0 ; +} +/* * Read what the UMP client received until it is quiet, and keep the * packets of message type @mt. Returns the number of packets kept.
@@ -212,11 +254,11 @@ static int send_events(struct seq_client *src, struct seq_client *dst, ARRAY_SIZE(ev))); \ }while (0 ) -/* compare the RPN/NRPN packets received with the expected ones */ -#define EXPECT_PACKETS (...) do { \ +/* compare the packets of message type @mt with the expected ones */ +#define EXPECT_MT_PACKETS (mt, ...) do { \ const struct packet want[] = { __VA_ARGS__ }; \ struct packet got[MAX_PACKETS]; \ - int n = read_packets(&self->dst, 4, got); \ + int n = read_packets(&self->dst, mt, got); \ int i; \ \ ASSERT_EQ((int)ARRAY_SIZE(want), n); \
@@ -226,12 +268,22 @@ static int send_events(struct seq_client *src, struct seq_client *dst, } \ }while (0 ) +/* compare the MIDI 2.0 channel voice packets received */ +#define EXPECT_PACKETS (...) EXPECT_MT_PACKETS(4, __VA_ARGS__) +#define EXPECT_NO_PACKETS () do { \ struct packet got[MAX_PACKETS]; \ \ EXPECT_EQ(0, read_packets(&self->dst, 4, got)); \ } while (0) +#define SEND_NOTE (type, ch, note, velocity) \ + ASSERT_EQ(0, send_note(&self->src, &self->dst, \ + SNDRV_SEQ_EVENT_##type, ch, note, velocity)) + +#define SEND_UMP (w0, w1) \ + ASSERT_EQ(0, send_ump(&self->src, &self->dst, w0, w1)) +/* RPN 0/1 with Data Entry */ TEST_F(seq_ump, rpn) {
@@ -416,6 +468,20 @@ TEST_F(seq_ump, c14_null_nrpn_keeps_rpn_number) EXPECT_PACKETS({ RPN(0, 0, 1), data14(0x10, 0) }); } +/* a note-on with zero velocity is a note-off */ +TEST_F(seq_ump, note_on_zero_velocity) +{ +SEND_NOTE (NOTEON ,1 ,60 ,0 ); + EXPECT_PACKETS({ 0x40813c00, 0 }); +} + +/* a key pressure of zero is a key pressure, not a note-off */ +TEST_F(seq_ump, key_pressure) +{ +SEND_NOTE (KEYPRESS ,2 ,64 ,0 ); + EXPECT_PACKETS({ 0x40a24000, 0 }); +} +/* a UMP MIDI 1.0 client gets the CCs as they are, RPN selection included */ TEST (midi1_passthrough) {
@@ -449,4 +515,109 @@ TEST(midi1_passthrough)close_client (&dst); } +/* a pair of clients whose MIDI versions each test chooses */ +FIXTURE (seq_pair) { +struct seq_client src; +struct seq_client dst; +}; + +FIXTURE_SETUP (seq_pair) +{ + self->src.fd =-1 ; + self->dst.fd =-1 ; +} + +FIXTURE_TEARDOWN (seq_pair) +{ +close_client (&self->src); +close_client (&self->dst); +} + +static int open_pair (struct seq_client *dst,int dst_version, +struct seq_client *src,int src_version,int *skip) +{ +int err; + + err =open_client (dst, dst_version,CAP_IN , skip); +if (!err) + err =open_client (src, src_version,CAP_OUT , skip); +return err; +} + +/* to UMP MIDI 1.0, a key pressure of zero is a key pressure */ +TEST_F(seq_pair, midi1_key_pressure) +{ +int skip =0 ; +int err; + + err =open_pair (&self->dst,SNDRV_SEQ_CLIENT_UMP_MIDI_1_0 , + &self->src,0 , &skip); +if (skip) +SKIP (return,"UMP sequencer client unavailable: %s" , +strerror (-err)); +ASSERT_EQ (0 , err); + +SEND_NOTE (KEYPRESS ,2 ,64 ,0x50 ); +SEND_NOTE (KEYPRESS ,2 ,64 ,0 ); + EXPECT_MT_PACKETS(2, { 0x20a24050, 0 }, { 0x20a24000, 0 }); +} + +/* to UMP MIDI 1.0, a note-on with zero velocity is a note-off */ +TEST_F(seq_pair, midi1_note_on_zero_velocity) +{ +int skip =0 ; +int err; + + err =open_pair (&self->dst,SNDRV_SEQ_CLIENT_UMP_MIDI_1_0 , + &self->src,0 , &skip); +if (skip) +SKIP (return,"UMP sequencer client unavailable: %s" , +strerror (-err)); +ASSERT_EQ (0 , err); + +SEND_NOTE (NOTEON ,1 ,60 ,0 ); + EXPECT_MT_PACKETS(2, { 0x20813c00, 0 }); +} + +/* UMP MIDI 1.0 to MIDI 2.0: a note-on with zero velocity is a note-off */ +TEST_F(seq_pair, ump_midi1_to_midi2_note) +{ +int skip =0 ; +int err; + + err =open_pair (&self->dst,SNDRV_SEQ_CLIENT_UMP_MIDI_2_0 , + &self->src,SNDRV_SEQ_CLIENT_UMP_MIDI_1_0 , &skip); +if (skip) +SKIP (return,"UMP sequencer client unavailable: %s" , +strerror (-err)); +ASSERT_EQ (0 , err); + +SEND_UMP (0x20913c00 ,0 ); + EXPECT_PACKETS({ 0x40813c00, 0 }); +} + +/* +* UMP MIDI 2.0 to MIDI 1.0: a note-on whose velocity scales down to zero +* stays a note-on with velocity 1, and a note-off stays a note-off +*/ +TEST_F(seq_pair, ump_midi2_to_midi1_note) +{ +int skip =0 ; +int err; + + err =open_pair (&self->dst,SNDRV_SEQ_CLIENT_UMP_MIDI_1_0 , + &self->src,SNDRV_SEQ_CLIENT_UMP_MIDI_2_0 , &skip); +if (skip) +SKIP (return,"UMP sequencer client unavailable: %s" , +strerror (-err)); +ASSERT_EQ (0 , err); + +SEND_UMP (0x40913c00 ,0xc8000000 ); +SEND_UMP (0x40913c00 ,0x01000000 ); +SEND_UMP (0x40913c00 ,0 ); +SEND_UMP (0x40813c00 ,0 ); + EXPECT_MT_PACKETS(2, { 0x20913c64, 0 }, { 0x20913c01, 0 }, + { 0x20913c01, 0 }, { 0x20813c00, 0 }); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.43.0
Takashi Iwai
AppliedQuoted 20 lines · HyeongJun An
Hello Takashi,
While extending seq-ump-test, I found three conversion bugs in
seq_ump_convert.c. In each case the UMP core or the other sequencer path already does the right thing, and the fix follows it.Patches 1-3 are the fixes. They also apply to for-linus. Patch 4 adds the tests on top of
seq-ump-test.cin for-next.Without patches 1-3, the new tests
midi1_key_pressure,ump_midi1_to_midi2_noteandump_midi2_to_midi1_notefail and the other 25 pass. With them, all 28 pass.Prepared with Claude Code (Claude Opus 5.5). Tested on
x86_64under qemu, onsound/for-next.Best regards,
HyeongJun An
Quoted 6 lines
HyeongJun An (4):
ALSA: seq: ump: Don't turn a zero key pressure into a Note Off
ALSA: seq: ump: Convert a zero-velocity Note On to Note Off for MIDI2.0ALSA: seq: ump: Keep a soft MIDI 2.0 Note On from becoming a Note Off selftests/alsa: Test the sequencer UMP Note On/Off conversion
Applied all four patches now to for-next branch. Thanks.
Takashi