Skip to content

Commit

Permalink
Fix WAV duration (fix #71)
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Jun 12, 2016
1 parent 3e7d9a2 commit c5e88d3
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 53 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"semver": "^5.1.0",
"svg-inline-react": "1.0.1",
"teeny-conf": "0.2.1",
"walk-sync": "0.2.6"
"walk-sync": "0.2.6",
"wav-file-info": "0.0.7"
},
"devDependencies": {
"babel-core": "6.8.0",
Expand Down
68 changes: 17 additions & 51 deletions src/js/actions/LibraryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import AppActions from './AppActions';
import app from '../utils/app';
import utils from '../utils/utils';

import mmd from 'musicmetadata';
import fs from 'fs';
import path from 'path';
import mime from 'mime';
import walkSync from 'walk-sync';
import mime from 'mime';

const dialog = electron.remote.dialog;

Expand Down Expand Up @@ -118,68 +116,36 @@ export default {
if(i < filesListFiltered.length) {

var file = filesListFiltered[i];
var stream = fs.createReadStream(file);

// store in DB here
mmd(stream, { duration: true }, function (err, metadata) {
utils.getMetadata(file, (metadata) => {

AppActions.settings.refreshProgress(parseInt(i * 100 / filesListFiltered.length));

forloop(i + 1);
if(err) console.warn('An error occured while reading ' + file + ' id3 tags: ' + err);

fs.realpath(file, (err, realpath) => {

app.db.find({ path: metadata.path }, function (err, docs) {
if(err) console.warn(err);

// We don't want it
delete metadata.picture;

// File path and scheme type
metadata.path = realpath;
metadata.type = 'track';

// Unknown metas
if(metadata.artist.length === 0) metadata.artist = ['Unknown artist'];
if(metadata.album === null || metadata.album === '') metadata.album = 'Unknown';
if(metadata.title === null || metadata.title === '') metadata.title = path.parse(file).base;
if(metadata.duration == '') metadata.duration = 0; // .wav problem

metadata.playCount = 0;

// Formated metas for sorting
metadata.loweredMetas = {
artist : metadata.artist.map(meta => utils.stripAccents(meta.toLowerCase())),
album : utils.stripAccents(metadata.album.toLowerCase()),
albumartist : metadata.albumartist.map(meta => utils.stripAccents(meta.toLowerCase())),
title : utils.stripAccents(metadata.title.toLowerCase()),
genre : metadata.genre.map(meta => utils.stripAccents(meta.toLowerCase()))
}

app.db.find({ path: metadata.path }, function (err, docs) {
if(err) console.warn(err);
if(docs.length === 0) { // Track is not already in database
// Let's insert in the data
app.db.insert(metadata, function (err, newDoc) {
if(err) console.warn(err);
if(i === filesListFiltered.length - 1) {
AppActions.library.refreshTracks();
AppDispatcher.dispatch({
actionType : AppConstants.APP_LIBRARY_REFRESH_END
});
}
});
} else {
if(docs.length === 0) { // Track is not already in database
// Let's insert in the data
app.db.insert(metadata, function (err, newDoc) {
if(err) console.warn(err);
if(i === filesListFiltered.length - 1) {
AppActions.library.refreshTracks();
AppDispatcher.dispatch({
actionType : AppConstants.APP_LIBRARY_REFRESH_END
});
}
});
} else {
if(i === filesListFiltered.length - 1) {
AppActions.library.refreshTracks();
AppDispatcher.dispatch({
actionType : AppConstants.APP_LIBRARY_REFRESH_END
});
}
}); // db.find
}); // fs.realpath
}); // mmd
}
}); // db.find
}); // utils.getMetadata
}
})(0);
} else {
Expand Down
122 changes: 121 additions & 1 deletion src/js/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
*/

import path from 'path';
import fs from 'fs';

import mime from 'mime';
import mmd from 'musicmetadata';
import wavInfo from 'wav-file-info';


export default {

let utils = {

/**
* Parse an int to a more readable string
Expand Down Expand Up @@ -149,5 +154,120 @@ export default {
}

return chunks;
},

/**
* Get a file metadata
*
* @param file (string)
* @return object
*
*/
getMetadata: function(file, callback) {

/* output should be something like this:
{
album : null,
albumartist : null,
artist : null,
disk : null,
duration : null,
genre : null,
loweredMetas : null,
path : null,
playCount : null,
title : null,
track : null,
type : null,
year : null
}
*/

var type = mime.lookup(file);

if(['audio/wav', 'audio/x-wav', 'audio/wave', 'audio/x-pn-wav'].indexOf(type) > -1) { // If WAV

wavInfo.infoByFilename(file, function(err, info){

if (err) console.warn(err);

fs.realpath(file, (err, realpath) => {

var metadata = {
album : 'Unknown',
albumartist : [],
artist : ['Unknown artist'],
disk : {
no: 0,
of: 0
},
duration : info.duration,
genre : [],
loweredMetas : {},
path : realpath,
playCount : 0,
title : path.parse(file).base,
track : {
no: 0,
of: 0
},
type : 'track',
year : ''
};

metadata.loweredMetas = {
artist : metadata.artist.map(meta => utils.stripAccents(meta.toLowerCase())),
album : utils.stripAccents(metadata.album.toLowerCase()),
albumartist : metadata.albumartist.map(meta => utils.stripAccents(meta.toLowerCase())),
title : utils.stripAccents(metadata.title.toLowerCase()),
genre : metadata.genre.map(meta => utils.stripAccents(meta.toLowerCase()))
}

callback(metadata);
});
});

} else {

var stream = fs.createReadStream(file);

mmd(stream, { duration: true }, function (err, data) {

if(err) console.warn('An error occured while reading ' + file + ' id3 tags: ' + err);

fs.realpath(file, (err, realpath) => {

if(err) console.warn(err);

var metadata = {
album : data.album === null || data.album === '' ? 'Unknown' : data.album,
albumartist : data.albumartist,
artist : data.artist.length === 0 ? ['Unknown artist'] : data.artist,
disk : data.disk,
duration : data.duration == '' ? 0 : data.duration,
genre : data.genre,
loweredMetas : {},
path : realpath,
playCount : 0,
title : data.title === null || data.title === '' ? path.parse(file).base : data.title,
track : data.track,
type : 'track',
year : data.year
};

metadata.loweredMetas = {
artist : data.artist.map(meta => utils.stripAccents(meta.toLowerCase())),
album : utils.stripAccents(data.album.toLowerCase()),
albumartist : data.albumartist.map(meta => utils.stripAccents(meta.toLowerCase())),
title : utils.stripAccents(data.title.toLowerCase()),
genre : data.genre.map(meta => utils.stripAccents(meta.toLowerCase()))
}

callback(metadata);
});
});
}
}
}

export default utils;

0 comments on commit c5e88d3

Please sign in to comment.