Author Topic: using BASS_ChannelGetData to get audio at realtime playback speed  (Read 117 times)

jakob

  • Posts: 105
hi
I need to stream realtime audio (like if i used a player). The problem i am having with using a player is that it needs a playback device in the system. The streaming is done from a server, so there might not be a playback device. I have tried by using BASS_ChannelGetData but i don't know how to do this realtime.
this is what i have done:
Code: [Select]
int encodeHandle = BassEnc.BASS_Encode_Start(decodingStream, null, BASSEncode.BASS_ENCODE_PCM | BASSEncode.BASS_ENCODE_AUTOFREE | BASSEncode.BASS_ENCODE_NOHEAD, null, IntPtr.Zero);

            int bytesRead = 0;
            MemoryStream ms = null;

            while (Bass.BASS_ChannelIsActive(decodingStream) == BASSActive.BASS_ACTIVE_PLAYING || bytesRead != 0)
            {
                byte[] buf = new byte[2000];
                bytesRead = Bass.BASS_ChannelGetData(decodingStream, buf, buf.Length);
                if (bytesRead == -1)
                {
                    BASSError error = Bass.BASS_ErrorGetCode();
                    if (error == BASSError.BASS_ERROR_ENDED)
                        break;
                }
                Trace.WriteLine("byteRead=" + bytesRead);
                if (bytesRead > 0)
                {
                    ms = new MemoryStream(bytesRead);
                    ms.Write(buf, 0, bytesRead);
                    if (DecodingDataReady != null)
                        DecodingDataReady(ms.ToArray());
                }
            }

I actually also need to be able to specify samplefreq and channels of the data that i get from BASS_ChannelGetData. For example no matter what format the audio is i would like to use BASS_ChannelGetData to read 16kHz and 16bit Mono audio.
Is this possible?

Ian @ un4seen

  • Administrator
  • Posts: 20393
I need to stream realtime audio (like if i used a player). The problem i am having with using a player is that it needs a playback device in the system. The streaming is done from a server, so there might not be a playback device. I have tried by using BASS_ChannelGetData but i don't know how to do this realtime.

If you are encoding the data then you could simply use the BASS_ENCODE_LIMIT flag in the BASS_Encode_Start call to limit the processing to real-time speed. Each BASS_ChannelGetData call will then be delayed by the required amount of time.

I actually also need to be able to specify samplefreq and channels of the data that i get from BASS_ChannelGetData. For example no matter what format the audio is i would like to use BASS_ChannelGetData to read 16kHz and 16bit Mono audio.
Is this possible?

You can use the BASSmix add-on to do that. Create a mixer with the wanted format (via BASS_Mixer_StreamCreate), plug the decoder into it (via BASS_Mixer_StreamAddChannel), and make the BASS_Encode_Start and BASS_ChannelGetData calls on the mixer.

Code: [Select]
resampler = BASS_Mixer_StreamCreate(16000, 1, BASS_STREAM_DECODE|BASS_MIXER_END); // create a 16KHz mono 16-bit mixer
BASS_Mixer_StreamAddChannel(resampler, decodingStream, 0); // plug the decoder into it

jakob

  • Posts: 105
it works, thank you for your help  ;D