示例#1
0
        public static AudioFeature FromBytesXML(byte[] buf)
        {
            String        xmlData       = StringUtils.GetString(buf);
            XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(xmlData));

            MandelEllis mandelEllis = new MandelEllis();

            mandelEllis.ReadXML(xmlTextReader);
            return(mandelEllis);
        }
示例#2
0
        /// <summary>Get Distance</summary>
        /// <seealso cref="">comirva.audio.feature.AudioFeature#GetDistance(comirva.audio.feature.AudioFeature)</seealso>
        public override double GetDistance(AudioFeature f)
        {
            if (!(f is MandelEllis))
            {
                new Exception("Can only handle AudioFeatures of type Mandel Ellis, not of: " + f);
                return(-1);
            }
            MandelEllis other = (MandelEllis)f;

            return(KullbackLeibler(this.gmmMe, other.gmmMe) + KullbackLeibler(other.gmmMe, this.gmmMe));
        }
示例#3
0
        public static AudioFeature FromBytesCompressed(byte[] byteArray)
        {
            using (Stream stream = new MemoryStream(byteArray)) {
                Matrix mean           = Matrix.LoadBinary(stream);
                Matrix covarMatrix    = Matrix.LoadBinary(stream);
                Matrix covarMatrixInv = Matrix.LoadBinary(stream);
                stream.Flush();

                MandelEllis.GmmMe gmmme = new MandelEllis.GmmMe(mean, covarMatrix, covarMatrixInv);
                var mandelEllis         = new MandelEllis(gmmme);
                return(mandelEllis);
            }
        }
		public AudioFeature Calculate(double[] input)
		{
			//pack the mfccs into a pointlist
			double[][] mfccCoefficients = mfcc.Process(input);

			//check if element 0 exists
			if(mfccCoefficients.Length == 0)
				throw new ArgumentException("The input stream is to short to process;");

			//create mfcc matrix
			Matrix mfccs = new Matrix(mfccCoefficients);
			#if DEBUG
			mfccs.WriteText("mfccdata-mandelellis.txt");
			mfccs.DrawMatrixGraph("mfccdata-mandelellis.png");
			#endif

			// compute mean
			//Matrix mean = mfccs.Mean(1).Transpose();
			Matrix mean = mfccs.Mean(2);
			#if DEBUG
			mean.WriteText("mean-mandelellis.txt");
			mean.DrawMatrixGraph("mean-mandelellis.png");
			#endif
			
			// create covariance matrix
			Matrix covarMatrix = mfccs.Cov();
			#if DEBUG
			covarMatrix.WriteText("covariance-mandelellis.txt");
			covarMatrix.DrawMatrixGraph("covariance-mandelellis.png");
			#endif
			
			// Inverse Covariance
			Matrix covarMatrixInv;
			try {
				//covarMatrixInv = covarMatrix.Inverse();
				covarMatrixInv = covarMatrix.InverseGausJordan();
			} catch (Exception) {
				Console.Error.WriteLine("Mandel Ellis Extraction Failed!");
				return null;
			}
			#if DEBUG
			covarMatrixInv.WriteText("inverse_covariance-mandelellis.txt");
			covarMatrixInv.DrawMatrixGraph("inverse_covariance-mandelellis.png");
			#endif

			MandelEllis.GmmMe gmmMe = new MandelEllis.GmmMe(mean, covarMatrix, covarMatrixInv);
			MandelEllis mandelEllis = new MandelEllis(gmmMe);
			return mandelEllis;
		}
示例#5
0
        /// <summary>Get Distance</summary>
        /// <seealso cref="">comirva.audio.feature.AudioFeature#GetDistance(comirva.audio.feature.AudioFeature)</seealso>
        public override double GetDistance(AudioFeature f, AudioFeature.DistanceType t)
        {
            if (!(f is MandelEllis))
            {
                new Exception("Can only handle AudioFeatures of type Mandel Ellis, not of: " + f);
                return(-1);
            }
            MandelEllis other = (MandelEllis)f;

            DistanceMeasure distanceMeasure = DistanceMeasure.Euclidean;

            switch (t)
            {
            case AudioFeature.DistanceType.Dtw_Euclidean:
                distanceMeasure = DistanceMeasure.Euclidean;
                break;

            case AudioFeature.DistanceType.Dtw_SquaredEuclidean:
                distanceMeasure = DistanceMeasure.SquaredEuclidean;
                break;

            case AudioFeature.DistanceType.Dtw_Manhattan:
                distanceMeasure = DistanceMeasure.Manhattan;
                break;

            case AudioFeature.DistanceType.Dtw_Maximum:
                distanceMeasure = DistanceMeasure.Maximum;
                break;

            case AudioFeature.DistanceType.KullbackLeiblerDivergence:
            default:
                return(KullbackLeibler(this.gmmMe, other.gmmMe) + KullbackLeibler(other.gmmMe, this.gmmMe));
            }
            Dtw dtw = new Dtw(this.GetArray(), other.GetArray(), distanceMeasure, true, true, null, null, null);

            return(dtw.GetCost());
        }
示例#6
0
		/// <summary>
		/// Find Similar Tracks to one or many audio files using their unique database id(s)
		/// </summary>
		/// <param name="id">an array of unique database ids for the audio files to search for similar matches</param>
		/// <param name="exclude">an array of unique database ids to ignore (normally the same as the id array)</param>
		/// <param name="db">database</param>
		/// <param name="analysisMethod">analysis method (SCMS or MandelEllis)</param>
		/// <param name="numToTake">max number of entries to return</param>
		/// <param name="percentage">percentage below and above the duration in ms when querying (used if between 0.1 - 0.9)</param>
		/// <param name="distanceType">distance method to use (KullbackLeiblerDivergence is default)</param>
		/// <returns>a  list of query results</returns>
		public static List<FindSimilar.QueryResult> SimilarTracksList(int[] id, int[] exclude, Db db, Analyzer.AnalysisMethod analysisMethod, int numToTake=25, double percentage=0.2, AudioFeature.DistanceType distanceType = AudioFeature.DistanceType.KullbackLeiblerDivergence) {

			AudioFeature[] seedAudioFeatures = null;
			AudioFeature[] audioFeatures = null;
			switch (analysisMethod) {
				case Analyzer.AnalysisMethod.MandelEllis:
					seedAudioFeatures = new MandelEllis[id.Length];
					audioFeatures = new MandelEllis[100];
					break;
				case Analyzer.AnalysisMethod.SCMS:
					seedAudioFeatures = new Scms[id.Length];
					audioFeatures = new Scms[100];
					break;
			}
			
			for (int i = 0; i < seedAudioFeatures.Length; i++) {
				seedAudioFeatures[i] = db.GetTrack(id[i], analysisMethod);
			}
			
			// Get all tracks from the DB except the seedSongs
			IDataReader r = db.GetTracks(exclude, seedAudioFeatures[0].Duration, percentage);
			
			// store results in a query results list
			List<FindSimilar.QueryResult> queryResultList = new List<FindSimilar.QueryResult>();
			
			int[] mapping = new int[100];
			int read = 1;
			double d;
			double dcur;
			float count;
			
			while (read > 0) {
				read = db.GetNextTracks(ref r, ref audioFeatures, ref mapping, 100, analysisMethod);
				for (int i = 0; i < read; i++) {
					
					d = 0;
					count = 0;
					for (int j = 0; j < seedAudioFeatures.Length; j++) {
						dcur = seedAudioFeatures[j].GetDistance(audioFeatures[i], distanceType);
						
						// convert to positive values
						dcur = Math.Abs(dcur);

						d += dcur;
						count++;
					}
					if (d > 0) {
						QueryResult queryResult = new QueryResult();
						queryResult.Id = mapping[i];
						queryResult.Path = audioFeatures[i].Name;
						queryResult.Duration = audioFeatures[i].Duration;
						queryResult.Similarity = d/count;
						queryResultList.Add(queryResult);
					}
				}
			}
			
			var sortedList = (from row in queryResultList
			                  orderby row.Similarity ascending
			                  select new QueryResult {
			                  	Id = row.Id,
			                  	Path = row.Path,
			                  	Duration = row.Duration,
			                  	Similarity = row.Similarity
			                  }).Take(numToTake).ToList();
			
			return sortedList;
		}
