shithub: sox

Download patch

ref: 271f865d486f712b5ea2aae1f23cf455374f4c7d
parent: 05ac00e00968ae0a1fde4f81ef11b847d58450d1
author: cbagwell <cbagwell>
date: Tue Feb 1 13:49:50 EST 2000

Bugfixes to wavwritehdr() for GSM support and invalid outputs.
Improved OSS drivers error checking.

--- a/src/oss.c
+++ b/src/oss.c
@@ -79,7 +79,12 @@
     if (ft->info.channels == -1) ft->info.channels = 1;
     else if (ft->info.channels > 2) ft->info.channels = 2;
 
-    ioctl(fileno(ft->fp), SNDCTL_DSP_RESET, 0);
+    if (ioctl(fileno(ft->fp), SNDCTL_DSP_RESET, 0) < 0)
+    {
+	fail("Unable to reset OSS driver.  Possibly accessing an invalid file/device");
+	return(ST_EOF);
+    }
+    ft->file.size = 0;
     ioctl (fileno(ft->fp), SNDCTL_DSP_GETBLKSIZE, &ft->file.size);
     if (ft->file.size < 4 || ft->file.size > 65536) {
 	    fail("Invalid audio buffer size %d", ft->file.size);
@@ -100,8 +105,9 @@
     }
 
     tmp = samplesize;
-    ioctl(fileno(ft->fp), SNDCTL_DSP_SAMPLESIZE, &tmp);
-    if (tmp != samplesize) {
+    if (ioctl(fileno(ft->fp), SNDCTL_DSP_SAMPLESIZE, &tmp) < 0 || 
+	tmp != samplesize)
+    {
 	fail("Unable to set the sample size to %d", samplesize);
 	return (ST_EOF);
     }
@@ -110,8 +116,8 @@
     else dsp_stereo = 0;
 
     tmp = dsp_stereo;
-    ioctl(fileno(ft->fp), SNDCTL_DSP_STEREO, &tmp);
-    if (tmp != dsp_stereo) {
+    if (ioctl(fileno(ft->fp), SNDCTL_DSP_STEREO, &tmp) < 0 ||
+	tmp != dsp_stereo) {
 	ft->info.channels = 1;
 	warn("Couldn't set to %s", dsp_stereo?  "stereo":"mono");
 	dsp_stereo = 0;
@@ -118,9 +124,9 @@
     }
 
     tmp = ft->info.rate;
-    ioctl (fileno(ft->fp), SNDCTL_DSP_SPEED, &tmp);
-    if (ft->info.rate != tmp) {
-	/* If the rate the sound card is using is not within 2% of what
+    if (ioctl (fileno(ft->fp), SNDCTL_DSP_SPEED, &tmp) < 0 || 
+    	ft->info.rate != tmp) {
+	/* If the rate the sound card is using is not within 1% of what
 	 * the user specified then override the user setting.
 	 * The only reason not to always override this is because of
 	 * clock-rounding problems. Sound cards will sometimes use
@@ -128,8 +134,8 @@
 	 * this and having strange output file rates for something that
 	 * we can't hear anyways.
 	 */
-	if (ft->info.rate - tmp > (tmp * .02) || 
-	    tmp - ft->info.rate > (tmp * .02)) {
+	if (ft->info.rate - tmp > (tmp * .01) || 
+	    tmp - ft->info.rate > (tmp * .01)) {
 	    warn("Unable to set audio speed to %d (set to %d)",
 		     ft->info.rate, tmp);
 	    ft->info.rate = tmp;
--- a/src/raw.c
+++ b/src/raw.c
@@ -274,7 +274,7 @@
 			}
 			return done;
 		default:
-			fail("Drop through in rawread!");
+			break;
 	}
 	fail("Sorry, don't have code to read %s, %s",
 		st_encodings_str[ft->info.style], st_sizes_str[ft->info.size]);
@@ -454,10 +454,10 @@
 				}
 				return done;
 			case ST_ENCODING_ULAW:
-fail("No U-Law support for shorts (try -b option ?)");
+				fail("No U-Law support for shorts");
 				return 0;
 			case ST_ENCODING_ALAW:
-fail("No A-Law support for shorts (try -b option ?)");
+				fail("No A-Law support for shorts");
 				return 0;
 		    }
 		    break;
@@ -484,8 +484,7 @@
 			}
 			return done;
 		default:
