/// <summary> /// Get track info from the filename /// </summary> /// <param name = "mintracklen">Min track length</param> /// <param name = "maxtracklen">Max track length</param> /// <param name = "filename">Filename from which to extract the requested info</param> /// <param name = "proxy">Audio proxy to read tags</param> /// <returns>Track to be analyzed further / null if the track is not eligible</returns> public static Track GetTrackInfo(int mintracklen, int maxtracklen, string filename, BassProxy proxy) { TAG_INFO tags = proxy.GetTagInfoFromFile(filename); //get file tags string artist, title; double duration; if (tags == null) { /*The song does not contain any tags*/ artist = "Unknown"; title = "Unknown"; duration = 60; } else { /*The song contains related tags*/ artist = tags.artist; title = tags.title; duration = tags.duration; } if (String.IsNullOrEmpty(artist)) /*assign a name to music files that don't have tags*/ artist = "Unknown"; if (String.IsNullOrEmpty(title)) /*assign a title to music files that don't have tags*/ title = "Unknown"; if (duration < mintracklen || duration > maxtracklen) /*check the duration of a music file*/ return null; Track track = new Track { Artist = artist, Title = title, TrackLength = duration, Path = Path.GetFullPath(filename) }; return track; }
/// <summary> /// Draw the spectrogram of the audio file /// </summary> private void BtnDrawSpectrumClick(object sender, EventArgs e) { if (String.IsNullOrEmpty(_tbPathToFile.Text)) { MessageBox.Show(Resources.SelectAPathToBeDrawn, Resources.SelectFile, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (!File.Exists(Path.GetFullPath(_tbPathToFile.Text))) { MessageBox.Show(Resources.NoSuchFile, Resources.NoSuchFile, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } SaveFileDialog sfd = new SaveFileDialog { Filter = Resources.FileFilterJPeg, FileName = Path.GetFileNameWithoutExtension(_tbPathToFile.Text) + "_spectrum_" + ".jpg" }; if (sfd.ShowDialog() == DialogResult.OK) { FadeControls(false); Action action = () => { using (BassProxy proxy = new BassProxy()) { FingerprintManager manager = new FingerprintManager(); float[][] data = manager.CreateSpectrogram(proxy, Path.GetFullPath(_tbPathToFile.Text), 0, 0); double duration = proxy.GetTagInfoFromFile(Path.GetFullPath(_tbPathToFile.Text)).duration; Bitmap image = Imaging.GetSpectrogramImage(data, (int) _nudWidth.Value, (int) _nudHeight.Value); image.Save(sfd.FileName, ImageFormat.Jpeg); image.Dispose(); } }; action.BeginInvoke(((result) => { FadeControls(true); action.EndInvoke(result); MessageBox.Show(Resources.ImageIsDrawn, Resources.Finished, MessageBoxButtons.OK, MessageBoxIcon.Information); }), null); } }
/// <summary> /// Actual synchronous insert in the database /// </summary> /// <param name = "start">Start index</param> /// <param name = "end">End index</param> private void InsertInDatabase(int start, int end) { BassProxy proxy = new BassProxy(); //Proxy used to read from file IStride stride = null; Invoke(new Action(() => { stride = WinUtils.GetStride((StrideType) _cmbStrideType.SelectedIndex, //Get stride according to the underlying combo box selection (int) _nudStride.Value, 0, _fingerManager.SamplesPerFingerprint); }), null); Action actionInterface = () => { _pbTotalSongs.PerformStep(); _nudProcessed.Value = _processed; _nudLeft.Value = _left; _nudBadFiles.Value = _badFiles; _nudDetectedDuplicates.Value = _duplicates; }; Action<object[], Color> actionAddItems = (parameters, color) => { int index = _dgvFillDatabase.Rows.Add(parameters); _dgvFillDatabase.FirstDisplayedScrollingRowIndex = index; if (color != Color.Empty) _dgvFillDatabase.Rows[index].DefaultCellStyle.BackColor = color; }; for (int i = start; i < end; i++) //Process the corresponding files { if (_stopFlag) return; TAG_INFO tags = proxy.GetTagInfoFromFile(_fileList[i]); //Get Tags from file if (tags == null) { //TAGS are null _badFiles++; Invoke(actionAddItems, new Object[] {"TAGS ARE NULL", _fileList[i], 0, 0}, Color.Red); continue; } string artist = tags.artist; //Artist string title = tags.title; //Title double duration = tags.duration; //Duration if (duration < MIN_TRACK_LENGTH || duration > MAX_TRACK_LENGTH) //Check whether the duration is ok { //Duration too small _badFiles++; Invoke(actionAddItems, new Object[] {"BAD DURATION", _fileList[i], 0, 0}, Color.Red); continue; } if (String.IsNullOrEmpty(artist) || String.IsNullOrEmpty(title)) //Check whether the tags are properly defined { //Title or Artist tag is null _badFiles++; Invoke(actionAddItems, new Object[] {"TAGS MISSING", _fileList[i], 0, 0}, Color.Red); continue; } Album album = GetCoresspondingAlbum(tags); //Get Album (whether new or unknown or aborted) if (album == null) //Check whether the user aborted return; Track track = null; lock (this) { try { if (_dalManager.ReadTrackByArtistAndTitleName(artist, title) != null) // Check if this file is already in the database { _duplicates++; //There is such file in the database continue; } track = new Track(-1, artist, title, album.Id, (int) duration); //Create New Track _dalManager.InsertTrack(track); //Insert new Track in the database } catch (Exception e) { //catch any exception and abort the insertion MessageBox.Show(e.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } int count = 0; try { List<bool[]> images = _fingerManager.CreateFingerprints(proxy, _fileList[i], stride); //Create Fingerprints and insert them in database List<Fingerprint> inserted = Fingerprint.AssociateFingerprintsToTrack(images, track.Id); _dalManager.InsertFingerprint(inserted); count = inserted.Count; switch (_hashAlgorithm) //Hash if there is a need in doing so { case HashAlgorithm.LSH: //LSH + Min Hash has been chosen HashFingerprintsUsingMinHash(inserted, track, _hashTables, _hashKeys); break; case HashAlgorithm.NeuralHasher: HashFingerprintsUsingNeuralHasher(inserted, track); break; case HashAlgorithm.None: break; } } catch (Exception e) { //catch any exception and abort the insertion MessageBox.Show(e.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Invoke(actionAddItems, new Object[] {artist, title, album.Name, duration, count}, Color.Empty); _left--; _processed++; Invoke(actionInterface); } }
private void ProcessFiles() { Action finishDel = delegate { buttonStop.Enabled = false; buttonPause.Enabled = false; buttonStartConversion.Enabled = true; _stopped = false; }; Process process = new Process { StartInfo = { FileName = "ffmpeg.exe", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, RedirectStandardInput = true, RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false } }; Action del = delegate { labelCurrentProcessed.Text = _currentProceesedFiles.ToString(); labelSkipped.Text = _skipped.ToString(); richTextBox1.AppendText(process.StandardError.ReadToEnd() + "\n"); richTextBox1.Focus(); richTextBox1.SelectionStart = richTextBox1.Text.Length; }; using (BassProxy proxy = new BassProxy()) { foreach (string f in _fileList) { if (_stopped) { Invoke(finishDel); Thread.CurrentThread.Abort(); } while (_pause) { if (_stopped) { Invoke(finishDel); Thread.CurrentThread.Abort(); } Thread.Sleep(1000); } TAG_INFO tags = null; try { //Read Tags from the file tags = proxy.GetTagInfoFromFile(f); } catch { _skipped++; _currentProceesedFiles++; Invoke(del); continue; } //Compose the output name of the wav file if (String.IsNullOrEmpty(tags.title) || tags.title.Length == 0) { //Skip file _skipped++; _currentProceesedFiles++; Invoke(del); continue; } string artist = ""; if (String.IsNullOrEmpty(tags.artist)) { if (tags.composer == null) { _skipped++; _currentProceesedFiles++; Invoke(del); continue; } artist = tags.composer; } else artist = tags.artist; string outfilename = tags.title + " + " + artist + ".wav"; string outfile = _outputPath + "\\" + outfilename; string arguments = "-i \"" + f + "\" -ac 1 -ar " + _samplingRate + " -ab " + _bitRate + " \"" + outfile + "\""; process.StartInfo.Arguments = arguments; process.Start(); process.StandardInput.Write("n"); _currentProceesedFiles++; Invoke(del); } } Invoke(finishDel); }