Mercurial > mplayer.hg
annotate libao2/ao_pcm.c @ 6379:48e3f3cc291a
FreeBSD Real support
author | nexus |
---|---|
date | Mon, 10 Jun 2002 16:19:46 +0000 |
parents | 2eec40929570 |
children | 827f08ddf044 |
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" |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
8 #include "afmt.h" |
1107 | 9 #include "audio_out.h" |
10 #include "audio_out_internal.h" | |
11 | |
12 static ao_info_t info = | |
13 { | |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
14 "RAW PCM/WAVE file writer audio output", |
1107 | 15 "pcm", |
16 "Atmosfear", | |
17 "" | |
18 }; | |
19 | |
20 LIBAO_EXTERN(pcm) | |
21 | |
4914
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
22 extern int vo_pts; |
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
23 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
24 char *ao_outputfilename = NULL; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
25 int ao_pcm_waveheader = 1; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
26 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
27 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
28 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
29 #define WAV_ID_FMT 0x20746d66 /* "fmt " */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
30 #define WAV_ID_DATA 0x61746164 /* "data" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
31 #define WAV_ID_PCM 0x0001 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
32 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
33 struct WaveHeader |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
34 { |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
35 unsigned long riff; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
36 unsigned long file_length; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
37 unsigned long wave; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
38 unsigned long fmt; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
39 unsigned long fmt_length; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
40 short fmt_tag; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
41 short channels; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
42 unsigned long sample_rate; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
43 unsigned long bytes_per_second; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
44 short block_align; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
45 short bits; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
46 unsigned long data; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
47 unsigned long data_length; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
48 }; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
49 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
50 /* init with default values */ |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
51 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
|
52 le2me_32(WAV_ID_RIFF), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
53 le2me_32(0x00000000), |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 le2me_32(WAV_ID_DATA), |
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(0x00000000) |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
65 }; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
66 |
1107 | 67 static FILE *fp = NULL; |
68 | |
69 // to set/get/query special features/parameters | |
70 static int control(int cmd,int arg){ | |
71 return -1; | |
72 } | |
73 | |
74 // open & setup audio device | |
75 // return: 1=success 0=fail | |
76 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
|
77 int bits; |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
78 if(!ao_outputfilename) { |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
79 ao_outputfilename = (char *) malloc(sizeof(char) * 14); |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
80 strcpy(ao_outputfilename, |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
81 (ao_pcm_waveheader ? "audiodump.wav" : "audiodump.pcm")); |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
82 } |
1107 | 83 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
84 /* 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
|
85 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
|
86 really be a switch to be correct in all cases */ |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
87 if (format == AFMT_S16_BE) { bits = 16; } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
88 else { bits = format; } |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
89 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
90 wavhdr.channels = le2me_16(channels); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
91 wavhdr.sample_rate = le2me_32(rate); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
92 wavhdr.bytes_per_second = rate * (bits / 8) * channels; |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
93 wavhdr.bytes_per_second = le2me_32(wavhdr.bytes_per_second); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
94 wavhdr.bits = le2me_16(bits); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
95 |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
96 printf("PCM: File: %s (%s)\n" |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
97 "PCM: Samplerate: %iHz Channels: %s Format %s\n", |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
98 ao_outputfilename, (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate, |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
99 (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format)); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
100 printf("PCM: Info: fastest dumping is achieved with -vo null " |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
101 "-hardframedrop.\n" |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
102 "PCM: Info: to write WAVE files use -waveheader (default); " |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
103 "for RAW PCM -nowaveheader.\n"); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
104 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
105 fp = fopen(ao_outputfilename, "wb"); |
1107 | 106 |
4914
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
107 ao_data.outburst = 65536; |
1107 | 108 |
109 | |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
110 if(fp) { |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
111 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
|
112 fseek(fp, sizeof(wavhdr), SEEK_SET); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
113 return 1; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
114 } |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
115 printf("PCM: Failed to open %s for writing!\n", ao_outputfilename); |
1107 | 116 return 0; |
117 } | |
118 | |
119 // close audio device | |
120 static void uninit(){ | |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
121 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
122 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
|
123 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
|
124 wavhdr.file_length = le2me_32(wavhdr.file_length); |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
125 fseek(fp, 0, SEEK_SET); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
126 fwrite(&wavhdr,sizeof(wavhdr),1,fp); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
127 } |
1107 | 128 fclose(fp); |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
129 free(ao_outputfilename); |
1107 | 130 } |
131 | |
132 // stop playing and empty buffers (for seeking/pause) | |
133 static void reset(){ | |
134 | |
135 } | |
136 | |
137 // stop playing, keep buffers (for pause) | |
138 static void audio_pause() | |
139 { | |
140 // for now, just call reset(); | |
141 reset(); | |
142 } | |
143 | |
144 // resume playing, after audio_pause() | |
145 static void audio_resume() | |
146 { | |
147 } | |
148 | |
149 // return: how many bytes can be played without blocking | |
150 static int get_space(){ | |
151 | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
152 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
|
153 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
|
154 return ao_data.outburst; |
1107 | 155 } |
156 | |
157 // plays 'len' bytes of 'data' | |
158 // it should round it down to outburst*n | |
159 // return: number of bytes played | |
160 static int play(void* data,int len,int flags){ | |
161 | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
162 #ifdef WORDS_BIGENDIAN |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
163 register int i; |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
164 unsigned short *buffer = (unsigned short *) data; |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
165 |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
166 if (wavhdr.bits == 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
|
167 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
|
168 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
|
169 } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
170 } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
171 /* FIXME: take care of cases with more than 8 bits here? */ |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
172 #endif |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
173 |
1107 | 174 //printf("PCM: Writing chunk!\n"); |
175 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
|
176 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
177 if(ao_pcm_waveheader) |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
178 wavhdr.data_length += len; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
179 |
1107 | 180 return len; |
181 } | |
182 | |
3095 | 183 // return: delay in seconds between first and last sample in buffer |
184 static float get_delay(){ | |
1107 | 185 |
3095 | 186 return 0.0; |
1107 | 187 } |
188 | |
189 | |
190 | |
191 | |
192 | |
193 |