Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Un4seen.Bass;
- using Un4seen.Bass.Misc;
- using Un4seen.BassWasapi;
- namespace audioVisualizer
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- List<byte> _spectrumdata = new List<byte>();
- int devindex;
- float[] fft = new float[1024];
- int _lines = 3;
- WASAPIPROC wasapiProcess;
- BassWasapiHandler _wasapi;
- private void button1_Click(object sender, EventArgs e)
- {
- var array = (comboBox1.Items[comboBox1.SelectedIndex] as string).Split(' ');
- devindex = Convert.ToInt32(array[0]);
- //MessageBox.Show(devindex + "lol");
- bool result = BassWasapi.BASS_WASAPI_Init(devindex, 0, 0, BASSWASAPIInit.BASS_WASAPI_BUFFER, 1f, 0.05f, wasapiProcess, IntPtr.Zero);
- BassWasapi.BASS_WASAPI_Start();
- System.Threading.Thread.Sleep(500);
- timer1.Enabled = true;
- /*
- //BassWasapiHandler lol = new BassWasapiHandler(devindex, false, 48000, 2, 0f, 0f);
- BassWasapiHandler _wasapi = new BassWasapiHandler(devindex, false, 48000, 2, 0f, 0f);
- int stream = Bass.BASS_StreamCreateFile("C:\test.mp3", 0, 0,
- BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);
- if (stream != 0)
- {
- // assign WASAPI output in shared-mode
- _wasapi = new BassWasapiHandler(-1, false, 48000, 2, 0f, 0f);
- // add the source channel
- _wasapi.AddOutputSource(stream, BASSFlag.BASS_DEFAULT);
- // init and start WASAPI
- _wasapi.Init();
- _wasapi.Start();
- }*/
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- _spectrumdata.Clear();
- int ret = BassWasapi.BASS_WASAPI_GetData(fft, (int)BASSData.BASS_DATA_FFT2048);
- Console.WriteLine(fft[1024 * 16500 / 44100]);
- if (ret < -1) return;
- int x, y;
- int b0 = 0;
- 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.Clear();
- foreach(byte data in _spectrumdata)
- {
- Console.Write("[");
- for(int i = 0; i < data; i += 4)
- {
- Console.Write("=");
- }
- Console.Write("]\n");
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- for (int i = 0; i < BassWasapi.BASS_WASAPI_GetDeviceCount(); i++)
- {
- var device = BassWasapi.BASS_WASAPI_GetDeviceInfo(i);
- if (device.IsEnabled && device.IsLoopback)
- {
- comboBox1.Items.Add(string.Format("{0} - {1}", i, device.name));
- }
- }
- comboBox1.SelectedIndex = 0;
- Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATETHREADS, false);
- Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
- wasapiProcess = new WASAPIPROC(wasapiProcessHack);
- }
- private int wasapiProcessHack(IntPtr buffer, int length, IntPtr user)
- {
- return length;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- _wasapi.Stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement