Skip to content

Commit

Permalink
Automatic song finding based on matching titles and duration, generat…
Browse files Browse the repository at this point in the history
…ing confidence score for each match and selecting the best one
  • Loading branch information
nukeop committed Feb 26, 2017
1 parent a582ed9 commit 873c5ee
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/components/MainContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class MainContent extends Component {
songFinder.getTrack(
album.artist,
el.recording.title,
el.recording.length,
(track) => {
track.data.thumbnail = album.image[2]['#text'];
if (i===0 && playNow) {
Expand Down
1 change: 1 addition & 0 deletions app/containers/AlbumViewContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class AlbumViewContainer extends Component {
songFinder.getTrack(
track.recording.artistCredits[0].artist.name,
track.recording.title,
track.length,
(track) => {
this.props.home.playNow(track);
}
Expand Down
53 changes: 48 additions & 5 deletions app/utils/SongFinder.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
const strSim = require('string-similarity');
const yt = require('../api/Youtube');

function getTrack(artist, title, callback) {
const similarityThreshold = 0.9; //When to consider a song to be a match
const ratioTolerance = 0.1;

function selectBestMatch(matches, callback) {
matches.sort((a, b) => {return b.confidence - a.confidence;});
callback(matches[0].track);
}

function getTrack(artist, title, length, callback) {
// To automatically find the best match, we first compare title similarity,
// and if it's above a certain threshold, we compare the length. If it's
// within acceptable range, we add it to potential matches, then we
// return the best match.

var fullTitle = artist+ ' - '+ title;
var matches = [];

yt.youtubeTrackSearch(fullTitle, (response) => {
response.data.items.some((el, i) => {
if (el.snippet.title === fullTitle) {

var processed = 0;

response.data.items.map((el, i) => {

var similarity = strSim.compareTwoStrings(fullTitle, el.snippet.title);
if (similarity > similarityThreshold) {
var newItem = {
source: 'youtube',
artist: artist,
Expand All @@ -22,10 +42,33 @@ function getTrack(artist, title, callback) {
};

yt.youtubeFetchVideoDetails(newItem, () => {
callback(newItem);
var ytmillis = newItem.data.length.split(':');
ytmillis = (ytmillis[0]*60 + 1*ytmillis[1]) * 1000;
var ratio = ytmillis/length;

if (Math.abs(1.0-ratio) < ratioTolerance) {
matches.push({
confidence: (similarity+(1.0-Math.abs(1.0-ratio)))/2.0,
track: newItem
});
processed++;
if (processed===response.data.items.length) {
selectBestMatch(matches, callback);
}
} else {
processed++;
if (processed===response.data.items.length) {
selectBestMatch(matches, callback);
}
}
});

return true;

} else {
processed++;
if (processed===response.data.items.length) {
selectBestMatch(matches, callback);
}
}
});
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@
"mp3monkey": "^1.0.1",
"musicbrainz": "^0.2.6",
"react-debounce-input": "^2.4.2",
"react-sortable-hoc": "^0.5.0"
"react-sortable-hoc": "^0.5.0",
"string-similarity": "^1.1.0"
},
"dependencies": {
"electron-debug": "^1.1.0",
Expand Down

0 comments on commit 873c5ee

Please sign in to comment.