示例#7
0
		/// <summary>
		/// Find Similar Tracks to one or many audio files using their unique database id(s)
		/// </summary>
		/// <param name="id">an array of unique database ids for the audio files to search for similar matches</param>
		/// <param name="exclude">an array of unique database ids to ignore (normally the same as the id array)</param>
		/// <param name="db">database</param>
		/// <param name="analysisMethod">analysis method (SCMS or MandelEllis)</param>
		/// <param name="numToTake">max number of entries to return</param>
		/// <param name="percentage">percentage below and above the duration in ms when querying (used if between 0.1 - 0.9)</param>
		/// <param name="distanceType">distance method to use (KullbackLeiblerDivergence is default)</param>
		/// <returns>a dictinary list of key value pairs (filepath and distance)</returns>
		public static Dictionary<KeyValuePair<int, string>, double> SimilarTracks(int[] id, int[] exclude, Db db, Analyzer.AnalysisMethod analysisMethod, int numToTake=25, double percentage=0.2, AudioFeature.DistanceType distanceType = AudioFeature.DistanceType.KullbackLeiblerDivergence)
		{
			DbgTimer t = new DbgTimer();
			t.Start();
			
			AudioFeature[] seedAudioFeatures = null;
			AudioFeature[] audioFeatures = null;
			switch (analysisMethod) {
				case Analyzer.AnalysisMethod.MandelEllis:
					seedAudioFeatures = new MandelEllis[id.Length];
					audioFeatures = new MandelEllis[100];
					break;
				case Analyzer.AnalysisMethod.SCMS:
					seedAudioFeatures = new Scms[id.Length];
					audioFeatures = new Scms[100];
					break;
			}
			
			for (int i = 0; i < seedAudioFeatures.Length; i++) {
				seedAudioFeatures[i] = db.GetTrack(id[i], analysisMethod);
			}
			
			// Get all tracks from the DB except the seedSongs
			IDataReader r = db.GetTracks(exclude, seedAudioFeatures[0].Duration, percentage);
			
			// store results in a dictionary
			var NameDictionary = new Dictionary<KeyValuePair<int, string>, double>();
			
			int[] mapping = new int[100];
			int read = 1;
			double d;
			double dcur;
			float count;
			
			while (read > 0) {
				read = db.GetNextTracks(ref r, ref audioFeatures, ref mapping, 100, analysisMethod);
				for (int i = 0; i < read; i++) {
					
					d = 0;
					count = 0;
					for (int j = 0; j < seedAudioFeatures.Length; j++) {
						dcur = seedAudioFeatures[j].GetDistance(audioFeatures[i], distanceType);
						
						// convert to positive values
						dcur = Math.Abs(dcur);

						d += dcur;
						count++;
					}
					if (d > 0) {
						NameDictionary.Add(new KeyValuePair<int,string>(mapping[i], audioFeatures[i].Name), d/count);
						//NameDictionary.Add(new KeyValuePair<int,string>(mapping[i], String.Format("{0} ({1} ms)", audioFeatures[i].Name, audioFeatures[i].Duration)), d/count);
					}
				}
			}
			
			// sort by non unique values
			var sortedDict = (from entry in NameDictionary orderby entry.Value ascending select entry)
				.Take(numToTake)
				.ToDictionary(pair => pair.Key, pair => pair.Value);

			Console.Out.WriteLine(String.Format("Found Similar to ({0}) in {1} ms", String.Join(",", seedAudioFeatures.Select(p=>p.Name)), t.Stop().TotalMilliseconds));
			return sortedDict;
		}
示例#8
0
		/// <summary>
		/// Find Similar Tracks to an audio file using its file path
		/// </summary>
		/// <param name="searchForPath">audio file path</param>
		/// <param name="db">database</param>
		/// <param name="analysisMethod">analysis method (SCMS or MandelEllis)</param>
		/// <param name="numToTake">max number of entries to return</param>
		/// <param name="percentage">percentage below and above the duration in ms when querying (used if between 0.1 - 0.9)</param>
		/// <param name="distanceType">distance method to use (KullbackLeiblerDivergence is default)</param>
		/// <returns>a  list of query results</returns>
		public static List<FindSimilar.QueryResult> SimilarTracksList(string searchForPath, Db db, Analyzer.AnalysisMethod analysisMethod, int numToTake=25, double percentage=0.2, AudioFeature.DistanceType distanceType = AudioFeature.DistanceType.KullbackLeiblerDivergence) {

			FileInfo fi = new FileInfo(searchForPath);
			AudioFeature seedAudioFeature = null;
			AudioFeature[] audioFeatures = null;
			switch (analysisMethod) {
				case Analyzer.AnalysisMethod.MandelEllis:
					seedAudioFeature = Analyzer.AnalyzeMandelEllis(fi);
					audioFeatures = new MandelEllis[100];
					break;
				case Analyzer.AnalysisMethod.SCMS:
					seedAudioFeature = Analyzer.AnalyzeScms(fi);
					audioFeatures = new Scms[100];
					break;
			}
			
			// Get all tracks from the DB except the seedSongs
			IDataReader r = db.GetTracks(null, seedAudioFeature.Duration, percentage);
			
			// store results in a query results list
			List<FindSimilar.QueryResult> queryResultList = new List<FindSimilar.QueryResult>();
			
			int[] mapping = new int[100];
			int read = 1;
			double dcur;
			
			while (read > 0) {
				read = db.GetNextTracks(ref r, ref audioFeatures, ref mapping, 100, analysisMethod);
				for (int i = 0; i < read; i++) {
					dcur = seedAudioFeature.GetDistance(audioFeatures[i], distanceType);
					
					// convert to positive values
					dcur = Math.Abs(dcur);
					
					QueryResult queryResult = new QueryResult();
					queryResult.Id = mapping[i];
					queryResult.Path = audioFeatures[i].Name;
					queryResult.Duration = audioFeatures[i].Duration;
					queryResult.Similarity = dcur;
					queryResultList.Add(queryResult);
				}
			}
			
			var sortedList = (from row in queryResultList
			                  orderby row.Similarity ascending
			                  select new QueryResult {
			                  	Id = row.Id,
			                  	Path = row.Path,
			                  	Duration = row.Duration,
			                  	Similarity = row.Similarity
			                  }).Take(numToTake).ToList();
			
			return sortedList;
		}
