-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectsounds.js
executable file
·121 lines (106 loc) · 2.88 KB
/
connectsounds.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/***
* made by cHLeB@ & Lukas:(+2015 R.I.P)
*/
if (!Function.prototype.bind) {
Function.prototype.bind = function(thisObj) {
var fn = this;
var args = Array.prototype.slice.call(arguments, 1);
return function() {
return fn.apply(thisObj, args.concat(Array.prototype.slice.call(arguments)));
}
}
};
var fs = require('fs');
var exec = require('child_process').exec;
var ConnectSoundFiles = function(path, fileName){
this.path = path || './';
this.templateFileName = fileName;
this.soundTypes = ['wav', 'ogg', 'mp3'];
this.soundFiles = [];
this.gameSounds = {};
this.gameSounds.clips = [];
this.soundLength = 0;
this.soundDescIndex = 0;
this.getFiles();
};
ConnectSoundFiles.prototype.getFiles = function(){
fs.readdir(this.path, this.filesGet.bind(this));
};
ConnectSoundFiles.prototype.filesGet = function(error, files){
if (!!error) {
console.log('error dir');
} else {
if (files instanceof Array) {
var i, f, ft, t;
for(i=0;i<files.length;i++){
f = files[i];
ft = f.split('.');
t = ft[ft.length-1];
if (this.soundTypes.indexOf(t) > -1) {
this.soundFiles.push(f);
}
}
}
// call sox
this.makeSoxArguments();
}
};
/**
* this parameters is for mp3 format but sox need mp3 handler. Work with wav also but badly.
*/
ConnectSoundFiles.prototype.makeSoxArguments = function(){
var mainCall = './sox ';
for(var i=0;i<this.soundFiles.length;i++){
var file = this.soundFiles[i];
var call = '"| ./sox '+file+' -c 1 -r 4410 -p pad 0 0.1" '
mainCall += call;
}
mainCall += '-t mp3 -C 32 '+this.path+''+this.templateFileName+'.mp3';
console.log(mainCall);
exec(mainCall, this.soxExec.bind(this));
};
ConnectSoundFiles.prototype.soxExec = function(error, args){
if (!!error) {
console.log('sox error');
} else {
this.makeSoundDescription();
}
};
ConnectSoundFiles.prototype.makeSoundDescription = function(){
if (this.soundDescIndex < this.soundFiles.length) {
var file = this.soundFiles[this.soundDescIndex];
var call = './sox --i -D '+file;
exec(call, this.addSoundDesc.bind(this, file));
this.soundDescIndex++;
} else {
this.makeDescFile();
}
};
ConnectSoundFiles.prototype.addSoundDesc = function(file, error, out){
if(!!error){
console.log('error desc');
} else {
var fts = file.split('.');
fts.pop();
var f = fts.join('.');
var dur = parseFloat(out)+0.1;
var obj = {
name : f,
startTime : this.soundLength,
duration : dur,
volume : 0.5,
};
this.soundLength += dur;
this.gameSounds.clips.push(obj);
this.makeSoundDescription();
}
};
ConnectSoundFiles.prototype.makeDescFile = function(){
var descFile = this.path+''+this.templateFileName+'.js';
fs.open(descFile, 'w', function(){});
fs.writeFile(descFile, 'var GameSounds = '+JSON.stringify(this.gameSounds), function(err){
if (!err) { console.log(err); }
});
console.log(this.gameSounds);
};
var csf = new ConnectSoundFiles('./', 'gameSounds');