Author Topic: BASS for Android  (Read 702748 times)

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1825 on: 10 Feb '21 - 15:01 »
BASS_CONFIG_DEV_NONSTOP is disabled by default, so if you aren't changing that setting (via BASS_SetConfig) then that won't be what's causing the problem. But do check whether you're leaving the MIDI stream playing even when it's not playing any events, ie. call BASS_ChannelStop (or BASS_StreamFree) when it isn't in use.

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1826 on: 15 Feb '21 - 08:59 »
BASS_CONFIG_DEV_NONSTOP is disabled by default, so if you aren't changing that setting (via BASS_SetConfig) then that won't be what's causing the problem. But do check whether you're leaving the MIDI stream playing even when it's not playing any events, ie. call BASS_ChannelStop (or BASS_StreamFree) when it isn't in use.
Thanks. I also have removed the WAKE_LOCK permission ir order that the Android OS can destroy the app in background when needed (this fact has stopped the error). Actually, when the app calls to onPause(), the MIDI stream is stopped, but I will  also implement the ChannelStop.

Regarding the latency, any more advices to try to reduce it even more? Reducing BASS_CONFIG_DEV_PERIOD and BASS_CONFIG_DEV_BUFFER to minimum values (with the given config in previous posts), the app achieves around 70-75ms in playing a sound after tapping a key on the screen (Android 10 on a SnapDragon 855), but I think AAudio can get better results (around 50ms). Any advices in configuration? maybe reducing the frequency of Bass initialization or disabling some other optimizations? Thanks


Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1827 on: 15 Feb '21 - 16:51 »
Regarding the latency, any more advices to try to reduce it even more? Reducing BASS_CONFIG_DEV_PERIOD and BASS_CONFIG_DEV_BUFFER to minimum values (with the given config in previous posts), the app achieves around 70-75ms in playing a sound after tapping a key on the screen (Android 10 on a SnapDragon 855), but I think AAudio can get better results (around 50ms). Any advices in configuration? maybe reducing the frequency of Bass initialization or disabling some other optimizations? Thanks

The only other config option is BASS_CONFIG_ANDROID_AAUDIO, which controls whether AAudio is used and whether in low-latency mode. That's enabled by default (when available), so nothing to change. Of course, make sure you don't use the BASS_DEVICE_AUDIOTRACK flag in your BASS_Init call to request AudioTrack output instead.

Are you seeing lower latency from other apps? Different devices can have different latency, even if they have the same Android OS version, so comparisons should be made on the same device.

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1828 on: 15 Feb '21 - 18:02 »
Regarding the latency, any more advices to try to reduce it even more? Reducing BASS_CONFIG_DEV_PERIOD and BASS_CONFIG_DEV_BUFFER to minimum values (with the given config in previous posts), the app achieves around 70-75ms in playing a sound after tapping a key on the screen (Android 10 on a SnapDragon 855), but I think AAudio can get better results (around 50ms). Any advices in configuration? maybe reducing the frequency of Bass initialization or disabling some other optimizations? Thanks

The only other config option is BASS_CONFIG_ANDROID_AAUDIO, which controls whether AAudio is used and whether in low-latency mode. That's enabled by default (when available), so nothing to change. Of course, make sure you don't use the BASS_DEVICE_AUDIOTRACK flag in your BASS_Init call to request AudioTrack output instead.

Are you seeing lower latency from other apps? Different devices can have different latency, even if they have the same Android OS version, so comparisons should be made on the same device.

Thanks for your reply. I have tried the BASS_CONFIG_ANDROID_AAUDIO option in the past, but now it is kept in default values, so it is fine then.

Correct, I have seen other apps with lower latency in the same device.

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1829 on: 16 Feb '21 - 17:22 »
Please check what minbuf/latency/initflags/speakers/freq values you get from BASS_GetInfo (after calling BASS_Init).

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1830 on: 17 Feb '21 - 09:17 »
Please check what minbuf/latency/initflags/speakers/freq values you get from BASS_GetInfo (after calling BASS_Init).

This is what BASS_GetInfo reports:

Code: [Select]
    minbuf: 10
    latency: 35
    initflags: 0
    speakers: 2
    freq: 48000

Thanks

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1831 on: 17 Feb '21 - 14:41 »
That looks like the default BASS_CONFIG_DEV_BUFFER (40) and BASS_CONFIG_DEV_PERIOD (10) settings are in effect. Have you tried lowering them? If not, please give that a try and see what you get then. I would suggest first try just setting BASS_CONFIG_DEV_BUFFER to 30. Note the changes need to be made before calling BASS_Init (or call BASS_Free and BASS_Init again).

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1832 on: 17 Feb '21 - 15:58 »
That looks like the default BASS_CONFIG_DEV_BUFFER (40) and BASS_CONFIG_DEV_PERIOD (10) settings are in effect. Have you tried lowering them? If not, please give that a try and see what you get then. I would suggest first try just setting BASS_CONFIG_DEV_BUFFER to 30. Note the changes need to be made before calling BASS_Init (or call BASS_Free and BASS_Init again).

