Author Topic: Streaming MIDI events  (Read 147 times)

gicci

  • Posts: 92
Streaming MIDI events
« on: 19 Sep '22 - 01:27 »
I am trying to stream MIDI events from a MIDI keyboard on Android on top of a MIDI file playback.

I have a question regarding the requirements of the ByteBuffer that shall be used: if I try to pass the buffer as received by the onSend() method of the Android MidiReceiver, wrapped in a ByteBuffer with

Code: [Select]
ByteBuffer data = ByteBuffer.wrap(msg, offset, count)
int n = XBASSMIDI.BASS_MIDI_StreamEvents(chanId,  BASS_MIDI_EVENTS_RAW, data, data.remaining())

I get n = 0 and no playback. If I make a copy of it before passing, it works:

Code: [Select]
byte[] x = new byte[count];
System.arraycopy(msg, offset, x, 0, count);
ByteBuffer data = ByteBuffer.wrap(x);
int n = XBASSMIDI.BASS_MIDI_StreamEvents(chanId,  BASS_MIDI_EVENTS_RAW, data, data.remaining())

it works. Could it be an issue related to the fact that offset is not zero?

Ian @ un4seen

  • Administrator
  • Posts: 24726
Re: Streaming MIDI events
« Reply #1 on: 19 Sep '22 - 14:59 »
Yes, I suspect the issue there will be the offset, as BASS/BASSMIDI always reads from the start of the provided ByteBuffer. Does the problem only happen when it isn't 0?

Instead of creating a new array and wrapping it, you could try using allocateDirect to create the ByteBuffer and use the "put" method to copy the array to it. That may be a bit faster.

Code: [Select]
ByteBuffer data = ByteBuffer.allocateDirect(count);
data.put(msg, offset, count);

gicci

  • Posts: 92
Re: Streaming MIDI events
« Reply #2 on: 19 Sep '22 - 23:59 »
Yes, I have just tested and forcing offset 1 with the copied array doesn't work as well. I will reuse the same allocated objects performing only the copy, the amount of data is quite low and it will not be an issue.

I have another question. Currently I use a MIDI stream in decode mode plugged in a mixer. To pause the MIDI file playback I pause the mixer, but this of course besides pausing the MIDI file playback also prevents the playback of the events received from the MIDI device. I guess this would happen the same if I remove the mixer and pause the MIDI stream.
Is there any possibility to pause the loading of the events from the MIDI file, maintaining the possibility to stream MIDI events?
An alternative I am thinking about is to use another MIDI stream for this purpose, where I could replicate the same setup of the MIDI playback stream. In this case I guess anyway I cannot plug it in the same mixer, where I have audio effects (equalizer etc.) and I then should replicate them as well. Any suggestion?

Thank you!

Ian @ un4seen

  • Administrator
  • Posts: 24726
Re: Streaming MIDI events
« Reply #3 on: 20 Sep '22 - 16:01 »
Yes, if you want to be able to play events independently of the MIDI file then you would need to create a separate MIDI stream. You could plug both into the same mixer. When you want to pause the MIDI file stream, set the BASS_MIXER_CHAN_PAUSE flag on it via BASS_Mixer_ChannelFlags (and unset the flag to resume).