📄 sound.c
字号:
sf->write_date = date; chan = mus_file_open_read(name); data_size = lseek(chan, 0L, SEEK_END); sf->true_file_length = data_size; sf->samples = mus_bytes_to_samples(sf->data_format, data_size); CLOSE(chan, name); return(sf); } /* otherwise our data base is out-of-date, so clear it out */ free_sound_file(sf); } } return(NULL);}static sound_file *find_sound_file(const char *name){ int i; /* perhaps we already have the needed data... (90% hit rate here) */ if ((previous_sf) && (strcmp(previous_sf->file_name, name) == 0) && (previous_sf->write_date == local_file_write_date(name))) return(previous_sf); if (name) for (i = 0; i < sound_table_size; i++) if ((sound_table[i]) && (strcmp(name, sound_table[i]->file_name) == 0)) { previous_sf = check_write_date(name, sound_table[i]); return(previous_sf); } return(NULL);}static void display_sound_file_entry(FILE *fp, const char *name, sound_file *sf){ int i, lim; time_t date; char timestr[64]; char *comment; date = sf->write_date; if (date != 0) {#if HAVE_STRFTIME strftime(timestr, 64, "%a %d-%b-%Y %H:%M:%S", localtime(&date));#else sprintf(timestr, "%d", (int)date);#endif } else sprintf(timestr, "(date cleared)"); fprintf(fp, " %s: %s, chans: %d, srate: %d, type: %s, format: %s, samps: " OFF_TD, name, timestr, sf->chans, sf->srate, mus_header_type_name(sf->header_type), mus_data_format_name(sf->data_format), sf->samples); if (sf->loop_modes) { if (sf->loop_modes[0] != 0) fprintf(fp, ", loop mode %d: %d to %d", sf->loop_modes[0], sf->loop_starts[0], sf->loop_ends[0]); if (sf->loop_modes[1] != 0) fprintf(fp, ", loop mode %d: %d to %d, ", sf->loop_modes[1], sf->loop_starts[1], sf->loop_ends[1]); fprintf(fp, ", base: %d, detune: %d", sf->base_note, sf->base_detune); } if (sf->maxamps) { lim = sf->chans; if (lim > 0) { if (lim > 64) lim = 64; for (i = 0; i < lim; i++) { if (i > 1) fprintf(fp, ", "); fprintf(fp, " %.3f at %.3f ", MUS_SAMPLE_TO_FLOAT(sf->maxamps[i]), (sf->srate > 0.0) ? (float)((double)(sf->maxtimes[i]) / (double)(sf->srate)) : (float)(sf->maxtimes[i])); } } } if (mus_file_probe(name)) { comment = mus_sound_comment(name); if (comment) { fprintf(fp, "\n comment: %s", comment); FREE(comment); } } else fprintf(fp, " [defunct]"); fprintf(fp, "\n");}void mus_sound_report_cache(FILE *fp){ sound_file *sf; int entries = 0; int i; fprintf(fp, "sound table:\n"); for (i = 0; i < sound_table_size; i++) { sf = sound_table[i]; if (sf) { display_sound_file_entry(fp, sf->file_name, sf); entries++; } } fprintf(fp, "\nentries: %d\n", entries); fflush(fp);}static sound_file *fill_sf_record(const char *name, sound_file *sf){ int i; sf->data_location = mus_header_data_location(); sf->samples = mus_header_samples(); sf->data_format = mus_header_format(); sf->srate = mus_header_srate(); /* if (sf->srate < 0) sf->srate = 0; */ sf->chans = mus_header_chans(); /* if (sf->chans < 0) sf->chans = 0; */ sf->datum_size = mus_bytes_per_sample(sf->data_format); sf->header_type = mus_header_type(); sf->original_sound_format = mus_header_original_format(); sf->true_file_length = mus_header_true_length(); sf->comment_start = mus_header_comment_start(); sf->comment_end = mus_header_comment_end(); if (((sf->header_type == MUS_AIFC) || (sf->header_type == MUS_AIFF) || (sf->header_type == MUS_RIFF)) && (mus_header_aux_comment_start(0) != 0)) { sf->aux_comment_start = (off_t *)CALLOC(4, sizeof(off_t)); sf->aux_comment_end = (off_t *)CALLOC(4, sizeof(off_t)); for (i = 0; i < 4; i++) { sf->aux_comment_start[i] = mus_header_aux_comment_start(i); sf->aux_comment_end[i] = mus_header_aux_comment_end(i); } } sf->type_specifier = mus_header_type_specifier(); sf->bits_per_sample = mus_header_bits_per_sample(); sf->fact_samples = mus_header_fact_samples(); sf->block_align = mus_header_block_align(); sf->write_date = local_file_write_date(name); if (mus_header_loop_mode(0) > 0) { sf->loop_modes = (int *)CALLOC(2, sizeof(int)); sf->loop_starts = (int *)CALLOC(2, sizeof(int)); sf->loop_ends = (int *)CALLOC(2, sizeof(int)); for (i = 0; i < 2; i++) { sf->loop_modes[i] = mus_header_loop_mode(i); if ((sf->header_type == MUS_AIFF) || (sf->header_type == MUS_AIFC)) { sf->loop_starts[i] = mus_header_mark_position(mus_header_loop_start(i)); sf->loop_ends[i] = mus_header_mark_position(mus_header_loop_end(i)); } else { sf->loop_starts[i] = mus_header_loop_start(i); sf->loop_ends[i] = mus_header_loop_end(i); } } sf->base_detune = mus_header_base_detune(); sf->base_note = mus_header_base_note(); } previous_sf = sf; return(sf);}static sound_file *read_sound_file_header(const char *name){ mus_sound_initialize(); if (mus_header_read(name) != MUS_ERROR) return(fill_sf_record(name, add_to_sound_table(name))); return(NULL);}static sound_file *getsf(const char *arg) { sound_file *sf = NULL; if (arg == NULL) return(NULL); sf = find_sound_file(arg); if (sf) return(sf); return(read_sound_file_header(arg));}#define MUS_SF(Filename, Expression) \ sound_file *sf; \ sf = getsf(Filename); \ if (sf) return(Expression); \ return(MUS_ERROR)off_t mus_sound_samples(const char *arg) {MUS_SF(arg, sf->samples);}off_t mus_sound_frames(const char *arg) {MUS_SF(arg, (sf->chans > 0) ? (sf->samples / sf->chans) : 0);}int mus_sound_datum_size(const char *arg) {MUS_SF(arg, sf->datum_size);}off_t mus_sound_data_location(const char *arg) {MUS_SF(arg, sf->data_location);}int mus_sound_chans(const char *arg) {MUS_SF(arg, sf->chans);}int mus_sound_srate(const char *arg) {MUS_SF(arg, sf->srate);}int mus_sound_header_type(const char *arg) {MUS_SF(arg, sf->header_type);}int mus_sound_data_format(const char *arg) {MUS_SF(arg, sf->data_format);}int mus_sound_original_format(const char *arg) {MUS_SF(arg, sf->original_sound_format);}off_t mus_sound_comment_start(const char *arg) {MUS_SF(arg, sf->comment_start);}off_t mus_sound_comment_end(const char *arg) {MUS_SF(arg, sf->comment_end);}off_t mus_sound_length(const char *arg) {MUS_SF(arg, sf->true_file_length);}int mus_sound_fact_samples(const char *arg) {MUS_SF(arg, sf->fact_samples);}time_t mus_sound_write_date(const char *arg) {MUS_SF(arg, sf->write_date);}int mus_sound_type_specifier(const char *arg) {MUS_SF(arg, sf->type_specifier);}int mus_sound_block_align(const char *arg) {MUS_SF(arg, sf->block_align);}int mus_sound_bits_per_sample(const char *arg) {MUS_SF(arg, sf->bits_per_sample);}float mus_sound_duration(const char *arg) { float val = -1.0; sound_file *sf; sf = getsf(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_loop_info(const char *arg){ sound_file *sf; int *info; sf = getsf(arg); if ((sf) && (sf->loop_modes)) { 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); } else return(NULL);}void mus_sound_set_loop_info(const char *arg, int *loop){ sound_file *sf; sf = getsf(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]; }}char *mus_sound_comment(const char *name){ off_t start, end, len; int fd, full_len; /* comment string lengths */ char *sc = NULL, *auxcom; sound_file *sf = NULL; sf = getsf(name); if (sf == NULL) return(NULL); start = mus_sound_comment_start(name); end = mus_sound_comment_end(name); if (end == 0) { if (sf->aux_comment_start) { if (mus_sound_header_type(name) == MUS_RIFF) return(mus_header_riff_aux_comment(name, sf->aux_comment_start, sf->aux_comment_end)); if ((mus_sound_header_type(name) == MUS_AIFF) || (mus_sound_header_type(name) == MUS_AIFC)) return(mus_header_aiff_aux_comment(name, sf->aux_comment_start, sf->aux_comment_end)); } return(NULL); } if (end > mus_sound_length(name)) return(NULL); len = end - start + 1; if (len > 0) { /* open and get the comment */ size_t bytes; 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 (((mus_sound_header_type(name) == MUS_AIFF) || (mus_sound_header_type(name) == 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) { 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_open_input(const char *arg) { int fd = -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -