Skip to content

Commit

Permalink
Minor updates for fingerprinter
Browse files Browse the repository at this point in the history
  • Loading branch information
wo80 committed Jan 16, 2016
1 parent 43dad1b commit 194005e
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 98 deletions.
34 changes: 1 addition & 33 deletions Fingerprinter/Audio/AudioDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,6 @@ public abstract class AudioDecoder : IAudioDecoder
protected int sampleRate;
protected int channels;

protected int sourceSampleRate;
protected int sourceBitDepth;
protected int sourceChannels;
protected int duration;

protected bool ready;

public int SourceSampleRate
{
get { return sourceSampleRate; }
}

public int SourceBitDepth
{
get { return sourceBitDepth; }
}

public int SourceChannels
{
get { return sourceChannels; }
}

public int Duration
{
get { return duration; }
}

public bool Ready
{
get { return ready; }
}

public int SampleRate
{
get { return sampleRate; }
Expand All @@ -60,7 +28,7 @@ public int Channels
get { return channels; }
}

public abstract void Load(string file);
public AudioProperties Format { get; protected set; }

public abstract bool Decode(IAudioConsumer consumer, int maxLength);

Expand Down
44 changes: 44 additions & 0 deletions Fingerprinter/Audio/AudioProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fingerprinter.Audio
{
public class AudioProperties
{
/// <summary>
/// Gets the sample rate of the audio source.
/// </summary>
public int SampleRate { get; private set; }

/// <summary>
/// Gets the sample rate of the audio source (must be 16 bits per sample).
/// </summary>
public int BitDepth { get; private set; }

/// <summary>
/// Gets the number of channels.
/// </summary>
public int Channels { get; private set; }

/// <summary>
/// Gets the duration of the audio source (in seconds).
/// </summary>
public int Duration { get; private set; }

public AudioProperties()
: this(0, 0, 0, 0)
{
}

public AudioProperties(int sampleRate, int bitDepth, int channels, int duration)
{
this.SampleRate = sampleRate;
this.BitDepth = bitDepth;
this.Channels = channels;
this.Duration = duration;
}
}
}
12 changes: 1 addition & 11 deletions Fingerprinter/Audio/IAudioDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ namespace Fingerprinter.Audio
/// </summary>
public interface IAudioDecoder : IDecoder, IDisposable
{
int SourceSampleRate { get; }
int SourceBitDepth { get; }
int SourceChannels { get; }

int Duration { get; }
bool Ready { get; }

/// <summary>
/// Load an audio file.
/// </summary>
void Load(string file);
AudioProperties Format { get; }
}
}
69 changes: 38 additions & 31 deletions Fingerprinter/Audio/NAudioDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,32 @@ namespace Fingerprinter.Audio
using NAudio.Wave;

