forked from calufornia/audio-split
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaming_waveform.js
157 lines (133 loc) · 3.98 KB
/
streaming_waveform.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Derived from waveform-node package. */
var Promise = require('bluebird');
var fs = require('fs');
var _ = require('lodash')
var spawn = require('child_process').spawn;
var log_10 = function(arg){
return Math.log(arg) / Math.LN10;
}
var coefficient_to_db = function(coeff){
return 20.0 * log_10(coeff);
}
var log_meter = function(power, lower_db, upper_db, non_linearity){
if(power < lower_db){
return 0;
}
else{
return Math.pow((power - lower_db) / (upper_db - lower_db), non_linearity);
}
}
var alt_log_meter = function(power){
return log_meter(power, -192.0, 0.0, 8.0);
}
var waveformTypeEnum = {
STACK: 0,
LINE: 1
}
module.exports = Promise.coroutine(function*(filepath, numOfSample) {
const totalSamples = yield new Promise(function(resolve, reject) {
var outputStr = '';
var oddByte = null;
var gotData = false;
var samples = [];
var channel = 0;
var total = 0;
var ffmpeg = spawn('ffmpeg', ['-i', filepath, '-f','s16le', '-acodec','pcm_s16le','-y','pipe:1']);
ffmpeg.stdout.on('data', function(data){
gotData = true;
var value;
var i = 0;
var dataLen = data.length;
// If there is a leftover byte from the previous block, combine it with the
// first byte from this block
if (oddByte !== null) {
value = ((data.readInt8(i++, true) << 8) | oddByte) / 32767.0;
//samples.push(value);
total += 1
channel = ++channel % 2;
}
for (; i < dataLen; i += 2) {
value = data.readInt16LE(i, true) / 32767.0;
//samples.push(value);
total += 1
channel = ++channel % 2;
}
oddByte = (i < dataLen) ? data.readUInt8(i, true) : null;
});
ffmpeg.stderr.on('data', function(data) {
// Text info from ffmpeg is output to stderr
//outputStr += data.toString();
});
ffmpeg.stderr.on('end', function() {
if (gotData) {
resolve(total)
}
resolve(0)
});
});
var samplesPerPeak = Math.ceil(totalSamples / numOfSample);
const frequencies = yield new Promise(function(resolve, reject) {
var outputStr = '';
var oddByte = null;
var gotData = false;
var samples = [];
var channel = 0;
var total = 0;
var partialMax = 0;
var sampleIdx = 0;
var peaks = [];
var currMax = 0;
var ffmpeg = spawn('ffmpeg', ['-i', filepath,'-f','s16le', '-acodec','pcm_s16le','-y','pipe:1']);
ffmpeg.stdout.on('data', function(data){
gotData = true;
var value;
var i = 0;
var dataLen = data.length;
// If there is a leftover byte from the previous block, combine it with the
// first byte from this block
if (oddByte !== null) {
value = ((data.readInt8(i++, true) << 8) | oddByte) / 32767.0;
samples.push(value);
var absVal = Math.abs(value);
if(absVal > partialMax){
partialMax = absVal;
}
if(sampleIdx >= samplesPerPeak){
var currMax = alt_log_meter(coefficient_to_db(partialMax));
peaks.push(currMax);
samples = []
sampleIdx = 0;
partialMax = 0;
}
sampleIdx++;
channel = ++channel % 2;
}
for (; i < dataLen; i += 2) {
value = data.readInt16LE(i, true) / 32767.0;
samples.push(value);
var absVal = Math.abs(value);
sampleIdx++;
if(absVal > partialMax){
partialMax = absVal;
}
if(sampleIdx >= samplesPerPeak){
var currMax = alt_log_meter(coefficient_to_db(partialMax));
peaks.push(currMax);
samples = []
sampleIdx = 0;
partialMax = 0;
}
channel = ++channel % 2;
}
oddByte = (i < dataLen) ? data.readUInt8(i, true) : null;
});
ffmpeg.stderr.on('data', function(data) {
// Text info from ffmpeg is output to stderr
//outputStr += data.toString();
});
ffmpeg.stderr.on('end', function() {
resolve(peaks)
});
});
return frequencies
})