-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
74 lines (60 loc) · 2.02 KB
/
test.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
var aubio = require('./index.js');
var ref = require('ref');
var get_file_bpm = function(path, params) {
// create source
var source = aubio.new_aubio_source(path, params.samplerate, params.hop_s);//params.samplerate, params.hop_s);
try {
source.readPointer();
}
catch (e) {
console.log('failed opening ' + path);
return;
}
var samplerate = aubio.aubio_source_get_samplerate(source);
console.log('samplerate: ' + samplerate);
var total_frames = 0;
var tmp_read = ref.alloc('int');
// create tempo
var tempo = aubio.new_aubio_tempo('default', params.win_s, params.hop_s, params.samplerate);
var beats = [];
var total_bpm = 0;
// create pitch
var pitch = aubio.new_aubio_pitch('default', params.win_s, params.hop_s, params.samplerate);
aubio.aubio_pitch_set_unit(pitch, 'Hz')
// create output for source
var samples = aubio.new_fvec(params.hop_s);
// create output for pitch and beat
var out_fvec = aubio.new_fvec(1);
while(true) {
aubio.aubio_source_do(source, samples, tmp_read);
aubio.aubio_pitch_do(pitch, samples, out_fvec);
var cur_time = total_frames / samplerate;
var last_pitch = aubio.fvec_get_sample(out_fvec, 0);
//console.log('pitch at %d seconds: %d Hz', cur_time, last_pitch);
aubio.aubio_tempo_do(tempo, samples, out_fvec);
var is_beat = aubio.fvec_get_sample(out_fvec, 0);
if (is_beat) {
var last_beat = aubio.aubio_tempo_get_last_s(tempo);
var last_bpm = aubio.aubio_tempo_get_bpm(tempo);
beats.push(last_beat);
console.log('beat at %d (%d bpm)', last_beat, last_bpm);
}
var read = tmp_read.deref();
total_frames += read;
if(read != params.hop_s) { break; }
}
var cur_time = total_frames / samplerate;
console.log('total time : %d seconds (%d frames)', cur_time, total_frames);
console.log('found %d beats total', beats.length);
aubio.del_aubio_source(source);
aubio.del_aubio_tempo(tempo);
}
if (process.argv[2]) {
var filename = process.argv[2];
}
console.log('opening ' + filename);
get_file_bpm(filename, {
samplerate: 44100,
win_s : 1024,
hop_s : 512,
});