/// <summary>
/// Decode using the NAudio library. Great audio library, but the MP3 decoder is kinda slow.
/// Decode using the NAudio library..
/// </summary>
public class NAudioDecoder : AudioDecoder
{
WaveStream reader;
string extension;

public override void Load(string file)
{
// Dispose on every new load
Dispose(false);

ready = false;

extension = Path.GetExtension(file).ToLowerInvariant();
string file;

if (extension.Equals(".wav"))
{
reader = new WaveFileReader(file);
}
else
{
reader = new Mp3FileReader(file);
}

var format = reader.WaveFormat;

this.sampleRate = format.SampleRate;
this.channels = format.Channels;
public NAudioDecoder(string file)
{
this.file = file;

this.sourceSampleRate = format.SampleRate;
this.sourceBitDepth = format.BitsPerSample;
this.sourceChannels = format.Channels;
this.duration = (int)reader.TotalTime.TotalSeconds;
this.ready = (format.BitsPerSample == 16);
// Open the WaveStream and keep it open until Dispose() is called. This might lock
// the file. A better approach would be to open the stream only when needed.
Initialize();
}

public override bool Decode(IAudioConsumer consumer, int maxLength)
{
if (!ready) return false;
if (reader == null) return false;

int remaining, length, size;
byte[] buffer = new byte[2 * BUFFER_SIZE];
short[] data = new short[BUFFER_SIZE];

// Samples to read to get maxLength seconds of audio
remaining = maxLength * this.sourceChannels * this.sampleRate;
remaining = maxLength * this.Format.Channels * this.sampleRate;

// Bytes to read
length = 2 * Math.Min(remaining, BUFFER_SIZE);
Expand All @@ -81,6 +60,33 @@ public override bool Decode(IAudioConsumer consumer, int maxLength)
return true;
}

private void Initialize()
{
var extension = Path.GetExtension(file).ToLowerInvariant();

if (extension.Equals(".wav"))
{
reader = new WaveFileReader(file);
}
else
{
reader = new Mp3FileReader(file);
}

var format = reader.WaveFormat;

this.sampleRate = format.SampleRate;
this.channels = format.Channels;

this.Format = new AudioProperties(format.SampleRate, format.BitsPerSample,
format.Channels, (int)reader.TotalTime.TotalSeconds);

if (format.BitsPerSample != 16)
{
Dispose(true);
}
}

#region IDisposable implementation

private bool hasDisposed = false;
Expand All @@ -99,6 +105,7 @@ public void Dispose(bool disposing)
{
reader.Close();
reader.Dispose();
reader = null;
}
}

Expand Down
7 changes: 4 additions & 3 deletions Fingerprinter/Fingerprinter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="NAudio, Version=1.7.2.19, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NAudio.1.7.2\lib\net35\NAudio.dll</HintPath>
<Reference Include="NAudio, Version=1.7.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.1.7.3\lib\net35\NAudio.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="Audio\AudioDecoder.cs" />
<Compile Include="Audio\AudioProperties.cs" />
<Compile Include="Audio\BassDecoder.cs" />
<Compile Include="Audio\IAudioDecoder.cs" />
<Compile Include="Audio\NAudioDecoder.cs" />
Expand Down
41 changes: 22 additions & 19 deletions Fingerprinter/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using AcoustID;
using AcoustID.Web;
using Fingerprinter.Audio;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using AcoustID;
using AcoustID.Web;
using Fingerprinter.Audio;
using System.Threading;

namespace Fingerprinter
{
Expand All @@ -24,9 +24,6 @@ public MainForm()
private void MainForm_Load(object sender, EventArgs e)
{
Fpcalc.Path = @"D:\Projects\AcoustId\extern\fpcalc.exe";

decoder = new NAudioDecoder();
//decoder = new BassDecoder();
}

private void btnOpen_Click(object sender, EventArgs e)
Expand All @@ -36,26 +33,32 @@ private void btnOpen_Click(object sender, EventArgs e)

if (dlg.ShowDialog() == DialogResult.OK)
{
lbFile.Text = dlg.FileName;
if (decoder != null)
{
decoder.Dispose();
}

ResetAll();
try
{
decoder = new NAudioDecoder(dlg.FileName);
//decoder = new BassDecoder();

decoder.Load(dlg.FileName);
lbFile.Text = dlg.FileName;

int bits = decoder.SourceBitDepth;
int channels = decoder.SourceChannels;
ResetAll();

int bits = decoder.Format.BitDepth;
int channels = decoder.Format.Channels;

if (decoder.Ready)
{
lbAudio.Text = String.Format("{0}Hz, {1}bit{2}, {3}",
decoder.SourceSampleRate, bits, bits != 16 ? " (not supported)" : "",
decoder.Format.SampleRate, bits, bits != 16 ? " (not supported)" : "",
channels == 2 ? "stereo" : (channels == 1 ? "mono" : "multi-channel"));

lbDuration.Text = decoder.Duration.ToString();
lbDuration.Text = decoder.Format.Duration.ToString();

btnFingerPrint.Enabled = true;
}
else
catch
{
lbAudio.Text = "Failed to load audio";
lbDuration.Text = String.Empty;
Expand Down Expand Up @@ -244,7 +247,7 @@ private void ProcessFile(string file)
{
if (File.Exists(file))
{
if (decoder.Ready)
if (decoder != null)
{
//btnOpen.Enabled = false;
btnFingerPrint.Enabled = false;
Expand Down
2 changes: 1 addition & 1 deletion Fingerprinter/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NAudio" version="1.7.2" targetFramework="net40-Client" />
<package id="NAudio" version="1.7.3" targetFramework="net40-client" />
</packages>

0 comments on commit 194005e

Please sign in to comment.