Skip to content

Commit

Permalink
slow response with 44.1/48khz device, label position bug fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken SUGII committed Nov 18, 2022
1 parent 8c0f3f1 commit e4b6d64
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 67 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Simply use a PC sound card or device to play music on your favorite player, for example, foobar2000, Youtube or Amazon Music in various browsers, AppleMusic (iTunes), Spotify desktop app, etc., **SpeAnaLED** should intercept sounds and work.
- **SpeAnaLED** can be re-sized, so you can reside on your PC desktop by putting it on your "Startup" folder.
- **SpeAnaLED** is a tiny program. Its size of executable file is about 64KB itsdelf, about 900KB including dlls. In the case of my 8 years old PC, CPU load is 0.1% or less, uses about 90MB process memory. No use of DirextX 2D, only GDI/GDI+ Win32API(System.Drawing(.Drawing2D)) is used.
- Yes, this is a one of "Reinventing the wheel" project.

## Requirements
- Windows PC with WASAPI (Vista or later)
Expand Down Expand Up @@ -75,7 +76,7 @@

## Other precautions
<p><strong>SpeAnaLED</strong> is simply made to be seen and enjoyed by myself. The values including frequency displayed is most likely not accurate.</p>
<p>If a problem occurs, first try deleting your configuration file(SpeAnaLED.conf) and restarting <strong>SpeAnaLED</strong>. (After that, enumerate sound device from setting dialog again.)</p>
<p>If a problem occurs, first try deleting your configuration file(SpeAnaLED.conf) and restart <strong>SpeAnaLED</strong>. (After that, enumerate sound device from setting dialog again.)</p>
<p>Have fun music!</p>

## License
Expand Down
65 changes: 42 additions & 23 deletions SpeAnaLED/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ internal class Analyzer
public List<byte> _spectrumdata; // spectrum data buffer
public readonly DispatcherTimer _timer1; // timer that refreshes the display
private BASSData _DATAFLAG; // for "interreave" format
private BASSData _DATAFLAG_8192; // for Hi-Reso
private BASSData _DATAFLAG_16384; // for Hi-Reso
//private BASSData _DATAFLAG_4096; // for < 96000
//private BASSData _DATAFLAG_8192; // reserved
//private BASSData _DATAFLAG_16384; // for Hi-Reso
public int _devicenumber;
private readonly bool _UNICODE; // codepage switch
public readonly ComboBox _devicelist; // for subscribe
Expand All @@ -34,12 +35,12 @@ internal class Analyzer
public event EventHandler SpectrumChanged; // for fire
public event EventHandler NumberOfChannelChanged; // for fire
private int _mixfreq;
private float _mixfreqMulti;
private float _mixfreqMultiplyer;
private readonly float _freqShift = (float)Math.Round(Math.Log(20000/*hz*/, 2) - /*in increments of*/10, 2);
public bool inInit = true;

public int _channel; // 1: "mix-data"(mono) 2: L+R
private int _lines; // default number of spectrum lines
private int _lines; // number of spectrum lines

public Analyzer(ComboBox devicelist, Button enumButton, Button deviceResetButton, ComboBox numberOfbarsComboBox, RadioButton monoRadio, Label freqLabel) // Control data for event subscribe
{
Expand All @@ -59,8 +60,6 @@ internal class Analyzer
_timer1.IsEnabled = false;
_spectrumdata = new List<byte>();
_devicenumber = Form1.DeviceNumber();
_DATAFLAG_8192 = _channel > 1 ? BASSData.BASS_DATA_FFT16384 | BASSData.BASS_DATA_FFT_INDIVIDUAL : BASSData.BASS_DATA_FFT8192;
_DATAFLAG_16384 = _channel > 1 ? BASSData.BASS_DATA_FFT32768 | BASSData.BASS_DATA_FFT_INDIVIDUAL : BASSData.BASS_DATA_FFT16384;
_lines = Convert.ToInt16(_form2NumberOfBarsComboBox.SelectedItem);
_UNICODE = Form1.Unicode();

Expand Down Expand Up @@ -88,8 +87,7 @@ private void Init() // Analyzer class initialization. DO NOT call twice or m

string itemName = string.Format("{0} - {1}", _devicenumber, device.name);
_mixfreq = device.mixfreq;
_DATAFLAG = _mixfreq > 96000 ? _DATAFLAG_16384 : _DATAFLAG_8192;
_mixfreqMulti = 44100f / _mixfreq * (_mixfreq > 96000 ? 2f : 1f);
SetParamFromFreq(_mixfreq); // set _DATAFLAG and _mixfreqMulti
_devicelist.SelectedIndex = _devicelist.Items.IndexOf(itemName);

Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATETHREADS, false);
Expand Down Expand Up @@ -131,7 +129,7 @@ public bool Enable
else
{
_initialized = true;
if (_mixfreq != 0f) _form2FreqLabel.Text = (_mixfreq / 1000f).ToString("0.0") + " KHz";
if (_mixfreq != 0f) _form2FreqLabel.Text = (_mixfreq / 1000f).ToString("0.0") + " khz";
}
}
bool startResult = BassWasapi.BASS_WASAPI_Start();
Expand Down Expand Up @@ -171,18 +169,22 @@ private void Timer1_Tick(object sender, EventArgs e)
float[] peak = new float[] { 0f, 0f }; // 0=Left(mono), 1=Right

