📄 read_data.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) 1986-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: David Talkin * Checked by: * Revised by: David Talkin, Rod Johnson, John Shore, Alan Parker * * Brief description: * Data access methods for commonly encountered data types. */static char *sccs_id = "@(#)read_data.c 1.43 5/1/98 ATT/ESI/ERL";#include <sys/file.h>#ifdef OS5#include <sys/fcntl.h>#endif#include <esps/esps.h>#include <esps/fea.h>#include <esps/feasd.h>#include <esps/feaspec.h>#include <Objects.h>#include <spectrogram.h>#include <esps/esignal_fea.h>#include <esps/pc_wav.h>#ifndef NULL#define NULL 0#endifshort get_fea_type();char *get_fea_ptr();void free_fea_rec();struct fea_data *allo_fea_rec();void free_signal();int intern_size_rec();extern int debug_level;extern int max_buff_bytes; /* defined in globals.c */void check_file_size();/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */isa_homogeneous_vector_signal_etc(s) Signal *s;{ if (s && s->header) { if (s->header->esps_hdr && (s->header->magic == ESPS_MAGIC)) { struct header *h = s->header->esps_hdr; int ok_byte_order = ((h->common.edr || edr_default(h->common.machine_code)) && !get_pc_wav_hdr(h));#if defined(SUN386i) || defined(DS3100) || defined(VAX) || defined(DEC_ALPHA) || defined(LINUX) ok_byte_order = !ok_byte_order;#endif if (get_esignal_hdr(h)) return FALSE; if (get_genhd_val("sphere_hd_ptr", h, (double) 0.0)) return FALSE; if (get_genhd_val("encoding", h, (double) 0.0)) return FALSE; if (h->common.tag) return FALSE; if (s->type == P_MIXED) { if (*s->types) { int *tp = s->types, i = s->dim - 1, t = *tp++, ish = 1; while (i-- > 0) if (*tp++ != t) ish = 0; return (ish && ok_byte_order && (h->hd.fea->field_count == 1)); } else { fprintf(stderr, "Apparantly bogus header passed to isa_homogeneous_vector_signal_etc(%s)\n", s->name); return (FALSE); } } else return (ok_byte_order && IS_GENERIC(s->type)); } else if (s->header->magic == SIGNAL_MAGIC) return (IS_GENERIC(s->type)); } return (FALSE);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */type_of_signal(s) Signal *s;{ if (isa_homogeneous_vector_signal_etc(s)) { if (s->type == P_MIXED) return (*s->types); } return (s->type);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */size_of_fea_unmixed(s) Signal *s;{ if (s && s->header && s->header->esps_hdr && (s->header->magic == ESPS_MAGIC) && s->types) { int typsize; switch (*s->types) { case P_SHORTS: case P_USHORTS: typsize = sizeof(short); break; case P_INTS: case P_UINTS: typsize = sizeof(int); break; case P_FLOATS: typsize = sizeof(float); break; case P_DOUBLES: typsize = sizeof(double); break; case P_CHARS: case P_UCHARS: typsize = sizeof(char); break; default: typsize = sizeof(short); fprintf(stderr, "Faking element size to shorts (probably wrong)\n"); break; } return (typsize); } return (0);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */install_signal_methods(s) Signal *s;{ if (IS_TAGGED_FEA(s) && (get_genhd_val("src_sf", s->header->esps_hdr, -1.0) > 0.0)) install_tag_fea_methods(s);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */read_data(s, start, page_size) Signal *s; double start, page_size;{ int fd; int retrn; int typsize; char *sp; char *name; Signal *get_any_signal(); char *get_sphere_hdr(); if (s) { if (debug_level >= 1) { fprintf(stderr, "read_data: s->name = \"%s\". s->file = %d.\n", s->name, s->file); fprintf(stderr, "read_data: start: %f, page_size: %f\n", start, page_size); }#ifdef DEMO if (!(s->header && s->header->magic == ESPS_MAGIC) && (s->type & SPECIAL_SIGNALS) != SIG_FORMANTS ) return FALSE;#endif if (is_feasd_sphere(s)) close_sig_file(s); if ((fd = s->file) < 0) { if (!((s->name) && ((s->file = fd = open(s->name, O_RDONLY)) >= 0))) { fprintf(stderr, "read_data(): Can't open the data file or file doesn't exist\n"); return (FALSE); } if (s->header && s->header->magic == ESPS_MAGIC) { char *sp; s->header->strm = fdopen(fd, "r"); if (!is_feasd_sphere(s)) if (debug_level) fprintf(stderr,"in read_data, not a sphere file, seeking..\n"); if (fseek(s->header->strm, (long) s->bytes_skip, 0)) { fprintf(stderr, "read_data: Can't skip header (%d bytes) of ESPS file.\n", s->bytes_skip); return FALSE; } } } install_signal_methods(s); if (s->start_time > start) start = s->start_time; if ((!IS_TAGGED_FEA(s)) && (page_size >= 0.0) && (page_size < 1.0 / s->freq)) { /* check for silly size */ /* * fprintf(stderr,"%s: Requested segment too short (%g s); no * records read.\n", "read_data", page_size); */ return TRUE; } /* if read length is 0, don't check type */ /* * check for special sigs before anything which assumes vector sigs. */ if (s->type & SPECIAL_SIGNALS) { switch (s->type & SPECIAL_SIGNALS) { case SIG_SPECTROGRAM: if (s->header && s->header->magic == ESPS_MAGIC) switch (s->header->esps_hdr->common.type) { case FT_FEA: switch (s->header->esps_hdr->hd.fea->fea_type) { case FEA_SPEC: return get_esps_fspec(s, start, page_size); break; default: fprintf(stderr, "%s: wrong FEA subtype for spectrogram.\n", "read_data"); return FALSE; break; } break; case FT_SPEC: default: fprintf(stderr, "%s: wrong ESPS file type for spectrogram.\n", "read_data"); return FALSE; break; } break; default: /* fall through to standard handling */ break; } } if (IS_GENERIC(s->type)) { /* If negative pg size, want entire file. */ if ((page_size < 0.0) && (s->freq > 0.0)) /* * this works for periodic signals, but not for other types */ page_size = ((double) s->file_size) / s->freq; if ((s->type & SIG_FORMAT) == SIG_ASCII) return (get_ascii(s, start, page_size)); switch (s->type & (~SPECIAL_SIGNALS)) { case P_SHORTS: case P_USHORTS: typsize = sizeof(short); break; case P_INTS: case P_UINTS: typsize = sizeof(int); break; case P_FLOATS: typsize = sizeof(float); break; case P_DOUBLES: typsize = sizeof(double); break; case P_CHARS: case P_UCHARS: typsize = sizeof(char); break; case P_MIXED: if (!(s->header && s->header->magic == ESPS_MAGIC && s->header->esps_hdr->common.type == FT_FEA)) { fprintf(stderr, "%s: signal type P_MIXED but not ESPS FEA file.\n", "read_data"); return FALSE; } if (!(typsize = size_of_fea_unmixed(s))) { fprintf(stderr, "weird signal type in read_data(%s)\n", s->name); return (FALSE); } break; default: fprintf(stderr, "Unknown data type %d in read_data()\n", s->type); return FALSE; break; } if (s->header && s->header->magic == ESPS_MAGIC) { switch (s->header->esps_hdr->common.type) { case FT_SD: return get_esps_sd(s, start, page_size, typsize); case FT_FEA: if (debug_level) fprintf(stderr, "is FT_FEA,s->type: %d\n", s->type); if (s->header->esps_hdr->hd.fea->fea_type == FEA_SD && s->type == P_SHORTS) { if (debug_level) fprintf(stderr, "calling get_esps_sd\n"); return get_esps_sd(s, start, page_size, typsize); } else { if (isa_homogeneous_vector_signal_etc(s)) { if (debug_level) fprintf(stderr, "calling get_binary\n"); return (get_binary(s, start, page_size, typsize)); } else { /* * all other FEA files */ if (debug_level) fprintf(stderr, "calling get_esps_fea\n"); return get_esps_fea(s, start, page_size); } } break; default: fprintf(stderr, "read_data: Called on unsupported ESPS file type.\n"); return FALSE; break; } } switch (s->type & (~SPECIAL_SIGNALS)) { case P_SHORTS: case P_USHORTS: { char *bloc; if (retrn = get_binary(s, start, page_size, typsize)) { bloc = *((char **) s->data); if (s->header && s->header->magic == RHEADR_MAGIC) byte_swap(bloc, s->buff_size * 2); } return (retrn); } default: return (get_binary(s, start, page_size, typsize)); break; } } else fprintf(stderr, "Unknown data type %d in read_data()\n", s->type); } else fprintf(stderr, "Null Signal pointer in read_data()\n"); return (FALSE);}/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* Read ESPS signals from a sampled-data file. */get_esps_sd(s, start, page_size, typsize) Signal *s; double start, page_size; int typsize;{ int dim, sam_to_skip, old_size; int i, j; int max_buff_size = max_buff_bytes / typsize; int loc, bytes_per_sam; FILE *strm = s->header->strm; struct header *hdr = s->header->esps_hdr; short **spp; if (s->type != P_SHORTS) { fprintf(stderr, "get_esps_sd: FEA_SD or SD file; Signal type not P_SHORT.\n"); return FALSE; } sam_to_skip = ((start - s->start_time) * s->freq) + 0.5; dim = s->dim; old_size = s->buff_size; bytes_per_sam = dim * typsize; s->buff_size = (int) (0.5 + (page_size * s->freq)); spp = (short **) s->data; if (s->buff_size == 0) { if (spp) { for (i = 0; i < dim; i++) if (spp[i]) free((char *) spp[i]); free(s->data); s->data = NULL; } return TRUE; } if (s->buff_size > max_buff_size) { s->buff_size = max_buff_size; fprintf(stderr, "%s: read request exceeds max_buff_size; limiting to %f sec.\n", "get_esps_sd", ((double) s->buff_size) / s->freq); } if (s->data && (old_size == s->buff_size) && (sam_to_skip == s->start_samp)) return TRUE;/* ugly hack to work around sphere problem*/ if ((int)get_genhd_val("sphere_hd_ptr", hdr, (double) 0.0) && sam_to_skip) sam_to_skip -= 1; if (s->file_size <= sam_to_skip) { fprintf(stderr, "File %s too small to reach requested segment at time %g\n", s->name, start); return FALSE; } s->start_samp = sam_to_skip; check_file_size(s, FALSE); if (is_feasd_sphere(s)) { if(debug_level) fprintf(stderr, "get_esps_sd: Is a sphere file, do not skip hdr\n"); } else if (fseek(strm, (long) s->bytes_skip, 0)) { fprintf(stderr, "get_esps_sd: Can't skip header of ESPS SD file.\n"); return FALSE; } if (debug_level) fprintf(stderr, "calling fea_skiprec(%x, %d, %x)\n", strm, sam_to_skip, hdr); fea_skiprec(strm, (long) sam_to_skip, hdr); if (!spp) { spp = (short **) malloc(sizeof(short *) * dim); if (!spp) { s->buff_size = 0; fprintf(stderr, "get_esps_sd: Can't allocate data pointer array.\n"); return FALSE; } for (i = 0; i < dim; i++) spp[i] = NULL; s->data = (caddr_t) spp; old_size = 0; } if (old_size != s->buff_size) for (i = 0; i < dim; i++) /* allocate an array for each dim. */ { if (spp[i]) free((char *) spp[i]); spp[i] = (short *) malloc(s->buff_size * sizeof(short)); if (!spp[i]) { fprintf(stderr, "get_esps_sd: Can't allocate data array.\n"); break; } } if (old_size == s->buff_size || i == dim) { if (dim == 1) { if (debug_level >= 1) fprintf(stderr, "get_esps_sd: Calling get_sd_recs().\n"); loc = get_sd_recs(spp[0], s->buff_size, s->header->esps_hdr, s->header->strm); } else { short *buf, *p, *q; long nread; int bsize = 500; struct feasd *feasd_rec; loc = 0; buf = (short *) malloc((size_t)(bytes_per_sam * bsize)); if (!buf) fprintf(stderr, "get_esps_sd: Can't allocate data buffer.\n"); else { feasd_rec = allo_feasd_recs(s->header->esps_hdr, SHORT, (long) bsize, buf, NO); if (!feasd_rec) fprintf(stderr, "get_esps_sd: Can't allocate feasd_rec.\n"); else { do { int sam_left = s->buff_size - loc; if (bsize > sam_left) bsize = sam_left; nread = get_feasd_recs(feasd_rec, 0L, (long) bsize, s->header->esps_hdr, s->header->strm);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -