Example #1
0
 private static Song generateSongObject(MFCC mfcc, File file) {
   Song song;
   String[] artistAndTitle = file.getName().replace(".wav", "").split(" - ", 2);
   short[] data = WaveIO.readWave(file);
   if (artistAndTitle.length == 2) {
     song = new Song(artistAndTitle[0], artistAndTitle[1], mfcc.process(data), null);
   } else {
     song = new Song(null, file.getName().replace(".wav", ""), mfcc.process(data), null);
   }
   return song;
 }
Example #2
0
  private static PriorityQueue<Result> match(
      MFCC mfcc, File sampleFilePath, String databaseFilePath)
      throws FileNotFoundException, IOException, ClassNotFoundException {
    System.err.println("Processing sample...");
    Timer.greset();

    //		Song sample = (Song)ObjectIO.readObject("C:/Users/Ian/Google
    // Drive/Music/TestcaseVideo/tc5.db");
    //		double sampleMfcc[][] = sample.getMfcc();

    short[] sampleData = WaveIO.readWave(sampleFilePath);
    double sampleMfcc[][] = mfcc.process(sampleData);
    int sampleLength = sampleMfcc.length;

    System.err.println("Sample processed. Matching...");
    Timer.gtime();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(databaseFilePath));
    PriorityQueue<Result> results = new PriorityQueue<Result>();

    while (true) {
      Song song;
      try {
        song = (Song) ois.readObject();
      } catch (EOFException e) {
        break;
      }

      //			System.out.println(song);
      //			if(!song.getArtist().equals("Steve Jablonsky")) continue;
      //			else System.out.println("Steve Jablonsky");

      PriorityQueue<Result> songResults = new PriorityQueue<Result>();

      int songLength = song.getMfcc().length;

      int i, j, k;
      for (i = 0; i < songLength; i++) {
        for (k = 0; k < sampleLength; k++) { // sampleOffset
          double totalMfccScores = 0.0;

          for (j = k; j < sampleLength && i + j < songLength; j++) {
            double mfccSimilarity = Cosine.getDistance(song.getMfcc()[i + j], sampleMfcc[j]);
            if (mfccSimilarity < MFCC_SIMILARITY_THRESHOLD) break;

            totalMfccScores += mfccSimilarity;
          }

          int frameCount = j - k;

          if (frameCount >= FRAME_COUNT_ACCEPTANCE_THRESHOLD
              && totalMfccScores / frameCount >= MFCC_ACCEPTANCE_THRESHOLD) {
            songResults.add(
                new Result(totalMfccScores / frameCount, i, k, frameCount, song.toString()));
          }
        }
      }

      //			System.out.println("PRE: " + songResults.size());
      flattenResults(songResults, songLength, sampleLength);
      //			System.out.println("POST: " + songResults.size());
      results.addAll(songResults);
    }

    ois.close();

    removeMultipleHits(results, sampleLength);

    //		System.out.println("Results count: " + results.size());
    //		int printCount = 0;
    //		while(!results.isEmpty() && printCount != 100) {
    //			System.err.println(results.poll());
    //			printCount++;
    //		}

    for (Result i : results) System.err.println(i);
    System.err.println();

    // Reorder results by sample start time
    PriorityQueue<Result> tempResults = new PriorityQueue<>(Result.SAMPLE_START_TIME_COMPARATOR);
    tempResults.addAll(results);
    results = tempResults;

    System.err.println("Matching done.");
    Timer.gtime();

    return results;
  }