Author Topic: is it possible to continue using a stream after using BASSStreamProc.BASS_STREAM  (Read 126 times)

pgruebele

  • Posts: 41
I need to process small segments of audio (up to 192 samples) and for each segment I need to get the complete output. 

So what I am doing is repeating this sequence for each pass (up to 192 samples) of samples I process.

         if ((handleF1toN = Bass.BASS_StreamCreatePush(fakeSampleRate, 1, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_MONO, IntPtr.Zero)) == 0)
            throw new Exception("Error opening audio stream error=" + Bass.BASS_ErrorGetCode());
         if (!BassMix.BASS_Mixer_StreamAddChannel(mixerF1toN, handleF1toN, BASSFlag.BASS_MIXER_DOWNMIX | BASSFlag.BASS_MIXER_NORAMPIN))
            throw new Exception("Error down-mixing audio error=" + Bass.BASS_ErrorGetCode());
         var num = Bass.BASS_StreamPutData(handleF1toN, dataIn, (dataInLength * 2) | (int)BASSStreamProc.BASS_STREAMPROC_END);
         var dataf2Length = Bass.BASS_ChannelGetData(mixerF1toN, dataf2, numSamples * 2);
         if (!BassMix.BASS_Mixer_ChannelRemove(handleF1toN))
            throw new Exception("Error BASS_Mixer_ChannelRemove handleF1toN error=" + Bass.BASS_ErrorGetCode());
         if (!Bass.BASS_StreamFree(handleF1toN))
            throw new Exception("Error BASS_StreamFree handleF1toN error=" + Bass.BASS_ErrorGetCode());

The reason I am allocating and freeing the stream each time is because BASSStreamProc.BASS_STREAM seems to terminate the stream in a way that I cannot continue using it.
However, allocating and freeing these streams for each pass produces a lot of overhead.

Is there a way to do this without re-creating the stream on each pass?

Thanks!

Ian @ un4seen

  • Administrator
  • Posts: 21020
If you remove the BASS_STREAMPROC_END flag from the BASS_StreamPutData call then you will be able to feed more data to the stream and it will not be necessary to recreate it each time. Alternatively, you can call BASS_ChannelSetPosition with pos=0 to reset the stream.

pgruebele

  • Posts: 41
That won't work for my scenario.

Without the BASS_STREAMPROC_END, BASS_ChannelGetData() returns fewer samples than with BASS_STREAMPROC_END, and I need all the samples (same number of samples as are returned with the BASS_STREAMPROC_END flag)

Ian @ un4seen

  • Administrator
  • Posts: 21020
Are you using the mixer to change the source's sample rate? If so, the mixer will hold back (buffer) some data in the first BASS_ChannelGetData call because multiple input samples are used to calculate each output sample. The buffered data will be flushed once the end of the source is reached. If your segments are separate (not intended to be played gaplessly) then you should indeed use the BASS_STREAMPROC_END flag to signal that, and you can use BASS_ChannelSetPosition to reset the stream instead of recreating it.

pgruebele

  • Posts: 41
Great thanks.

I got this to work as you describe by adding this at the end:

         if (!BassMix.BASS_Mixer_ChannelSetPosition(handleF1toN, 0))
            throw new Exception("Error BASS_StreamFree handleFNto1 error=" + Bass.BASS_ErrorGetCode());

BASS_ChannelSetPosition() did not seem to do the trick.