Mercurial > mplayer.hg
annotate libao2/ao_pcm.c @ 18125:08f997fbe16d
Simplify link removal.
author | diego |
---|---|
date | Mon, 17 Apr 2006 00:28:36 +0000 |
parents | 96fdfbad5b1a |
children | b23c0e341e3e |
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; |
18092
96fdfbad5b1a
-ao pcm:fast suboption for faster-than-realtime dumping
reimar
parents:
17566
diff
changeset
|
30 static int fast = 0; |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
31 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
32 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
33 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
34 #define WAV_ID_FMT 0x20746d66 /* "fmt " */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
35 #define WAV_ID_DATA 0x61746164 /* "data" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
36 #define WAV_ID_PCM 0x0001 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
37 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
38 struct WaveHeader |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
39 { |
11270 | 40 uint32_t riff; |
41 uint32_t file_length; | |
42 uint32_t wave; | |
43 uint32_t fmt; | |
44 uint32_t fmt_length; | |
45 uint16_t fmt_tag; | |
46 uint16_t channels; | |
47 uint32_t sample_rate; | |
48 uint32_t bytes_per_second; | |
49 uint16_t block_align; | |
50 uint16_t bits; | |
51 uint32_t data; | |
52 uint32_t data_length; | |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
53 }; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
54 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
55 /* init with default values */ |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
56 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
|
57 le2me_32(WAV_ID_RIFF), |
9026 | 58 /* 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 le2me_32(WAV_ID_DATA), |
9223
b639f4560740
temporary 'inifinte' length disabled (commit r1.13 reversed) due to user
arpi
parents:
9156
diff
changeset
|
70 0, //le2me_32(0x7ffff000) |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
71 }; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
72 |
1107 | 73 static FILE *fp = NULL; |
74 | |
75 // to set/get/query special features/parameters | |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
9277
diff
changeset
|
76 static int control(int cmd,void *arg){ |
1107 | 77 return -1; |
78 } | |
79 | |
80 // open & setup audio device | |
81 // return: 1=success 0=fail | |
82 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
|
83 int bits; |
14298
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}, |
14539 | 86 {"file", OPT_ARG_MSTRZ, &ao_outputfilename, NULL}, |
18092
96fdfbad5b1a
-ao pcm:fast suboption for faster-than-realtime dumping
reimar
parents:
17566
diff
changeset
|
87 {"fast", OPT_ARG_BOOL, &fast, NULL}, |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
88 {NULL} |
14300 | 89 }; |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
90 // set defaults |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
91 ao_pcm_waveheader = 1; |
14539 | 92 ao_outputfilename = |
93 strdup((ao_pcm_waveheader)?"audiodump.wav":"audiodump.pcm"); | |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
94 if (subopt_parse(ao_subdevice, subopts) != 0) { |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
95 return 0; |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
96 } |
7658 | 97 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
98 /* 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
|
99 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
|
100 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
|
101 |
7658 | 102 bits=8; |
103 switch(format){ | |
14245 | 104 case AF_FORMAT_S8: |
105 format=AF_FORMAT_U8; | |
106 case AF_FORMAT_U8: | |
7658 | 107 break; |
108 default: | |
14245 | 109 format=AF_FORMAT_S16_LE; |
7658 | 110 bits=16; |
111 break; | |
112 } | |
113 | |
114 ao_data.outburst = 65536; | |
115 ao_data.buffersize= 2*65536; | |
116 ao_data.channels=channels; | |
117 ao_data.samplerate=rate; | |
118 ao_data.format=format; | |
119 ao_data.bps=channels*rate*(bits/8); | |
120 | |
121 wavhdr.channels = le2me_16(ao_data.channels); | |
122 wavhdr.sample_rate = le2me_32(ao_data.samplerate); | |
123 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
|
124 wavhdr.bits = le2me_16(bits); |
16243
178b8b4a62c6
Set block_align in header, seems MatLab can not handle files without.
reimar
parents:
14539
diff
changeset
|
125 wavhdr.block_align = le2me_16(ao_data.channels * (bits / 8)); |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
126 |
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
127 wavhdr.data_length=le2me_32(0x7ffff000); |
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
128 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
|
129 |
14264 | 130 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_FileInfo, ao_outputfilename, |
131 (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate, | |
132 (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format)); | |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
133 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
|
134 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
135 fp = fopen(ao_outputfilename, "wb"); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
136 if(fp) { |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
137 if(ao_pcm_waveheader){ /* Reserve space for wave header */ |
6501 | 138 fwrite(&wavhdr,sizeof(wavhdr),1,fp); |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
139 wavhdr.file_length=wavhdr.data_length=0; |
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
140 } |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
141 return 1; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
142 } |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
143 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_PCM_CantOpenOutputFile, |
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
144 ao_outputfilename); |
1107 | 145 return 0; |
146 } | |
147 | |
148 // close audio device | |
12145 | 149 static void uninit(int immed){ |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
150 |
6501 | 151 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
|
152 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
|
153 wavhdr.file_length = le2me_32(wavhdr.file_length); |
6762 | 154 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
|
155 fwrite(&wavhdr,sizeof(wavhdr),1,fp); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
156 } |
1107 | 157 fclose(fp); |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
158 if (ao_outputfilename) |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
159 free(ao_outputfilename); |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
160 ao_outputfilename = NULL; |
1107 | 161 } |
162 | |
163 // stop playing and empty buffers (for seeking/pause) | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
164 static void reset(void){ |
1107 | 165 |
166 } | |
167 | |
168 // stop playing, keep buffers (for pause) | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
169 static void audio_pause(void) |
1107 | 170 { |
171 // for now, just call reset(); | |
172 reset(); | |
173 } | |
174 | |
175 // resume playing, after audio_pause() | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
176 static void audio_resume(void) |
1107 | 177 { |
178 } | |
179 | |
180 // return: how many bytes can be played without blocking | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
181 static int get_space(void){ |
1107 | 182 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
183 if(vo_pts) |
18092
96fdfbad5b1a
-ao pcm:fast suboption for faster-than-realtime dumping
reimar
parents:
17566
diff
changeset
|
184 return ao_data.pts < vo_pts + fast * 30000 ? ao_data.outburst : 0; |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
185 return ao_data.outburst; |
1107 | 186 } |
187 | |
188 // plays 'len' bytes of 'data' | |
189 // it should round it down to outburst*n | |
190 // return: number of bytes played | |
191 static int play(void* data,int len,int flags){ | |
192 | |
7658 | 193 // let libaf to do the conversion... |
194 #if 0 | |
195 //#ifdef WORDS_BIGENDIAN | |
196 if (ao_data.format == AFMT_S16_LE) { | |
197 unsigned short *buffer = (unsigned short *) data; | |
198 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
|
199 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
|
200 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
|
201 } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
202 } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
203 #endif |
7658 | 204 |
1107 | 205 //printf("PCM: Writing chunk!\n"); |
206 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
|
207 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
208 if(ao_pcm_waveheader) |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
209 wavhdr.data_length += len; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
210 |
1107 | 211 return len; |
212 } | |
213 | |
3095 | 214 // return: delay in seconds between first and last sample in buffer |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
215 static float get_delay(void){ |
1107 | 216 |
3095 | 217 return 0.0; |
1107 | 218 } |
219 | |
220 | |
221 | |
222 | |
223 | |
224 |