Author Topic: How to catch finishing of playing of hsample,hchannel and/or hmusic  (Read 3011 times)

sashakozlovskiy

  • Posts: 54
Hello everybody. Please,help me how i can catch,when playing of some track via bass library was finished in c++ code. Yes,probably i can get somehow length of hsample,hchannel and/or hmusic in ms,and set timer on it,but may be exists some simpler way to catch this. If no,how i can know length of track in ms and how i can add feature request,to bass developer add this feature. Sdl library,or android mediaplayer class,have this callback,but not bass library. Thanks everybody for any help.

Ian @ un4seen

  • Administrator
  • Posts: 25720
You can set a BASS_SYNC_END sync via BASS_ChannelSetSync to receive a notification/callback when a BASS channel reaches the end. Please see the BASS_ChannelSetSync documentation for details. Note that syncs can't be set on sample channels, so you will need to use streams instead for this (you can use the BASS_SAMCHAN_STREAM flag with BASS_SampleGetChannel to create a stream from a sample).

sashakozlovskiy

  • Posts: 54
Thank you very much for your reply. 1. Why i can't use callbacks  for hsample directly? 2. When i will create stream using way,as you wrote,should i free handle as hchannel,hsample or as hstream? 3. If my track has looping,whether exists possibility catch starting of n-th playing of track?

sashakozlovskiy

  • Posts: 54
Why do you ignore my latest message? I also have a question,should i remove callback after freeing of hchannel,hsample,hmusic and/or hstream?

Ian @ un4seen

  • Administrator
  • Posts: 25720
When i will create stream using way,as you wrote,should i free handle as hchannel,hsample or as hstream?

BASS_SampleGetChannel with the BASS_SAMCHAN_STREAM flag will create a stream, so you should use BASS_StreamFree to free it. You can also use the BASS_STREAM_AUTOFREE flag to have it freed automatically at the end of playback.

If my track has looping,whether exists possibility catch starting of n-th playing of track?

A BASS_SYNC_END sync will be called for each loop. So you could use that to count the loops and stop the stream when it reaches the n'th one. Note a sync for this purpose should have the BASS_SYNC_MIXTIME flag set, so that it's called before looping (not after).

should i remove callback after freeing of hchannel,hsample,hmusic and/or hstream?

Freeing a BASS channel will free all of its resources, so you don't need to seprately free the syncs.

sashakozlovskiy

  • Posts: 54
Thank you very much for your reply. If i will use autofree flag for stream,whether callback will call,and if yes,whether it will be call before freeing of the stream? Can i free stream inside a callback? About looping,i meaned,what before looping it can be pause,so may be exists callback,which allow to know,when pause was finished and nth playing will started. Also in doc i read what this callback can call not only after finishing of track,but after playing of it at nth ms (position).

Ian @ un4seen

  • Administrator
  • Posts: 25720
If i will use autofree flag for stream,whether callback will call,and if yes,whether it will be call before freeing of the stream?

If the stream is freed at the end of the file then a BASS_SYNC_END sync would be called before it's freed. Note you can also set a BASS_SYNC_FREE sync if you want to know when the stream is freed by any means.

Can i free stream inside a callback?

You can free a stream within a normal sync callback, but not a "mixtime" sync callback. Please see the SYNCPROC (and STREAMPROC) documentation for what's unsafe.

About looping,i meaned,what before looping it can be pause,so may be exists callback,which allow to know,when pause was finished and nth playing will started.

There isn't a sync/callback for starting playback, but if you want to do something upon each playback start then you could simply put that next to the BASS_ChannelPlay/Start calls.

sashakozlovskiy

  • Posts: 54
> You can free a stream within a normal sync callback, but not a "mixtime" sync callback.

Ok,how in this case i can free stream after nth looping,because i should use mix_time flag,to get event after nth playing. If i will try free stream in "mixtime" sync callback,whether exception will be thrown?

sashakozlovskiy

  • Posts: 54
And autofree flag,as i understand,will not work for infinit loop,because we haven't any sence in it,because in this case bass dont know,when we want to stop this playing.

Ian @ un4seen

  • Administrator
  • Posts: 25720
You cannot call BASS_StreamFree in the mixtime SYNCPROC, but you can call BASS_ChannelStop to end the stream and it will then be freed if the BASS_STREAM_AUTOFREE flag is set. Another option is to remove the BASS_SAMPLE_LOOP flag with BASS_ChannelFlags, instead of calling BASS_ChannelStop.

