annotate libao2/ao_pcm.c @ 5205:47259a4f3216

show sub/av delay at osdlevel=1 - patch by Marcin Juszkiewicz <marcinj@netbox.pl>
author arpi
date Tue, 19 Mar 2002 19:22:00 +0000
parents de4074ab4e5f
children 7e082f42497a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
1 #include <stdio.h>
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
2 #include <stdlib.h>
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
3
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
4 #include "audio_out.h"
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
5 #include "audio_out_internal.h"
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
6
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
7 static ao_info_t info =
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
8 {
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
9 "RAW PCM/WAVE file writer audio output",
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
10 "pcm",
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
11 "Atmosfear",
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
12 ""
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
13 };
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
14
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
15 LIBAO_EXTERN(pcm)
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
16
4914
de4074ab4e5f good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents: 3095
diff changeset
17 extern int vo_pts;
de4074ab4e5f good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents: 3095
diff changeset
18
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
19 char *ao_outputfilename = NULL;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
20 int ao_pcm_waveheader = 1;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
21
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
22 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
23 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
24 #define WAV_ID_FMT 0x20746d66 /* "fmt " */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
25 #define WAV_ID_DATA 0x61746164 /* "data" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
26 #define WAV_ID_PCM 0x0001
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
27
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
28 struct WaveHeader
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
29 {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
30 unsigned long riff;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
31 unsigned long file_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
32 unsigned long wave;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
33 unsigned long fmt;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
34 unsigned long fmt_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
35 short fmt_tag;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
36 short channels;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
37 unsigned long sample_rate;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
38 unsigned long bytes_per_second;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
39 short block_align;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
40 short bits;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
41 unsigned long data;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
42 unsigned long data_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
43 };
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
44
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
45
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
46 static struct WaveHeader wavhdr = {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
47 WAV_ID_RIFF,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
48 0x00000000,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
49 WAV_ID_WAVE,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
50 WAV_ID_FMT,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
51 16,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
52 WAV_ID_PCM,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
53 2,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
54 44100,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
55 192000,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
56 4,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
57 16,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
58 WAV_ID_DATA,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
59 0x00000000
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
60 };
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
61
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
62 static FILE *fp = NULL;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
63
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
64 // to set/get/query special features/parameters
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
65 static int control(int cmd,int arg){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
66 return -1;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
67 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
68
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
69 // open & setup audio device
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
70 // return: 1=success 0=fail
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
71 static int init(int rate,int channels,int format,int flags){
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
72 if(!ao_outputfilename) {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
73 ao_outputfilename = (char *) malloc(sizeof(char) * 14);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
74 strcpy(ao_outputfilename,(ao_pcm_waveheader ? "audiodump.wav" : "audiodump.pcm"));
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
75 }
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
76
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
77 wavhdr.channels = channels;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
78 wavhdr.sample_rate = rate;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
79 wavhdr.bytes_per_second = rate * (format / 8) * channels;
1113
atmosfear
parents: 1112
diff changeset
80 wavhdr.bits = format;
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
81
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
82 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));
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
83 printf("PCM: Info - fastest dumping is achieved with -vo null -hardframedrop.\n");
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
84 printf("PCM: Info - to write WAVE files use -waveheader (default), for RAW PCM -nowaveheader.\n");
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
85 fp = fopen(ao_outputfilename, "wb");
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
86
4914
de4074ab4e5f good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents: 3095
diff changeset
87 ao_data.outburst = 65536;
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
88
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
89
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
90 if(fp) {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
91 if(ao_pcm_waveheader) /* Reserve space for wave header */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
92 fseek(fp, sizeof(wavhdr), SEEK_SET);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
93 return 1;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
94 }
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
95 printf("PCM: Failed to open %s for writing!\n", ao_outputfilename);
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
96 return 0;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
97 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
98
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
99 // close audio device
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
100 static void uninit(){
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
101
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
102 if(ao_pcm_waveheader){ /* Write wave header */
4914
de4074ab4e5f good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents: 3095
diff changeset
103 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8;
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
104 fseek(fp, 0, SEEK_SET);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
105 fwrite(&wavhdr,sizeof(wavhdr),1,fp);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
106 }
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
107 fclose(fp);
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
108 free(ao_outputfilename);
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
109 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
110
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
111 // stop playing and empty buffers (for seeking/pause)
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
112 static void reset(){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
113
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
114 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
115
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
116 // stop playing, keep buffers (for pause)
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
117 static void audio_pause()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
118 {
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
119 // for now, just call reset();
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
120 reset();
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
121 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
122
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
123 // resume playing, after audio_pause()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
124 static void audio_resume()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
125 {
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
126 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
127
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
128 // return: how many bytes can be played without blocking
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
129 static int get_space(){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
130
4914
de4074ab4e5f good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents: 3095
diff changeset
131 return ao_data.pts < vo_pts ? ao_data.outburst : 0;
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
132 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
133
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
134 // plays 'len' bytes of 'data'
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
135 // it should round it down to outburst*n
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
136 // return: number of bytes played
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
137 static int play(void* data,int len,int flags){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
138
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
139 //printf("PCM: Writing chunk!\n");
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
140 fwrite(data,len,1,fp);
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
141
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
142 if(ao_pcm_waveheader)
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
143 wavhdr.data_length += len;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
144
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
145 return len;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
146 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
147
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1113
diff changeset
148 // return: delay in seconds between first and last sample in buffer
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1113
diff changeset
149 static float get_delay(){
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
150
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1113
diff changeset
151 return 0.0;
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
152 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
153
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
154
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
155
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
156
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
157
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
158