diff -urN midi_prog-/buf.c midi_prog/buf.c --- midi_prog-/buf.c Thu Aug 27 00:00:00 2015 +++ midi_prog/buf.c Wed Sep 9 00:00:00 2015 @@ -309,11 +309,33 @@ 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); + struct tbuf *tb = &ab->tb; + struct rbuf *rb = &tb->rb; + int from_i = (int) tbuf_sec_to_sridx(tb, from_sec); + int to_i = (int) tbuf_sec_to_sridx(tb, to_sec); int i; double sum = 0; if(from_i >= to_i) return 0; - for(i=from_i; itb.rb, i); + for(i=from_i; itb; + struct rbuf *rb = &tb->rb; + int from_i = (int) tbuf_sec_to_sridx(tb, from_sec); + int to_i = (int) tbuf_sec_to_sridx(tb, to_sec); + int i = from_i, t = i; + double v, tv = rbuf_get_dbl(rb, i); + + for(i++; i tv){ + tv = v; + t = i; + } + } + if(ret_sec) *ret_sec = tbuf_sridx_to_sec(tb, t); + return tv; } diff -urN midi_prog-/buf.h midi_prog/buf.h --- midi_prog-/buf.h Thu Aug 27 00:00:00 2015 +++ midi_prog/buf.h Wed Sep 9 00:00:00 2015 @@ -86,5 +86,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); +double abuf_max(struct abuf *ab, double from_sec, double to_sec, double *ret_sec); #endif diff -urN midi_prog-/pitdet.c midi_prog/pitdet.c --- midi_prog-/pitdet.c Tue Sep 8 00:00:00 2015 +++ midi_prog/pitdet.c Wed Sep 9 00:00:00 2015 @@ -206,5 +206,5 @@ int pitdet_onoff_judge(struct pitdet *pd, double v) { - return v / pd->max_v >= pd->thres; + return v >= pd->thres * pd->max_v; } diff -urN midi_prog-/tool.c midi_prog/tool.c --- midi_prog-/tool.c Tue Sep 8 00:00:00 2015 +++ midi_prog/tool.c Wed Sep 9 00:00:00 2015 @@ -100,13 +100,15 @@ } struct note_onoff{ - struct chatt ct; + struct chatt ct, ct2; struct abuf ab; struct pitdet *pd; int note; double on_sec; double pre_release; int cut_over_tone; + double peak, peak_thres; + int onoff; } note_onoff[ NOTE_N ]; void @@ -120,6 +122,11 @@ nto->on_sec = -1; nto->pre_release = opt_double("-pre_release", ac, av, 0); nto->cut_over_tone = opt_idx("-cut_over_tone", ac, av) > 0; + + chatt_init(&nto->ct2, lmt_sec); + nto->peak = -1; + nto->peak_thres = opt_double("-peak_thres", ac, av, 0.1); + nto->onoff = 0; } double @@ -167,20 +174,43 @@ { double on_sec, off_sec; int tick, velo; + int chg, chg2, chg_a = -1; abuf_add(&nto->ab, v); - if(!chatt_add(&nto->ct, sec, onoff)) return; - - if(nto->ct.onoff){ - nto->on_sec = nto->ct.sec; - return; + chg = chatt_add(&nto->ct, sec, onoff); + if(nto->peak >= 0){ + nto->peak = MAX(nto->peak, v); + chg2 = chatt_add(&nto->ct2, sec, v > nto->peak * nto->peak_thres); } + if(!nto->onoff){ + if(nto->peak >= 0 && chg2 && nto->ct2.onoff){ + nto->on_sec = nto->ct2.sec; + chg_a = 1; + }else if(chg && nto->ct.onoff){ + nto->on_sec = nto->ct.sec; + chg_a = 1; + + nto->peak = abuf_max(&nto->ab, nto->on_sec, sec, NULL); + chatt_set(&nto->ct2, 1); + } + }else{ + if(chg && !nto->ct.onoff){ + off_sec = nto->ct.sec; + chg_a = 0; + + nto->peak = -1; + }else if(chg2 && !nto->ct2.onoff){ + off_sec = nto->ct2.sec; + chg_a = 0; + } + } + if(chg_a < 0) return; + if((nto->onoff = chg_a) > 0) return; if(nto->on_sec < 0) return; on_sec = nto->on_sec; - off_sec = nto->ct.sec; nto->on_sec = -1; if(nto->pre_release > 0){ @@ -218,7 +248,7 @@ v = pitdet_get(pd, j, sec); int o = pitdet_onoff_judge(pd, v); onoff |= o; - if(o) sum += v; + if(o) sum += MAX(v, 0); } v = sum; note_onoff_add(¬e_onoff[i], sec, onoff, v, ch); diff -urN midi_prog-/util.c midi_prog/util.c --- midi_prog-/util.c Sat Sep 5 00:00:00 2015 +++ midi_prog/util.c Wed Sep 9 00:00:00 2015 @@ -191,4 +191,11 @@ return 1; } +void +chatt_set(struct chatt *ct, int onoff) +{ + ct->onoff = onoff; + ct->chg_try_sec = -1; +} + /* EOF */ diff -urN midi_prog-/util.h midi_prog/util.h --- midi_prog-/util.h Sat Aug 29 00:00:00 2015 +++ midi_prog/util.h Wed Sep 9 00:00:00 2015 @@ -56,5 +56,6 @@ void chatt_init(struct chatt *ct, double lmt_sec); int chatt_add(struct chatt *ct, double sec, int onoff); +void chatt_set(struct chatt *ct, int onoff); #endif