Author Topic: ChannelGetLength issue  (Read 262 times)

KevC

  • Posts: 22
ChannelGetLength issue
« on: 30 Jul '24 - 22:31 »
Hi, this code is generating -1 when looking for the length of stream is so far.

Code: [Select]

Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_16BITS, IntPtr.Zero);

int _bassStream = Bass.BASS_StreamCreatePush(44100, 2, BASSFlag.BASS_SAMPLE_FLOAT, IntPtr.Zero);

Bass.BASS_ChannelPause(_bassStream);

Bass.BASS_StreamPutData(_bassStream, buffer, buffer.Length * 4);

long h = Bass.BASS_ChannelGetLength(_bassStream);


Where buffer is float[44100].  Does anyone know what the problem is?
Thanks.

KevC

  • Posts: 22
Re: ChannelGetLength issue
« Reply #1 on: 31 Jul '24 - 16:53 »
SOLVED.

I found the command:

Code: [Select]

Bass.BASS_ChannelGetPosition(_bassStream, BASSMode.BASS_POS_DECODE) / 4;


...and it does exactly whats required.

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: ChannelGetLength issue
« Reply #2 on: 31 Jul '24 - 17:20 »
Yep, "push" streams don't have a determined length, so BASS_ChannelGetLength will fail with BASS_ERROR_NOTAVAIL. Using BASS_ChannelGetPosition with BASS_POS_BYTE+BASS_POS_DECODE instead will give you the amount of data decoded/processed by the stream, but note it won't include any unprocessed data currently waiting in the push queue/buffer (see the BASS_StreamPutData return value for that). If you would like it all included, another option could be to just keep count of your BASS_StreamPutData "length" parameter values.

KevC

  • Posts: 22
Re: ChannelGetLength issue
« Reply #3 on: 31 Jul '24 - 22:08 »
I want to get access to all of whats been placed in the streambuffer(starting at 0) up to the very end.  This code is to transfer the stream in its entirety to the floats[] array.  It works if I dont use ChannelPlay till the very end(or not at all), but if I play the channel and it tries to copy the buffer(while playing) im getting what appears to be a part of the audio.

Ive tried BASS_ChannelSetPosition(_bassStream, 0) but that appears to clear the buffer first.

Code: [Select]
Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_16BITS, IntPtr.Zero);

int _bassStream = Bass.BASS_StreamCreatePush(44100, 2, BASSFlag.BASS_SAMPLE_FLOAT, IntPtr.Zero);

Bass.BASS_ChannelPause(_bassStream);

Bass.BASS_StreamPutData(_bassStream, buffer, buffer.Length * 4);

Bass.BASS_ChannelPlay(_bassStream, false);

long amount = Bass.BASS_ChannelGetPosition(_bassStream, BASSMode. BASS_POS_DECODE) / 4;

float[] floats = new float[amount];

for (int cnt = 0; cnt < amount; cnt++)
    floats[cnt] = Bass.BASS_ChannelGetData(_bassStream, floats, (int)amount);

Last post you said something about keeping track of the total, would I need to do this?  And how would that make it work?

Thanks.

KevC

  • Posts: 22
Re: ChannelGetLength issue
« Reply #4 on: 31 Jul '24 - 22:10 »
...or might the best way to be using a dummy stream?  I cant guarantee the stream wont have started playing before its all written, or even if its all written and played before wanting to copy the buffer data.

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: ChannelGetLength issue
« Reply #5 on: 1 Aug '24 - 12:24 »
Do you actually want to play the push stream, or are you using it to basically just hold and join blocks of data? If the latter then you should add the BASS_STREAM_DECODE flag to the BASS_StreamCreatePush call and remove the BASS_ChannelPlay call. The BASS_ChannelGetData call(s) will then fetch data directly from the push stream's queue/buffer rather than a playback buffer.

KevC

  • Posts: 22
Re: ChannelGetLength issue
« Reply #6 on: 1 Aug '24 - 15:30 »
Ideally i want to have access to the whole stream thats been written to it with Push command, but also to be able to play it on a whim.  I want to be able to save it as a wav(my own software that takes a float[]).  What may complicate things a bit later is when I start using effects and 3d positioning to alter(through software Im assuming and not the hardware itself) and so want to also save that stream too.

So my own synth will create samples I save to the streambuffer.

Later Ill want to save/play/add effects to the stream.

Thanks.  Hope thats a little clearer.  Just now Ill opt to use 2 streams with 1 of those being decode form and see how it goes.

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: ChannelGetLength issue
« Reply #7 on: 2 Aug '24 - 16:28 »
Ideally i want to have access to the whole stream thats been written to it with Push command, but also to be able to play it on a whim.

If you may want to access the data multiple times then a push stream isn't good for that because it discards the data after first use. It'd be better to use something like a dynamic array ("List" in C# I think). You could still use a push stream for playback, or more efficiently (avoids duplicate data in memory), use a STREAMPROC callback function that reads the data from the aforementioned dynamic array.