changeset 5837:7e082f42497a

- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros. - Fix with audio only files.
author atmos4
date Fri, 26 Apr 2002 13:07:03 +0000
parents dcb0a2bf3345
children d53251b98ed6
files libao2/ao_pcm.c
diffstat 1 files changed, 57 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/libao2/ao_pcm.c	Fri Apr 26 12:52:06 2002 +0000
+++ b/libao2/ao_pcm.c	Fri Apr 26 13:07:03 2002 +0000
@@ -1,6 +1,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "config.h"
+#include "bswap.h"
+#include "afmt.h"
 #include "audio_out.h"
 #include "audio_out_internal.h"
 
@@ -42,21 +45,21 @@
 	unsigned long data_length;
 };
 
-
+/* init with default values */
 static struct WaveHeader wavhdr = {
-	WAV_ID_RIFF,
-	0x00000000,
-	WAV_ID_WAVE,
-	WAV_ID_FMT,
-	16,
-	WAV_ID_PCM,
-	2,
-	44100,
-	192000,
-	4,
-	16,
-	WAV_ID_DATA,
-	0x00000000
+	le2me_32(WAV_ID_RIFF),
+	le2me_32(0x00000000),
+	le2me_32(WAV_ID_WAVE),
+	le2me_32(WAV_ID_FMT),
+	le2me_32(16),
+	le2me_16(WAV_ID_PCM),
+	le2me_16(2),
+	le2me_32(44100),
+	le2me_32(192000),
+	le2me_16(4),
+	le2me_16(16),
+	le2me_32(WAV_ID_DATA),
+	le2me_32(0x00000000)
 };
 
 static FILE *fp = NULL;
@@ -69,19 +72,34 @@
 // open & setup audio device
 // return: 1=success 0=fail
 static int init(int rate,int channels,int format,int flags){
+	int bits;
 	if(!ao_outputfilename) {
 		ao_outputfilename = (char *) malloc(sizeof(char) * 14);
-		strcpy(ao_outputfilename,(ao_pcm_waveheader ? "audiodump.wav" : "audiodump.pcm"));
+		strcpy(ao_outputfilename,
+		       (ao_pcm_waveheader ? "audiodump.wav" : "audiodump.pcm"));
 	}
 	
-	wavhdr.channels = channels;
-	wavhdr.sample_rate = rate;
-	wavhdr.bytes_per_second = rate * (format / 8) * channels;
-	wavhdr.bits = format;
+	/* bits is only equal to format if (format == 8) or (format == 16);
+	   this means that the following "if" is a kludge and should
+	   really be a switch to be correct in all cases */
+	if (format == AFMT_S16_BE) { bits = 16;	}
+	else { bits = format; }
 
-	printf("PCM: File: %s (%s) Samplerate: %iHz Channels: %s Format %s\n", ao_outputfilename, (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate, (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format));
-	printf("PCM: Info - fastest dumping is achieved with -vo null -hardframedrop.\n");
-	printf("PCM: Info - to write WAVE files use -waveheader (default), for RAW PCM -nowaveheader.\n");
+	wavhdr.channels = le2me_16(channels);
+	wavhdr.sample_rate = le2me_32(rate);
+	wavhdr.bytes_per_second = rate * (bits / 8) * channels;
+	wavhdr.bytes_per_second = le2me_32(wavhdr.bytes_per_second);
+	wavhdr.bits = le2me_16(bits);
+
+	printf("PCM: File: %s (%s)\n"
+	       "PCM: Samplerate: %iHz Channels: %s Format %s\n",
+	       ao_outputfilename, (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate,
+	       (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format));
+	printf("PCM: Info: fastest dumping is achieved with -vo null "
+	       "-hardframedrop.\n"
+	       "PCM: Info: to write WAVE files use -waveheader (default); "
+	       "for RAW PCM -nowaveheader.\n");
+
 	fp = fopen(ao_outputfilename, "wb");
 
 	ao_data.outburst = 65536;
@@ -101,6 +119,7 @@
 	
 	if(ao_pcm_waveheader){ /* Write wave header */
 		wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8;
+		wavhdr.file_length = le2me_32(wavhdr.file_length);
 		fseek(fp, 0, SEEK_SET);
 		fwrite(&wavhdr,sizeof(wavhdr),1,fp);
 	}
@@ -128,7 +147,9 @@
 // return: how many bytes can be played without blocking
 static int get_space(){
 
-    return ao_data.pts < vo_pts ? ao_data.outburst : 0;
+    if(vo_pts)
+      return ao_data.pts < vo_pts ? ao_data.outburst : 0;
+    return ao_data.outburst;
 }
 
 // plays 'len' bytes of 'data'
@@ -136,9 +157,21 @@
 // return: number of bytes played
 static int play(void* data,int len,int flags){
 
+#ifdef WORDS_BIGENDIAN
+	register int i;
+	unsigned short *buffer = (unsigned short *) data;
+
+	if (wavhdr.bits == le2me_16(16)) {
+	  for(i = 0; i < len/2; ++i) {
+	    buffer[i] = le2me_16(buffer[i]);
+	  }
+	}
+	/* FIXME: take care of cases with more than 8 bits here? */
+#endif 
+	
 	//printf("PCM: Writing chunk!\n");
 	fwrite(data,len,1,fp);
-	
+
 	if(ao_pcm_waveheader)
 		wavhdr.data_length += len;