1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/****************************************************************
* July 5, 1991
* Copyright 1991 Lance Norskog And Sundry Contributors
* This source code is freely redistributable and may be used for
* any purpose. This copyright notice must be maintained.
* Lance Norskog And Sundry Contributors are not responsible for
* the consequences of using this software.
****************************************************************
* jpc : modif for dec alpha on which long int are 64bits long
****************************************************************/
/****************************************************************
* Sound Tools raw format file.
* Includes .ub, .uw, .sb, .sw, and .ul formats at end
*
* Notes: most of the headerless formats set their handlers to raw
* in their startread/write routines.
****************************************************************/
#include "st.h"
#include "libst.h"
extern void sciprint __PARAMS((char *fmt, ...));
/****************************************************************
* Read raw file data, and convert it to
* the sox internal signed long format.
* Warning : on some dec-alpha long x = LEFT(int y,16)
* does not give the proper result (first shift then cast )
* but the result seams to be given by first cast then shift which
* is not what we want. Thus we have changed
* *buf++ = LEFT(datum,XXX) into
* *buf++ = datum = LEFT(datum,XXX) to force the result we want
****************************************************************/
#if defined(__alpha)|defined(__ia64__)
int rawread(ft_t ft, int *buf, long int nsamp)
#else
int rawread(ft_t ft, long int *buf, long int nsamp)
#endif
{
int count;
#if defined(__alpha)|defined(__ia64__)
register int datum;
#else
register long datum;
#endif
int done = 0;
count=0;
switch(ft->info.size)
{
case BYTESCI: switch(ft->info.style)
{
case SIGN2:
while(done < nsamp)
{
datum = getc(ft->fp);
if (feof(ft->fp))
return done;
/* scale signed up to long's range */
*buf++ = datum = LEFT(datum, 24);
done++;
}
return done;
case UNSIGNED:
while(done < nsamp)
{
datum = getc(ft->fp);
if (feof(ft->fp))
return done;
/* Convert to signed */
datum ^= 128;
/* scale signed up to long's range */
*buf++ = datum = LEFT(datum, 24);
done++;
}
return done;
case ULAW:
while(done < nsamp)
{
datum = getc(ft->fp);
if (feof(ft->fp))
return done;
datum = st_ulaw_to_linear((unsigned char)datum);
/* scale signed up to long's range */
*buf++ = datum = LEFT(datum, 16);
done++;
}
return done;
case ALAW:
while(done < nsamp)
{
datum = getc(ft->fp);
if (feof(ft->fp))
return done;
datum = st_Alaw_to_linear((unsigned char)datum);
/* scale signed up to long's range */
*buf++ = datum = LEFT(datum, 16);
done++;
}
return done;
}
case WORDSCI:
switch(ft->info.style)
{
case SIGN2:
while(done < nsamp)
{
datum = rshort(ft);
if (feof(ft->fp))
return done;
/* scale signed up to long's range */
*buf++ = datum = LEFT(datum, 16);
/** if ( count < 5 ) {
sciprint("datum %d %d %d \r\n",*(buf-1),sizeof(long),sizeof(int));
count++;
}
**/
done++;
}
return done;
case UNSIGNED:
while(done < nsamp)
{
datum = rshort(ft);
if (feof(ft->fp))
return done;
/* Convert to signed */
datum ^= 0x8000;
/* scale signed up to long's range */
*buf++ = datum = LEFT(datum, 16);
done++;
}
return done;
case ULAW:
sciprint("No U-Law support for shorts\r\n");
ft->ierr=1;
return done;
case ALAW:
sciprint("No A-Law support for shorts\r\n");
ft->ierr=1;
return done;
}
case FLOATSCI:
while(done < nsamp)
{
datum = (long) rfloat(ft);
if (feof(ft->fp))
return done;
*buf++ = datum = LEFT(datum, 16);
done++;
}
return done;
default:
sciprint("Drop through in rawread!\r\n");
ft->ierr=1;
}
sciprint("Sorry, don't have code to read %s, %s\r\n",
styles[ft->info.style], sizes[ft->info.size]);
ft->ierr=1;
return done;
}
/****************************************************************
* Convert the sox internal signed long format
* to the raw file data, and write it.
****************************************************************/
void rawwrite(ft_t ft, long int *buf, long int nsamp)
{
register int datum;
int done = 0;
switch(ft->info.size)
{
case BYTESCI: switch(ft->info.style)
{
case SIGN2:
while(done < nsamp)
{
/* scale signed up to long's range */
datum = RIGHT(*buf++, 24);
putc(datum, ft->fp);
done++;
}
return;
case UNSIGNED:
while(done < nsamp)
{
/* scale signed up to long's range */
datum = RIGHT(*buf++, 24);
/* Convert to unsigned */
datum ^= 128;
putc(datum, ft->fp);
done++;
}
return;
case ULAW:
while(done < nsamp)
{
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
/* round up to 12 bits of data */
datum += 0x8; /* + 0b1000 */
datum = st_linear_to_ulaw(datum);
putc(datum, ft->fp);
done++;
}
return;
case ALAW:
while(done < nsamp)
{
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
/* round up to 12 bits of data */
datum += 0x8; /* + 0b1000 */
datum = st_linear_to_Alaw(datum);
putc(datum, ft->fp);
done++;
}
return;
}
case WORDSCI: switch(ft->info.style) {
case SIGN2:
while(done < nsamp) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
wshort(ft, datum);
done++;
}
return;
case UNSIGNED:
while(done < nsamp) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
/* Convert to unsigned */
datum ^= 0x8000;
wshort(ft, datum);
done++;
}
return;
case ULAW:
sciprint("No U-Law support for shorts (try -b option ?)\r\n");
ft->ierr=1;
return;
case ALAW:
sciprint("No A-Law support for shorts (try -b option ?)\r\n");
ft->ierr=1;
return;
}
case FLOATSCI:
while(done < nsamp) {
/* scale signed up to long's range */
datum = RIGHT(*buf++, 16);
wfloat(ft, (double) datum);
done++;
}
return;
default:
{
sciprint("Drop through in rawwrite!\r\n");
ft->ierr=1;
}
}
sciprint("Sorry, don't have code to write %s, %s\r\n",
styles[ft->info.style], sizes[ft->info.size]);
ft->ierr=1;
}
|