Author Topic: BASS get float data  (Read 104 times)

Daniele

  • Guest
BASS get float data
« on: 11 Jan '19 - 15:29 »
Hi everyone,
I've got a request on BASS API and C#
I downloaded a project which get FFT data from an audio source and draws a spectrum, and it's ok.
Now I'm trying to move the C# logic into a PLC (for test) and I'd like to get audio source signal, so I suppose I should get FLOAT data.
The problem is that I get always a "0" inside the value.
I'm not expert in C#, so I ask for your support.

Below the original FFT code
Thanks in advance to everyone.

Regards,

   Daniele

--------------------------------------------------

     private void Init()
        {
            bool result = false;
            for (int i = 0; i < BassWasapi.BASS_WASAPI_GetDeviceCount(); i++)
            {
                var device = BassWasapi.BASS_WASAPI_GetDeviceInfo(i);
                if (device.IsEnabled && device.IsLoopback)
                {
                    _devicelist.Items.Add(string.Format("{0} - {1}", i, device.name));
                }
            }
            _devicelist.SelectedIndex = 0;
            Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATETHREADS, false);
            result = Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
            if (!result) throw new Exception("Init Error");
        }

        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data
            if (ret < -1) return;
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x=0; x<_lines; x++)
            {
                float peak = 0;
                int b1 = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023) b1 = 1023;
                if (b1 <= b0) b1 = b0 + 1;
                for (;b0<b1;b0++)
                {
                    if (peak < _fft[1 + b0]) peak = _fft[1 + b0];
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255) y = 255;
                if (y < 0) y = 0;
                _spectrumdata.Add((byte)y);

                //Console.Write("{0, 3} ", y);
                Console.Write("{0, 3} ", y);
                _adsdata
  • = Convert.ToByte(y);

                //Console.Write(Convert.ToString(_lines), y);
             }

            if (DisplayEnable) _spectrum.Set(_spectrumdata);
            if (Serial != null)
            {
                Serial.Write(_spectrumdata.ToArray(), 0, _spectrumdata.Count);
            }
            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();
            _l.Value = Utils.LowWord32(level);
            _r.Value = Utils.HighWord32(level);
            if (level == _lastlevel && level != 0) _hanctr++;
            _lastlevel = level;

Ian @ un4seen

  • Administrator
  • Posts: 21447
Re: BASS get float data
« Reply #1 on: 11 Jan '19 - 16:24 »
I've got a request on BASS API and C#
I downloaded a project which get FFT data from an audio source and draws a spectrum, and it's ok.
Now I'm trying to move the C# logic into a PLC (for test) and I'd like to get audio source signal, so I suppose I should get FLOAT data.

Do you mean you want to get PCM sample data instead of FFT data? If so, you can do that by replacing the BASS_DATA_FFT2048 flag with the number of bytes of data that you want in the BASS_WASAPI_GetData call.

The problem is that I get always a "0" inside the value.

Do you mean the BASS_WASAPI_GetData call is returning 0? If so, it could be that BASS_WASAPI_Start hasn't been called to start the WASAPI device. If it's returning -1, check that you have included the BASS_WASAPI_BUFFER flag in your BASS_WASAPI_Init call.

Daniele

  • Guest
Re: BASS get float data
« Reply #2 on: 12 Jan '19 - 09:15 »
Hi lan,
thank you for support.
As said before, I don't know very well C#. I know better PLC programming
Can I ask you a sample of the 3 calls with data types? the init, start, and get data.
Consider that the sample above works perfectly (the audio spectrum is ok and using a breakpoint I can see FFT data).
Thanks in advance.
BR,

       Daniele

Ian @ un4seen

  • Administrator
  • Posts: 21447
Re: BASS get float data
« Reply #3 on: 14 Jan '19 - 13:19 »
Please see the LIVESPEC.C example that's included in the BASSWASAPI package for a demonstration. The "specmode==3" block handles the waveform display.

Daniele

  • Guest
Re: BASS get float data
« Reply #4 on: 15 Jan '19 - 07:48 »
Hi Ian,
thank you for suggestion. I'vee seen it works with both FFT and sample data.
There's only one thing I can't understand.

In the method "get data" I choose the buffer and its size. But where do I choose to get time domain data or FFT frequency domain data? Which is the method or the parameter?

Thanks in advance.

Regards,

    Daniele

Daniele

  • Guest
Re: BASS get float data
« Reply #5 on: 15 Jan '19 - 08:57 »
Hi Everyone,
I think I solved the request.
I tried using the channel get level instead of get data. In this way I can receive time domain sound wave (or I suppose it..)
Thanks to anyone for help and, in case of suggestions, please let me know.
Regards,


         Daniele

Ian @ un4seen

  • Administrator
  • Posts: 21447
Re: BASS get float data
« Reply #6 on: 15 Jan '19 - 13:20 »
In the method "get data" I choose the buffer and its size. But where do I choose to get time domain data or FFT frequency domain data? Which is the method or the parameter?

If you use one of the BASS_DATA_FFT constants (eg. BASS_DATA_FFT2048) in the BASS_WASAPI_GetData call then it will return frequency domain FFT data, otherwise it will return time domain PCM data ("length" bytes of it). BASS_WASAPI_GetData uses BASS_ChannelGetData internally and supports the same options, so please also see the BASS_ChannelGetData documentation for more information.