static Xen g_mus_sound_set_maxamp(Xen file, Xen vals) { int chans; char *filename; Xen_check_type(Xen_is_string(file), file, 1, S_set S_mus_sound_maxamp, "a string"); Xen_check_type(Xen_is_list(vals), vals, 2, S_set S_mus_sound_maxamp, "a list"); filename = mus_expand_filename(Xen_string_to_C_string(file)); chans = mus_sound_chans(filename); /* presumably any actual error here will be trapped via mus-error (raised in mus_header_read via read_sound_file_header), * so chans <= 0 is actually in the header? */ if (chans > 0) { Xen lst; int i, j, len; mus_float_t *mvals; mus_long_t *times; len = Xen_list_length(vals); if (len < (chans * 2)) Xen_wrong_type_arg_error(S_set S_mus_sound_maxamp, 2, vals, "max amp list length must = 2 * chans"); if (len > chans * 2) len = chans * 2; mvals = (mus_float_t *)calloc(chans, sizeof(mus_float_t)); times = (mus_long_t *)calloc(chans, sizeof(mus_long_t)); for (i = 0, j = 0, lst = Xen_copy_arg(vals); i < len; i += 2, j++, lst = Xen_cddr(lst)) { times[j] = Xen_llong_to_C_llong(Xen_car(lst)); mvals[j] = Xen_real_to_C_double(Xen_cadr(lst)); } fprintf(stderr, "set in g_mus_sound_set_maxamp\n"); mus_sound_set_maxamps(filename, chans, mvals, times); free(mvals); free(times); if (filename) free(filename); } else { if (filename) free(filename); Xen_error(BAD_HEADER, Xen_list_1(C_string_to_Xen_string(S_set S_mus_sound_maxamp ": chans <= 0"))); } return(vals); }
mus_long_t mus_sound_maxamps(const char *ifile, int chans, mus_float_t *vals, mus_long_t *times) { mus_long_t frames; int i, ichans, chn; sound_file *sf; sf = get_sf(ifile); if (sf->chans <= 0) return(MUS_ERROR); if ((sf) && (sf->maxamps)) { if (chans > sf->maxamps_size) ichans = sf->maxamps_size; else ichans = chans; for (chn = 0; chn < ichans; chn++) { times[chn] = sf->maxtimes[chn]; vals[chn] = sf->maxamps[chn]; } frames = sf->samples / sf->chans; return(frames); } { int j, bufnum, ifd; mus_long_t n, curframes; mus_float_t *buffer, *samp; mus_long_t *time; mus_float_t **ibufs; ifd = mus_sound_open_input(ifile); if (ifd == MUS_ERROR) return(MUS_ERROR); ichans = mus_sound_chans(ifile); frames = mus_sound_frames(ifile); if (frames == 0) { mus_sound_close_input(ifd); return(0); } mus_file_seek_frame(ifd, 0); ibufs = (mus_float_t **)calloc(ichans, sizeof(mus_float_t *)); bufnum = 8192; for (j = 0; j < ichans; j++) ibufs[j] = (mus_float_t *)calloc(bufnum, sizeof(mus_float_t)); time = (mus_long_t *)calloc(ichans, sizeof(mus_long_t)); samp = (mus_float_t *)calloc(ichans, sizeof(mus_float_t)); for (n = 0; n < frames; n += bufnum) { if ((n + bufnum) < frames) curframes = bufnum; else curframes = (frames - n); mus_file_read(ifd, 0, curframes - 1, ichans, ibufs); for (chn = 0; chn < ichans; chn++) { buffer = (mus_float_t *)(ibufs[chn]); for (i = 0; i < curframes; i++) { mus_float_t abs_samp; abs_samp = fabs(buffer[i]); if (abs_samp > samp[chn]) { time[chn] = i + n; samp[chn] = abs_samp; } } } } mus_sound_close_input(ifd); mus_sound_set_maxamps(ifile, ichans, samp, time); /* save the complete set */ if (ichans > chans) ichans = chans; for (chn = 0; chn < ichans; chn++) { times[chn] = time[chn]; vals[chn] = samp[chn]; } free(time); free(samp); for (j = 0; j < ichans; j++) free(ibufs[j]); free(ibufs); return(frames); } }