示例#9
0
		/// <summary>
		/// Find Similar Tracks to an audio file using its file path
		/// </summary>
		/// <param name="searchForPath">audio file path</param>
		/// <param name="db">database</param>
		/// <param name="analysisMethod">analysis method (SCMS or MandelEllis)</param>
		/// <param name="numToTake">max number of entries to return</param>
		/// <param name="percentage">percentage below and above the duration in ms when querying (used if between 0.1 - 0.9)</param>
		/// <param name="distanceType">distance method to use (KullbackLeiblerDivergence is default)</param>
		/// <returns>a dictinary list of key value pairs (filepath and distance)</returns>
		public static Dictionary<KeyValuePair<int, string>, double> SimilarTracks(string searchForPath, Db db, Analyzer.AnalysisMethod analysisMethod, int numToTake=25, double percentage=0.2, AudioFeature.DistanceType distanceType = AudioFeature.DistanceType.KullbackLeiblerDivergence)
		{
			DbgTimer t = new DbgTimer();
			t.Start();

			FileInfo fi = new FileInfo(searchForPath);
			AudioFeature seedAudioFeature = null;
			AudioFeature[] audioFeatures = null;
			switch (analysisMethod) {
				case Analyzer.AnalysisMethod.MandelEllis:
					seedAudioFeature = Analyzer.AnalyzeMandelEllis(fi);
					audioFeatures = new MandelEllis[100];
					break;
				case Analyzer.AnalysisMethod.SCMS:
					seedAudioFeature = Analyzer.AnalyzeScms(fi);
					audioFeatures = new Scms[100];
					break;
			}
			
			// Get all tracks from the DB except the seedSongs
			IDataReader r = db.GetTracks(null, seedAudioFeature.Duration, percentage);
			
			// store results in a dictionary
			var NameDictionary = new Dictionary<KeyValuePair<int, string>, double>();
			
			int[] mapping = new int[100];
			int read = 1;
			double dcur;
			
			while (read > 0) {
				read = db.GetNextTracks(ref r, ref audioFeatures, ref mapping, 100, analysisMethod);
				for (int i = 0; i < read; i++) {
					dcur = seedAudioFeature.GetDistance(audioFeatures[i], distanceType);
					
					// convert to positive values
					dcur = Math.Abs(dcur);
					
					NameDictionary.Add(new KeyValuePair<int,string>(mapping[i], audioFeatures[i].Name), dcur);
				}
			}
			
			// sort by non unique values
			var sortedDict = (from entry in NameDictionary orderby entry.Value ascending select entry)
				.Take(numToTake)
				.ToDictionary(pair => pair.Key, pair => pair.Value);
			
			Console.Out.WriteLine(String.Format("Found Similar to ({0}) in {1} ms", seedAudioFeature.Name, t.Stop().TotalMilliseconds));
			return sortedDict;
		}
示例#10
0
		public static AudioFeature FromBytesXML(byte[] buf) {
			String xmlData = StringUtils.GetString(buf);
			XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(xmlData));
			
			MandelEllis mandelEllis = new MandelEllis();
			mandelEllis.ReadXML(xmlTextReader);
			return mandelEllis;
		}
示例#11
0
		public static AudioFeature FromBytesCompressed(byte[] byteArray)
		{
			using (Stream stream = new MemoryStream(byteArray)) {
				Matrix mean = Matrix.LoadBinary(stream);
				Matrix covarMatrix = Matrix.LoadBinary(stream);
				Matrix covarMatrixInv = Matrix.LoadBinary(stream);
				stream.Flush();
				
				MandelEllis.GmmMe gmmme = new MandelEllis.GmmMe(mean, covarMatrix, covarMatrixInv);
				var mandelEllis = new MandelEllis(gmmme);
				return mandelEllis;
			}
		}