-		    fail("Drop through in rawwrite!");
-		    return 0;
+			break;
 	}
 	fail("Sorry, don't have code to write %s, %s",
 		st_encodings_str[ft->info.style], st_sizes_str[ft->info.size]);
--- a/src/wav.c
+++ b/src/wav.c
@@ -1147,25 +1147,9 @@
 
 	int rc;
 
-	if (ft->info.style != ST_ENCODING_ADPCM &&
-	    ft->info.style != ST_ENCODING_IMA_ADPCM &&
-	    ft->info.style != ST_ENCODING_GSM
-	   )
-	{
-		rc = st_rawstartwrite(ft);
-		if (rc)
-		    return rc;
-	}
-
 	wSamplesPerSecond = ft->info.rate;
 	wChannels = ft->info.channels;
 
-	if (wChannels == 0 || wChannels>64) /* FIXME: arbitrary upper limit */
-	{
-	    fail("Channels(%d) out-of-range\n",wChannels);
-	    return ST_EOF;
-	}
-
 	switch (ft->info.size)
 	{
 		case ST_SIZE_BYTE:
@@ -1172,34 +1156,49 @@
 		        wBitsPerSample = 8;
 			if (ft->info.style != ST_ENCODING_UNSIGNED &&
 			    ft->info.style != ST_ENCODING_ULAW &&
-			    ft->info.style != ST_ENCODING_ALAW)
+			    ft->info.style != ST_ENCODING_ALAW &&
+			    ft->info.style != ST_ENCODING_GSM)
 			{
-				warn("Only support unsigned, ulaw, or alaw with 8-bit data.  Forcing to unsigned");
+				warn("Do not support %s with 8-bit data.  Forcing to unsigned",st_encodings_str[ft->info.style]);
 				ft->info.style = ST_ENCODING_UNSIGNED;
 			}
 			break;
 		case ST_SIZE_WORD:
 			wBitsPerSample = 16;
-			if ((ft->info.style == ST_ENCODING_UNSIGNED ||
-			     ft->info.style == ST_ENCODING_ULAW ||
-			     ft->info.style == ST_ENCODING_ALAW) &&
-			    !second_header)
+			if (ft->info.style != ST_ENCODING_SIGN2)
 			{
-				warn("Do not support Unsigned, ulaw, or alaw with 16 bit data.  Forcing to Signed");
+				warn("Do not support %s with 16-bit data.  Forcing to Signed.",st_encodings_str[ft->info.style]);
 				ft->info.style = ST_ENCODING_SIGN2;
 			}
 			break;
 		case ST_SIZE_DWORD:
 			wBitsPerSample = 32;
+			if (ft->info.style != ST_ENCODING_SIGN2)
+			{
+				warn("Do not support %s with 16-bit data.  Forcing to Signed.",st_encodings_str[ft->info.style]);
+				ft->info.style = ST_ENCODING_SIGN2;
+			}
+
 			break;
 		default:
-			wBitsPerSample = 32;
+			warn("Do not support %s in WAV files.  Forcing to Signed Words.",st_sizes_str[ft->info.size]);
+			ft->info.style = ST_ENCODING_SIGN2;
+			ft->info.size = ST_SIZE_WORD;
+			wBitsPerSample = 16;
 			break;
 	}
 
-	bytespersample = ST_SIZE_WORD;	/* common default */
-	wSamplesPerBlock = 1;	/* common default */
+	if (ft->info.style != ST_ENCODING_ADPCM &&
+	    ft->info.style != ST_ENCODING_IMA_ADPCM &&
+	    ft->info.style != ST_ENCODING_GSM)
+	{
+		rc = st_rawstartwrite(ft);
+		if (rc)
+		    return rc;
+	}
 
+	wSamplesPerBlock = 1;	/* common default for PCM data */
+
 	switch (ft->info.style)
 	{
 		case ST_ENCODING_UNSIGNED:
@@ -1210,12 +1209,10 @@
 			break;
 		case ST_ENCODING_ALAW:
 			wFormatTag = WAVE_FORMAT_ALAW;
-	    		bytespersample = ST_SIZE_BYTE;
 	    		wBlockAlign = wChannels;
 			break;
 		case ST_ENCODING_ULAW:
 			wFormatTag = WAVE_FORMAT_MULAW;
-	    		bytespersample = ST_SIZE_BYTE;
 	    		wBlockAlign = wChannels;
 			break;
 		case ST_ENCODING_IMA_ADPCM: