2002-05-30 11:53:51 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-06-12 11:00:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-05-30 11:53:51 +00:00
|
|
|
#include <string.h>
|
2001-06-12 11:00:15 +00:00
|
|
|
|
2006-11-29 15:02:45 +00:00
|
|
|
#include "libavutil/common.h"
|
2006-12-07 11:58:07 +00:00
|
|
|
#include "mpbswap.h"
|
2005-01-02 14:41:47 +00:00
|
|
|
#include "subopt-helper.h"
|
2004-12-27 17:30:15 +00:00
|
|
|
#include "libaf/af_format.h"
|
2001-06-12 11:00:15 +00:00
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
2004-09-18 20:31:28 +00:00
|
|
|
|
2001-06-12 11:00:15 +00:00
|
|
|
|
|
|
|
static ao_info_t info =
|
|
|
|
{
|
2001-06-12 14:24:26 +00:00
|
|
|
"RAW PCM/WAVE file writer audio output",
|
2001-06-12 11:00:15 +00:00
|
|
|
"pcm",
|
|
|
|
"Atmosfear",
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBAO_EXTERN(pcm)
|
|
|
|
|
2002-03-02 18:54:58 +00:00
|
|
|
extern int vo_pts;
|
|
|
|
|
2005-01-02 14:41:47 +00:00
|
|
|
static char *ao_outputfilename = NULL;
|
|
|
|
static int ao_pcm_waveheader = 1;
|
2006-04-14 13:18:35 +00:00
|
|
|
static int fast = 0;
|
2001-06-12 14:24:26 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
2003-10-26 13:43:30 +00:00
|
|
|
uint32_t riff;
|
|
|
|
uint32_t file_length;
|
|
|
|
uint32_t wave;
|
|
|
|
uint32_t fmt;
|
|
|
|
uint32_t fmt_length;
|
|
|
|
uint16_t fmt_tag;
|
|
|
|
uint16_t channels;
|
|
|
|
uint32_t sample_rate;
|
|
|
|
uint32_t bytes_per_second;
|
|
|
|
uint16_t block_align;
|
|
|
|
uint16_t bits;
|
|
|
|
uint32_t data;
|
|
|
|
uint32_t data_length;
|
2001-06-12 14:24:26 +00:00
|
|
|
};
|
|
|
|
|
2002-04-26 13:07:03 +00:00
|
|
|
/* init with default values */
|
2006-11-26 13:19:32 +00:00
|
|
|
static struct WaveHeader wavhdr;
|
2001-06-12 14:24:26 +00:00
|
|
|
|
2001-06-12 11:00:15 +00:00
|
|
|
static FILE *fp = NULL;
|
|
|
|
|
|
|
|
// to set/get/query special features/parameters
|
2003-03-21 16:42:50 +00:00
|
|
|
static int control(int cmd,void *arg){
|
2001-06-12 11:00:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open & setup audio device
|
|
|
|
// return: 1=success 0=fail
|
|
|
|
static int init(int rate,int channels,int format,int flags){
|
2002-04-26 13:07:03 +00:00
|
|
|
int bits;
|
2005-01-02 14:41:47 +00:00
|
|
|
opt_t subopts[] = {
|
|
|
|
{"waveheader", OPT_ARG_BOOL, &ao_pcm_waveheader, NULL},
|
2005-01-19 17:18:25 +00:00
|
|
|
{"file", OPT_ARG_MSTRZ, &ao_outputfilename, NULL},
|
2006-04-14 13:18:35 +00:00
|
|
|
{"fast", OPT_ARG_BOOL, &fast, NULL},
|
2005-01-02 14:41:47 +00:00
|
|
|
{NULL}
|
2005-01-02 16:01:36 +00:00
|
|
|
};
|
2005-01-02 14:41:47 +00:00
|
|
|
// set defaults
|
|
|
|
ao_pcm_waveheader = 1;
|
2006-05-13 19:04:39 +00:00
|
|
|
|
2005-01-02 14:41:47 +00:00
|
|
|
if (subopt_parse(ao_subdevice, subopts) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2006-05-14 10:21:56 +00:00
|
|
|
if (!ao_outputfilename){
|
|
|
|
ao_outputfilename =
|
|
|
|
strdup(ao_pcm_waveheader?"audiodump.wav":"audiodump.pcm");
|
|
|
|
}
|
2006-05-13 19:04:39 +00:00
|
|
|
|
2002-04-26 13:07:03 +00:00
|
|
|
/* 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 */
|
|
|
|
|
2002-10-07 19:51:00 +00:00
|
|
|
bits=8;
|
|
|
|
switch(format){
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S8:
|
|
|
|
format=AF_FORMAT_U8;
|
|
|
|
case AF_FORMAT_U8:
|
2002-10-07 19:51:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2004-12-27 17:30:15 +00:00
|
|
|
format=AF_FORMAT_S16_LE;
|
2002-10-07 19:51:00 +00:00
|
|
|
bits=16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ao_data.outburst = 65536;
|
|
|
|
ao_data.buffersize= 2*65536;
|
|
|
|
ao_data.channels=channels;
|
|
|
|
ao_data.samplerate=rate;
|
|
|
|
ao_data.format=format;
|
|
|
|
ao_data.bps=channels*rate*(bits/8);
|
|
|
|
|
2006-11-26 13:19:32 +00:00
|
|
|
wavhdr.riff = le2me_32(WAV_ID_RIFF);
|
|
|
|
wavhdr.wave = le2me_32(WAV_ID_WAVE);
|
|
|
|
wavhdr.fmt = le2me_32(WAV_ID_FMT);
|
|
|
|
wavhdr.fmt_length = le2me_32(16);
|
|
|
|
wavhdr.fmt_tag = le2me_16(WAV_ID_PCM);
|
2002-10-07 19:51:00 +00:00
|
|
|
wavhdr.channels = le2me_16(ao_data.channels);
|
|
|
|
wavhdr.sample_rate = le2me_32(ao_data.samplerate);
|
|
|
|
wavhdr.bytes_per_second = le2me_32(ao_data.bps);
|
2002-04-26 13:07:03 +00:00
|
|
|
wavhdr.bits = le2me_16(bits);
|
2005-08-17 11:27:11 +00:00
|
|
|
wavhdr.block_align = le2me_16(ao_data.channels * (bits / 8));
|
2003-02-04 18:22:43 +00:00
|
|
|
|
2006-11-26 13:19:32 +00:00
|
|
|
wavhdr.data = le2me_32(WAV_ID_DATA);
|
2003-02-04 18:22:43 +00:00
|
|
|
wavhdr.data_length=le2me_32(0x7ffff000);
|
|
|
|
wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8;
|
2002-04-26 13:07:03 +00:00
|
|
|
|
2004-12-28 19:11:14 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_FileInfo, ao_outputfilename,
|
|
|
|
(ao_pcm_waveheader?"WAVE":"RAW PCM"), rate,
|
|
|
|
(channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format));
|
2004-09-18 20:31:28 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_HintInfo);
|
2002-04-26 13:07:03 +00:00
|
|
|
|
2001-06-12 14:24:26 +00:00
|
|
|
fp = fopen(ao_outputfilename, "wb");
|
|
|
|
if(fp) {
|
2003-02-04 18:22:43 +00:00
|
|
|
if(ao_pcm_waveheader){ /* Reserve space for wave header */
|
2002-06-22 12:10:02 +00:00
|
|
|
fwrite(&wavhdr,sizeof(wavhdr),1,fp);
|
2003-02-04 18:22:43 +00:00
|
|
|
wavhdr.file_length=wavhdr.data_length=0;
|
|
|
|
}
|
2001-06-12 14:24:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2004-09-18 20:31:28 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_PCM_CantOpenOutputFile,
|
|
|
|
ao_outputfilename);
|
2001-06-12 11:00:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2004-04-06 17:55:36 +00:00
|
|
|
static void uninit(int immed){
|
2001-06-12 14:24:26 +00:00
|
|
|
|
2002-06-22 12:10:02 +00:00
|
|
|
if(ao_pcm_waveheader && fseek(fp, 0, SEEK_SET) == 0){ /* Write wave header */
|
2002-03-02 18:54:58 +00:00
|
|
|
wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8;
|
2002-04-26 13:07:03 +00:00
|
|
|
wavhdr.file_length = le2me_32(wavhdr.file_length);
|
2002-07-21 12:13:40 +00:00
|
|
|
wavhdr.data_length = le2me_32(wavhdr.data_length);
|
2001-06-12 14:24:26 +00:00
|
|
|
fwrite(&wavhdr,sizeof(wavhdr),1,fp);
|
|
|
|
}
|
2001-06-12 11:00:15 +00:00
|
|
|
fclose(fp);
|
2005-01-02 14:41:47 +00:00
|
|
|
if (ao_outputfilename)
|
|
|
|
free(ao_outputfilename);
|
|
|
|
ao_outputfilename = NULL;
|
2001-06-12 11:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing and empty buffers (for seeking/pause)
|
2006-02-09 14:08:03 +00:00
|
|
|
static void reset(void){
|
2001-06-12 11:00:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing, keep buffers (for pause)
|
2006-02-09 14:08:03 +00:00
|
|
|
static void audio_pause(void)
|
2001-06-12 11:00:15 +00:00
|
|
|
{
|
|
|
|
// for now, just call reset();
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// resume playing, after audio_pause()
|
2006-02-09 14:08:03 +00:00
|
|
|
static void audio_resume(void)
|
2001-06-12 11:00:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// return: how many bytes can be played without blocking
|
2006-02-09 14:08:03 +00:00
|
|
|
static int get_space(void){
|
2001-06-12 11:00:15 +00:00
|
|
|
|
2002-04-26 13:07:03 +00:00
|
|
|
if(vo_pts)
|
2006-04-14 13:18:35 +00:00
|
|
|
return ao_data.pts < vo_pts + fast * 30000 ? ao_data.outburst : 0;
|
2002-04-26 13:07:03 +00:00
|
|
|
return ao_data.outburst;
|
2001-06-12 11:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// plays 'len' bytes of 'data'
|
|
|
|
// it should round it down to outburst*n
|
|
|
|
// return: number of bytes played
|
|
|
|
static int play(void* data,int len,int flags){
|
|
|
|
|
2002-10-07 19:51:00 +00:00
|
|
|
// let libaf to do the conversion...
|
|
|
|
#if 0
|
|
|
|
//#ifdef WORDS_BIGENDIAN
|
|
|
|
if (ao_data.format == AFMT_S16_LE) {
|
|
|
|
unsigned short *buffer = (unsigned short *) data;
|
|
|
|
register int i;
|
2002-04-26 13:07:03 +00:00
|
|
|
for(i = 0; i < len/2; ++i) {
|
|
|
|
buffer[i] = le2me_16(buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2002-10-07 19:51:00 +00:00
|
|
|
|
2001-06-12 11:00:15 +00:00
|
|
|
//printf("PCM: Writing chunk!\n");
|
|
|
|
fwrite(data,len,1,fp);
|
2002-04-26 13:07:03 +00:00
|
|
|
|
2001-06-12 14:24:26 +00:00
|
|
|
if(ao_pcm_waveheader)
|
|
|
|
wavhdr.data_length += len;
|
|
|
|
|
2001-06-12 11:00:15 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2001-11-24 05:21:22 +00:00
|
|
|
// return: delay in seconds between first and last sample in buffer
|
2006-02-09 14:08:03 +00:00
|
|
|
static float get_delay(void){
|
2001-06-12 11:00:15 +00:00
|
|
|
|
2001-11-24 05:21:22 +00:00
|
|
|
return 0.0;
|
2001-06-12 11:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|