Great! Even less than 50ms now :) . It was my fault, since the config was applied after the initialization and not before. I suppose that lowering too much can cause glitches or cuts in some devices. Thanks a lot

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1833 on: 17 Feb '21 - 17:03 »
Good to hear that helped. A smaller device buffer will mean less time for the output processing to be done and could indeed result in stuttering sound. You could make it configurable in your app to allow the user to tweak it to what their device can handle.

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1834 on: 17 Feb '21 - 17:13 »
I think that is a great idea. Thanks again

Ionut Cristea

  • Posts: 1560
Re: BASS for Android
« Reply #1835 on: 26 Feb '21 - 07:03 »
Hello,
BassFX build for x86_64 seems to be missing in the package. Does anyone know where i can find it?

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1836 on: 26 Feb '21 - 11:24 »
Maybe it is a bit OT question, but if multiple activities have to access the BASS lib, sharing streams, fonts, etc., what should be the best approach? Singleton, singleton using Application class, service? The objective is, as much as possible, the instance of BASS could be maintained accross the application lifecycle. Thanks

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1837 on: 26 Feb '21 - 16:11 »
BassFX build for x86_64 seems to be missing in the package. Does anyone know where i can find it?

An update including x86_64 support is available here:

   www.un4seen.com/files/z/0/bass_fx24-android-beta.zip

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1838 on: 1 Mar '21 - 20:18 »
I am recording and encoding a file to MP3 while a MIDI file is playing. The aim is that they are synced at the start. The playback stream and the recording one have the same frequency and number of channels. When the recording is stopped, trying to play both streams using BASS_ChannelSetLink, a delay of around 200ms is appreciated in the encoded stream, instead of BASS_ChannelGetPosition for both streams returns the same value, something that does not happen while recording.

This is the code for recording and encoding while and midi playback. In this case, using BASS_ChannelSetLink does not seem to work.

Code: [Select]
recordStream=BASS_RecordStart(44100,2,MAKELONG(0,5),RecordingCallback,0);
BASS.BASS_ChannelSetAttribute(recordStream, BASS_ATTRIB_BUFFER, 0);
BASSenc_MP3.BASS_Encode_MP3_StartFile(recordStream, null, 0,filename);

streamMidi = BASSMIDI.BASS_MIDI_StreamCreateFile("other_file.mid", 0, 0, 0, 0);
BASS.BASS_ChannelSetAttribute(streamMidi, BASS_ATTRIB_BUFFER, 0);
BASS.BASS_ChannelPlay(streamMidi, false);


BASS.RECORDPROC RecordingCallback = new BASS.RECORDPROC() {
public boolean RECORDPROC(int handle, ByteBuffer buffer, int length, Object user) {
return true;
}
};

Is there a way to both streams get synced during the recording? Thanks

EDIT: another additional question that I noticed is that seeking does not seem work on BASS_ChannelSetLink , is it right? Wheen seeking in the channel that start the link, the linked channel does not follows the main one.
« Last Edit: 2 Mar '21 - 08:30 by rrhh_fx »

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1839 on: 2 Mar '21 - 17:57 »
Using BASS_ChannelSetLink to link the recorded file and MIDI file should ensure that they start playing simultaneously, but the issue is probably that the recording was never in sync to begin with, ie. the recording didn't start exactly when the MIDI file did. So even if the files do start playing simultaneously, it won't sound like it. You can confirm whether that is the case by loading both files in a sample editor and seeing if they line up.

The solution will be to either stagger playback of the files, or add/remove data at the start of the recorded file to bring it into sync with the MIDI file. In both cases, you will first need to know what the latency was when the recording was made, but I'm not sure if there's a reliable way to get that on Android. Perhaps it will be possible to at least get close. Please start by first loading both files in a sample editor to check what the latency was, and try that a few times to see if it varies much.

EDIT: another additional question that I noticed is that seeking does not seem work on BASS_ChannelSetLink , is it right? Wheen seeking in the channel that start the link, the linked channel does not follows the main one.

That is correct. Linked channels start/stop (BASS_ChannelPlay/Pause/Stop) together but not change position (BASS_ChannelSetPosition) together. You can implement that by pausing them, setting the position of both, and then resuming:

Code: [Select]
BASS_ChannelPause(mainstream);
BASS_ChannelSetPosition(mainstream, ...);
BASS_ChannelSetPosition(linkedstream, ...);
BASS_ChannelPlay(mainstream, 0);

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1840 on: 3 Mar '21 - 13:05 »
Using BASS_ChannelSetLink to link the recorded file and MIDI file should ensure that they start playing simultaneously, but the issue is probably that the recording was never in sync to begin with, ie. the recording didn't start exactly when the MIDI file did. So even if the files do start playing simultaneously, it won't sound like it. You can confirm whether that is the case by loading both files in a sample editor and seeing if they line up.

