Skip to content

Commit a3c50ef

Browse files
committed
get wave data from server
1 parent 9f57d8e commit a3c50ef

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

bootstrap.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var fs = require('fs');
88

99
var url = require('url');
1010

11+
var querystring = require('querystring');
12+
1113

1214
var serverPort = 35000;
1315
var serverIp = '127.0.0.1';
@@ -16,10 +18,10 @@ http.createServer(function (request, response) {
1618

1719
var urlParts = url.parse(request.url);
1820

19-
if (urlParts.path.split('.').pop() === 'js') {
21+
if (urlParts.pathname.split('.').pop() === 'js') {
2022

2123
// not secure but this is a prototype
22-
fs.readFile('client' + urlParts.path, function(error, fileContent) {
24+
fs.readFile('client' + urlParts.pathname, function(error, fileContent) {
2325

2426
if (!error) {
2527

@@ -39,7 +41,9 @@ http.createServer(function (request, response) {
3941

4042
} else {
4143

42-
switch(urlParts.path) {
44+
console.log(urlParts);
45+
46+
switch(urlParts.pathname) {
4347
case '/':
4448
fs.readFile('client/index.html', function(error, html) {
4549

@@ -60,9 +64,31 @@ http.createServer(function (request, response) {
6064
});
6165
break;
6266
case '/getwavedata':
63-
var trackId = '1135703';
64-
var peaksAmount = '200';
65-
getWaveData(trackId, peaksAmount);
67+
68+
var queryObject = querystring.parse(urlParts.query);
69+
70+
if (typeof queryObject !== 'undefined' && queryObject.trackId !== 'undefined' && queryObject.peaksAmount !== 'undefined') {
71+
72+
getWaveData(queryObject.trackId, queryObject.peaksAmount, function(error, peaks) {
73+
74+
if (!error) {
75+
76+
response.writeHead(200, { 'Content-Type': 'application/json' });
77+
response.write('{ "peaks": ' + JSON.stringify(peaks) + ' }');
78+
response.end();
79+
80+
} else {
81+
82+
response.writeHead(500, { 'Content-Type': 'application/json' });
83+
response.write('{ error: ' + error + ' }');
84+
response.end();
85+
86+
}
87+
88+
});
89+
90+
}
91+
6692
break;
6793
default:
6894
response.writeHead(404, { 'Content-Type': 'text/html' });
@@ -76,7 +102,7 @@ http.createServer(function (request, response) {
76102

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

79-
var getWaveData = function getWaveDataFunction(trackId, peaksAmount) {
105+
var getWaveData = function getWaveDataFunction(trackId, peaksAmount, callback) {
80106

81107
var audioDataAnalyzer = new AudioDataAnalyzer();
82108

@@ -93,19 +119,19 @@ var getWaveData = function getWaveDataFunction(trackId, peaksAmount) {
93119

94120
if (!error) {
95121

96-
console.log(peaks);
122+
callback(false, peaks);
97123

98124
} else {
99125

100-
console.log(error);
126+
callback(error);
101127

102128
}
103129

104130
});
105131

106132
} else {
107133

108-
console.log(error);
134+
callback(error);
109135

110136
}
111137

library/audioDataAnalyzer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
120120
* @param {type} callback
121121
* @returns {undefined}
122122
*/
123-
analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmount, callback) {
123+
analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountRaw, callback) {
124124

125125
this.getData(trackPath, function(error, trackData) {
126126

@@ -129,6 +129,8 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmount,
129129
console.log('ffprobe track data: ');
130130
console.log(trackData);
131131

132+
var peaksAmount = parseInt(peaksAmountRaw);
133+
132134
// get audio pcm as 16bit little endians
133135
var ffmpegSpawn = childProcess.spawn(
134136
'ffmpeg',

0 commit comments

Comments
 (0)