diff -urN midi_prog-/out.c midi_prog/out.c --- midi_prog-/out.c Sat Aug 29 00:00:00 2015 +++ midi_prog/out.c Thu Oct 1 00:00:00 2015 @@ -4,7 +4,7 @@ sox_version(void) { FILE *fp; - int v1, v2, v3; + int v1 = 0, v2 = 0, v3 = 0; if((fp = popen("sox --version | sed -e 's/^.*v//'", "r")) == NULL) return -1; fscanf(fp, "%d.%d.%d", &v1, &v2, &v3); @@ -15,7 +15,8 @@ static char * sox_bit_len_fmt(int bit_len) { - return sox_version() >= 140400 ? + int v = sox_version(); + return v >= 140400 || v == 0 ? (bit_len == 8 ? "-b 8" : "-b 16") : (bit_len == 8 ? "-b" : "-w"); } diff -urN midi_prog-/pitdet.c midi_prog/pitdet.c --- midi_prog-/pitdet.c Wed Sep 30 00:00:00 2015 +++ midi_prog/pitdet.c Thu Oct 1 00:00:00 2015 @@ -113,7 +113,6 @@ for(j=0; jm; j++){ double v = MAX(pd->wk[j].v, 0); tbuf_set_dbl(&pd->arr[j].ab.tb, sec, v); - tbuf_set_dbl(&pd->arr[j].ab_bak.tb, sec, v); } } @@ -122,8 +121,9 @@ { int j; double sec, osec = -1, nsec = -1; + struct max_sum mx; - pd->max_v = -1; + max_sum_init(&mx); for(j=0; jm; j++){ struct pitch *p = &pd->arr[j]; @@ -135,8 +135,10 @@ sec = tbuf_newest_sec(&p->ab.tb); if(nsec == -1 || sec < nsec) nsec = sec; - if(p->ab.max_v > pd->max_v) pd->max_v = p->ab.max_v; + max_sum_add(&mx, p->ab.max_v); } + pd->max_v = mx.max_v; + if((pd->oldest.update = (pd->oldest.sec != osec)) != 0) pd->oldest.sec = osec; if((pd->newest.update = (pd->newest.sec != nsec)) != 0) pd->newest.sec = nsec; @@ -151,12 +153,19 @@ pitdet_add_after(struct pitdet *pd) { int j, chg_v; + double sec = pd->newest.sec; - pitdet_shadow_cut(pd, pd->newest.sec); + pitdet_shadow_cut(pd, sec); +#if 1 + for(j=0; jm; j++){ + double v = tbuf_get_dbl(&pd->arr[j].ab.tb, sec); + tbuf_set_dbl(&pd->arr[j].ab_bak.tb, sec, v); + } +#endif for(j=0; jm; j++){ struct pitch *p = &pd->arr[j]; - chg_v = onoff_stat_add_sec(&p->ost, pd->newest.sec, pd->max_v); + chg_v = onoff_stat_add_sec(&p->ost, sec, pd->max_v); if(pd->cut_over_tone && chg_v == 0) pitdet_cut_overtone(pd, j); } } diff -urN midi_prog-/tool.c midi_prog/tool.c --- midi_prog-/tool.c Wed Sep 30 00:00:00 2015 +++ midi_prog/tool.c Thu Oct 1 00:00:00 2015 @@ -176,87 +176,83 @@ fprintf(stderr, "\n"); } +#if 0 +static char * +note_to_str(int note, char *buf) +{ + char *tbl[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", NULL }; + sprintf(buf, "%s%d", tbl[note % 12], note / 12); + return buf; +} +#endif + int ch_get(int note, struct pitdet *pd, double on_sec, double off_sec) { - double from = on_sec + (off_sec - on_sec) * 0; + double from = on_sec + (off_sec - on_sec) * 0.1; double to = on_sec + (off_sec - on_sec) * 0.9; - int rates[3], i; - struct abuf_n ab_n[4]; + int rates[3]; + int i, k, ch; + struct abuf_n ab_n[6], ab_n_bak[6]; + struct max_sum mx; for(i=0; idiv; k++){ int j = note * pd->div + k; struct pitch *p = pitdet_pitch_get(pd, j, ov); - ab_n[i].p[k] = p ? (ov == 1 ? &p->ab : &p->ab_bak) : NULL; - //ab_n[i].p[k] = p ? &p->ab_bak : NULL; - //ab_n[i].p[k] = p ? &p->ab : NULL; + ab_n[i].p[k] = p ? &p->ab : NULL; + ab_n_bak[i].p[k] = p ? &p->ab_bak : NULL; } abuf_n_init(&ab_n[i], NULL, pd->div); + abuf_n_init(&ab_n_bak[i], NULL, pd->div); } - for(i=0; ioldest.sec, on_sec, off_sec, pd->newest.sec); - corr = 0; - k_max = -1; + for(i=0; idiv; k++){ - double c = abuf_correlation_by_type(ab_n[0].p[k], ab_n[i+1].p[k], from, to, NULL); -//fprintf(stderr, "ov=%d k=%d c=%f\n", i+2, k, c); - if(k_max < 0 || c > corr){ - corr = c; - k_max = k; - } + double mean = abuf_mean_by_type(ab_n_bak[i].p[k], from, to, NULL); + fprintf(stderr, "%7f ", mean); } - k = k_max; - - //if(corr < ABUF_CORR_STRONG) continue; - if(corr < ABUF_CORR_MID) continue; - -#if 1 -fprintf(stderr, "ck1 %f\n", -abuf_correlation_by_type(ab_n[0].p[k], ab_n[i+1].p[k], from, to, NULL) -); -#endif - - K = abuf_corr_k_by_type(ab_n[0].p[k], ab_n[i+1].p[k], from, to, NULL); -fprintf(stderr, "K=%f Kcnt=%d\n", K, Kcnt++); - -#if 0 - -ck1 0.749551 -K=0.573503 Kcnt=318 - 9 [ 0 0 0 ] cnt=-1 - 7 [ 4 777 777 ] cnt=2 -13 [ 2 1 0 ] cnt=2 - 8 [ 8 777 777 ] cnt=3 -10 [ 777 4 777 ] cnt=3 -12 [ 777 1 0 ] cnt=3 -14 [ 777 777 1 ] cnt=3 - 6 [ 1 0 777 ] cnt=7 - 2 [ 777 2 777 ] cnt=9 -15 [ 0 777 777 ] cnt=13 - 3 [ 777 1 777 ] cnt=15 - 4 [ 5 777 777 ] cnt=15 -11 [ 3 777 777 ] cnt=25 - 5 [ 2 777 777 ] cnt=39 - 1 [ 1 777 777 ] cnt=42 - 0 [ 777 777 777 ] cnt=9270 -ovr_wrt_cnt=89 - + fprintf(stderr, " | "); + for(k=0; kdiv; k++){ + double mean = abuf_mean_by_type(ab_n[i].p[k], from, to, NULL); + fprintf(stderr, "%7f ", mean); + } + fprintf(stderr, "\n"); + } + fprintf(stderr, "\n"); +} #endif - - - rates[i] = (int)(K * 5); + i = 0; + max_sum_init(&mx); + for(k=0; kdiv; k++){ + double mean = abuf_mean_by_type(ab_n[i].p[k], from, to, NULL); + max_sum_add(&mx, mean); } - return ch_inf_get(rates)->ch; + k = mx.max_i; + + for(i=0; ich; + + //while((ch = rand() % 16) == 9); + ch = note % 15; + if(ch >= 9) ch++; + + return ch; } struct note_onoff{ @@ -266,11 +262,14 @@ int note; double pre_release; int onoff; + struct abuf_n ab_n; } note_onoff[ NOTE_N ]; void note_onoff_init(struct note_onoff *nto, struct pitdet *pd, int note, int ac, char **av) { + int k; + onoff_stat_init(&nto->ost, &nto->ab, ac, av); abuf_init(&nto->ab, pd->tick_freq, 2*4*4); nto->pd = pd; @@ -278,19 +277,19 @@ nto->pre_release = opt_double("-pre_release", ac, av, 0); nto->onoff = 0; - ch_inf_init(); -} - -double -note_onoff_v(struct note_onoff *nto, double on_sec, double off_sec) -{ - return abuf_mean(&nto->ab, on_sec, off_sec, NULL); + for(k=0; kdiv; k++){ + int j = note * pd->div + k; + struct pitch *p = pitdet_pitch_get(pd, j, 1); + nto->ab_n.p[k] = p ? &p->ab : NULL; + } + abuf_n_init(&nto->ab_n, NULL, pd->div); } int note_onoff_velo(struct note_onoff *nto, double on_sec, double off_sec) { - double v = note_onoff_v(nto, on_sec, off_sec); + + double v = abuf_mean(&nto->ab, on_sec, off_sec, NULL); int velo = (int)(128 * v / nto->pd->max_v); if(velo < 0) velo = 0; if(velo > 127) velo = 127; @@ -298,13 +297,13 @@ } void -note_onoff_add(struct note_onoff *nto, double sec, double v) +note_onoff_add(struct note_onoff *nto, double sec) { double on_sec, off_sec; int tick, velo, chg_v, ch; + double v = abuf_get_by_type(&nto->ab_n.ab, sec); abuf_add(&nto->ab, v); - chg_v = onoff_stat_add_sec(&nto->ost, sec, nto->pd->max_v); if(chg_v < 0) return; @@ -338,23 +337,13 @@ static void midi_out(struct pitdet *pd) { - double sec = pd->oldest.sec; - int i, k; + double sec = (pd->newest.sec + pd->oldest.sec) * 0.5; + int i; if(sec < 0) return; for(i=0; idiv; k++){ - int j = i * pd->div + k; - struct pitch *p = pitdet_pitch_get(pd, j, 1); - ab_n.p[k] = p ? &p->ab : NULL; - } - abuf_n_init(&ab_n, NULL, pd->div); - v = abuf_n_get(&ab_n, sec); - note_onoff_add(¬e_onoff[i], sec, v); + note_onoff_add(¬e_onoff[i], sec); } } @@ -389,6 +378,7 @@ in_init(&in, ac, av, NULL); pitdet_init(&pd, in.smp_freq, ac, av); + ch_inf_init(); for(i=0; i 0){ @@ -402,7 +392,9 @@ while(pd_load){ if(pitdet_load(&pd) == EOF){ +#if 1 ch_inf_show(); +#endif return 0; } pitdet_add_after(&pd); diff -urN midi_prog-/util.c midi_prog/util.c --- midi_prog-/util.c Wed Sep 9 00:00:00 2015 +++ midi_prog/util.c Thu Oct 1 00:00:00 2015 @@ -198,4 +198,34 @@ ct->chg_try_sec = -1; } + +void +max_sum_init(struct max_sum *p) +{ + p->n = 0; + p->max_i = p->min_i = -1; +} + +void +max_sum_add(struct max_sum *p, double v) +{ + if(p->max_i < 0 || v > p->max_v){ + p->max_v = v; + p->max_i = p->n; + } + if(p->min_i < 0 || v < p->min_v){ + p->min_v = v; + p->min_i = p->n; + } + p->sum += v; + p->n++; +} + +double +max_sum_mean(struct max_sum *p) +{ + if(p->n == 0) return -1; + return p->sum / p->n; +} + /* EOF */ diff -urN midi_prog-/util.h midi_prog/util.h --- midi_prog-/util.h Wed Sep 9 00:00:00 2015 +++ midi_prog/util.h Thu Oct 1 00:00:00 2015 @@ -58,4 +58,17 @@ int chatt_add(struct chatt *ct, double sec, int onoff); void chatt_set(struct chatt *ct, int onoff); + +struct max_sum{ + double sum; + int n; + + double max_v, min_v; + int max_i, min_i; +}; + +void max_sum_init(struct max_sum *p); +void max_sum_add(struct max_sum *p, double v); +double max_sum_mean(struct max_sum *p); + #endif