Delay in pressing button and sound(sf2) output from Android device

30 views
Skip to first unread message

Shivaraj Sajjan

unread,
Aug 8, 2020, 5:09:27 AM8/8/20
to android-midi
I am creating Android Project which has one button. I am playing sound using sound font files(sf2). This code is working fine. But when I play it there lot of latency (delay) between my button click and sound coming out of Android device. How to reduce this? Please help me

Code :


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
SF2Soundbank sf = new SF2Soundbank(getAssets().open("SmallTimGM6mb.sf2"));
synth = new SoftSynthesizer();
synth.open();
synth.loadAllInstruments(sf);
synth.getChannels()[0].programChange(0);
synth.getChannels()[1].programChange(1);
recv = synth.getReceiver();
} catch (IOException e) {
e.printStackTrace();
} catch (MidiUnavailableException e) {
e.printStackTrace();
}


this.findViewById(R.id.piano).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
if (action == MotionEvent.ACTION_DOWN) {
try {
ShortMessage msg = new ShortMessage();
msg.setMessage(ShortMessage.NOTE_ON, 0, 60, 127);
recv.send(msg, -1);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
try {
ShortMessage msg = new ShortMessage();
msg.setMessage(ShortMessage.NOTE_OFF, 0, 60, 127);
recv.send(msg, -1);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
}
return true;
}
});

}


Phil Burk

unread,
Aug 13, 2020, 2:57:01 PM8/13/20
to android-midi
Are you using the com.sun.media.sound.SF2Soundbank?
How are you doing that?

The Java implementation is probably using Java AudioTrack.
Hopefully they are using the PERFORMANCE_MODE_LOW_LATENCY.



That could reduce latency by 70-80 msec.

Note the sample rates must match the native rate.

Phil Burk


--
You received this message because you are subscribed to the Google Groups "android-midi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-midi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-midi/2f0ab52e-c014-406c-8e88-88413339d274o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages