changeset 1112:b1cf1087ec33

Added support for writing wave files and specifying filename to write to.
author atmosfear
date Tue, 12 Jun 2001 14:24:26 +0000
parents a3c3e1de6f02
children d92e19f1ae71
files cfg-mplayer.h libao2/ao_pcm.c
diffstat 2 files changed, 80 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/cfg-mplayer.h	Tue Jun 12 14:13:26 2001 +0000
+++ b/cfg-mplayer.h	Tue Jun 12 14:24:26 2001 +0000
@@ -27,6 +27,9 @@
 extern int osd_level;
 extern int sub_unicode;
 
+extern char *ao_outputfilename;
+extern int ao_pcm_waveheader;
+
 #ifdef HAVE_X11
 extern char *mDisplayName;
 #endif
@@ -104,6 +107,10 @@
 	{"dumpfile", &stream_dump_name, CONF_TYPE_STRING, 0, 0, 0},
 	{"dumpaudio", &stream_dump_type, CONF_TYPE_FLAG, 0, 0, 1},
 	{"dumpvideo", &stream_dump_type, CONF_TYPE_FLAG, 0, 0, 2},
+	
+	{"aofile", &ao_outputfilename, CONF_TYPE_STRING, 0, 0, 0},
+	{"waveheader", &ao_pcm_waveheader, CONF_TYPE_FLAG, 0, 0, 1},
+	{"nowaveheader", &ao_pcm_waveheader, CONF_TYPE_FLAG, 0, 1, 0},
 
 //	{"auds", &avi_header.audio_codec, CONF_TYPE_STRING, 0, 0, 0},
 //	{"vids", &avi_header.video_codec, CONF_TYPE_STRING, 0, 0, 0},
--- a/libao2/ao_pcm.c	Tue Jun 12 14:13:26 2001 +0000
+++ b/libao2/ao_pcm.c	Tue Jun 12 14:24:26 2001 +0000
@@ -6,7 +6,7 @@
 
 static ao_info_t info = 
 {
-	"PCM writer audio output",
+	"RAW PCM/WAVE file writer audio output",
 	"pcm",
 	"Atmosfear",
 	""
@@ -22,6 +22,49 @@
 // ao_outburst
 // ao_buffersize
 
+char *ao_outputfilename = NULL;
+int ao_pcm_waveheader = 1;
+
+#define WAV_ID_RIFF 0x46464952 /* "RIFF" */
+#define WAV_ID_WAVE 0x45564157 /* "WAVE" */
+#define WAV_ID_FMT  0x20746d66 /* "fmt " */
+#define WAV_ID_DATA 0x61746164 /* "data" */
+#define WAV_ID_PCM  0x0001
+
+struct WaveHeader
+{
+	unsigned long riff;
+	unsigned long file_length;
+	unsigned long wave;
+	unsigned long fmt;
+	unsigned long fmt_length;
+	short fmt_tag;
+	short channels;
+	unsigned long sample_rate;
+	unsigned long bytes_per_second;
+	short block_align;
+	short bits;
+	unsigned long data;
+	unsigned long data_length;
+};
+
+
+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
+};
+
 static FILE *fp = NULL;
 
 // to set/get/query special features/parameters
@@ -32,22 +75,44 @@
 // open & setup audio device
 // return: 1=success 0=fail
 static int init(int rate,int channels,int format,int flags){
+	if(!ao_outputfilename) {
+		ao_outputfilename = (char *) malloc(sizeof(char) * 14);
+		strcpy(ao_outputfilename,(ao_pcm_waveheader ? "audiodump.wav" : "audiodump.pcm"));
+	}
 	
-	printf("PCM: File: audiodump.pcm Samplerate: %iHz Channels: %s Format %s\n", rate, (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format));
+	wavhdr.fmt_length = 16; /* = format? */
+	wavhdr.channels = channels;
+	wavhdr.sample_rate = rate;
+	wavhdr.bytes_per_second = rate * (format / 8) * channels;
+	wavhdr.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");
-	fp = fopen("audiodump.pcm", "wb");
+	printf("PCM: Info - to write WAVE files use -waveheader (default), for RAW PCM -nowaveheader.\n");
+	fp = fopen(ao_outputfilename, "wb");
 
 	ao_outburst = 4096;
 
 
-	if(fp) return 1;
-	printf("PCM: Failed to open audiodump.pcm for writing!\n");
+	if(fp) {
+		if(ao_pcm_waveheader) /* Reserve space for wave header */
+			fseek(fp, sizeof(wavhdr), SEEK_SET);
+		return 1;
+	}
+	printf("PCM: Failed to open %s for writing!\n", ao_outputfilename);
 	return 0;
 }
 
 // close audio device
 static void uninit(){
+	
+	if(ao_pcm_waveheader){ /* Write wave header */
+		wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr);
+		fseek(fp, 0, SEEK_SET);
+		fwrite(&wavhdr,sizeof(wavhdr),1,fp);
+	}
 	fclose(fp);
+	free(ao_outputfilename);
 }
 
 // stop playing and empty buffers (for seeking/pause)
@@ -81,6 +146,9 @@
 	//printf("PCM: Writing chunk!\n");
 	fwrite(data,len,1,fp);
 	
+	if(ao_pcm_waveheader)
+		wavhdr.data_length += len;
+	
 	return len;
 }