Repository navigation
Expand file tree
/
Copy pathaudio_processor.js
More file actions
84 lines (70 loc) · 2.85 KB
/
Copy pathaudio_processor.js
File metadata and controls
84 lines (70 loc) · 2.85 KB
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
const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
const babblelabs =require('./babblelabs');
function trimSilenceFromAudio(filePath, callback = () => { }) {
const fileExtension = filePath.split('.').pop();
// const tmpPath = path.join('tmp', `tmpAudio-${Date.now()}.${fileExtension}`);
const targetPath = path.join('tmp', `silenced-${Date.now()}.${fileExtension}`);
exec(`sox ${filePath} ${targetPath} silence -l 1 0.1 1% -1 2.0 1%`, (err) => {
if (err || !fs.existsSync(targetPath)) {
// fs.unlink(tmpPath, () => { });
return callback(err);
}
return callback(null, targetPath);
// exec(`sox ${tmpPath} ${targetPath} silence -l 1 0.1 1% reverse`, (err, stdout, stderr) => {
// fs.unlink(tmpPath, () => { });
// if (err || !fs.existsSync(targetPath)) {
// fs.unlink(targetPath, () => { });
// return callback(err);
// }
// return callback(null, targetPath);
// })
})
}
function clearBackgroundNoise(filePath, callback = () => {}) {
const fileExtension = filePath.split('.').pop();
const targetPath = path.join('tmp', `cleared-${Date.now()}.${fileExtension}`);
babblelabs.clearAudio(filePath, targetPath)
.then(() => {
return callback(null, targetPath);
})
.catch((err) => {
return callback(err);
})
}
function compressAudioFile(filePath, callback) {
const fileExtension = filePath.split('.').pop();
const targetPath = path.join('tmp', `tiny-${Date.now()}.${fileExtension}`);
exec(`sox ${filePath} ${targetPath} remix 1`, (err) => {
if (err || !fs.existsSync(targetPath)) return callback(err);
return callback(null, targetPath);
});
}
function convertExtension(filePath, targetExtension, callback) {
const outputPath = path.join(__dirname, 'tmp', `converted_${Date.now()}.${targetExtension}`);
exec(`ffmpeg -i ${filePath} ${outputPath}`, (err) => {
if (err) return callback(err);
return callback(null, outputPath);
})
}
function convertToWav(filePath, callback) {
const outputPath = path.join(__dirname, 'tmp', `converted_${Date.now()}.wav`);
exec(`ffmpeg -i ${filePath} ${outputPath}`, (err) => {
if (err) return callback(err);
return callback(null, outputPath);
})
}
module.exports = {
clearBackgroundNoise,
trimSilenceFromAudio,
compressAudioFile,
convertToWav,
convertExtension,
}
/* Commands used to trim silence from begining and end of file
sox in.wav out.wav silence -l 1 0.1 1% -1 2.0 1% reverse
Trim audio from the begining of the file, then reverse it
sox tmp.wav out.wav silence -l 1 0.1 1% -1 2.0 1% reverse
Trim audio from the begining of the reversed version, then reverse again to get original order
*/