| 462 | } |
| 463 | |
| 464 | static float FindAndReportMax(CSOUND *csound, SCALE *thissc, |
| 465 | SNDFILE *infile, OPARMS *oparms) |
| 466 | { |
| 467 | MYFLT buffer[BUFFER_LEN]; |
| 468 | long read_in; |
| 469 | double tpersample; |
| 470 | double max, min; |
| 471 | long mxpos, minpos; |
| 472 | int32_t maxtimes, mintimes; |
| 473 | int32_t i, chans = thissc->p->nchanls; |
| 474 | int32_t block = 0; |
| 475 | int32_t bufferLenFrames = (int32_t) BUFFER_LEN / chans; |
| 476 | int32_t bufferLenSamples = bufferLenFrames * chans; |
| 477 | |
| 478 | tpersample = 1.0 / (double) thissc->p->sr; |
| 479 | max = 0.0; mxpos = 0; maxtimes = 0; |
| 480 | min = 0.0; minpos = 0; mintimes = 0; |
| 481 | while ((read_in = csound->getsndin(csound, infile, buffer, |
| 482 | bufferLenSamples, thissc->p)) > 0) { |
| 483 | for (i = 0; i < read_in; i++) { |
| 484 | //j = (i / chans) + (bufferLenFrames * block); |
| 485 | if (buffer[i] >= max) ++maxtimes; |
| 486 | if (buffer[i] <= min) ++mintimes; |
| 487 | if (buffer[i] > max) |
| 488 | max = buffer[i], mxpos = i + bufferLenSamples * block, maxtimes = 1; |
| 489 | if (buffer[i] < min) |
| 490 | min = buffer[i], minpos = i + bufferLenSamples * block, mintimes = 1; |
| 491 | } |
| 492 | block++; |
| 493 | if (oparms->heartbeat) { |
| 494 | csound->MessageS(csound, CSOUNDMSG_REALTIME, "%c\b", "|/-\\"[block&3]); |
| 495 | } |
| 496 | } |
| 497 | csound->Message(csound, Str("Max val %.3f at index %ld (time %.4f, chan %d) " |
| 498 | "%d times\n"), max, (long) mxpos / (long) chans, |
| 499 | tpersample * (double) mxpos / (double) chans, |
| 500 | ((int32_t) mxpos % chans) + 1, (int32_t) maxtimes); |
| 501 | csound->Message(csound, Str("Min val %.3f at index %ld (time %.4f, chan %d) " |
| 502 | "%d times\n"), min, (long) minpos / (long) chans, |
| 503 | tpersample * (double) minpos / (double) chans, |
| 504 | ((int32_t) minpos % chans) + 1, (int32_t) mintimes); |
| 505 | csound->Message(csound, Str("Max scale factor = %.3f\n"), |
| 506 | (double) csound->Get0dBFS(csound)/ (max > -min ? |
| 507 | max:-min)); |
| 508 | return (float) (max > -min ? max : -min); |
| 509 | } |
| 510 | |
| 511 | /* module interface */ |
| 512 | |