Skip to content

Commit 3660910

Browse files
committed
audioAnalyzer object tweaks
1 parent 845c2ab commit 3660910

File tree

4 files changed

+119
-73
lines changed

4 files changed

+119
-73
lines changed

bootstrap.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ http.createServer(function (request, response) {
115115
if (typeof queryObject !== 'undefined' && queryObject.trackId !== 'undefined' && queryObject.trackFormat !== 'undefined') {
116116

117117
var trackName = queryObject.trackId + '.' + queryObject.trackFormat;
118-
var trackPath = 'downloaded_tracks/' + trackName;
118+
119+
if (queryObject.serverDirectory === undefined) {
120+
121+
queryObject.serverDirectory = './downloads';
122+
123+
}
124+
125+
var trackPath = queryObject.serverDirectory + '/' + trackName;
119126

120127
var fileStat = fs.statSync(trackPath);
121128

library/audioDataAnalyzer.js

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ var childProcessSpawn = require('child_process').spawn;
1010
*/
1111
var analyzer = function analyzerConstructor() {
1212

13+
this.stdoutFfprobeOuputString = '';
14+
this.stderrFfprobeOuputString = '';
15+
16+
this.stderrFfmpgegOuputString = '';
17+
18+
this.samples = [];
19+
this.peaksInPercent = [];
20+
21+
this.trackData = {};
22+
1323
};
1424

1525
/**
@@ -22,13 +32,10 @@ var analyzer = function analyzerConstructor() {
2232
*/
2333
analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
2434

25-
var stdoutOuputString = '';
26-
var stderrOuputString = '';
27-
28-
var trackData = {};
29-
3035
//console.log(trackPath);
3136

37+
var that = this;
38+
3239
// ffprobe file data
3340
var ffprobeSpawn = childProcessSpawn(
3441
'ffprobe',
@@ -49,43 +56,43 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
4956
// ffprobe recieves data on stdout
5057
ffprobeSpawn.stdout.on('data', function(data) {
5158

52-
stdoutOuputString += data;
59+
that.stdoutFfprobeOuputString += data;
5360

5461
});
5562

5663
ffprobeSpawn.stdout.on('end', function(data) {
5764

5865
//console.log('ffprobeSpawn stdout end');
59-
//console.log(stdoutOuputString);
66+
//console.log(that.stdoutFfprobeOuputString);
6067

61-
if (stdoutOuputString !== '') {
68+
if (that.stdoutFfprobeOuputString !== '') {
6269

6370
// parse the ffprobe json string response
64-
var stdoutOuput = JSON.parse(stdoutOuputString);
71+
var stdoutOuput = JSON.parse(that.stdoutFfprobeOuputString);
6572

6673
//console.log(stdoutOuput);
6774
//console.log(Object.keys(stdoutOuput).length);
6875

6976
if (Object.keys(stdoutOuput).length > 0) {
7077

7178
// create a trackdata object with the informations we need
72-
trackData.duration = stdoutOuput['format']['duration'];
73-
trackData.size = stdoutOuput['format']['size'];
74-
trackData.bitRate = stdoutOuput['format']['bit_rate'];
75-
trackData.sampleRate = stdoutOuput['streams'][0]['sample_rate'];
76-
trackData.channels = stdoutOuput['streams'][0]['channels'];
79+
that.trackData.duration = stdoutOuput['format']['duration'];
80+
that.trackData.size = stdoutOuput['format']['size'];
81+
that.trackData.bitRate = stdoutOuput['format']['bit_rate'];
82+
that.trackData.sampleRate = stdoutOuput['streams'][0]['sample_rate'];
83+
that.trackData.channels = stdoutOuput['streams'][0]['channels'];
7784

7885
}
7986

80-
//console.log(trackData);
87+
//console.log(that.trackData);
8188

8289
}
8390

8491
});
8592

8693
ffprobeSpawn.stderr.on('data', function(data) {
8794

88-
stderrOuputString += data;
95+
that.stderrFfprobeOuputString += data;
8996

9097
});
9198

@@ -102,23 +109,23 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
102109
// if the code is an error code
103110
if (code > 0) {
104111

105-
if (stderrOuputString === '') {
112+
if (that.stderrFfprobeOuputString === '') {
106113

107-
stderrOuputString = 'unknown ffprobe error';
114+
that.stderrFfprobeOuputString = 'unknown ffprobe error';
108115

109116
}
110117

111-
callback(stderrOuputString);
118+
callback(that.stderrFfprobeOuputString);
112119

113120
} else {
114121

115-
//console.log(trackData);
116-
//console.log(Object.keys(trackData).length);
122+
//console.log(that.trackData);
123+
//console.log(Object.keys(that.trackData).length);
117124

118125
// if the trackdata object isnt empty
119-
if (Object.keys(trackData).length > 0) {
126+
if (Object.keys(that.trackData).length > 0) {
120127

121-
callback(false, trackData);
128+
callback(false, that.trackData);
122129

123130
} else {
124131

@@ -165,6 +172,8 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
165172
*/
166173
analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountRaw, callback) {
167174

175+
var that = this;
176+
168177
this.getData(trackPath, function(error, trackData) {
169178

170179
if (!error) {
@@ -199,14 +208,9 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
199208
'pipe:1' // pipe to stdout
200209
]
201210
);
202-
203-
var stdoutOuputString = '';
204-
var stderrOuputString = '';
205211

206212
//ffmpegSpawn.stdout.setEncoding('utf8');
207213
ffmpegSpawn.stderr.setEncoding('utf8');
208-
209-
var samples = [];
210214

211215
ffmpegSpawn.stdout.on('data', function(buffer) {
212216

@@ -225,7 +229,7 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
225229

226230
var positiveSample = Math.abs(buffer.readInt16LE(i, false));
227231

228-
samples.push(positiveSample);
232+
that.samples.push(positiveSample);
229233

230234
}
231235

@@ -235,7 +239,7 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
235239

236240
//console.log('ffmpegSpawn stdout end');
237241

238-
var samplesLength = samples.length;
242+
var samplesLength = that.samples.length;
239243

240244
// check if we got enough samples to put at least one sample
241245
// into each peak
@@ -244,7 +248,6 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
244248
// calculate how much samples we have to put into one peak
245249
var samplesCountPerPeak = Math.floor(samplesLength / peaksAmount);
246250
var peaks = [];
247-
var peaksInPercent = [];
248251

249252
var i;
250253
var start = 0;
@@ -255,7 +258,7 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
255258
for (i = 0; i < peaksAmount; i++) {
256259

257260
// get a series of samples collection
258-
var peaksGroup = samples.slice(start, end);
261+
var peaksGroup = that.samples.slice(start, end);
259262
var x;
260263
var samplesSum = 0;
261264
var peaksGroupLength = peaksGroup.length;
@@ -289,31 +292,17 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
289292

290293
var peakInPercent = Math.round((peaks[y] / highestPeak) * 100);
291294

292-
peaksInPercent.push(peakInPercent);
295+
that.peaksInPercent.push(peakInPercent);
293296

294297
}
295-
296-
callback(false, peaksInPercent);
297-
298-
} else {
299298

300-
if (samplesLength === 0) {
301-
302-
callback('no output recieved');
303-
304-
} else if (samplesLength < peaksAmount) {
305-
306-
callback('not enough peaks in this song for a full wave');
307-
308-
}
309-
310299
}
311300

312301
});
313302

314303
ffmpegSpawn.stderr.on('data', function(data) {
315304

316-
//console.log(data.toString());
305+
that.stderrFfprobeOuputString += data;
317306

318307
});
319308

@@ -325,7 +314,41 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
325314

326315
ffmpegSpawn.on('exit', function(code) {
327316

328-
//console.log('ffmpegSpawn exit, code: ' + code);
317+
if (code > 0) {
318+
319+
if (that.stderrFfmpegOuputString === '') {
320+
321+
that.stderrFfmpegOuputString = 'unknown ffmpeg error';
322+
323+
}
324+
325+
callback(that.stderrFfmpegOuputString);
326+
327+
} else {
328+
329+
var peaksInPercentLength = that.peaksInPercent.length;
330+
331+
if (peaksInPercentLength > 0) {
332+
333+
callback(false, that.peaksInPercent);
334+
335+
} else {
336+
337+
var samplesLength = that.samples.length;
338+
339+
if (samplesLength === 0) {
340+
341+
callback('no output recieved');
342+
343+
} else if (samplesLength < peaksAmount) {
344+
345+
callback('not enough peaks in this song for a full wave');
346+
347+
}
348+
349+
}
350+
351+
}
329352

330353
});
331354

library/fileDownloader.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ var downloader = function downloaderConstructor() {
2929
*/
3030
downloader.prototype.writeToDisc = function(options, callback) {
3131

32+
//console.log(options);
33+
3234
if (options === undefined) {
3335

3436
options = {};
3537

3638
}
37-
38-
if (options.serverDirectory === undefined) {
39-
40-
options.serverDirectory = './downloads';
41-
42-
}
4339

4440
var directoryManager = new DirectoryManager();
4541

@@ -185,7 +181,7 @@ downloader.prototype.downloadFile = function downloadFileFunction(downloadOption
185181
var requestOptions = {
186182
hostname: downloadOptions.remoteHost,
187183
port: downloadOptions.remotePort,
188-
path: downloadOptions.remotePath + downloadOptions.fileName,
184+
path: downloadOptions.remotePath,
189185
method: downloadOptions.method
190186
};
191187

library/waveformData.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,22 @@ var queryObjectToOptions = function queryObjectToOptionsFunction(queryObject) {
1414
peaksAmount: queryObject.peaksAmount,
1515
method: 'GET',
1616
serverDirectory: queryObject.serverDirectory,
17-
fileName: queryObject.trackId + '.' + queryObject.trackFormat
17+
fileName: queryObject.trackId + '.' + queryObject.trackFormat,
18+
service: queryObject.service
1819
};
1920

21+
if (options.serverDirectory === undefined) {
22+
23+
options.serverDirectory = './downloads';
24+
25+
}
26+
27+
if (options.service === undefined) {
28+
29+
options.service = 'jamendo';
30+
31+
}
32+
2033
return options;
2134

2235
};
@@ -25,6 +38,8 @@ var analyzeAudio = function analyzeAudioFunction(filePath, options, callback) {
2538

2639
// initialize the audioAnalyzer
2740
var audioDataAnalyzer = new AudioDataAnalyzer();
41+
42+
//console.log(filePath);
2843

2944
// analyze the track using ffmpeg
3045
audioDataAnalyzer.getPeaks(filePath, options.peaksAmount, function getValuesCallback(error, peaks) {
@@ -56,25 +71,30 @@ var getRemoteWaveData = function getRemoteWaveDataFunction(queryObject, callback
5671

5772
// track options
5873
var options = queryObjectToOptions(queryObject);
59-
60-
// track format
61-
switch(queryObject.trackFormat) {
62-
case 'ogg':
63-
options.formatCode = 'ogg1';
64-
break;
65-
default:
66-
options.formatCode = 'mp31';
67-
}
74+
75+
//console.log(queryObject.trackFormat);
6876

6977
// service options
70-
if (queryObject.service === 'jamendo') {
71-
72-
options.remoteHost = 'storage-new.newjamendo.com';
73-
options.remotePath = '/download/track/';
74-
options.remotePort = 80;
78+
switch(queryObject.service) {
79+
80+
case 'jamendo':
81+
default:
82+
83+
// track format
84+
switch(queryObject.trackFormat) {
85+
case 'ogg':
86+
options.formatCode = 'ogg1';
87+
break;
88+
default:
89+
options.formatCode = 'mp31';
90+
}
7591

92+
options.remoteHost = 'storage-new.newjamendo.com';
93+
options.remotePath = '/download/track/' + options.trackId + '/' + options.formatCode;
94+
options.remotePort = 80;
95+
7696
}
77-
97+
7898
// initialize the track downloader
7999
var fileDownloader = new FileDownloader();
80100

0 commit comments

Comments
 (0)