Author Topic: BASS_StreamPutData  (Read 41 times)

herf

  • Posts: 3
BASS_StreamPutData
« on: 10 Jun '21 - 16:21 »
Hi I figured that BASS_StreamPutData only handle PCM and not my encoded data array.  Is there a "Decoder" I can use, or is there another why to play a MP3 encoded data array, maybe some simple C# to get me started...?

Ian @ un4seen

  • Administrator
  • Posts: 23619
Re: BASS_StreamPutData
« Reply #1 on: 11 Jun '21 - 14:18 »
If you have all of the MP3 data ready in memory then you can use BASS_StreamCreateFile with "mem=true" (or the "IntPtr memory" overload if you're using BASS.Net). Another more flexible option (doesn't need all of the data up front) is to use BASS_StreamCreateFileUser, but that's also more complicated (requires callback functions). Please see the documentation for details on both functions.

herf

  • Posts: 3
Re: BASS_StreamPutData
« Reply #2 on: 11 Jun '21 - 15:19 »
Thank you for your reply!

I want to play the audio data received from the network. I looked up the relevant documents and thought that there was only bass_ Streamcreatefileuser and bass_ Streamcreatepush might suit me. The two methods have their own shortcomings, which can't satisfy me, for example: 1. Bass_ Streamcreatepush can only play PCM format. 2、BASS_ Using bass after streamcreatefileuser_ Streamputfiledata playback is incomplete, I try to use bass.bass_ SetConfig(BASSConfig.BASS_ CONFIG_ NET_ The effect of buffer (1000) was not obvious.

Look at my code:

Code: [Select]
public byte[] test_Data1;
        public byte[] test_Data2;
        public byte[] test_Data3;
        //........There are many test data in the same format.
        //........It is obtained by a different mechanism and is only used for testing purposes here

        private int _stream = 0;
        private BASS_FILEPROCS _myStreamCreateUser;

        public void myTest_BASS_StreamCreateFileUser()
        {
            if (_myStreamCreateUser == null)
            {
                Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_NET_PREBUF, 0);
                _myStreamCreateUser = new BASS_FILEPROCS(new FILECLOSEPROC(MyFileProcUserClose),
                    new FILELENPROC(MyFileProcUserLength),new FILEREADPROC(MyFileProcUserRead), null);
            }
            //Use test_Data1 to create the data needed for the handle stream because test_Data1 contains audio header data
            this._stream = Bass.BASS_StreamCreateFileUser(BASSStreamSystem.STREAMFILE_BUFFERPUSH, BASSFlag.BASS_STREAM_BLOCK, _myStreamCreateUser, IntPtr.Zero);
            Bass.BASS_ChannelPlay(this._stream, false);
        }

        private int readHeadLen = 0;
        private int MyFileProcUserRead(IntPtr buffer, int length, IntPtr user)
        {
            Debug.Print("Read_length:" + length.ToString());
            try
            {
                byte[] data = new byte[length];
                int HeadLen = test_Data1.Length;
                int bytesread = HeadLen - readHeadLen;
                if (bytesread > 0)
                {
                    if (bytesread > length) bytesread = length;
                    Buffer.BlockCopy(test_Data1, readHeadLen, data, 0, bytesread);
                    readHeadLen += bytesread;
                    Marshal.Copy(data, 0, buffer, bytesread);
                }
                Debug.Print("Read:" + bytesread.ToString());
                return bytesread;
            }
            catch { return 0; }
        }
        private void MyFileProcUserClose(IntPtr user)
        {
            Debug.Print("MyFileProcUserClose");
        }

        private long MyFileProcUserLength(IntPtr user)
        {
            Debug.Print("MyFileProcUserLength");
            return 0L;
        }

When the handle is created, it calls BASS_StreamPutFileData
Code: [Select]
       
        public void testPush2()
        {
            if (_stream != 0)
                Bass.BASS_StreamPutFileData(_stream, test_Data2, test_Data2.Length);
        }
        public void textPush3()
        {
            if (_stream != 0)
                Bass.BASS_StreamPutFileData(_stream, test_Data3, test_Data3.Length);
        }

There is no sound when calling testpush2(), but there is sound after calling testpush3(). What did I do wrong?
Help me!

Ian @ un4seen

  • Administrator
  • Posts: 23619
Re: BASS_StreamPutData
« Reply #3 on: 11 Jun '21 - 17:05 »
It's hard to say what the problem is without knowing the "test_Data2" and "test_Data3" contents, but it could be that "test_Data2" doesn't contain valid data? If it's MP3 data, does BASS_StreamCreateFile recognise it?

Note that you should pass any data that you have remaining after the BASS_StreamCreateFileUser call to BASS_StreamPutFileData, before you start passing it any new data. There is some example code for handling this in the BASS.Net BASS_StreamCreateFileUser documentation.

I would generally recommend starting with the STREAMFILE_BUFFER system, and get the FILEREADPROC callback function working well before switching to the STREAMFILE_BUFFERPUSH system (and BASS_StreamPutFileData). The FILEREADPROC callback will still be needed after switching, so it's good to get that working first.