sashakozlovskiy

  • Posts: 54
Ok,but can i free this stream out of callback,or it will be the same requires,as in callback? How i can delete loop flag for the channel?

Ian @ un4seen

  • Administrator
  • Posts: 25720
Yes, it is fine to call BASS_StreamFree outside of a mixtime callback. Wouldn't be much point having the function if it couldn't be called anywhere :)

You can remove the BASS_SAMPLE_LOOP flag with BASS_ChannelFlags like this:

Code: [Select]
BASS_ChannelFlags(handle, 0, BASS_SAMPLE_LOOP);

Please see the BASS_ChannelFlags documentation for details.

sashakozlovskiy

  • Posts: 54
Hello. When i try:
```
BASS_ChannelSetSync(back_channel, BASS_SYNC_ONETIME, BASS_SYNC_END, finishCallback, 0); //for channel,which created from hsample as stream.
BASS_ChannelPlay(back_channel, FALSE);

static void CALLBACK finishCallback(HSYNC handle, DWORD channel, DWORD data, void* user)
{
...
}
```
I get crash of my app. If i set callback after playing,i haven't crash,but it seems,what callback not call. If i set callback without playing,i haven't crash too. Also i have other questions about your library:
1. Some file types not supported hsample,such as xm,mod,mo3,it,mtm,umx. Whether it fix now,and if not,why? Because i want simplefy my code,because for this types we use hmusic,and for others - hsample.
2. May be i can get file path from hsample/hchannel,or i should myself save it,during setting of sound/music?

Chris

  • Posts: 2189
Quote
BASS_ChannelSetSync(back_channel, BASS_SYNC_ONETIME, BASS_SYNC_END, finishCallback, 0);
 //for channel,which created from hsample as stream.
hi im not a C++ Coder but that cannot work should be something like this
Code: [Select]
BASS_ChannelSetSync(back_channel, BASS_SYNC_ONETIME | BASS_SYNC_END, 0, finishCallback, 0); 

https://www.un4seen.com/doc/#bass/BASS_ChannelSetSync.html

sashakozlovskiy

  • Posts: 54
Thank you very much. Now,it seems,all ok. Yes,i mistake,it c,not c++. But according bass doc BASS_SYNC_END should be,as i understand,third param of BASS_ChannelSetSync method. Thank you very much for your help.

sashakozlovskiy

  • Posts: 54
Why do you ignore my previous questions?

Ian @ un4seen

  • Administrator
  • Posts: 25720
What question? Do you mean the BASS_SYNC_END comment? If so, that needs to be in the 2nd parameter, like Chris posted above.

sashakozlovskiy

  • Posts: 54
1. Yes,it works,but what we should set in third param of BASS_ChannelSetSync func. Because,i thought before,it should be BASS_SYNC_END param. 2. I probably not asked before,but ask now: whether it possible get path to file,if i know hsample or hchannel. 3. if i call bass_channel_free,should i call bass_sample_free,if i create hchannel from hsample?

Ian @ un4seen

  • Administrator
  • Posts: 25720
1. Yes,it works,but what we should set in third param of BASS_ChannelSetSync func. Because,i thought before,it should be BASS_SYNC_END param.

The sync type (eg. BASS_SYNC_END) goes in the 2nd parameter. The 3rd parameter ("param") depends on the sync type, as described in the BASS_ChannelSetSync documentation. Please note that it is "not used" in the case of BASS_SYNC_END syncs.

2. I probably not asked before,but ask now: whether it possible get path to file,if i know hsample or hchannel.

You can get the filename of a stream (HSTREAM) from BASS_ChannelGetInfo, but not of a sample (HSAMPLE/HCHANNEL) currently. If you need the filename of a sample then you could retain a copy of the filename that you used in the BASS_SampleLoad call.

3. if i call bass_channel_free,should i call bass_sample_free,if i create hchannel from hsample?

If you want to free a HCHANNEL then you should only call BASS_ChannelFree, not BASS_SampleFree. BASS_SampleFree will free a sample (HSAMPLE) and all of its channels.

sashakozlovskiy

  • Posts: 54
1. If i created channel from hsample,and want to free sound,it enough call bass_channelFree (or bass_musicFree or bass_streamFree) and i shouldn't call bass_sampleFree?
2. So,i can't get direct file path even from hstream channel? Why features of hsample so limited? Why we can't set callbacks to it,but should create stream for this,get file name from channels,created from hsample,etc. Why we can't get direct file path,which we set for specific channel? How i can create feature request for bass library,to have support of this features?

