/
BeatTracker.bsv
170 lines (129 loc) · 4.69 KB
/
BeatTracker.bsv
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
158
159
160
161
162
163
164
165
166
167
168
169
170
import Types::*;
import BeatClassifier::*;
import Metronome::*;
import ClientServer::*;
import GetPut::*;
import FixedPoint::*;
import Vector::*;
import FIFO::*;
import FIFOF::*;
interface BeatTracker;
method Action q_sync();
method Action putSampleInput(AudioSample in);
method ActionValue#(TopLvlOut) getBeatInfo();
endinterface
module mkBeatTracker(BeatTracker);
BeatClassifier bc <- mkBeatClassifierTest();
MetArr#(Metronome) mb <- replicateM(mkMetronome);
//MetronomeBankController mbc <- mkMetronomeBankController();
//FIFOF#(TopLvlOut) bt_info <- mkFIFOF1();
//Reg#(TopLvlOut) bt_info <- mkReg(0);
//FIFO#(TopLvlOut) bt_info <- mkFIFO1();
FIFOF#(TopLvlOut) bt_info <- mkSizedFIFOF(valueof(NumMetronomes));
Reg#(Bool) init_done <- mkReg(False);
Reg#(Bool) sync_next <- mkReg(False);
Reg#(Bool) sync_this <- mkReg(False);
Reg#(BeatGuess) beat_guess <- mkReg(0);
Reg#(UInt#(TLog#(TAdd#(NumMetronomes,1)))) mb_out_count <- mkReg(0);
rule init(!init_done);
/*
mb[0].set_tempo(calc_tempo_increment(150));
mb[1].set_tempo(calc_tempo_increment(140));
mb[2].set_tempo(calc_tempo_increment(130));
mb[3].set_tempo(calc_tempo_increment(120));
// */
//* {{{
for (Integer m=0; m<valueof(NumMetronomes); m=m+1) begin
mb[m].set_tempo(calc_tempo_increment(min_tempo+fromInteger(m)));
end
// }}} */
init_done <= True;
endrule
Reg#(PulserCount) mpc <- mkReg(0);
rule pulser(init_done);
if (mpc != fromInteger(pulser_count_max)) begin
mpc <= mpc + 1;
end else begin
mpc <= 0;
for (Integer m=0; m<valueof(NumMetronomes); m=m+1) begin
mb[m].pulse();
end
end
endrule
/* {{{
rule beat_injector(init_done);
BeatGuess beat_guess_i = fromInteger(0);
if (mb_out_count == fromInteger(0)) begin
beat_guess_i <- bc.response.get();
beat_guess <= beat_guess_i;
if (valueof(NumMetronomes) > 1) mb_out_count <= fromInteger(1);
end else begin
beat_guess_i = beat_guess;
if ( mb_out_count == fromInteger(valueof(TSub#(NumMetronomes, 1))) ) begin
mb_out_count <= fromInteger(0);
sync_next <= False;
end else begin
mb_out_count <= mb_out_count + fromInteger(1);
end
end
let x <- mb[mb_out_count].inject_beat(beat_guess_i);
if (sync_next) bt_info.enq(make_output(x));
endrule
// }}} */
/* {{{
rule beat_injector_start(init_done && mb_out_count == fromInteger(0));
let beat_guess_i <- bc.response.get();
beat_guess <= beat_guess_i;
mb_out_count <= fromInteger(1);
sync_this <= sync_next;
sync_next <= False;
endrule
rule beat_injector_cont(init_done &&
mb_out_count > fromInteger(0) &&
mb_out_count <= fromInteger(num_metronomes));
let x <- mb[mb_out_count-1].inject_beat(beat_guess);
bt_info.enq(make_output(x));
if ( mb_out_count == fromInteger(num_metronomes) ) begin
mb_out_count <= fromInteger(0);
//sync_this <= False;
end else begin
mb_out_count <= mb_out_count + fromInteger(1);
end
endrule
// }}} */
//* {{{
rule beat_injector_start(init_done && mb_out_count == fromInteger(0));
let beat_guess_i <- bc.response.get();
beat_guess <= beat_guess_i;
let x <- mb[0].inject_beat(beat_guess_i);
if (sync_next) bt_info.enq(make_output(x));
sync_this <= sync_next;
sync_next <= False;
if (valueof(NumMetronomes) > 1) begin
mb_out_count <= fromInteger(1);
end
endrule
rule beat_injector_cont(init_done &&
mb_out_count > fromInteger(0) &&
mb_out_count < fromInteger(valueof(NumMetronomes)));
let x <- mb[mb_out_count].inject_beat(beat_guess);
if (sync_this) bt_info.enq(make_output(x));
if ( mb_out_count == fromInteger(valueof(TSub#(NumMetronomes, 1))) ) begin
mb_out_count <= fromInteger(0);
end else begin
mb_out_count <= mb_out_count + fromInteger(1);
end
endrule
// }}} */
method Action q_sync() if (!sync_next);
sync_next <= True;
endmethod
method Action putSampleInput(AudioSample x);
bc.request.put(x);
endmethod
method ActionValue#(TopLvlOut) getBeatInfo();
let x = bt_info.first;
bt_info.deq();
return x;
endmethod
endmodule