ref: 987ddbfb95e682dab4798336a6fda56ca82ac117
parent: a99b80e0dd49ea72692879671da3ed16891e5183
author: cbagwell <cbagwell>
date: Thu Mar 4 23:46:00 EST 1999
Fixed various bugs introduced by block read/writes.
--- a/src/maud.c
+++ b/src/maud.c
@@ -12,6 +12,9 @@
*
* supports: mono and stereo, linear, a-law and u-law reading and writing
*
+ * March 3, 1999 - cbagwell
+ * Changed to use rawread for reading.
+ *
*/
#include "st.h"
@@ -27,6 +30,7 @@
};
void maudwriteheader(P1(ft_t));
+void rawread(P3(ft_t, LONG *, LONG));
void rawwrite(P3(ft_t, LONG *, LONG));
/*
@@ -50,10 +54,13 @@
unsigned short chaninf;
ULONG chunksize;
-
+
int littlendian = 1;
char *endptr;
+ /* Needed for rawread() */
+ rawstartread(ft);
+
endptr = (char *) &littlendian;
/* maud is in big endian format. Swap whats read in
* on little endian machines.
@@ -181,125 +188,7 @@
ft_t ft;
LONG *buf, len;
{
- register int datum;
- int done = 0;
-
- if (ft->info.channels == 1) {
- if (ft->info.size == BYTE) {
- switch(ft->info.style) {
- case UNSIGNED:
- while(done < len) {
- datum = getc(ft->fp);
- if (feof(ft->fp)) return done;
- /* Convert to signed */
- datum ^= 128;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 24);
- done++;
- }
- break;
- case ULAW:
- /* grab table from Posk stuff */
- while(done < len) {
- datum = getc(ft->fp);
- if (feof(ft->fp)) return done;
- datum = st_ulaw_to_linear(datum);
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
- done++;
- }
- break;
- case ALAW:
- while(done < len) {
- datum = st_Alaw_to_linear((unsigned char) getc(ft->fp));
- if (feof(ft->fp)) return done;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
- done++;
- }
- break;
- }
- }
- else {
- while(done < len) {
- datum = rshort(ft);
- if (feof(ft->fp)) return done;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
- done++;
- }
- }
- }
- else { /* stereo */
- if (ft->info.size == BYTE) {
- switch(ft->info.style) {
- case UNSIGNED:
- while(done < len) {
- datum = getc(ft->fp);
- if (feof(ft->fp)) return done;
- /* Convert to signed */
- datum ^= 128;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 24);
-
- datum = getc(ft->fp);
- if (feof(ft->fp)) return done;
- /* Convert to signed */
- datum ^= 128;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 24);
- done += 2;
- }
- break;
- case ULAW:
- /* grab table from Posk stuff */
- while(done < len) {
- datum = getc(ft->fp);
- if (feof(ft->fp)) return done;
- datum = st_ulaw_to_linear(datum);
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
-
- datum = getc(ft->fp);
- if (feof(ft->fp)) return done;
- datum = st_ulaw_to_linear(datum);
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
- done += 2;
- }
- break;
- case ALAW:
- while(done < len) {
- datum = st_Alaw_to_linear((unsigned char) getc(ft->fp));
- if (feof(ft->fp)) return done;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
-
- datum = st_Alaw_to_linear((unsigned char) getc(ft->fp));
- if (feof(ft->fp)) return done;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
- done += 2;
- }
- break;
- }
- }
- else {
- while(done < len) {
- datum = rshort(ft);
- if (feof(ft->fp)) return done;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
-
- datum = rshort(ft);
- if (feof(ft->fp)) return done;
- /* scale signed up to long's range */
- *buf++ = LEFT(datum, 16);
- done += 2;
- }
- }
- }
- return done;
+ rawread(ft, buf, len);
}
/*
@@ -309,6 +198,8 @@
void maudstopread(ft)
ft_t ft;
{
+ /* Needed because of rawread() */
+ rawstopread(ft);
}
void maudstartwrite(ft)
@@ -319,6 +210,9 @@
int littlendian = 1;
char *endptr;
+ /* Needed for rawwrite() */
+ rawstartwrite(ft);
+
endptr = (char *) &littlendian;
/* maud is in big endian format. Swap whats read in
* on little endian machines.
@@ -357,6 +251,9 @@
void maudstopwrite(ft)
ft_t ft;
{
+ /* Flush out remaining samples*/
+ rawstopwrite(ft);
+
/* All samples are already written out. */
if (fseek(ft->fp, 0L, 0) != 0) fail("can't rewind output file to rewrite MAUD header");
--- a/src/raw.c
+++ b/src/raw.c
@@ -55,8 +55,8 @@
{
if (ft->file.eof)
return(0);
- ft->file.count = read(fileno(ft->fp), (char *)ft->file.buf,
- ft->file.size);
+ ft->file.count = fread(ft->file.buf, 1, ft->file.size,
+ ft->fp);
ft->file.pos = 0;
if (ft->file.count == 0)
{
@@ -80,15 +80,14 @@
if (ft->file.pos == ft->file.count-1)
{
*ft->file.buf = *(ft->file.buf + ft->file.pos);
- ft->file.count = read(fileno(ft->fp),
- (char *)(ft->file.buf + 1),
- ft->file.size-1) + 1;
+ ft->file.count = fread(ft->file.buf + 1,
+ 1, ft->file.size-1,
+ ft->fp);
}
else
{
- ft->file.count = read(fileno(ft->fp),
- (char *)ft->file.buf,
- ft->file.size);
+ ft->file.count = fread(ft->file.buf, 1, ft->file.size,
+ ft->fp);
}
ft->file.pos = 0;
if (ft->file.count < 2)
@@ -111,8 +110,8 @@
if (ft->file.count < sizeof(float))
{
- ft->file.count = read(fileno(ft->fp), (char *)ft->file.buf,
- ft->file.size);
+ ft->file.count = fread(ft->file.buf, 1, ft->file.size,
+ ft->fp);
if (ft->file.count == 0)
{
ft->file.eof = 1;
@@ -244,7 +243,7 @@
void blockflush(ft)
ft_t ft;
{
- if (write(fileno(ft->fp), ft->file.buf, ft->file.pos) != ft->file.pos)
+ if (fwrite(ft->file.buf, 1, ft->file.pos, ft->fp) != ft->file.pos)
{
fail("Error writing data to file");
}
--- a/src/sndrtool.c
+++ b/src/sndrtool.c
@@ -2,6 +2,9 @@
* Sounder/Sndtool format handler: W V Neisius, February 1992
*
* June 28, 93: force output to mono.
+ *
+ * March 3, 1999 - cbagwell@sprynet.com
+ * Forced extra comment fields to zero.
*/
#include <math.h>
@@ -97,7 +100,7 @@
char *endptr;
/* Needed for rawwrite() */
- rawstopwrite(ft);
+ rawstartwrite(ft);
endptr = (char *) &littlendian;
/* sndt is in little endian format so
@@ -122,6 +125,21 @@
void sndrstartwrite(ft)
ft_t ft;
{
+ int littlendian = 1;
+ char *endptr;
+
+ /* Needed for rawread() */
+ rawstartread(ft);
+
+ endptr = (char *) &littlendian;
+ /* sndr is in little endian format so
+ * swap bytes on big endian machines
+ */
+ if (!*endptr)
+ {
+ ft->swap = ft->swap ? 0 : 1;
+ }
+
/* write header */
ft->info.channels = 1;
ft->info.style = UNSIGNED;
@@ -154,15 +172,15 @@
void sndtstopwrite(ft)
ft_t ft;
{
-struct sndpriv *p = (struct sndpriv *) ft->priv;
+ struct sndpriv *p = (struct sndpriv *) ft->priv;
-/* fixup file sizes in header */
-if (fseek(ft->fp, 0L, 0) != 0)
- fail("can't rewind output file to rewrite SND header");
-sndtwriteheader(ft, p->nsamples);
-
- /* Needed for rawwrite() */
+ /* Flush remaining buffer out */
rawstopwrite(ft);
+
+ /* fixup file sizes in header */
+ if (fseek(ft->fp, 0L, 0) != 0)
+ fail("can't rewind output file to rewrite SND header");
+ sndtwriteheader(ft, p->nsamples);
}
/*======================================================================*/
@@ -185,6 +203,7 @@
wshort (ft,0);
wshort (ft,10);
wshort (ft,4);
+memset (name_buf, 0, 96);
sprintf (name_buf,"%s - File created by Sound Exchange",ft->filename);
fwrite (name_buf, 1, 96, ft->fp);
}