diff -urN midi_prog-/buf.c midi_prog/buf.c --- midi_prog-/buf.c Wed Aug 26 00:34:39 2015 +++ midi_prog/buf.c Thu Aug 27 00:00:00 2015 @@ -305,3 +305,15 @@ if(ab->max_v < v) ab->max_v = v; ab->sum += v - ov; } + +double +abuf_mean(struct abuf *ab, double from_sec, double to_sec) +{ + int from_i = (int) tbuf_sec_to_sridx(&ab->tb, from_sec); + int to_i = (int) tbuf_sec_to_sridx(&ab->tb, to_sec); + int i; + double sum = 0; + if(from_i >= to_i) return 0; + for(i=from_i; itb.rb, i); + return sum / (to_i - from_i); +} diff -urN midi_prog-/buf.h midi_prog/buf.h --- midi_prog-/buf.h Wed Aug 26 00:34:19 2015 +++ midi_prog/buf.h Thu Aug 27 00:00:00 2015 @@ -85,5 +85,6 @@ void abuf_init(struct abuf *ab, double smp_freq, double wsec); void abuf_add(struct abuf *ab, double v); +double abuf_mean(struct abuf *ab, double from_sec, double to_sec); #endif diff -urN midi_prog-/pitch.c midi_prog/pitch.c --- midi_prog-/pitch.c Wed Aug 12 00:00:00 2015 +++ midi_prog/pitch.c Thu Aug 27 00:00:00 2015 @@ -46,12 +46,12 @@ void pitch_init(struct pitch *p, double smp_freq, double freq, int wn, - double abuf_wsec) + double tick_freq, double abuf_wsec) { p->freq = freq; p->wn = wn; sbuf_init(&p->sb, smp_freq, pitch_wsec(p->freq, wn)); - abuf_init(&p->ab, 2*96, abuf_wsec); + abuf_init(&p->ab, tick_freq, abuf_wsec); } void diff -urN midi_prog-/pitch.h midi_prog/pitch.h --- midi_prog-/pitch.h Wed Aug 12 00:00:00 2015 +++ midi_prog/pitch.h Thu Aug 27 00:00:00 2015 @@ -15,7 +15,7 @@ double pitch_freq(double wsec, double wn); void pitch_init(struct pitch *p, double smp_freq, double freq, int wn, - double abuf_wsec); + double tick_freq, double abuf_wsec); void pitch_add(struct pitch *p, double v); #endif diff -urN midi_prog-/pitdet.c midi_prog/pitdet.c --- midi_prog-/pitdet.c Wed Aug 26 01:00:00 2015 +++ midi_prog/pitdet.c Thu Aug 27 00:00:00 2015 @@ -14,7 +14,7 @@ void pitdet_init(struct pitdet *pd, double smp_freq, int adj_cent, - int div, double abuf_wsec, double thres) + int div, double tick_freq, double abuf_wsec, double thres) { int j; @@ -30,7 +30,7 @@ double next_freq = pitdet_note_to_freq(pd, next_note); int wn = (int) pitch_wn(freq, next_freq); - pitch_init(&pd->arr[j], smp_freq, freq, wn, abuf_wsec); + pitch_init(&pd->arr[j], smp_freq, freq, wn, tick_freq, abuf_wsec); } pd->oldest.sec = -1; diff -urN midi_prog-/pitdet.h midi_prog/pitdet.h --- midi_prog-/pitdet.h Tue Aug 25 00:00:00 2015 +++ midi_prog/pitdet.h Thu Aug 27 12:53:34 2015 @@ -27,7 +27,7 @@ double pitdet_freq_to_note(struct pitdet *pd, double freq); void pitdet_init(struct pitdet *pd, double smp_freq, int adj_cent, - int div, double abuf_wsec, double thres); + int div, double tick_freq, double abuf_wsec, double thres); void pitdet_add(struct pitdet *pd, double v); diff -urN midi_prog-/tool.c midi_prog/tool.c --- midi_prog-/tool.c Tue Aug 25 00:00:00 2015 +++ midi_prog/tool.c Thu Aug 27 00:00:00 2015 @@ -80,7 +80,7 @@ struct chatt{ double lmt_sec, sec, chg_try_sec; int onoff; -} chatt[ NOTE_N ]; +}; void chatt_init(struct chatt *ct, double lmt_sec) @@ -112,25 +112,80 @@ return 1; } +struct note_onoff{ + struct chatt ct; + struct abuf ab; + struct pitdet *pd; + int note; + double on_sec; +} note_onoff[ NOTE_N ]; + +void +note_onoff_init(struct note_onoff *nto, double lmt_sec, double tick_freq, struct pitdet *pd, int note) +{ + chatt_init(&nto->ct, lmt_sec); + abuf_init(&nto->ab, tick_freq, 2*4*4); + nto->pd = pd; + nto->note = note; + nto->on_sec = -1; +} + +void +note_onoff_add(struct note_onoff *nto, double sec, int onoff, double v, int ch) +{ + double on_sec, off_sec; + int tick, velo; + + abuf_add(&nto->ab, v); + + if(!chatt_add(&nto->ct, sec, onoff)) return; + + if(nto->ct.onoff){ + nto->on_sec = nto->ct.sec; + return; + } + + if(nto->on_sec < 0) return; + + on_sec = nto->on_sec; + off_sec = nto->ct.sec; + nto->on_sec = -1; + + v = abuf_mean(&nto->ab, on_sec, off_sec); + + velo = (int)(128 * v / nto->pd->max_v); + if(velo <= 0) return; + if(velo > 127) velo = 127; + + tick = pitdet_sec_to_tick(nto->pd, on_sec); + printf("abs=%08d %s ch=%d note=%d velo=%d\n", + tick, "on ", ch, nto->note, velo); + + tick = pitdet_sec_to_tick(nto->pd, off_sec); + printf("abs=%08d %s ch=%d note=%d velo=%d\n", + tick, "off", ch, nto->note, velo); + + fflush(stdout); +} + static void midi_out(struct pitdet *pd) { double sec = pd->oldest.sec; int i, k; - int tick, ch = 0, velo = 100; + int ch = 0; for(i=0; idiv; k++){ int j = i * pd->div + k; - onoff |= pitdet_onoff(pd, sec, j, NULL); + int o = pitdet_onoff(pd, sec, j, &v); + onoff |= o; + if(o) sum += v; } - if(!chatt_add(&chatt[i], sec, onoff)) continue; - - tick = pitdet_sec_to_tick(pd, chatt[i].sec); - printf("abs=%08d %s ch=%d note=%d velo=%d\n", - tick, chatt[i].onoff ? "on " : "off", ch, i, velo); - fflush(stdout); + v = sum; + note_onoff_add(¬e_onoff[i], sec, onoff, v, ch); } } @@ -145,16 +200,18 @@ int adj_cent = opt_int("-adj_cent", ac, av, 0); int div = opt_int("-div", ac, av, 1); double abuf_wsec = opt_double("-abuf_wsec", ac, av, 2.5); + double tick_freq = opt_double("-tick_freq", ac, av, 2*96); int show = opt_idx("-show", ac, av) > 0; int midi = opt_idx("-midi", ac, av) > 0; + int abuf_wsec_chk = opt_idx("-abuf_wsec_chk", ac, av) > 0; double lmt_sec = opt_double("-lmt_sec", ac, av, 2.0/32); double thres = opt_double("-thres", ac, av, 0.1); int sec, s; - for(i=0; i= pd.newest.sec){ + fprintf(stderr, "%f >= %f\n", pd.oldest.sec, pd.newest.sec); + } } return 0; }