Ian @ un4seen

  • Administrator
  • Posts: 25720
1. If i created channel from hsample,and want to free sound,it enough call bass_channelFree (or bass_musicFree or bass_streamFree) and i shouldn't call bass_sampleFree?

Yes. If you want to free a single sample channel then you should not call BASS_SampleFree because it will all of them (from the same sample).

2. So,i can't get direct file path even from hstream channel? Why features of hsample so limited? Why we can't set callbacks to it,but should create stream for this,get file name from channels,created from hsample,etc. Why we can't get direct file path,which we set for specific channel?

Sample channels don't have all the features of streams because they are meant to be as lightweight as possible, ie. minimal CPU and memory usage. When the extra features (eg. syncs) are needed then you can create a stream instead by using the BASS_SAMCHAN_STREAM flag with BASS_SampleGetChannel.

sashakozlovskiy

  • Posts: 54
1. But if i want fully free some hsample,whether it enough call bass_sample_free,or i should free channel too?
2. I thought,what hsample use other algorithm,than stream,so it use less cpu,but it also not contains some usefull features. Because if,for example,hsample allow to use callbacks,i shouldn't add stream flag for hchannel,so it required less RAM,than stream. In my opinion stream useful only in case,when we dynamically load new data,i.e internet stream or some bytes,i.e which we get during generation of sine wave. In other cases,in my opinion,it enough hsample.

Ian @ un4seen

  • Administrator
  • Posts: 25720
BASS_SampleFree frees all of a sample's channels (HCHANNEL and HSTREAM), so there is no need to call BASS_ChannelFree then too.

sashakozlovskiy

  • Posts: 54
Hello again. I have a several new questions about your library. 1. what is the param user* data in callback. Can i insert some value. I rewriting some file,which before me use sdl library,so i want to insert in it,for example,channel number (from 0 to 7 ait channels in sdl lib). But for music (because i also use sound) i want to set -1. Can i set this editional data,or no? 2. Can i dynamically load short* buff (generated array of bytes) to play it and get callback,when playing was finished? If yes,may be exists example,how to do it? Additionally i have a question,how correct send callback,to get it not only after finishing of playing,but each changing of position and how i can get this position in callback and understand,whether playing was finished? 3. In sdl we can set panning from 0 to 255 for left and right channel separately. In bass we can set only panning for one channel from -1 to 1 (1 it right). But may be exists way,to set volume for left/right channel instead of it,such as it works in sdl? 4. Whether it possible play hsample/hchannel with fading? If yes,how i can do this? Thank you very much for your help.

Ian @ un4seen

  • Administrator
  • Posts: 25720
what is the param user* data in callback. Can i insert some value. I rewriting some file,which before me use sdl library,so i want to insert in it,for example,channel number (from 0 to 7 ait channels in sdl lib). But for music (because i also use sound) i want to set -1. Can i set this editional data,or no?

You can set the callback's "user" parameter value in your BASS_ChannelSetSync call.

Can i dynamically load short* buff (generated array of bytes) to play it and get callback,when playing was finished? If yes,may be exists example,how to do it? Additionally i have a question,how correct send callback,to get it not only after finishing of playing,but each changing of position and how i can get this position in callback and understand,whether playing was finished?

If you want to stick with sample functions then you could use BASS_SampleCreate and BASS_SampleSetData (instead of BASS_SampleLoad) for custom generated data. Alternatively, you could use BASS_StreamCreate. Please see the documentation for details on those functions.

In sdl we can set panning from 0 to 255 for left and right channel separately. In bass we can set only panning for one channel from -1 to 1 (1 it right). But may be exists way,to set volume for left/right channel instead of it,such as it works in sdl?

You can use a combination of BASS_ATTRIB_VOL and BASS_ATTRIB_PAN settings, as described in this thread:

   www.un4seen.com/forum/?topic=17480

Whether it possible play hsample/hchannel with fading? If yes,how i can do this?

You can use BASS_ChannelSlideAttribute with BASS_ATTRIB_VOL to fade-in/out. Note that if you want a fade-out to start at a specific position then you will probably need to use a stream (HSTREAM) instead of a sample channel (HCHANNEL), so that you can set a BASS_SYNC_POS sync on it.