static int mus_sound_set_field(const char *arg, sf_field_t field, int val) { sound_file *sf; int result = MUS_NO_ERROR; sf = get_sf(arg); if (sf) { switch (field) { case SF_CHANS: sf->chans = val; break; case SF_SRATE: sf->srate = val; break; case SF_TYPE: sf->header_type = val; break; case SF_FORMAT: sf->data_format = val; sf->datum_size = mus_bytes_per_sample(val); break; default: result = MUS_ERROR; break; } } else result = MUS_ERROR; return(result); }
void mus_sound_set_saved_data(const char *arg, mus_float_t **data) { sound_file *sf; sf = get_sf(arg); if (sf) sf->saved_data = data; }
static int mus_sound_set_mus_long_t_field(const char *arg, sf_field_t field, mus_long_t val) { sound_file *sf; int result = MUS_NO_ERROR; sf = get_sf(arg); if (sf) { switch (field) { case SF_SIZE: sf->samples = val; break; case SF_LOCATION: sf->data_location = val; break; default: result = MUS_ERROR; break; } } else result = MUS_ERROR; return(result); }
int *mus_sound_loop_info(const char *arg) { sound_file *sf; sf = get_sf(arg); if ((sf) && (sf->loop_modes)) { int *info; info = (int *)calloc(MUS_LOOP_INFO_SIZE, sizeof(int)); if (sf->loop_modes[0] != 0) { info[0] = sf->loop_starts[0]; info[1] = sf->loop_ends[0]; info[6] = sf->loop_modes[0]; } if (sf->loop_modes[1] != 0) { info[2] = sf->loop_starts[1]; info[3] = sf->loop_ends[1]; info[7] = sf->loop_modes[1]; } info[4] = sf->base_note; info[5] = sf->base_detune; return(info); } return(NULL); }
int mus_sound_open_input(const char *arg) { int fd = -1; if (!(mus_file_probe(arg))) mus_error(MUS_CANT_OPEN_FILE, "mus_sound_open_input: can't open %s: %s", arg, STRERROR(errno)); else { sound_file *sf = NULL; mus_sound_initialize(); sf = get_sf(arg); if (sf) { fd = mus_file_open_read(arg); if (fd == -1) mus_error(MUS_CANT_OPEN_FILE, "mus_sound_open_input: can't open %s: %s", arg, STRERROR(errno)); else { mus_file_open_descriptors(fd, arg, sf->sample_type, sf->datum_size, sf->data_location, sf->chans, sf->header_type); lseek(fd, sf->data_location, SEEK_SET); if (sf->saved_data) mus_file_save_data(fd, sf->samples / sf->chans, sf->saved_data); } } } return(fd); }
int mus_sound_srate(const char *arg) { sound_file *sf; int result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->srate; return(result); }
int mus_sound_header_type(const char *arg) { sound_file *sf; int result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->header_type; return(result); }
mus_long_t mus_sound_comment_end(const char *arg) { sound_file *sf; mus_long_t result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->comment_end; return(result); }
mus_long_t mus_sound_length(const char *arg) { sound_file *sf; mus_long_t result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->true_file_length; return(result); }
int mus_sound_fact_samples(const char *arg) { sound_file *sf; int result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->fact_samples; return(result); }
mus_long_t mus_sound_frames(const char *arg) { sound_file *sf; mus_long_t result = MUS_ERROR; sf = get_sf(arg); if (sf) result = (sf->chans > 0) ? (sf->samples / sf->chans) : 0; return(result); }
int mus_sound_type_specifier(const char *arg) { sound_file *sf; int result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->type_specifier; return(result); }
int mus_sound_block_align(const char *arg) { sound_file *sf; int result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->block_align; return(result); }
mus_long_t mus_sound_data_location(const char *arg) { sound_file *sf; mus_long_t result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->data_location; return(result); }
mus_long_t mus_sound_samples(const char *arg) { sound_file *sf; mus_long_t result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->samples; return(result); }
int mus_sound_bits_per_sample(const char *arg) { sound_file *sf; int result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->bits_per_sample; return(result); }
time_t mus_sound_write_date(const char *arg) { sound_file *sf; time_t result = (time_t)(MUS_ERROR); sf = get_sf(arg); if (sf) result = sf->write_date; return(result); }
int mus_sound_original_format(const char *arg) { sound_file *sf; int result = MUS_ERROR; sf = get_sf(arg); if (sf) result = sf->original_sound_format; return(result); }
bool mus_sound_channel_maxamp_exists(const char *file, int chan) { sound_file *sf; sf = get_sf(file); return((sf) && (sf->maxtimes) && (sf->maxamps_size > chan) && (sf->maxtimes[chan] != -1)); }
mus_float_t mus_sound_channel_maxamp(const char *file, int chan, mus_long_t *pos) { sound_file *sf; sf = get_sf(file); if ((chan < sf->maxamps_size) && (sf->maxtimes)) { (*pos) = sf->maxtimes[chan]; return(sf->maxamps[chan]); } return(-1.0); }
int mus_sound_set_maxamps(const char *ifile, int chans, mus_float_t *vals, mus_long_t *times) { sound_file *sf; int result = MUS_NO_ERROR; /* fprintf(stderr, "set %s maxamps: %d %.3f %lld\n", ifile, chans, vals[0], times[0]); */ sf = get_sf(ifile); if (sf) { int i, ichans = 0; if (sf->maxamps) { if (chans > sf->maxamps_size) ichans = sf->maxamps_size; else ichans = chans; for (i = 0; i < ichans; i++) { sf->maxtimes[i] = times[i]; sf->maxamps[i] = vals[i]; } } else { ichans = sf->chans; if (sf->maxamps == NULL) { /* here we need to use the max, since the caller may be confused */ int max_chans; max_chans = ichans; if (max_chans < chans) max_chans = chans; sf->maxamps = (mus_float_t *)calloc(max_chans, sizeof(mus_float_t)); sf->maxtimes = (mus_long_t *)calloc(max_chans, sizeof(mus_long_t)); sf->maxamps_size = max_chans; } if (ichans > sf->maxamps_size) ichans = sf->maxamps_size; if (ichans > chans) ichans = chans; for (i = 0; i < ichans; i++) { sf->maxtimes[i] = times[i]; sf->maxamps[i] = vals[i]; } } } else result = MUS_ERROR; return(result); }
float mus_sound_duration(const char *arg) { float val = -1.0; sound_file *sf; sf = get_sf(arg); if (sf) { if ((sf->chans > 0) && (sf->srate > 0)) val = (float)((double)(sf->samples) / ((float)(sf->chans) * (float)(sf->srate))); else val = 0.0; } return(val); }
int mus_sound_mark_info(const char *arg, int **mark_ids, int **mark_positions) { sound_file *sf; int result = 0; sf = get_sf(arg); if (sf) { (*mark_ids) = sf->marker_ids; (*mark_positions) = sf->marker_positions; result = sf->markers; } return(result); }
bool mus_sound_maxamp_exists(const char *ifile) { sound_file *sf; sf = get_sf(ifile); if ((sf) && (sf->maxtimes)) { int i; for (i = 0; i < sf->maxamps_size; i++) if (sf->maxtimes[i] == -1) return(false); return(true); } return(false); }
void mus_sound_set_loop_info(const char *arg, int *loop) { sound_file *sf; sf = get_sf(arg); if (sf) { if (sf->loop_modes == NULL) { sf->loop_modes = (int *)calloc(2, sizeof(int)); sf->loop_starts = (int *)calloc(2, sizeof(int)); sf->loop_ends = (int *)calloc(2, sizeof(int)); } sf->loop_modes[0] = loop[6]; if (loop[6] != 0) { sf->loop_starts[0] = loop[0]; sf->loop_ends[0] = loop[1]; } else { sf->loop_starts[0] = 0; sf->loop_ends[0] = 0; } sf->loop_modes[1] = loop[7]; if (loop[7] != 0) { sf->loop_starts[1] = loop[2]; sf->loop_ends[1] = loop[3]; } else { sf->loop_starts[1] = 0; sf->loop_ends[1] = 0; } sf->base_note = loop[4]; sf->base_detune = loop[5]; } }
void mus_sound_channel_set_maxamp(const char *file, int chan, mus_float_t mx, mus_long_t pos) { sound_file *sf; sf = get_sf(file); if ((sf) && (sf->chans > chan)) { if (!(sf->maxamps)) { int i; sf->maxamps = (mus_float_t *)malloc(sf->chans * sizeof(mus_float_t)); sf->maxtimes = (mus_long_t *)malloc(sf->chans * sizeof(mus_long_t)); sf->maxamps_size = sf->chans; for (i = 0; i < sf->chans; i++) { sf->maxamps[i] = -1.0; sf->maxtimes[i] = -1; } } sf->maxamps[chan] = mx; sf->maxtimes[chan] = pos; } }
int mus_sound_override_header(const char *arg, int srate, int chans, int format, int type, mus_long_t location, mus_long_t size) { sound_file *sf; int result = MUS_NO_ERROR; /* perhaps once a header has been over-ridden, we should not reset the relevant fields upon re-read? */ sf = get_sf(arg); if (sf) { if (location != -1) sf->data_location = location; if (size != -1) sf->samples = size; if (format != -1) { sf->data_format = format; sf->datum_size = mus_bytes_per_sample(format); } if (srate != -1) sf->srate = srate; if (chans != -1) sf->chans = chans; if (type != -1) sf->header_type = type; } else result = MUS_ERROR; return(result); }
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); } }
char *mus_sound_comment(const char *name) { mus_long_t start, end, len; char *sc = NULL; sound_file *sf = NULL; sf = get_sf(name); if (sf) { start = sf->comment_start; end = sf->comment_end; if (end == 0) { if (sf->aux_comment_start) { if ((sf->header_type == MUS_RIFF) || (sf->header_type == MUS_RF64)) sc = mus_header_riff_aux_comment(name, sf->aux_comment_start, sf->aux_comment_end); if ((sf->header_type == MUS_AIFF) || (sf->header_type == MUS_AIFC)) sc = mus_header_aiff_aux_comment(name, sf->aux_comment_start, sf->aux_comment_end); } } else { if (end <= sf->true_file_length) { len = end - start + 1; if (len > 0) { /* open and get the comment */ ssize_t bytes; int fd; char *auxcom; fd = mus_file_open_read(name); if (fd == -1) return(NULL); lseek(fd, start, SEEK_SET); sc = (char *)calloc(len + 1, sizeof(char)); bytes = read(fd, sc, len); CLOSE(fd, name); if (((sf->header_type == MUS_AIFF) || (sf->header_type == MUS_AIFC)) && (sf->aux_comment_start) && (bytes != 0)) { auxcom = mus_header_aiff_aux_comment(name, sf->aux_comment_start, sf->aux_comment_end); if (auxcom) { size_t full_len; full_len = strlen(auxcom) + strlen(sc) + 2; sc = (char *)realloc(sc, full_len * sizeof(char)); strcat(sc, "\n"); strcat(sc, auxcom); } } } } } } return(sc); }
int mus_sound_bits_per_sample(const char *arg) { sound_file *sf; sf = get_sf(arg); return((sf) ? sf->bits_per_sample : MUS_ERROR); }