Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a more accurate comparison of floating point variables. Added memory release. #1165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions NAudio.Core/Dsp/WdlResampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// 3. This notice may not be removed or altered from any source distribution.


using NAudio.Core;
using System;

// default to floats for audio samples
Expand Down Expand Up @@ -129,7 +130,7 @@ public void SetRates(double rate_in, double rate_out)
{
if (rate_in < 1.0) rate_in = 1.0;
if (rate_out < 1.0) rate_out = 1.0;
if (rate_in != m_sratein || rate_out != m_srateout)
if (!rate_in.AreEqual(m_sratein) || !rate_out.AreEqual(m_srateout))
{
m_sratein = rate_in;
m_srateout = rate_out;
Expand Down Expand Up @@ -193,7 +194,7 @@ public int ResamplePrepare(int out_samples, int nch, out WDL_ResampleSample[] in

if (sreq < 0) sreq = 0;

again:
again:
Array.Resize(ref m_rsinbuf, (m_samples_in_rsinbuf + sreq) * nch);

int sz = m_rsinbuf.Length / ((nch != 0) ? nch : 1) - m_samples_in_rsinbuf;
Expand Down Expand Up @@ -488,7 +489,7 @@ private void BuildLowPass(double filtpos)
int wantsize = m_sincsize;
int wantinterp = m_sincoversize;

if (m_filter_ratio != filtpos ||
if (!m_filter_ratio.AreEqual(filtpos) ||
m_filter_coeffs_size != wantsize ||
m_lp_oversize != wantinterp)
{
Expand Down
29 changes: 29 additions & 0 deletions NAudio.Core/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace NAudio.Core
{
public static class Extensions
{
private const double Epsilon = 0.001;

/// <summary>
/// Returns true if the floating-point values are equal within the specified margin of error.
/// </summary>
/// <param name="val1">First value.</param>
/// <param name="val2">Second value.</param>
public static bool AreEqual(this double val1, double val2)
{
return Math.Abs(val1 - val2) < Epsilon;
}

/// <summary>
/// Returns true if the floating-point values are equal within the specified margin of error.
/// </summary>
/// <param name="val1">First value.</param>
/// <param name="val2">Second value.</param>
public static bool AreEqual(this float val1, float val2)
{
return Math.Abs(val1 - val2) < Epsilon;
}
}
}
2 changes: 1 addition & 1 deletion NAudio.Core/FileFormats/SoundFont/SoundFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public SoundFont(Stream sfFile)
throw new InvalidDataException(String.Format("Not a SoundFont ({0})", formHeader));
}
RiffChunk list = riff.GetNextSubChunk();
if (list.ChunkID == "LIST")
if (list != null && list.ChunkID == "LIST")
{
//RiffChunk r = list.GetNextSubChunk();
info = new InfoChunk(list);
Expand Down
5 changes: 3 additions & 2 deletions NAudio.Core/Utils/IEEE.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using NAudio.Core;
using System;


namespace NAudio.Utils
Expand Down Expand Up @@ -56,7 +57,7 @@ public static byte[] ConvertToIeeeExtended(double num)
sign = 0;
}

if (num == 0)
if (num.AreEqual(0))
{
expon = 0; hiMant = 0; loMant = 0;
}
Expand Down
6 changes: 4 additions & 2 deletions NAudio.Core/Wave/SampleProviders/VolumeSampleProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NAudio.Wave.SampleProviders
using NAudio.Core;

namespace NAudio.Wave.SampleProviders
{
/// <summary>
/// Very simple sample provider supporting adjustable gain
Expand Down Expand Up @@ -32,7 +34,7 @@ public VolumeSampleProvider(ISampleProvider source)
public int Read(float[] buffer, int offset, int sampleCount)
{
int samplesRead = source.Read(buffer, offset, sampleCount);
if (Volume != 1f)
if (!Volume.AreEqual(1f))
{
for (int n = 0; n < sampleCount; n++)
{
Expand Down
2 changes: 2 additions & 0 deletions NAudio.Core/Wave/WaveOutputs/AiffFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ protected override void Dispose(bool disposing)
// the GC thread if the code above caused an IOException (e.g. due to disk full)
outStream.Dispose(); // will close the underlying base stream
outStream = null;
writer.Dispose();
writer = null;
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions NAudio.Core/Wave/WaveOutputs/DirectSoundOut.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using NAudio.Core;
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
Expand Down Expand Up @@ -46,7 +47,8 @@ public class DirectSoundOut : IWavePlayer
/// </summary>
public static IEnumerable<DirectSoundDeviceInfo> Devices
{
get {
get
{
devices = new List<DirectSoundDeviceInfo>();
DirectSoundEnumerate(new DSEnumCallback(EnumCallback), IntPtr.Zero);
return devices;
Expand Down Expand Up @@ -355,7 +357,7 @@ public float Volume
}
set
{
if (value != 1.0f)
if (!value.AreEqual(1.0f))
{
throw new InvalidOperationException("Setting volume not supported on DirectSoundOut, adjust the volume on your WaveProvider instead");
}
Expand Down Expand Up @@ -680,7 +682,7 @@ private int Feed(int bytesToCopy)
//----------------------------------------------------------------------------------------------
// Minimal Native DirectSound COM interop interfaces
//----------------------------------------------------------------------------------------------
#region Native DirectSound COM Interface
#region Native DirectSound COM Interface

[StructLayout(LayoutKind.Sequential, Pack = 2)]
internal class BufferDescription
Expand Down Expand Up @@ -915,7 +917,7 @@ internal interface IDirectSoundNotify
/// <returns>HANDLE of the Desktop window</returns>
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
#endregion
#endregion
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions NAudio.Core/Wave/WaveOutputs/WaveFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ protected override void Dispose(bool disposing)
// the GC thread if the code above caused an IOException (e.g. due to disk full)
outStream.Dispose(); // will close the underlying base stream
outStream = null;
writer.Dispose();
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions NAudio.Core/Wave/WaveProviders/VolumeWaveProvider16.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using NAudio.Core;
using System;

namespace NAudio.Wave
{
Expand Down Expand Up @@ -55,14 +54,14 @@ public int Read(byte[] buffer, int offset, int count)
{
// always read from the source
int bytesRead = sourceProvider.Read(buffer, offset, count);
if (this.volume == 0.0f)
if (this.volume.AreEqual(0.0f))
{
for (int n = 0; n < bytesRead; n++)
{
buffer[offset++] = 0;
}
}
else if (this.volume != 1.0f)
else if (!this.volume.AreEqual(1.0f))
{
for (int n = 0; n < bytesRead; n += 2)
{
Expand Down
15 changes: 8 additions & 7 deletions NAudio.Core/Wave/WaveStreams/WaveChannel32.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using NAudio.Core;
using NAudio.Wave.SampleProviders;
using System;

namespace NAudio.Wave
{
Expand Down Expand Up @@ -32,7 +33,7 @@ public WaveChannel32(WaveStream sourceStream, float volume, float pan)
{
PadWithZeroes = true;

var providers = new ISampleChunkConverter[]
var providers = new ISampleChunkConverter[]
{
new Mono8SampleChunkConverter(),
new Stereo8SampleChunkConverter(),
Expand All @@ -56,7 +57,7 @@ public WaveChannel32(WaveStream sourceStream, float volume, float pan)
{
throw new ArgumentException("Unsupported sourceStream format");
}

// always outputs stereo 32 bit
waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(sourceStream.WaveFormat.SampleRate, 2);
destBytesPerSample = 8; // includes stereo factoring
Expand Down Expand Up @@ -147,7 +148,7 @@ public override int Read(byte[] destBuffer, int offset, int numBytes)
// 1. fill with silence
if (position < 0)
{
bytesWritten = (int) Math.Min(numBytes, 0 - position);
bytesWritten = (int)Math.Min(numBytes, 0 - position);
for (int n = 0; n < bytesWritten; n++)
destBuffer[n + offset] = 0;
}
Expand Down Expand Up @@ -185,7 +186,7 @@ public override int Read(byte[] destBuffer, int offset, int numBytes)
/// If true, Read always returns the number of bytes requested
/// </summary>
public bool PadWithZeroes { get; set; }


/// <summary>
/// <see cref="WaveStream.WaveFormat"/>
Expand Down Expand Up @@ -223,7 +224,7 @@ public override bool HasData(int count)
{
if (position + count < 0)
return false;
return (position < length) && (volume != 0);
return (position < length) && (!volume.AreEqual(0));
}
return false;
}
Expand Down Expand Up @@ -254,7 +255,7 @@ protected override void Dispose(bool disposing)
public event EventHandler<SampleEventArgs> Sample;

// reuse the same object every time to avoid making lots of work for the garbage collector
private SampleEventArgs sampleEventArgs = new SampleEventArgs(0,0);
private SampleEventArgs sampleEventArgs = new SampleEventArgs(0, 0);

/// <summary>
/// Raise the sample event (no check for null because it has already been done)
Expand Down