Ikulo / Server / Ikulo.Server.Core / OfaFingerprinter.cs

// OfaFingerprinter.cs
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyright (C) 2008 Johannes Athmer
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.
//
//  This program is free software. It comes without any warranty, to
//  the extent permitted by applicable law. You can redistribute it
//  and/or modify it under the terms of the Do What The Fuck You Want
//  To Public License, Version 2, as published by Sam Hocevar. See
//  http://sam.zoy.org/wtfpl/COPYING for more details.

namespace Ikulo.Server.Core
{
	using System;
	using System.IO;
	
	/// <summary>
	/// Creates audio fingerprints using the <see cref="OfaWrapper"/>.
	/// </summary>
	public class OfaFingerprinter : IFingerprinter
	{
		/// <summary>
		/// <see cref="IDecoder"/> to use for decoding an audio stream
		/// before passing it to libofa.
		/// </summary>
		private IDecoder decoder;
		
		/// <summary>
		/// Initializes a new instance of the <see cref="OfaFingerprinter"/> class.
		/// </summary>
		/// <param name="decoder">
		/// <see cref="IDecoder"/> to use for decoding an audio stream
		/// before passing it to libofa.
		/// </param>
		public OfaFingerprinter(IDecoder decoder)
		{
            if (null == decoder)
            {
                throw new ArgumentNullException("decoder");
            }
            
			this.decoder = decoder;
		}
		
		/// <summary>
		/// Creates an audio fingerprint of the file at
		/// the given <paramref name="path"/>.
		/// </summary>
		/// <param name="path">
		/// Path to the file to fingerprint.
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> with the fingerprint.
		/// </returns>
		public virtual string CreatePrint(string path)
		{
			return this.CreatePrint(new FileInfo(path));
		}
		
		/// <summary>
		/// Creates an audio fingerprint of the given <paramref name="file"/>. 
		/// </summary>
		/// <param name="file">
		/// A <see cref="FileInfo"/> with audio data to fingerprint.
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> with the fingerprint.
		/// </returns>
		public virtual string CreatePrint(FileInfo file)
		{
            if (null == file)
            {
                throw new ArgumentNullException("file");
            }
            
			int sampleRate = 0;
			AudioChannels audioChannels = AudioChannels.Stereo;
			
			this.FigureOutStuffUsingTagLib(out sampleRate,
			                               out audioChannels,
			                               file);			
			
			using (Stream stream = this.decoder.Decode(file))
			{
				return OfaWrapper.CreatePrint(stream, sampleRate, audioChannels);
			}
		}

		/// <summary>
		/// Tries to figure out the <paramref name="stream"/>'s
		/// <paramref name="sampleRate"/> and <paramref name="audioChannels"/>.
		/// </summary>
		/// <param name="sampleRate">
		/// The sample rate in <see cref="System.Int32"/>.
		/// </param>
		/// <param name="audioChannels">
		/// Number of <see cref="AudioChannels"/>.
		/// </param>
		/// <param name="file">
		/// A <see cref="FileInfo"/> with audio data.
		/// </param>
		private void FigureOutStuffUsingTagLib(out int sampleRate, out AudioChannels audioChannels, FileInfo file)
		{
			TagLib.File tagLibFile = TagLib.File.Create(file.FullName);
			sampleRate = this.FigureOutSampleRate(tagLibFile);
			audioChannels = this.FigureOutAudioChannels(tagLibFile);
		}
		
		/// <summary>
		/// Tries to figure out the <paramref name="file"/>'s sample rate.
		/// </summary>
		/// <param name="file">
		/// A <see cref="TagLib.File"/> to analyze.
		/// </param>
		/// <returns>
		/// The sample rate in <see cref="System.Int32"/>.
		/// </returns>
		private int FigureOutSampleRate(TagLib.File file)
		{
			return file.Properties.AudioSampleRate;
		}
		
		/// <summary>
		/// Tries to figure out the <paramref name="file"/>'s audio channels.
		/// </summary>
		/// <param name="file">
		/// A <see cref="TagLib.File"/> to analyze.
		/// </param>
		/// <returns>
		/// Number of <see cref="AudioChannels"/>.
		/// </returns>
		private AudioChannels FigureOutAudioChannels(TagLib.File file)
		{
			return file.Properties.AudioChannels.Equals(1) ? AudioChannels.Mono : AudioChannels.Stereo;
		}
		
#region IFingerprinter member		
		
		/// <summary>
		/// Creates an audio fingerprint of the file at
		/// the given <paramref name="path"/>.
		/// </summary>
		/// <param name="path">
		/// Path to the file to fingerprint.
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> with the fingerprint.
		/// </returns>
		string IFingerprinter.CreatePrint(string path)
		{
			return this.CreatePrint(path);
		}
		
		/// <summary>
		/// Creates an audio fingerprint of the given <paramref name="file"/>. 
		/// </summary>
		/// <param name="file">
		/// A <see cref="FileInfo"/> with audio data to fingerprint.
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/> with the fingerprint.
		/// </returns>
		string IFingerprinter.CreatePrint(FileInfo file)
		{
			return this.CreatePrint(file);
		}
		
#endregion
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.