I'm trying to write a noise machine, and I looked at the docs for both Bass and BASS.NET on filters and effects, but had no idea how to correctly implement them as they only described the parameters of each function and with no examples either. Here's my existing code so far:
public void StartStopSounds()
{
if (ButtonText == "Start")
{
ButtonText = "Stop";
if (!Bass.Init(-1, sampleRate, DeviceInitFlags.Default, IntPtr.Zero))
throw new Exception("BASS failed to start");
bufferLength = Bass.ChannelSeconds2Bytes(tonechannel, Bass.GetConfig(Configuration.PlaybackBufferLength) / 1000d);
tonedata = new short[bufferLength];
noiseStreamCreate = new(NoiseProc);
noisechannel = Bass.CreateStream(sampleRate, 2, BassFlags.Default, noiseStreamCreate, IntPtr.Zero);
noiseBufferLength = Bass.ChannelSeconds2Bytes(tonechannel, Bass.GetConfig(Configuration.PlaybackBufferLength) / 1000d);
noisedata = new short[noiseBufferLength];
Bass.ChannelPlay(noisechannel, false);
}
else
{
ButtonText = "Start";
Bass.ChannelStop(noisechannel);
Bass.StreamFree(noisechannel);
Bass.Free();
}
}
private int NoiseProc(int handle, IntPtr buffer, int waveCalculated, IntPtr user)
{
int length = waveCalculated / 2;
if (noisedata == null)
throw new ArgumentNullException(nameof(noisedata), "The buffer array is null");
for (int a = 0; a < length; a += 2)
{
noisedata[a] = (short)(NoiseVolume * 0.5 * Random.Shared.Next(-32767, 32767));
noisedata[a + 1] = noisedata[a]; //Keep it mono, by copying one channel to the other
noisePosition += frequency / sampleRate; //Reserved for noise types that use the X coordinate of the graph
}
Marshal.Copy(noisedata, 0, buffer, length);
return waveCalculated;
}
Here was what I tried before, but nothing happened:
var fx = Bass.ChannelSetFX(noisechannel, EffectType.BQF, 1);
var eq = new BQFParameters
{
lFilter = BQFType.LowPass,
fCenter = 200, // center frequency of the filter
fBandwidth = 18, // bandwidth of the filter
fGain = 15 // gain of the filter
};
Bass.FXSetParameters(fx, eq);