Skip to content

Commit 4484574

Browse files
committed
cli tool for waveform data
1 parent 2a7f9c8 commit 4484574

File tree

6 files changed

+264
-112
lines changed

6 files changed

+264
-112
lines changed

bootstrap.js

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
var AudioDataAnalyzer = require('./library/audioDataAnalyzer').analyzer;
2-
3-
var FileDownloader = require('./library/fileDownloader').downloader;
4-
51
var http = require('http');
62

73
var fs = require('fs');
@@ -10,6 +6,7 @@ var url = require('url');
106

117
var querystring = require('querystring');
128

9+
var waveformData = require('./library/waveformData');
1310

1411
var serverPort = 35000;
1512
var serverIp = '127.0.0.1';
@@ -84,31 +81,9 @@ http.createServer(function (request, response) {
8481

8582
var queryObject = querystring.parse(urlParts.query);
8683

87-
if (typeof queryObject !== 'undefined' && queryObject.trackId !== 'undefined' && queryObject.trackFormat !== 'undefined' && queryObject.peaksAmount !== 'undefined') {
88-
89-
// track format
90-
switch(options.fileExtension) {
91-
case 'mp3':
92-
formatCode = 'mp31';
93-
break;
94-
case 'ogg':
95-
formatCode = 'ogg1';
96-
break;
97-
default:
98-
throw 'unsupported file format';
99-
}
84+
if (typeof queryObject !== 'undefined') {
10085

101-
var options = {
102-
trackId: queryObject.trackId,
103-
trackFormat: queryObject.trackFormat,
104-
peaksAmount: queryObject.peaksAmount,
105-
host: 'storage-new.newjamendo.com',
106-
port: 80,
107-
method: 'GET',
108-
serverDirecotry: './downloaded_tracks'
109-
};
110-
111-
getWaveData(options, function(error, peaks) {
86+
waveformData.getRemoteWaveData(queryObject, function(error, peaks) {
11287

11388
if (!error) {
11489

@@ -174,52 +149,4 @@ http.createServer(function (request, response) {
174149

175150
}).listen(serverPort, serverIp);
176151

177-
console.log('server is listening, ip: ' + serverIp + ', port: ' + serverPort);
178-
179-
/**
180-
*
181-
* get the wave data for a given trackId
182-
*
183-
* @param {type} options
184-
* @param {type} callback
185-
* @returns {undefined}
186-
*/
187-
var getWaveData = function getWaveDataFunction(options, callback) {
188-
189-
// initialize the audioAnalyzer
190-
var audioDataAnalyzer = new AudioDataAnalyzer();
191-
192-
// initialize the track downloader
193-
var fileDownloader = new FileDownloader();
194-
195-
// download the track and write it on the disc of it does not already exist
196-
fileDownloader.writeToDisc(options, function writeFileCallback(error, trackPath) {
197-
198-
// if there was no error downloading and writing the track
199-
if (!error) {
200-
201-
// analyze the track using ffmpeg
202-
audioDataAnalyzer.getPeaks(trackPath, options, function getValuesCallback(error, peaks) {
203-
204-
// if there was no error analyzing the track
205-
if (!error) {
206-
207-
callback(false, peaks);
208-
209-
} else {
210-
211-
callback(error);
212-
213-
}
214-
215-
});
216-
217-
} else {
218-
219-
callback(error);
220-
221-
}
222-
223-
});
224-
225-
};
152+
console.log('server is listening, ip: ' + serverIp + ', port: ' + serverPort);

library/audioDataAnalyzer.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,22 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
5454

5555
if (stdoutOuputString !== '') {
5656

57-
console.log('stdoutOuput');
57+
//console.log('stdoutOuput');
5858
//console.log(stdoutOuputString);
5959

6060
// parse the ffprobe json string response
6161
var stdoutOuput = JSON.parse(stdoutOuputString);
62-
63-
// create a trackdata object with the informations we need
64-
trackData.duration = stdoutOuput['format']['duration'];
65-
trackData.size = stdoutOuput['format']['size'];
66-
trackData.bitRate = stdoutOuput['format']['bit_rate'];
67-
trackData.sampleRate = stdoutOuput['streams'][0]['sample_rate'];
68-
trackData.channels = stdoutOuput['streams'][0]['channels'];
62+
63+
if (Object.keys(stdoutOuput).length > 0) {
64+
65+
// create a trackdata object with the informations we need
66+
trackData.duration = stdoutOuput['format']['duration'];
67+
trackData.size = stdoutOuput['format']['size'];
68+
trackData.bitRate = stdoutOuput['format']['bit_rate'];
69+
trackData.sampleRate = stdoutOuput['streams'][0]['sample_rate'];
70+
trackData.channels = stdoutOuput['streams'][0]['channels'];
71+
72+
}
6973

7074
}
7175

@@ -92,6 +96,12 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
9296
// if the code is an error code
9397
if (code > 0) {
9498

99+
if (stderrOuputString === '') {
100+
101+
stderrOuputString = 'unknown ffprobe error';
102+
103+
}
104+
95105
callback(stderrOuputString);
96106

97107
} else {
@@ -134,10 +144,18 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
134144

135145
if (!error) {
136146

137-
console.log('ffprobe track data: ');
138-
console.log(trackData);
147+
//console.log('ffprobe track data: ');
148+
//console.log(trackData);
149+
150+
if (peaksAmountRaw !== undefined) {
139151

140-
var peaksAmount = parseInt(peaksAmountRaw);
152+
var peaksAmount = parseInt(peaksAmountRaw);
153+
154+
} else {
155+
156+
callback('peaksAmount is undefined');
157+
158+
}
141159

142160
// get audio pcm as 16bit little endians
143161
var ffmpegSpawn = childProcess.spawn(

library/directoryManager.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,36 @@ var directoryManager = function directoryManagerConstructor() {
2121
*/
2222
directoryManager.prototype.exists = function directoryExistsFunction(directory, callback) {
2323

24-
console.log('directoryExists: ' + directory);
24+
console.log('directory exists? directory: ' + directory);
2525

2626
if (callback !== undefined) {
2727

28-
// async exists
29-
fs.exists(directory, function(exists) {
28+
if (directory !== undefined) {
29+
30+
// async exists
31+
fs.exists(directory, function(exists) {
3032

31-
callback(false, exists);
33+
callback(false, exists);
3234

33-
});
35+
});
36+
37+
} else {
38+
39+
callback('directory is undefined');
40+
41+
}
3442

3543
} else {
3644

37-
fs.existsSync(directory);
45+
if (directory !== undefined) {
46+
47+
fs.existsSync(directory);
48+
49+
} else {
50+
51+
throw 'directory is undefined';
52+
53+
}
3854

3955
}
4056

@@ -50,7 +66,7 @@ directoryManager.prototype.exists = function directoryExistsFunction(directory,
5066
*/
5167
directoryManager.prototype.create = function createDirectoryFunction(directory, callback) {
5268

53-
console.log('createDirectory: ' + directory);
69+
console.log('create directory: ' + directory);
5470

5571
if (callback !== undefined) {
5672

@@ -70,7 +86,7 @@ directoryManager.prototype.create = function createDirectoryFunction(directory,
7086

7187
} else {
7288

73-
fs.mkdirSync(directory, 0666);
89+
return fs.mkdirSync(directory, 0666);
7490

7591
}
7692

library/fileDownloader.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ downloader.prototype.writeToDisc = function(options, callback) {
3535

3636
}
3737

38-
if (options.serverDirecotry === undefined) {
38+
if (options.serverDirectory === undefined) {
3939

40-
options.serverDirecotry = './downloads';
40+
options.serverDirectory = './downloads';
4141

4242
}
4343

@@ -46,7 +46,7 @@ downloader.prototype.writeToDisc = function(options, callback) {
4646
var that = this;
4747

4848
// check if the temporary tracks directory already exists
49-
directoryManager.exists(options.serverDirecotry, function directoryExistsCallback(error, exists) {
49+
directoryManager.exists(options.serverDirectory, function directoryExistsCallback(error, exists) {
5050

5151
// if there was no error checking if the directory exists
5252
if (!error) {
@@ -55,7 +55,7 @@ downloader.prototype.writeToDisc = function(options, callback) {
5555
if (!exists) {
5656

5757
// create a new directory
58-
directoryManager.create(options.serverDirecotry, createDirectoryCallback = function(error) {
58+
directoryManager.create(options.serverDirectory, createDirectoryCallback = function(error) {
5959

6060
// if there was no error creating the new directory
6161
if (!error) {
@@ -100,7 +100,7 @@ downloader.prototype.downloadIfNotExists = function downloadIfNotExists(options,
100100

101101
var fileManager = new FileManager();
102102

103-
var filePath = options.serverDirecotry + '/' + options.fileName;
103+
var filePath = options.serverDirectory + '/' + options.fileName;
104104

105105
var that = this;
106106

@@ -141,7 +141,7 @@ downloader.prototype.downloadIfNotExists = function downloadIfNotExists(options,
141141
*/
142142
downloader.prototype.downloadFile = function downloadFileFunction(downloadOptions, callback) {
143143

144-
console.log('downloadFile: ' + downloadOptions.path);
144+
console.log('downloadFile: ' + downloadOptions.fileName);
145145

146146
if (downloadOptions === undefined) {
147147

@@ -173,18 +173,24 @@ downloader.prototype.downloadFile = function downloadFileFunction(downloadOption
173173

174174
}
175175

176-
var writeStream = fs.createWriteStream(downloadOptions.filePath + downloadOptions.fileName);
176+
// the file path on the server
177+
var serverFilePath = downloadOptions.serverDirectory + '/' + downloadOptions.fileName;
178+
179+
// create a write stream
180+
var writeStream = fs.createWriteStream(serverFilePath);
177181

178182
// open a new write stream
179183
writeStream.on('open', function() {
180184

181185
var requestOptions = {
182186
hostname: downloadOptions.remoteHost,
183187
port: downloadOptions.remotePort,
184-
path: downloadOptions.remotePath,
188+
path: downloadOptions.remotePath + downloadOptions.fileName,
185189
method: downloadOptions.method
186190
};
187-
191+
192+
console.log(requestOptions);
193+
188194
// request the file from remote server
189195
var httpRequest = http.request(requestOptions, function(httpResponse) {
190196

@@ -207,7 +213,7 @@ downloader.prototype.downloadFile = function downloadFileFunction(downloadOption
207213
// close the write stream
208214
writeStream.end();
209215

210-
callback(false, downloadOptions.serverDirectory + downloadOptions.fileName);
216+
callback(false, serverFilePath);
211217

212218
});
213219

library/fileManager.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,36 @@ var fileManager = function fileManagerConstructor() {
2121
*/
2222
fileManager.prototype.exists = function fileExistsFunction(file, callback) {
2323

24-
console.log('file exists: ' + file);
24+
console.log('file exists? file: ' + file);
2525

2626
if (callback !== undefined) {
2727

28-
// async exists
29-
fs.exists(file, function(exists) {
28+
if (file !== undefined) {
29+
30+
// async exists
31+
fs.exists(file, function(exists) {
3032

31-
callback(false, exists);
33+
callback(false, exists);
3234

33-
});
35+
});
36+
37+
} else {
38+
39+
callback('file is undefined');
40+
41+
}
3442

3543
} else {
3644

37-
fs.existsSync(file);
45+
if (file !== undefined) {
46+
47+
return fs.existsSync(file);
48+
49+
} else {
50+
51+
throw 'file is undefined';
52+
53+
}
3854

3955
}
4056

0 commit comments

Comments
 (0)