The solution will be to either stagger playback of the files, or add/remove data at the start of the recorded file to bring it into sync with the MIDI file. In both cases, you will first need to know what the latency was when the recording was made, but I'm not sure if there's a reliable way to get that on Android. Perhaps it will be possible to at least get close. Please start by first loading both files in a sample editor to check what the latency was, and try that a few times to see if it varies much.

EDIT: another additional question that I noticed is that seeking does not seem work on BASS_ChannelSetLink , is it right? Wheen seeking in the channel that start the link, the linked channel does not follows the main one.

That is correct. Linked channels start/stop (BASS_ChannelPlay/Pause/Stop) together but not change position (BASS_ChannelSetPosition) together. You can implement that by pausing them, setting the position of both, and then resuming:

Code: [Select]
BASS_ChannelPause(mainstream);
BASS_ChannelSetPosition(mainstream, ...);
BASS_ChannelSetPosition(linkedstream, ...);
BASS_ChannelPlay(mainstream, 0);

Can GetChannelPosition help to know the latency between MIDI playback and MP3 recording? I have noticed that the recording position is minor than the playback one at the start, but as time increases it reaches and surpass it. If frequencies and channels are the same for both streams, I see it strange.

Another issue (think that minor) is that this import sentence "import static com.un4seen.bass.BASS.BASS_StreamCreateFile;" lets "un4seen" in red: "cannot resolve symbol un4seen", instead it compiles and runs fine, and it is the only import "in red" among multiple BASS imports.
« Last Edit: 3 Mar '21 - 13:13 by rrhh_fx »

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1841 on: 3 Mar '21 - 14:25 »
Can GetChannelPosition help to know the latency between MIDI playback and MP3 recording? I have noticed that the recording position is minor than the playback one at the start, but as time increases it reaches and surpass it. If frequencies and channels are the same for both streams, I see it strange.

If the recording position starts behind the playback position and then gradually overtakes it then that is indeed strange. It is possible for devices to run at a slightly different speed than requested, eg. not exactly 44100 Hz. In your case, it sounds like the recording device is running slightly faster than the playback device. It's a bit tricky to get right but it's possible to get around that by adjusting the playback speed via BASS_ATTRIB_FREQ, ie. raise that slightly if the playback is falling behind, and vice versa. You could implement the monitoring and adjusting in your RECORDPROC function.

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1842 on: 10 Mar '21 - 14:02 »
Thanks and apologize for the late response. Trying your suggestion works fine, though in final devices the difference in not so noticiable (was trying in the emulator first).

I have 2 more questions:

A BASSMIDI update with support for soundfont assets (via the BASS.Asset class) is up now in the 1st post. You can use it something like this:

Code: [Select]
int newfont=BASSMIDI.BASS_MIDI_FontInit(new BASS.Asset(getAssets(), "timgm6mb.sf2"), 0);

I am not able to load SF2 in such way. The asset has to copied to the internal memory or SD card to work. Any advice to get it work?

And regarding the Wake Lock issue, the app still produces an high percenteage, so if for example, the app has 2 MIDI streams, 1 file Stream and  1 BASS_FX_stream over this file stream, what should be the best sequence to free it up when pausing/quitting the app? Something similar to this one?

Code: [Select]
BASS.BASS_ChannelStop(streamMidi1);
BASS.BASS_ChannelStop(streamMidi2);
BASSMIDI.BASS_MIDI_FontFree(fontStream1);
BASSMIDI.BASS_MIDI_FontFree(fontStream2);
BASS.BASS_ChannelRemoveFX(streamFile, streamEffects);
BASS.BASS_ChannelStop(streamFile);
BASS.BASS_Free();

Thanks

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1843 on: 10 Mar '21 - 16:50 »
A BASSMIDI update with support for soundfont assets (via the BASS.Asset class) is up now in the 1st post. You can use it something like this:

Code: [Select]
int newfont=BASSMIDI.BASS_MIDI_FontInit(new BASS.Asset(getAssets(), "timgm6mb.sf2"), 0);

I am not able to load SF2 in such way. The asset has to copied to the internal memory or SD card to work. Any advice to get it work?

That's strange. Are audio file assets (WAV/MP3/MIDI/etc) working OK with the BASS.Asset stuff? If you're able to open the SF2 asset yourself, you could do that and use BASS_MIDI_FontInitUser (with associated callback functions) to load it.

And regarding the Wake Lock issue, the app still produces an high percenteage, so if for example, the app has 2 MIDI streams, 1 file Stream and  1 BASS_FX_stream over this file stream, what should be the best sequence to free it up when pausing/quitting the app? Something similar to this one?

Code: [Select]
BASS.BASS_ChannelStop(streamMidi1);
BASS.BASS_ChannelStop(streamMidi2);
BASSMIDI.BASS_MIDI_FontFree(fontStream1);
BASSMIDI.BASS_MIDI_FontFree(fontStream2);
BASS.BASS_ChannelRemoveFX(streamFile, streamEffects);
BASS.BASS_ChannelStop(streamFile);
BASS.BASS_Free();

BASS_Free will free all streams on the device, so the BASS_ChannelStop calls aren't strictly necessary. BASS_Stop may suffice, so you could try that first instead of BASS_Free, which would allow you to later resume without needing to recreate the streams. The BASS_MIDI_FontFree calls are probably unnecessary in either case, as inactive soundfonts shouldn't keep the device awake.

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1844 on: 10 Mar '21 - 17:23 »
That's strange. Are audio file assets (WAV/MP3/MIDI/etc) working OK with the BASS.Asset stuff? If you're able to open the SF2 asset yourself, you could do that and use BASS_MIDI_FontInitUser (with associated callback functions) to load it.

Yes, in other media stuff, it works. in the first line, newFont is 0 and newStream is -2147483645. Both asset files are on the root of the Assets folder.
Code: [Select]
int newFont = BASSMIDI.BASS_MIDI_FontInit(new BASS.Asset(getAssets(), "mypack.sf2"), 0);
int newStream=BASS_StreamCreateFile(new BASS.Asset(getAssets(), "sample1.mp3"),0,0,0);

BASS_Free will free all streams on the device, so the BASS_ChannelStop calls aren't strictly necessary. BASS_Stop may suffice, so you could try that first instead of BASS_Free, which would allow you to later resume without needing to recreate the streams. The BASS_MIDI_FontFree calls are probably unnecessary in either case, as inactive soundfonts shouldn't keep the device awake.
Understood, so I will try to take care in stopping all the streams. And about the FX streams, is it necessary to deattach them from the streams that are applied, or just they stop when the original stream is stopped? Thanks

EDIT: When trying to load the SF2, the error from BASS_ErrorGetCode() is 500 (BASS_ERROR_JAVA_CLASS)
« Last Edit: 10 Mar '21 - 17:30 by rrhh_fx »

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1845 on: 11 Mar '21 - 15:41 »
The issue is probably that the SF2 file is compressed in the app's APK file, which makes it impossible to access directly. You can add this to the app's gradle script to disable compression of SF2 files:

Code: [Select]
aaptOptions {
noCompress 'sf2'
}

BASS_Free will free all streams on the device, so the BASS_ChannelStop calls aren't strictly necessary. BASS_Stop may suffice, so you could try that first instead of BASS_Free, which would allow you to later resume without needing to recreate the streams. The BASS_MIDI_FontFree calls are probably unnecessary in either case, as inactive soundfonts shouldn't keep the device awake.
Understood, so I will try to take care in stopping all the streams. And about the FX streams, is it necessary to deattach them from the streams that are applied, or just they stop when the original stream is stopped? Thanks

BASS_Stop will stop everything, so you don't really need to bother stopping individual streams. You don't need to bother removing FX either, as they won't be doing any processing if the host stream isn't.

rrhh_fx

  • Posts: 21
Re: BASS for Android
« Reply #1846 on: 11 Mar '21 - 18:06 »
BASS_Stop will stop everything, so you don't really need to bother stopping individual streams. You don't need to bother removing FX either, as they won't be doing any processing if the host stream isn't.

Thanks. Good to know that.

The issue is probably that the SF2 file is compressed in the app's APK file, which makes it impossible to access directly. You can add this to the app's gradle script to disable compression of SF2 files:

Code: [Select]
aaptOptions {
noCompress 'sf2'
}

It works like a charm. The APK results a little bigger, but less code and not having to deal with I/O operations (and different source of possible issues). Thanks a lot, Ian. Great lib and support

firemen

  • Posts: 157
Re: BASS for Android
« Reply #1847 on: 17 Mar '21 - 12:01 »
www.un4seen.com/files/bass24-android.zip (updated: 3 Feb '21)

libbass.so
maybe I don't understand, but the last changes in the archive are 12/17/2019

Ian @ un4seen

  • Administrator
  • Posts: 23502
Re: BASS for Android
« Reply #1848 on: 17 Mar '21 - 17:27 »
The BASSWV files (in the basswv/libs folder) were updated on that date. If you're looking for the latest BASS build/beta, that can be found here:

   www.un4seen.com/stuff/bass-android.zip

aquinch

  • Posts: 21
Re: BASS for Android
« Reply #1849 on: 28 Mar '21 - 02:23 »
Hi,

Has anybody made any attempts to create a wrapper for BASS and BASSMIDI for Cordova?