if (_channel > 1)
freqValue = (int)(Math.Pow(2, (bandX * 10.0 / (_lines - 1)) + _freqShift) * _mixfreqMulti);
freqValue = (int)(Math.Pow(2, (bandX * 10.0 / (_lines - 1)) + _freqShift) * _mixfreqMultiplyer);
else
freqValue = (int)(Math.Pow(2, (bandX * 10.0 / (_lines - 1)) + _freqShift) / 5 * _mixfreqMulti); // I don't know why 5...
// denominator lager -> shift right
if (freqValue <= fftPos) freqValue = fftPos + 1; // if out of range, min. freq. selected
if (_mixfreq > 96000)
freqValue = (int)(Math.Pow(2, (bandX * 10.0 / (_lines - 1)) + _freqShift) / 5 * _mixfreqMultiplyer); // I don't know why 5...
// denominator lager -> shift right
if (freqValue <= fftPos) freqValue = fftPos + 1; // if out of range, min. freq. selected
if (_mixfreq <= 48000) // 44.1khz, 48khz
{
if (freqValue > 16384 * _channel - _channel) freqValue = 16384 * _channel - _channel;
if (freqValue > 4096 * _channel - _channel) freqValue = 4096 * _channel - _channel; // truncate last data
}
else
else if (_mixfreq <= 88200) // 88.2khz, 96khz
{
if (freqValue > 8192 * _channel - _channel) freqValue = 8192 * _channel - _channel;
}
else // 176.4khz, 192khz, 384khz and above?
{
if (freqValue > 8192 * _channel - _channel) freqValue = 8192 * _channel - _channel; // truncate last data
if (freqValue > 16384 * _channel - _channel) freqValue = 16384 * _channel - _channel;
}

for (; fftPos < freqValue; fftPos += _channel) // freq band in interreave step
Expand All @@ -209,6 +211,27 @@ private void Timer1_Tick(object sender, EventArgs e)
_spectrumdata.Clear();
}

private void SetParamFromFreq(int freq)
{
// freq is readonly

if (freq <= 48000 ) // 44.1khz, 48khz
{
_DATAFLAG = _channel > 1 ? BASSData.BASS_DATA_FFT8192 | BASSData.BASS_DATA_FFT_INDIVIDUAL : BASSData.BASS_DATA_FFT4096;
_mixfreqMultiplyer = 44100f / freq * 0.5f;
}
else if (freq <= 88200) // 88.2khz, 96khz
{
_DATAFLAG = _channel > 1 ? BASSData.BASS_DATA_FFT16384 | BASSData.BASS_DATA_FFT_INDIVIDUAL : BASSData.BASS_DATA_FFT8192;
_mixfreqMultiplyer = 44100f / freq;
}
else // 176.4khz, 192khz, 384khz and above?
{
_DATAFLAG = _channel > 1 ? BASSData.BASS_DATA_FFT32768 | BASSData.BASS_DATA_FFT_INDIVIDUAL : BASSData.BASS_DATA_FFT16384;
_mixfreqMultiplyer = 44100f / freq * 2f;
}
}

private void Form2_NumberOfBarIndexChanged(object sender, EventArgs e)
{
_lines = Convert.ToInt16(_form2NumberOfBarsComboBox.SelectedItem);
Expand Down Expand Up @@ -275,8 +298,7 @@ private void Form2_devicelist_SelectedIndexChanged(object sender, EventArgs e)
else
{
_mixfreq = device.mixfreq;
_DATAFLAG = _mixfreq > 96000 ? _DATAFLAG_16384 : _DATAFLAG_8192;
_mixfreqMulti = 44100f / _mixfreq * (_mixfreq > 96000 ? 2f : 1f);
SetParamFromFreq(_mixfreq); // set _DATAFLAG and _mixfreqMulti

bool result = Bass.BASS_Init(0, _mixfreq, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero); // need _mixfreq here
if (!result) throw new Exception("Source Device Initialize Error\r\n" + "ERROR CODE: " + Bass.BASS_ErrorGetCode().ToString());
Expand All @@ -299,10 +321,7 @@ private void Form2_MonoRadio_CheckChanged(object sender, EventArgs e)
_fft.Initialize();
_fft = new float[16384 * _channel];

//_DATAFLAG = _channel > 1 ? _DATAFLAG_16384 : _DATAFLAG_8192;
_DATAFLAG_8192 = _channel > 1 ? BASSData.BASS_DATA_FFT16384 | BASSData.BASS_DATA_FFT_INDIVIDUAL : BASSData.BASS_DATA_FFT8192;
_DATAFLAG_16384 = _channel > 1 ? BASSData.BASS_DATA_FFT32768 | BASSData.BASS_DATA_FFT_INDIVIDUAL : BASSData.BASS_DATA_FFT16384;
_DATAFLAG = _mixfreq > 96000 ? _DATAFLAG_16384 : _DATAFLAG_8192;
SetParamFromFreq(_mixfreq); // set _DATAFLAG and _mixfreqMulti

bool result = Bass.BASS_Init(0, _mixfreq, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
if (!result) throw new Exception("Source Device Initialize Error");
Expand Down
Loading

0 comments on commit e4b6d64

Please sign in to comment.