raw.c

Go to the documentation of this file.
00001 /****************************************************************
00002  * July 5, 1991
00003  * Copyright 1991 Lance Norskog And Sundry Contributors
00004  * This source code is freely redistributable and may be used for
00005  * any purpose.  This copyright notice must be maintained. 
00006  * Lance Norskog And Sundry Contributors are not responsible for 
00007  * the consequences of using this software.
00008  ****************************************************************
00009  * jpc : modif for dec alpha on which long int are 64bits long 
00010  ****************************************************************/
00011 
00012 /****************************************************************
00013  * Sound Tools raw format file.
00014  * Includes .ub, .uw, .sb, .sw, and .ul formats at end
00015  *
00016  * Notes: most of the headerless formats set their handlers to raw
00017  * in their startread/write routines.  
00018  ****************************************************************/
00019 /*-----------------------------------------------------------------------------------*/ 
00020 #include "st.h"
00021 #include "libst.h"
00022 #include "sciprint.h"
00023 /*-----------------------------------------------------------------------------------*/ 
00024 
00025 /****************************************************************
00026  * Read raw file data, and convert it to 
00027  * the sox internal signed long format. 
00028  * Warning : on some dec-alpha long x = LEFT(int y,16) 
00029  *  does not give the proper result (first shift then cast ) 
00030  * but the result seams to be given by first cast then shift which 
00031  * is not what we want. Thus we have changed 
00032  *  *buf++ = LEFT(datum,XXX) into 
00033  *  *buf++ = datum =  LEFT(datum,XXX) to force the result we want
00034  ****************************************************************/
00035 #if defined(__alpha)|defined(__ia64__)
00036 int rawread(ft_t ft, int *buf, long int nsamp)
00037 #else 
00038 int rawread(ft_t ft, long int *buf, long int nsamp)
00039 #endif 
00040 
00041 {
00042   int count;
00043 #if defined(__alpha)|defined(__ia64__)
00044   register int datum;
00045 #else
00046   register long datum;
00047 #endif
00048   int done = 0;
00049   count=0;
00050   switch(ft->info.size) 
00051     {
00052     case BYTESCI: switch(ft->info.style) 
00053       {
00054       case SIGN2:
00055         while(done < nsamp) 
00056           {
00057             datum = getc(ft->fp);
00058             if (feof(ft->fp))
00059               return done;
00060             /* scale signed up to long's range */
00061             *buf++ = datum = LEFT(datum, 24);
00062             done++;
00063           }
00064         return done;
00065       case UNSIGNED:
00066         while(done < nsamp) 
00067           {
00068             datum = getc(ft->fp);
00069             if (feof(ft->fp))
00070               return done;
00071             /* Convert to signed */
00072             datum ^= 128;
00073             /* scale signed up to long's range */
00074             *buf++ = datum =  LEFT(datum, 24);
00075             done++;
00076           }
00077         return done;
00078       case ULAW:
00079         while(done < nsamp) 
00080           {
00081             datum = getc(ft->fp);
00082             if (feof(ft->fp))
00083               return done;
00084             datum = st_ulaw_to_linear((unsigned char)datum);
00085             /* scale signed up to long's range */
00086             *buf++ = datum =  LEFT(datum, 16);
00087             done++;
00088           }
00089         return done;
00090       case ALAW:
00091         while(done < nsamp) 
00092           {
00093             datum = getc(ft->fp);
00094             if (feof(ft->fp))
00095               return done;
00096             datum =  st_Alaw_to_linear((unsigned char)datum);
00097             /* scale signed up to long's range */
00098             *buf++ = datum = LEFT(datum, 16);
00099             done++;
00100           }
00101         return done;
00102       }
00103     case WORDSCI: 
00104       switch(ft->info.style) 
00105         {
00106         case SIGN2:
00107           while(done < nsamp)
00108             {
00109               datum = rshort(ft);
00110               if (feof(ft->fp))
00111                 return done;
00112               /* scale signed up to long's range */
00113               *buf++ = datum =  LEFT(datum, 16);
00119               done++;
00120             }
00121           return done;
00122         case UNSIGNED:
00123           while(done < nsamp) 
00124             {
00125               datum = rshort(ft);
00126               if (feof(ft->fp))
00127                 return done;
00128               /* Convert to signed */
00129               datum ^= 0x8000;
00130               /* scale signed up to long's range */
00131               *buf++ = datum = LEFT(datum, 16);
00132               done++;
00133             }
00134           return done;
00135         case ULAW:
00136           sciprint("No U-Law support for shorts\r\n");
00137           ft->ierr=1;
00138           return done;
00139         case ALAW:
00140           sciprint("No A-Law support for shorts\r\n");
00141           ft->ierr=1;
00142           return done;
00143         }
00144     case FLOATSCI:
00145       while(done < nsamp) 
00146         {
00147           datum = (long) rfloat(ft);
00148           if (feof(ft->fp))
00149             return done;
00150           *buf++ = datum =  LEFT(datum, 16);
00151           done++;
00152         }
00153       return done;
00154     default:
00155       sciprint("Drop through in rawread!\r\n");
00156       ft->ierr=1;
00157     }
00158   sciprint("Sorry, don't have code to read %s, %s\r\n",
00159            styles[ft->info.style], sizes[ft->info.size]);
00160   ft->ierr=1;
00161   return done;
00162 }
00163 /*-----------------------------------------------------------------------------------*/ 
00164 /****************************************************************
00165  * Convert the sox internal signed long format 
00166  * to the raw file data, and write it.
00167  ****************************************************************/
00168 void rawwrite(ft_t ft, long int *buf, long int nsamp)
00169 {
00170   register int datum;
00171   int done = 0;
00172   switch(ft->info.size) 
00173     {
00174     case BYTESCI: switch(ft->info.style) 
00175       {
00176       case SIGN2:
00177         while(done < nsamp) 
00178           {
00179             /* scale signed up to long's range */
00180             datum = RIGHT(*buf++, 24);
00181             putc(datum, ft->fp);
00182             done++;
00183           }
00184         return;
00185       case UNSIGNED:
00186         while(done < nsamp) 
00187           {
00188             /* scale signed up to long's range */
00189             datum = RIGHT(*buf++, 24);
00190             /* Convert to unsigned */
00191             datum ^= 128;
00192             putc(datum, ft->fp);
00193             done++;
00194           }
00195         return;
00196       case ULAW:
00197         while(done < nsamp) 
00198           {
00199             /* scale signed up to long's range */
00200             datum = RIGHT(*buf++, 16);
00201             /* round up to 12 bits of data */
00202             datum += 0x8;       /* + 0b1000 */
00203             datum = st_linear_to_ulaw(datum);
00204             putc(datum, ft->fp);
00205             done++;
00206           }
00207         return;
00208       case ALAW:
00209         while(done < nsamp) 
00210           {
00211             /* scale signed up to long's range */
00212             datum = RIGHT(*buf++, 16);
00213             /* round up to 12 bits of data */
00214             datum += 0x8;       /* + 0b1000 */
00215             datum = st_linear_to_Alaw(datum);
00216             putc(datum, ft->fp);
00217             done++;
00218           }
00219         return;
00220       }
00221     case WORDSCI: switch(ft->info.style) {
00222     case SIGN2:
00223       while(done < nsamp) {
00224         /* scale signed up to long's range */
00225         datum = RIGHT(*buf++, 16);
00226         wshort(ft, datum);
00227         done++;
00228       }
00229       return;
00230     case UNSIGNED:
00231       while(done < nsamp) {
00232         /* scale signed up to long's range */
00233         datum = RIGHT(*buf++, 16);
00234         /* Convert to unsigned */
00235         datum ^= 0x8000;
00236         wshort(ft, datum);
00237         done++;
00238       }
00239       return;
00240     case ULAW:
00241       sciprint("No U-Law support for shorts (try -b option ?)\r\n");
00242       ft->ierr=1;
00243       return;
00244     case ALAW:
00245       sciprint("No A-Law support for shorts (try -b option ?)\r\n");
00246       ft->ierr=1;
00247       return;
00248     }
00249     case FLOATSCI:
00250       while(done < nsamp) {
00251         /* scale signed up to long's range */
00252         datum = RIGHT(*buf++, 16);
00253         wfloat(ft, (float) datum);
00254         done++;
00255       }
00256       return;
00257     default: 
00258       {
00259         sciprint("Drop through in rawwrite!\r\n");
00260         ft->ierr=1;
00261       }
00262       
00263     }
00264   sciprint("Sorry, don't have code to write %s, %s\r\n",styles[ft->info.style], sizes[ft->info.size]);
00265         ft->ierr=1;
00266 }
00267 /*-----------------------------------------------------------------------------------*/ 
00268 
00269 
00270 
00271 
00272 
00273 

Generated on Sun Mar 4 15:04:00 2007 for Scilab [trunk] by  doxygen 1.5.1