home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / windows3 / mtlabsnd.zip / MAT2.C < prev    next >
C/C++ Source or Header  |  1993-04-26  |  3KB  |  154 lines

  1. /*
  2.  * January 12, 1993
  3.  * Copyright 1993 George Woyansky and Kevin Bradley
  4.  * 
  5.  * This source code is freely redistributable and may be used for
  6.  * any purpose.  This copyright notice must be maintained. 
  7.  * 
  8.  */
  9.  
  10. /*
  11.  * Matlab .mat format conversion
  12.  */
  13.  
  14. #include "st.h"
  15.  
  16. /* Private data for .mat file */
  17. typedef struct matstuff{
  18.      long type;   /* type */
  19.      long mrows;  /* row dimension */
  20.      long ncols;  /* column dimension */
  21.      long imagf;  /* flag indicating imag part */
  22.      long namlen; /* name length (including NULL) */
  23. } *mat_t;
  24.  
  25. extern float volume, amplitude;
  26. extern long summary, verbose;
  27. int shift;
  28.  
  29. /*
  30.  * Do anything required before you start reading samples.
  31.  * Read file header
  32.  */
  33. matstartread(ft) 
  34. ft_t ft;
  35. {
  36.     mat_t    mat = (mat_t) ft->priv;
  37.     char name[80];
  38.  
  39.     /* If you need to seek around the input file. */
  40.     if (0 && ! ft->seekable)
  41.         fail(".mat input file must be a file, not a pipe");
  42.  
  43.     fread(mat, sizeof(struct matstuff), 1, ft->fp);
  44.  
  45.     fread(name, sizeof(char), mat->namlen, ft->fp);
  46.     if (ft->info.size == WORD)
  47.         ft->info.size = DBLWRD;
  48.     else ft->info.size = DBLBYT;
  49.     
  50. /*    if(ft->info.size == BYTE) shift = 24;
  51.         else shift = 16;
  52.         printf("Size of data = %d\n",shift);
  53.     ft->info.size = DOUBLE; */
  54.         if (ft->info.style = -1) 
  55.             ft->info.style = SIGN2;      /* Default - signed data */
  56. }
  57.  
  58. /*
  59.  * Read up to len samples from file.
  60.  * Convert to signed longs.
  61.  * Place in buf[].
  62.  * Return number of samples read.
  63.  */
  64.  
  65. matread(ft, buf, len) 
  66. ft_t ft;
  67. long *buf, len;
  68. {
  69.     mat_t    mat = (mat_t) ft->priv;
  70.     long i;
  71.         double data;
  72.  
  73.     if (len > mat->mrows) len = mat->mrows;
  74.     if (len == 0) return 0;
  75.  
  76.     i = rawread(ft,buf,len);
  77.     mat->mrows -= i;
  78.     return (int) i;
  79. }
  80.  
  81. /*
  82.  * Do anything required when you stop reading samples.  
  83.  * Don't close input file! 
  84.  */
  85. matstopread(ft)
  86. ft_t ft;
  87. {
  88. }
  89.  
  90. matstartwrite(ft)
  91. ft_t ft;
  92. {
  93.     mat_t    mat = (mat_t) ft->priv;
  94.     int    littlendian = 0;
  95.     char    *endptr;
  96.  
  97.     if (! ft->seekable)
  98.         fail("Output .mat file must be a file, not a pipe");
  99.  
  100.     endptr = (char *) &littlendian;
  101.     *endptr = 1;
  102.     if (!littlendian) ft->swap = 1;
  103.  
  104.     mat->mrows = 0;
  105.     matwritehdr(ft);
  106. }
  107.  
  108. matwritehdr(ft)
  109. ft_t ft;
  110. {
  111.     mat_t    mat = (mat_t) ft->priv;
  112.  
  113.     if (ft->info.size == WORD)
  114.         ft->info.size = DBLWRD;
  115.     else ft->info.size = DBLBYT;               /* default to bytes */
  116.  
  117.     mat->ncols = 1;
  118.     mat->type = 0;
  119.     mat->imagf = 0;
  120.         mat->namlen = 6;
  121.     fwrite(mat,sizeof(struct matstuff), 1, ft->fp);
  122.     fputs("sound",ft->fp);
  123.     putc('\0',ft->fp);
  124. }
  125.  
  126. matwrite(ft, buf, len)
  127. ft_t ft;
  128. long *buf, len;
  129. {
  130.     mat_t    mat = (mat_t) ft->priv;
  131.     long i,foo;
  132.     double data;
  133.  
  134.     mat->mrows += len; 
  135.  
  136.     rawwrite(ft,buf,len);
  137. }
  138.  
  139. void
  140. matstopwrite(ft)
  141. ft_t ft;
  142. {
  143.     /* All samples are already written out. */
  144.     /* If file header needs fixing up, for example it needs the */
  145.      /* the number of samples in a field, seek back and write them here. */
  146.     mat_t    mat = (mat_t) ft->priv;
  147.  
  148.     if (!ft->seekable)
  149.         return;
  150.     if (fseek(ft->fp, 0L, 0) != 0)
  151.         fail("Can't rewind output file to rewrite .mat header.");
  152.     matwritehdr(ft);
  153. }
  154.