Mercurial > mplayer.hg
annotate libao2/ao_pcm.c @ 14418:21af6e709348
synced to 1.75
author | gabrov |
---|---|
date | Fri, 07 Jan 2005 21:57:14 +0000 |
parents | 54e42c7eb713 |
children | de08cc60fd7e |
rev | line source |
---|---|
6237 | 1 #include "config.h" |
2 | |
1107 | 3 #include <stdio.h> |
4 #include <stdlib.h> | |
6237 | 5 #include <string.h> |
1107 | 6 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
7 #include "bswap.h" |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
8 #include "subopt-helper.h" |
14245 | 9 #include "libaf/af_format.h" |
1107 | 10 #include "audio_out.h" |
11 #include "audio_out_internal.h" | |
14123 | 12 #include "mp_msg.h" |
13 #include "help_mp.h" | |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
14 |
1107 | 15 |
16 static ao_info_t info = | |
17 { | |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
18 "RAW PCM/WAVE file writer audio output", |
1107 | 19 "pcm", |
20 "Atmosfear", | |
21 "" | |
22 }; | |
23 | |
24 LIBAO_EXTERN(pcm) | |
25 | |
4914
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
26 extern int vo_pts; |
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
27 |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
28 static char *ao_outputfilename = NULL; |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
29 static int ao_pcm_waveheader = 1; |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
30 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
31 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
32 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
33 #define WAV_ID_FMT 0x20746d66 /* "fmt " */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
34 #define WAV_ID_DATA 0x61746164 /* "data" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
35 #define WAV_ID_PCM 0x0001 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
36 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
37 struct WaveHeader |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
38 { |
11270 | 39 uint32_t riff; |
40 uint32_t file_length; | |
41 uint32_t wave; | |
42 uint32_t fmt; | |
43 uint32_t fmt_length; | |
44 uint16_t fmt_tag; | |
45 uint16_t channels; | |
46 uint32_t sample_rate; | |
47 uint32_t bytes_per_second; | |
48 uint16_t block_align; | |
49 uint16_t bits; | |
50 uint32_t data; | |
51 uint32_t data_length; | |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
52 }; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
53 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
54 /* init with default values */ |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
55 static struct WaveHeader wavhdr = { |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
56 le2me_32(WAV_ID_RIFF), |
9026 | 57 /* same conventions than in sox/wav.c/wavwritehdr() */ |
9223
b639f4560740
temporary 'inifinte' length disabled (commit r1.13 reversed) due to user
arpi
parents:
9156
diff
changeset
|
58 0, //le2me_32(0x7ffff024), |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
59 le2me_32(WAV_ID_WAVE), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
60 le2me_32(WAV_ID_FMT), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
61 le2me_32(16), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
62 le2me_16(WAV_ID_PCM), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
63 le2me_16(2), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
64 le2me_32(44100), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
65 le2me_32(192000), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
66 le2me_16(4), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
67 le2me_16(16), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
68 le2me_32(WAV_ID_DATA), |
9223
b639f4560740
temporary 'inifinte' length disabled (commit r1.13 reversed) due to user
arpi
parents:
9156
diff
changeset
|
69 0, //le2me_32(0x7ffff000) |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
70 }; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
71 |
1107 | 72 static FILE *fp = NULL; |
73 | |
74 // to set/get/query special features/parameters | |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
9277
diff
changeset
|
75 static int control(int cmd,void *arg){ |
1107 | 76 return -1; |
77 } | |
78 | |
79 // open & setup audio device | |
80 // return: 1=success 0=fail | |
81 static int init(int rate,int channels,int format,int flags){ | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
82 int bits; |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
83 strarg_t file; |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
84 opt_t subopts[] = { |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
85 {"waveheader", OPT_ARG_BOOL, &ao_pcm_waveheader, NULL}, |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
86 {"file", OPT_ARG_STR, &file, NULL}, |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
87 {NULL} |
14300 | 88 }; |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
89 // set defaults |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
90 ao_pcm_waveheader = 1; |
14327
54e42c7eb713
Default to audiodump.pcm with nowaveheader again, but document it in the manpage this time.
reimar
parents:
14301
diff
changeset
|
91 file.str = NULL; |
54e42c7eb713
Default to audiodump.pcm with nowaveheader again, but document it in the manpage this time.
reimar
parents:
14301
diff
changeset
|
92 file.len = 0; |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
93 if (subopt_parse(ao_subdevice, subopts) != 0) { |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
94 return 0; |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
95 } |
14327
54e42c7eb713
Default to audiodump.pcm with nowaveheader again, but document it in the manpage this time.
reimar
parents:
14301
diff
changeset
|
96 if (file.len > 0) { |
14301 | 97 ao_outputfilename = malloc(file.len + 1); |
98 memcpy(ao_outputfilename, file.str, file.len); | |
99 ao_outputfilename[file.len] = 0; | |
14327
54e42c7eb713
Default to audiodump.pcm with nowaveheader again, but document it in the manpage this time.
reimar
parents:
14301
diff
changeset
|
100 } |
54e42c7eb713
Default to audiodump.pcm with nowaveheader again, but document it in the manpage this time.
reimar
parents:
14301
diff
changeset
|
101 else |
54e42c7eb713
Default to audiodump.pcm with nowaveheader again, but document it in the manpage this time.
reimar
parents:
14301
diff
changeset
|
102 ao_outputfilename = |
54e42c7eb713
Default to audiodump.pcm with nowaveheader again, but document it in the manpage this time.
reimar
parents:
14301
diff
changeset
|
103 strdup((ao_pcm_waveheader)?"audiodump.wav":"audiodump.pcm"); |
7658 | 104 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
105 /* bits is only equal to format if (format == 8) or (format == 16); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
106 this means that the following "if" is a kludge and should |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
107 really be a switch to be correct in all cases */ |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
108 |
7658 | 109 bits=8; |
110 switch(format){ | |
14245 | 111 case AF_FORMAT_S8: |
112 format=AF_FORMAT_U8; | |
113 case AF_FORMAT_U8: | |
7658 | 114 break; |
115 default: | |
14245 | 116 format=AF_FORMAT_S16_LE; |
7658 | 117 bits=16; |
118 break; | |
119 } | |
120 | |
121 ao_data.outburst = 65536; | |
122 ao_data.buffersize= 2*65536; | |
123 ao_data.channels=channels; | |
124 ao_data.samplerate=rate; | |
125 ao_data.format=format; | |
126 ao_data.bps=channels*rate*(bits/8); | |
127 | |
128 wavhdr.channels = le2me_16(ao_data.channels); | |
129 wavhdr.sample_rate = le2me_32(ao_data.samplerate); | |
130 wavhdr.bytes_per_second = le2me_32(ao_data.bps); | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
131 wavhdr.bits = le2me_16(bits); |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
132 |
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
133 wavhdr.data_length=le2me_32(0x7ffff000); |
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
134 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8; |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
135 |
14264 | 136 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_FileInfo, ao_outputfilename, |
137 (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate, | |
138 (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format)); | |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
139 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_HintInfo); |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
140 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
141 fp = fopen(ao_outputfilename, "wb"); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
142 if(fp) { |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
143 if(ao_pcm_waveheader){ /* Reserve space for wave header */ |
6501 | 144 fwrite(&wavhdr,sizeof(wavhdr),1,fp); |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
145 wavhdr.file_length=wavhdr.data_length=0; |
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
146 } |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
147 return 1; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
148 } |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
149 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_PCM_CantOpenOutputFile, |
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
150 ao_outputfilename); |
1107 | 151 return 0; |
152 } | |
153 | |
154 // close audio device | |
12145 | 155 static void uninit(int immed){ |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
156 |
6501 | 157 if(ao_pcm_waveheader && fseek(fp, 0, SEEK_SET) == 0){ /* Write wave header */ |
4914
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
158 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8; |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
159 wavhdr.file_length = le2me_32(wavhdr.file_length); |
6762 | 160 wavhdr.data_length = le2me_32(wavhdr.data_length); |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
161 fwrite(&wavhdr,sizeof(wavhdr),1,fp); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
162 } |
1107 | 163 fclose(fp); |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
164 if (ao_outputfilename) |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
165 free(ao_outputfilename); |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
166 ao_outputfilename = NULL; |
1107 | 167 } |
168 | |
169 // stop playing and empty buffers (for seeking/pause) | |
170 static void reset(){ | |
171 | |
172 } | |
173 | |
174 // stop playing, keep buffers (for pause) | |
175 static void audio_pause() | |
176 { | |
177 // for now, just call reset(); | |
178 reset(); | |
179 } | |
180 | |
181 // resume playing, after audio_pause() | |
182 static void audio_resume() | |
183 { | |
184 } | |
185 | |
186 // return: how many bytes can be played without blocking | |
187 static int get_space(){ | |
188 | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
189 if(vo_pts) |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
190 return ao_data.pts < vo_pts ? ao_data.outburst : 0; |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
191 return ao_data.outburst; |
1107 | 192 } |
193 | |
194 // plays 'len' bytes of 'data' | |
195 // it should round it down to outburst*n | |
196 // return: number of bytes played | |
197 static int play(void* data,int len,int flags){ | |
198 | |
7658 | 199 // let libaf to do the conversion... |
200 #if 0 | |
201 //#ifdef WORDS_BIGENDIAN | |
202 if (ao_data.format == AFMT_S16_LE) { | |
203 unsigned short *buffer = (unsigned short *) data; | |
204 register int i; | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
205 for(i = 0; i < len/2; ++i) { |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
206 buffer[i] = le2me_16(buffer[i]); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
207 } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
208 } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
209 #endif |
7658 | 210 |
1107 | 211 //printf("PCM: Writing chunk!\n"); |
212 fwrite(data,len,1,fp); | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
213 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
214 if(ao_pcm_waveheader) |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
215 wavhdr.data_length += len; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
216 |
1107 | 217 return len; |
218 } | |
219 | |
3095 | 220 // return: delay in seconds between first and last sample in buffer |
221 static float get_delay(){ | |
1107 | 222 |
3095 | 223 return 0.0; |
1107 | 224 } |
225 | |
226 | |
227 | |
228 | |
229 | |
230 |