Skip to content

Commit 09cc9f5

Browse files
committed
commented
1 parent 2f6b898 commit 09cc9f5

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

bootstrap.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,37 @@ var querystring = require('querystring');
1414
var serverPort = 35000;
1515
var serverIp = '127.0.0.1';
1616

17+
/**
18+
*
19+
* create a new nodejs server handle incoming requests
20+
*
21+
* @param {type} request
22+
* @param {type} response
23+
*/
1724
http.createServer(function (request, response) {
1825

26+
// parse the url
1927
var urlParts = url.parse(request.url);
2028

2129
//console.log(urlParts);
2230

31+
// check if its is the url of a javascript file
2332
if (urlParts.pathname.split('.').pop() === 'js') {
2433

25-
// not secure but this is a prototype
34+
// if the file exists send it to the client
35+
// not really secure but this is a prototype
2636
fs.readFile('client' + urlParts.pathname, function(error, fileContent) {
2737

2838
if (!error) {
2939

40+
// send the static file to the client
3041
response.writeHead(200, { 'Content-Type': 'application/javascript' });
3142
response.write(fileContent);
3243
response.end();
3344

3445
} else {
3546

47+
// the file was not on the server send a 404 page to the client
3648
response.writeHead(404, { 'Content-Type': 'text/html' });
3749
response.write('page not found');
3850
response.end();
@@ -43,18 +55,22 @@ http.createServer(function (request, response) {
4355

4456
} else {
4557

58+
// handle the "routes"
4659
switch(urlParts.pathname) {
4760
case '/':
4861
fs.readFile('client/index.html', function(error, html) {
4962

5063
if (!error) {
5164

65+
// send the main html page to the client
5266
response.writeHead(200, { 'Content-Type': 'text/html' });
5367
response.write(html);
5468
response.end();
5569

5670
} else {
5771

72+
// the main page could not be found return a page not
73+
// found message
5874
response.writeHead(404, { 'Content-Type': 'text/html' });
5975
response.write('page not found');
6076
response.end();
@@ -73,12 +89,14 @@ http.createServer(function (request, response) {
7389

7490
if (!error) {
7591

92+
// success, send the track peaks to the client
7693
response.writeHead(200, { 'Content-Type': 'application/json' });
7794
response.write('{ "peaks": ' + JSON.stringify(peaks) + ' }');
7895
response.end();
7996

8097
} else {
8198

99+
// fail, send the error to the client
82100
response.writeHead(500, { 'Content-Type': 'application/json' });
83101
response.write('{ error: ' + error + ' }');
84102
response.end();
@@ -102,21 +120,36 @@ http.createServer(function (request, response) {
102120

103121
console.log('server is listening, ip: ' + serverIp + ', port: ' + serverPort);
104122

123+
/**
124+
*
125+
* get the wave data for a given trackId
126+
*
127+
* @param {type} trackId
128+
* @param {type} peaksAmount
129+
* @param {type} callback
130+
* @returns {undefined}
131+
*/
105132
var getWaveData = function getWaveDataFunction(trackId, peaksAmount, callback) {
106133

134+
// initialize the audioAnalyzer
107135
var audioDataAnalyzer = new AudioDataAnalyzer();
108136

137+
// initialize the track downloader
109138
var trackDownloader = new TrackDownloader();
110139

111140
var temporaryTracksDirecotry = './downloaded_tracks';
112141
var format = 'ogg';
113142

143+
// download the track and write it on the disc of it does not already exist
114144
trackDownloader.writeTrackToDisc(trackId, function writeTrackCallback(error, trackPath) {
115145

146+
// if there was no error downloading and writing the track
116147
if (!error) {
117148

149+
// analyze the track using ffmpeg
118150
audioDataAnalyzer.getPeaks(trackPath, peaksAmount, function getValuesCallback(error, peaks) {
119151

152+
// if there was no error analyzing the track
120153
if (!error) {
121154

122155
callback(false, peaks);

library/audioDataAnalyzer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var childProcess = require('child_process');
44

55
/**
6+
*
7+
* analyzer constructor
68
*
79
* @returns {analyzerConstructor}
810
*/
@@ -37,6 +39,7 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
3739
var stdoutOuputString = '';
3840
var stderrOuputString = '';
3941

42+
// ffprobe recieves data on stdout
4043
ffprobeSpawn.stdout.on('data', function(data) {
4144

4245
stdoutOuputString += data;
@@ -54,8 +57,10 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
5457
console.log('stdoutOuput');
5558
//console.log(stdoutOuputString);
5659

60+
// parse the ffprobe json string response
5761
var stdoutOuput = JSON.parse(stdoutOuputString);
5862

63+
// create a trackdata object with the informations we need
5964
trackData.duration = stdoutOuput['format']['duration'];
6065
trackData.size = stdoutOuput['format']['size'];
6166
trackData.bitRate = stdoutOuput['format']['bit_rate'];
@@ -84,12 +89,14 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
8489

8590
console.log('ffprobeSpawn exit, code: ' + code);
8691

92+
// if the code is an error code
8793
if (code > 0) {
8894

8995
callback(stderrOuputString);
9096

9197
} else {
9298

99+
// if the trackdata object isnt empty
93100
if (Object.keys(trackData).length > 0) {
94101

95102
callback(false, trackData);
@@ -117,6 +124,7 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
117124
* get pcm data of a track
118125
*
119126
* @param {type} trackPath
127+
* @param {type} peaksAmountRaw
120128
* @param {type} callback
121129
* @returns {undefined}
122130
*/
@@ -186,8 +194,11 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
186194

187195
var samplesLength = samples.length;
188196

197+
// check if we got enough samples to put at least one sample
198+
// into each peak
189199
if (samplesLength > peaksAmount) {
190200

201+
// calculate how much samples we have to put into one peak
191202
var samplesCountPerPeak = Math.floor(samplesLength / peaksAmount);
192203
var peaks = [];
193204
var peaksInPercent = [];
@@ -197,13 +208,16 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
197208
var end = start + samplesCountPerPeak;
198209
var highestPeak = 0;
199210

211+
// build as much peaks as got requested
200212
for (i = 0; i < peaksAmount; i++) {
201213

214+
// get a series of samples collection
202215
var peaksGroup = samples.slice(start, end);
203216
var x;
204217
var samplesSum = 0;
205218
var peaksGroupLength = peaksGroup.length;
206219

220+
// merge the samples into a single peak
207221
for (x = 0; x < peaksGroupLength; x++) {
208222

209223
samplesSum += peaksGroup[x];
@@ -212,6 +226,7 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
212226

213227
peaks.push(samplesSum);
214228

229+
// find the highest peak
215230
if (samplesSum > highestPeak) {
216231

217232
highestPeak = samplesSum;
@@ -226,6 +241,7 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
226241
var y;
227242
var peaksLength = peaks.length;
228243

244+
// convert the peaks into percantage values
229245
for (y = 0; y < peaksLength; y++) {
230246

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

library/directoryManager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
var fs = require('fs');
33

44
/**
5+
*
6+
* directory manager
57
*
68
* @returns {directoryManagerConstructor}
79
*/
@@ -10,6 +12,8 @@ var directoryManager = function directoryManagerConstructor() {
1012
};
1113

1214
/**
15+
*
16+
* check if the directory exists
1317
*
1418
* @param {type} directory
1519
* @param {type} callback
@@ -37,6 +41,8 @@ directoryManager.prototype.exists = function directoryExistsFunction(directory,
3741
};
3842

3943
/**
44+
*
45+
* create a new directory
4046
*
4147
* @param {type} directory
4248
* @param {type} callback

library/fileManager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
var fs = require('fs');
33

44
/**
5+
*
6+
* file manager
57
*
68
* @returns {fileManagerConstructor}
79
*/
@@ -11,7 +13,7 @@ var fileManager = function fileManagerConstructor() {
1113

1214
/**
1315
*
14-
* does the file exist
16+
* check if the file exists
1517
*
1618
* @param {type} file
1719
* @param {type} callback

library/trackDownloader.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var FileManager = require('./fileManager').fileManager;
1010

1111

1212
/**
13+
*
14+
* track downloader
1315
*
1416
* @returns {downloaderConstructor}
1517
*/
@@ -40,16 +42,22 @@ downloader.prototype.writeTrackToDisc = function(trackId, callback, temporaryTra
4042

4143
var that = this;
4244

45+
// check if the temporary tracks directory already exists
4346
directoryManager.exists(temporaryTracksDirecotry, function directoryExistsCallback(error, exists) {
4447

48+
// if there was no error checking if the directory exists
4549
if (!error) {
4650

51+
// if the directory does not exist
4752
if (!exists) {
4853

54+
// create a new directory
4955
directoryManager.create(temporaryTracksDirecotry, createDirectoryCallback = function(error) {
5056

57+
// if there was no error creating the new directory
5158
if (!error) {
5259

60+
// download the the track and store it on disc
5361
that.downloadIfNotExists(trackId, callback, temporaryTracksDirecotry, format);
5462

5563
} else {
@@ -62,6 +70,7 @@ downloader.prototype.writeTrackToDisc = function(trackId, callback, temporaryTra
6270

6371
} else {
6472

73+
// download the the track and store it on disc
6574
that.downloadIfNotExists(trackId, callback, temporaryTracksDirecotry, format);
6675

6776
}
@@ -77,6 +86,8 @@ downloader.prototype.writeTrackToDisc = function(trackId, callback, temporaryTra
7786
};
7887

7988
/**
89+
*
90+
* donwloads a track if does not already exist on disc
8091
*
8192
* @param {type} trackId
8293
* @param {type} callback
@@ -94,12 +105,15 @@ downloader.prototype.downloadIfNotExists = function downloadIfNotExists(trackId,
94105

95106
var that = this;
96107

108+
// check if the file already exists
97109
fileManager.exists(filePath, function fileExistsCallback(error, exists) {
98110

111+
// if there was no error checking if the file exists
99112
if (!error) {
100113

101114
if (!exists) {
102115

116+
// download the file and store it in the temporary directory
103117
that.downloadFile(trackId, callback, filePath, format);
104118

105119
} else {
@@ -117,7 +131,17 @@ downloader.prototype.downloadIfNotExists = function downloadIfNotExists(trackId,
117131
});
118132

119133
};
120-
134+
135+
/**
136+
*
137+
* download a file
138+
*
139+
* @param {type} trackId
140+
* @param {type} callback
141+
* @param {type} trackPath
142+
* @param {type} format
143+
* @returns {undefined}
144+
*/
121145
downloader.prototype.downloadFile = function downloadFileFunction(trackId, callback, trackPath, format) {
122146

123147
console.log('downloadFile: ' + trackId);
@@ -130,6 +154,7 @@ downloader.prototype.downloadFile = function downloadFileFunction(trackId, callb
130154

131155
var formatCode;
132156

157+
// track format
133158
switch(format) {
134159
case 'mp3':
135160
formatCode = 'mp31';
@@ -151,23 +176,29 @@ downloader.prototype.downloadFile = function downloadFileFunction(trackId, callb
151176

152177
var writeStream = fs.createWriteStream(trackPath);
153178

179+
// open a new write stream
154180
writeStream.on('open', function() {
155181

182+
// request the file from remote server
156183
var httpRequest = http.request(options, function(httpResponse) {
157184

158185
console.log('writeTrackToDisc httpRequest STATUS: ' + httpResponse.statusCode);
159186
console.log('writeTrackToDisc httpRequest HEADERS: ' + JSON.stringify(httpResponse.headers));
160187

188+
// on successful request
161189
httpResponse.on('data', function(chunk) {
162190

191+
// write the file
163192
writeStream.write(chunk);
164193

165194
});
166195

196+
// the connection got closed
167197
httpResponse.on('end', function() {
168198

169199
console.log('file ' + trackPath + ' got downloaded into ' + trackPath);
170200

201+
// close the write stream
171202
writeStream.end();
172203

173204
callback(false, trackPath);
@@ -176,6 +207,7 @@ downloader.prototype.downloadFile = function downloadFileFunction(trackId, callb
176207

177208
});
178209

210+
// the request to the remote server failed
179211
httpRequest.on('error', function(error) {
180212

181213
console.log('writeTrackToDisc, http request error: ' + error.message);
@@ -190,10 +222,12 @@ downloader.prototype.downloadFile = function downloadFileFunction(trackId, callb
190222

191223
});
192224

225+
// writing the file failed
193226
writeStream.on('error', function(error) {
194227

195228
console.log('writeTrackToDisc writeStream, error: ' + error);
196229

230+
// close the stream
197231
writeStream.end();
198232

199233
callback(error);

0 commit comments

Comments
 (0)