📄 signal.c
字号:
/* * This material contains unpublished, proprietary software of * Entropic Research Laboratory, Inc. Any reproduction, distribution, * or publication of this work must be authorized in writing by Entropic * Research Laboratory, Inc., and must bear the notice: * * "Copyright (c) 1987-1990 AT&T, Inc. * "Copyright (c) 1987-1990 Entropic Speech, Inc. * "Copyright (c) 1990-1996 Entropic Research Laboratory, Inc. * All rights reserved" * * The copyright notice above does not evidence any actual or intended * publication of this source code. * * Written by: * Checked by: * Revised by: * * Brief description: * */static char *sccs_id = "@(#)signal.c 1.42 9/28/98 ATT/ESI/ERL";#include <Objects.h>#include <spectrogram.h>#include <esps/esps.h>char *savestring();int free_data(), vec_time_to_index();double vec_buf_start(), vec_buf_end(), vec_sig_dur(), vec_index_to_time();extern int debug_level;extern int valid_header; /* ugly global defined in copheader.c; used * to ` indicate case where header is ok but * couldn't read data for some reason (e.g., * requested segment not in file *//* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */output_header(sig) Signal *sig;{ int fd, itsok;#ifdef DEMO return FALSE;#endif if ((fd = sig->file) < 0) { if (sig->header && sig->header->magic == ESPS_MAGIC) { struct header *ehead = NULL; if (ehead = sig->header->esps_hdr) { if (sig->freq > 0.0) { if (get_genhd_val("record_freq", ehead, -1.2) < 0.0) *add_genhd_d("record_freq", (double *) NULL, 1, ehead) = sig->freq; } if (get_genhd_val("start_time", ehead, -1.234) == -1.234) *add_genhd_d("start_time", (double *) NULL, 1, ehead) = sig->start_time; } sig->header->strm = fopen(sig->name, "w+"); if (sig->header->strm) fd = fileno(sig->header->strm); } else fd = open(sig->name, O_TRUNC | O_RDWR | O_CREAT, 0666); sig->file = fd; } if (fd >= 0) { if (sig->header && sig->header->header) { itsok = put_header(sig->header, fd); sig->bytes_skip = (sig->header->magic == ESPS_MAGIC) ? sig->header->esps_nbytes : sig->header->nbytes + 12; } else { itsok = 1; sig->bytes_skip = 0; } return (itsok); } return (FALSE);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Close the file descriptor or, for ESPS files, the stream pointer * associated with a Signal. */close_sig_file(sig) Signal *sig;{ char *get_sphere_hdr(); if (debug_level >= 1) { if (sig) printf("%s: closing \"%s\", descriptor %d, header->magic 0x%x.\n", "close_sig_file", (sig->name) ? sig->name: "NULL", sig->file, (sig->header) ? sig->header->magic : 0); else printf("%s: signal is NULL.\n", "close_sig_file"); } if (is_feasd_sphere(sig)) { if(debug_level) fprintf(stderr, "rewinding sphere file.\n"); sp_rewind(get_sphere_hdr(sig->header->esps_hdr)); return; } if (sig && sig->file >= 0) { if (sig->header && sig->header->magic == ESPS_MAGIC) { fclose(sig->header->strm); sig->header->strm = NULL; } else close(sig->file); } sig->file = SIG_CLOSED;}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Write the signal to the file specified by sig->file (a file descriptor), * or, if necessary, open a descriptor using sig->name and write to it. The * sig->buff_size specifies the amount of data to be written. */put_signal(sig) Signal *sig;{ double size, start; struct header *hdr; if (sig && sig->name) { size = BUF_DURATION(sig); start = BUF_START_TIME(sig); if (sig->start_time != start && sig->header) { if (sig->header->header) head_printf(sig->header, "start_time", (char *) &start); if ((hdr = sig->header->esps_hdr) && sig->header->magic == ESPS_MAGIC) { *(genhd_type("start_time", (int *) NULL, hdr) == DOUBLE ? get_genhd_d("start_time", hdr) : add_genhd_d("start_time", (double *) NULL, 1, hdr) ) = start; } } sig->start_time = start; sig->start_samp = 0; if (output_header(sig)) { if (sig->utils->write_data(sig, start, size)) { close_sig_file(sig); return (TRUE); } else printf("Problems writing data in put_signal()\n"); } else printf("Couldn't open an output file (%s) in put_signal()\n", sig->name); } else printf("Bad signal or no name in put_signal()\n"); return (FALSE);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * A file "name" is opened for reading. Its header is read and the * appropriate read method is invoked to read "page_size" (seconds) of data * starting at "start" (seconds). If the signal is not "generic," the caller * must specify its own "read_proc" (reading method). */Signal *get_signal(name, start, page_size, read_proc) char *name; double start, page_size; int (*read_proc) ();{ Signal *s; Header *h; int fd; if (debug_level) (void) fprintf(stderr, "get_signal: function entered\n"); if ((fd = open(name, O_RDONLY)) >= 0) { if (h = get_header(fd)) { if ((s = new_signal(name, fd, h, (caddr_t) NULL, 0, 0.0, 0))#ifdef DEMO && (h->magic == ESPS_MAGIC || ((s->type & SPECIAL_SIGNALS) == SIG_FORMANTS))#endif /* DEMO */ ) { valid_header = 1; if (read_proc) s->utils->read_data = read_proc; if (s->utils->read_data(s, start, page_size)) { return (s); } /* else printf("Trouble reading sample * data\n"); */ } /* else printf("Couldn't create a new * Signal\n"); */ } /* else printf("get_header failed\n"); */ close(fd); } /* else printf("File open on %s * failed\n",name); */ return (NULL);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Change the name of a signal to new. If a file descriptor is open for the * signal, close it when the signal is renamed. s->name is reallocated iff * more space is required than for the old name. *//* * If signal is associated with an ESPS file, make a new header with the old * one as source. */rename_signal(s, new) Signal *s; char *new;{ char comment[200]; if (new && *new && s && s->name) { if (!strcmp(s->name, new)) return (TRUE); if (s->header && s->header->magic == ESPS_MAGIC && s->header->esps_hdr) { struct header *hdr; sprintf(comment, "waves rename_signal %s %s\n", s->name, new); hdr = copy_header(s->header->esps_hdr);/* * don't do this, until I figure out a better way to free the esps header * without freeing open headers that might be embedded add_source_file(hdr, savestring(s->name), s->header->esps_hdr);*/ s->header->esps_hdr = hdr; set_pvd(hdr); add_comment(hdr, comment); } if (strlen(new) > strlen(s->name)) { free(s->name); if (!(s->name = (char *) malloc(strlen(new) + 1))) { printf("Allocation failure in rename_signal()\n"); return (FALSE); } } strcpy(s->name, new); close_sig_file(s); s->file = SIG_NEW; return (TRUE); } return (FALSE);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Create a new signal structure. Initialize all attributes assuming the * signal is periodic, scalar, shorts. It will be up to the caller to * initialize other cases properly. */Signal *new_signal(name, fd, header, data, samples, freq, dimensions) char *name; caddr_t data; Header *header; int fd, samples, dimensions; double freq;{ Signal *s; char *str; double maxt, mint; if (debug_level) (void) fprintf(stderr, "new_signal: function entered\n"); if ((s = (Signal *) malloc(sizeof(Signal)))) { if (name && *name) { if (!(str = malloc(strlen(name) + 1))) { free(s); printf("Can't allocate memory in new_signal()\n"); return (NULL); } strcpy(str, name); s->name = str; } else s->name = NULL; s->file = fd; s->file_size = 0; s->buff_size = 0; s->freq = 10000.0; s->dim = 1; s->start_samp = 0; s->start_time = 0.0; s->end_time = 0.0; s->band = freq / 2.0; s->band_low = 0.0; s->y_dat = NULL; s->x_dat = NULL; s->type = P_SHORTS; s->types = NULL; s->version = 1; s->obj = NULL; maxt = mint = 0.0; s->smax = &maxt; s->smin = &mint; s->others = NULL; s->header = NULL; s->views = NULL; s->idents = NULL; s->params = NULL; s->methods = NULL; /* install default utility methods */ if (s->utils = (Utils *) malloc(sizeof(Utils))) { s->utils->read_data = read_data; s->utils->write_data = write_data; s->utils->free_data = free_data; s->utils->buf_start = vec_buf_start; s->utils->buf_end = vec_buf_end; s->utils->sig_dur = vec_sig_dur; s->utils->index_to_time = vec_index_to_time; s->utils->time_to_index = vec_time_to_index; } else { free(s); printf("Can't allocate memory in new_signal()\n"); return (NULL); } s->bytes_skip = 0; s->header = header; w_read_header(s, header); if (samples) { if (!s->file_size) s->file_size = samples; s->buff_size = samples; } if (dimensions) s->dim = dimensions; else if (s->dim > 0) dimensions = s->dim; else dimensions = 1; if (freq != 0.0) s->freq = freq; if (!((s->smax = (double *) malloc(sizeof(double) * dimensions)) && (s->smin = (double *) malloc(sizeof(double) * dimensions)))) { printf("Can't allocate memory in new_signal()\n"); return (NULL); } s->smax[0] = maxt; s->smin[0] = mint; s->data = data; return (s); } return (NULL);}/******************************************************************/voidfree_ret(r) Reticle *r;{ if (r) { if (r->abs_label) free(r->abs_label); if (r->ord_label) free(r->ord_label); if (r->ordinate.maj.list) free(r->ordinate.maj.list); if (r->ordinate.min1.list) free(r->ordinate.min1.list); if (r->ordinate.min2.list) free(r->ordinate.min2.list); if (r->abscissa.maj.list) free(r->abscissa.maj.list); if (r->abscissa.min1.list) free(r->abscissa.min1.list); if (r->abscissa.min2.list) free(r->abscissa.min2.list); /* * Note that this doesn't do a pf_close() on any open pixfonts. If this * causes problems, change references to free_ret to free_reticle (in * reticle.c) and include about 250kbytes of sunview junk! */ free(r); }}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */voidfree_view(v) View *v;{ int i; if (v) { if (v->x_scale) free(v->x_scale); if (v->y_scale) free(v->y_scale); if (v->z_scale) free(v->z_scale); if (v->x_offset) free(v->x_offset); if (v->y_offset) free(v->y_offset); if (v->val_offset) free(v->val_offset); if (v->val_scale) free(v->val_scale); if (v->z_offset) free(v->z_offset); if (v->elements) free(v->elements); if (v->colors) free(v->colors); if (v->line_types) free(v->line_types); if (v->mark_reference) free(v->mark_reference); /* * if extra is more complex, need to check types and handle separately. */ if (v->extra_type == VIEW_BITMAP) { ViewBitmap *vbm = (ViewBitmap *) v->extra; if (v->free_extra && vbm->bitmap) v->free_extra(vbm->bitmap); if (v->extra) /* remove `extra' once contents are gone */ free(v->extra); } if (v->ret) { for (i = 0; i < v->dims; i++) free_ret(((char **) (v->ret))[i]); free(v->ret); } free(v); }}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * The resources used by Signal s and all s->others are returned to the * system. All open file descriptors are closed and all signal "views" are * freed. Note that free_signal() frees s->name, so that this should be set * to NULL before calling if it is not to be freed (the same is true for all * Signal elements: if a pointer is NULL, no free() operation is performed). */voidfree_signal(si) Signal *si;{ Signal *s, *s2; List *l, *l2; View *v, *v2; s = si; while (s) { if ((s->type & SPECIAL_SIGNALS) == SIG_SPECTROGRAM && s->params) free_spectrogram((Spectrogram *) s->params); if (s->smax) { free(s->smax); s->smax = NULL; } if (s->smin) { free(s->smin); s->smin = NULL; } if (s->x_dat) { free(s->x_dat); s->x_dat = NULL; } if (s->y_dat) { free(s->y_dat); s->y_dat = NULL; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -