annotate libao2/ao_sdl.c @ 33082:8b561660a6f7

Cosmetic: Use more appropriate variable names. The new names make code more intelligible.
author ib
date Thu, 31 Mar 2011 11:38:30 +0000
parents 32725ca88fed
children 0bce77ccae9a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27509
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
1 /*
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
2 * SDLlib audio output driver for MPlayer
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
3 *
23734
acfe034e5386 ISO8859-1 --> UTF-8
diego
parents: 23457
diff changeset
4 * Copyleft 2001 by Felix BĆ¼nemann (atmosfear@users.sf.net)
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
5 *
27509
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
6 * This file is part of MPlayer.
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
7 *
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
8 * MPlayer is free software; you can redistribute it and/or modify
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
9 * it under the terms of the GNU General Public License as published by
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
11 * (at your option) any later version.
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
12 *
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
13 * MPlayer is distributed in the hope that it will be useful,
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
16 * GNU General Public License for more details.
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
17 *
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
18 * You should have received a copy of the GNU General Public License along
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
19 * along with MPlayer; if not, write to the Free Software
d97a607821f1 Replace casual GPL notices by proper license headers.
diego
parents: 26757
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
21 */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
22
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
23 #include <stdio.h>
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
24 #include <stdlib.h>
12093
f54d02f6ddbf let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents: 12027
diff changeset
25 #include <string.h>
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
26
14123
a92101a7eb49 Make include paths consistent.
diego
parents: 13887
diff changeset
27 #include "config.h"
a92101a7eb49 Make include paths consistent.
diego
parents: 13887
diff changeset
28 #include "mp_msg.h"
a92101a7eb49 Make include paths consistent.
diego
parents: 13887
diff changeset
29 #include "help_mp.h"
8027
b9da278e4c92 verbose can be negative
arpi
parents: 7908
diff changeset
30
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
31 #include "audio_out.h"
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
32 #include "audio_out_internal.h"
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
33 #include "libaf/af_format.h"
30136
dc1d66a20ec2 Fixup SDL configure check:
reimar
parents: 29439
diff changeset
34 #ifdef CONFIG_SDL_SDL_H
dc1d66a20ec2 Fixup SDL configure check:
reimar
parents: 29439
diff changeset
35 #include <SDL/SDL.h>
dc1d66a20ec2 Fixup SDL configure check:
reimar
parents: 29439
diff changeset
36 #else
6184
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
37 #include <SDL.h>
30136
dc1d66a20ec2 Fixup SDL configure check:
reimar
parents: 29439
diff changeset
38 #endif
12093
f54d02f6ddbf let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents: 12027
diff changeset
39 #include "osdep/timer.h"
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
40
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
41 #include "libavutil/fifo.h"
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
42
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
43 static const ao_info_t info =
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
44 {
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
45 "SDLlib audio output",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
46 "sdl",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
47 "Felix Buenemann <atmosfear@users.sourceforge.net>",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
48 ""
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
49 };
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
50
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
51 LIBAO_EXTERN(sdl)
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
52
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
53 // turn this on if you want to use the slower SDL_MixAudio
12908
7b9b4f07d2c4 automatic loading of af_volume, original patch by Dan Christiansen (danchr (at) daimi (dot) au (dot) dk)
reimar
parents: 12714
diff changeset
54 #undef USE_SDL_INTERNAL_MIXER
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
55
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
56 // Samplesize used by the SDLlib AudioSpec struct
27727
48c1ae64255b Replace preprocessor check for WIN32 with checks for __MINGW32__ and __CYGWIN__.
diego
parents: 27509
diff changeset
57 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__AMIGAOS4__)
12019
6ede5366bc47 fix compilation with sdl on mingw patch by Nehal <nehalmistry at gmx.net>
faust3
parents: 11750
diff changeset
58 #define SAMPLESIZE 2048
12113
07d9c8fa6897 restore old SAMPLESIZE
faust3
parents: 12093
diff changeset
59 #else
07d9c8fa6897 restore old SAMPLESIZE
faust3
parents: 12093
diff changeset
60 #define SAMPLESIZE 1024
07d9c8fa6897 restore old SAMPLESIZE
faust3
parents: 12093
diff changeset
61 #endif
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
62
13832
2f8cfe66dbfd Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents: 13383
diff changeset
63 #define CHUNK_SIZE 4096
2f8cfe66dbfd Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents: 13383
diff changeset
64 #define NUM_CHUNKS 8
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
65 #define BUFFSIZE (NUM_CHUNKS * CHUNK_SIZE)
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
66
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
67 static AVFifoBuffer *buffer;
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
68
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
69 #ifdef USE_SDL_INTERNAL_MIXER
12093
f54d02f6ddbf let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents: 12027
diff changeset
70 static unsigned char volume=SDL_MIX_MAXVOLUME;
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
71 #endif
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
72
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
73 static int write_buffer(unsigned char* data,int len){
29126
f951680cfea2 Simplify: use av_fifo_space
reimar
parents: 28882
diff changeset
74 int free = av_fifo_space(buffer);
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
75 if (len > free) len = free;
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
76 return av_fifo_generic_write(buffer, data, len, NULL);
13832
2f8cfe66dbfd Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents: 13383
diff changeset
77 }
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
78
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
79 #ifdef USE_SDL_INTERNAL_MIXER
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
80 static void mix_audio(void *dst, void *src, int len) {
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
81 SDL_MixAudio(dst, src, len, volume);
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
82 }
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
83 #endif
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
84
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
85 static int read_buffer(unsigned char* data,int len){
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
86 int buffered = av_fifo_size(buffer);
13832
2f8cfe66dbfd Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents: 13383
diff changeset
87 if (len > buffered) len = buffered;
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
88 #ifdef USE_SDL_INTERNAL_MIXER
29439
02dec439f717 100l, av_fifo_generic_read does not return anything useful, so ignore its
reimar
parents: 29263
diff changeset
89 av_fifo_generic_read(buffer, data, len, mix_audio);
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
90 #else
29439
02dec439f717 100l, av_fifo_generic_read does not return anything useful, so ignore its
reimar
parents: 29263
diff changeset
91 av_fifo_generic_read(buffer, data, len, NULL);
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
92 #endif
29439
02dec439f717 100l, av_fifo_generic_read does not return anything useful, so ignore its
reimar
parents: 29263
diff changeset
93 return len;
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
94 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
95
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
96 // end ring buffer stuff
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
97
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
98
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
99 // to set/get/query special features/parameters
9633
12b1790038b0 64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents: 8027
diff changeset
100 static int control(int cmd,void *arg){
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
101 #ifdef USE_SDL_INTERNAL_MIXER
6184
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
102 switch (cmd) {
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
103 case AOCONTROL_GET_VOLUME:
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
104 {
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
105 ao_control_vol_t* vol = (ao_control_vol_t*)arg;
12093
f54d02f6ddbf let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents: 12027
diff changeset
106 vol->left = vol->right = volume * 100 / SDL_MIX_MAXVOLUME;
6184
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
107 return CONTROL_OK;
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
108 }
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
109 case AOCONTROL_SET_VOLUME:
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
110 {
12093
f54d02f6ddbf let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents: 12027
diff changeset
111 int diff;
6184
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
112 ao_control_vol_t* vol = (ao_control_vol_t*)arg;
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
113 diff = (vol->left+vol->right) / 2;
12093
f54d02f6ddbf let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents: 12027
diff changeset
114 volume = diff * SDL_MIX_MAXVOLUME / 100;
6184
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
115 return CONTROL_OK;
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
116 }
47297acadbfb Add volume control, patch by
atmos4
parents: 3137
diff changeset
117 }
12673
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
118 #endif
c1371fce7267 make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents: 12440
diff changeset
119 return CONTROL_UNKNOWN;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
120 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
121
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
122 // SDL Callback function
30590
d218228dc74d Mark some more functions that are not used outside of their files as static.
diego
parents: 30136
diff changeset
123 static void outputaudio(void *unused, Uint8 *stream, int len)
d218228dc74d Mark some more functions that are not used outside of their files as static.
diego
parents: 30136
diff changeset
124 {
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
125 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
126 //if(!full_buffers) printf("SDL: Buffer underrun!\n");
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
127
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
128 read_buffer(stream, len);
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
129 //printf("SDL: Full Buffers: %i\n", full_buffers);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
130 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
131
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
132 // open & setup audio device
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
133 // return: 1=success 0=fail
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
134 static int init(int rate,int channels,int format,int flags){
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
135
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
136 /* SDL Audio Specifications */
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
137 SDL_AudioSpec aspec, obtained;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
138
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
139 /* Allocate ring-buffer memory */
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
140 buffer = av_fifo_alloc(BUFFSIZE);
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
141
14264
cb5fbade8a5c af_fmt2str_short
alex
parents: 14245
diff changeset
142 mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_SDL_INFO, rate, (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format));
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
143
1189
7c6bcb281966 sdl_adriver liquidated, ao_subdevice used
al3x
parents: 1091
diff changeset
144 if(ao_subdevice) {
7c6bcb281966 sdl_adriver liquidated, ao_subdevice used
al3x
parents: 1091
diff changeset
145 setenv("SDL_AUDIODRIVER", ao_subdevice, 1);
13383
c1955840883d mp_msg transition of unmaintained audio output drivers.
ivo
parents: 12908
diff changeset
146 mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_SDL_DriverInfo, ao_subdevice);
1189
7c6bcb281966 sdl_adriver liquidated, ao_subdevice used
al3x
parents: 1091
diff changeset
147 }
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
148
7660
7ff053498bf2 fill ao_data correctly
arpi
parents: 6956
diff changeset
149 ao_data.channels=channels;
7ff053498bf2 fill ao_data correctly
arpi
parents: 6956
diff changeset
150 ao_data.samplerate=rate;
7ff053498bf2 fill ao_data correctly
arpi
parents: 6956
diff changeset
151 ao_data.format=format;
7ff053498bf2 fill ao_data correctly
arpi
parents: 6956
diff changeset
152
3137
b9ee2d8d7279 Audio delay bugfix
anders
parents: 3095
diff changeset
153 ao_data.bps=channels*rate;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
154 if(format != AF_FORMAT_U8 && format != AF_FORMAT_S8)
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
155 ao_data.bps*=2;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
156
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
157 /* The desired audio format (see SDL_AudioSpec) */
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
158 switch(format) {
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
159 case AF_FORMAT_U8:
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
160 aspec.format = AUDIO_U8;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
161 break;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
162 case AF_FORMAT_S16_LE:
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
163 aspec.format = AUDIO_S16LSB;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
164 break;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
165 case AF_FORMAT_S16_BE:
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
166 aspec.format = AUDIO_S16MSB;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
167 break;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
168 case AF_FORMAT_S8:
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
169 aspec.format = AUDIO_S8;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
170 break;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
171 case AF_FORMAT_U16_LE:
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
172 aspec.format = AUDIO_U16LSB;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
173 break;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
174 case AF_FORMAT_U16_BE:
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
175 aspec.format = AUDIO_U16MSB;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
176 break;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
177 default:
12440
56bdb9b7a4bc use fallback for unsupported formats instead of quitting
reimar
parents: 12151
diff changeset
178 aspec.format = AUDIO_S16LSB;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
179 ao_data.format = AF_FORMAT_S16_LE;
13383
c1955840883d mp_msg transition of unmaintained audio output drivers.
ivo
parents: 12908
diff changeset
180 mp_msg(MSGT_AO,MSGL_WARN,MSGTR_AO_SDL_UnsupportedAudioFmt, format);
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
181 }
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
182
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
183 /* The desired audio frequency in samples-per-second. */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
184 aspec.freq = rate;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
185
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
186 /* Number of channels (mono/stereo) */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
187 aspec.channels = channels;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
188
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
189 /* The desired size of the audio buffer in samples. This number should be a power of two, and may be adjusted by the audio driver to a value more suitable for the hardware. Good values seem to range between 512 and 8192 inclusive, depending on the application and CPU speed. Smaller values yield faster response time, but can lead to underflow if the application is doing heavy processing and cannot fill the audio buffer in time. A stereo sample consists of both right and left channels in LR ordering. Note that the number of samples is directly related to time by the following formula: ms = (samples*1000)/freq */
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
190 aspec.samples = SAMPLESIZE;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
191
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
192 /* This should be set to a function that will be called when the audio device is ready for more data. It is passed a pointer to the audio buffer, and the length in bytes of the audio buffer. This function usually runs in a separate thread, and so you should protect data structures that it accesses by calling SDL_LockAudio and SDL_UnlockAudio in your code. The callback prototype is:
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
193 void callback(void *userdata, Uint8 *stream, int len); userdata is the pointer stored in userdata field of the SDL_AudioSpec. stream is a pointer to the audio buffer you want to fill with information and len is the length of the audio buffer in bytes. */
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
194 aspec.callback = outputaudio;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
195
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
196 /* This pointer is passed as the first parameter to the callback function. */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
197 aspec.userdata = NULL;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
198
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
199 /* initialize the SDL Audio system */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
200 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) {
13383
c1955840883d mp_msg transition of unmaintained audio output drivers.
ivo
parents: 12908
diff changeset
201 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_SDL_CantInit, SDL_GetError());
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
202 return 0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
203 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
204
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
205 /* Open the audio device and start playing sound! */
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
206 if(SDL_OpenAudio(&aspec, &obtained) < 0) {
13383
c1955840883d mp_msg transition of unmaintained audio output drivers.
ivo
parents: 12908
diff changeset
207 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_SDL_CantOpenAudio, SDL_GetError());
26757
0fdf04b07ecb cosmetics: Remove pointless parentheses from return statements.
diego
parents: 25335
diff changeset
208 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
209 }
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
210
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
211 /* did we got what we wanted ? */
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
212 ao_data.channels=obtained.channels;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
213 ao_data.samplerate=obtained.freq;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
214
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
215 switch(obtained.format) {
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
216 case AUDIO_U8 :
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
217 ao_data.format = AF_FORMAT_U8;
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
218 break;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
219 case AUDIO_S16LSB :
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
220 ao_data.format = AF_FORMAT_S16_LE;
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
221 break;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
222 case AUDIO_S16MSB :
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
223 ao_data.format = AF_FORMAT_S16_BE;
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
224 break;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
225 case AUDIO_S8 :
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
226 ao_data.format = AF_FORMAT_S8;
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
227 break;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
228 case AUDIO_U16LSB :
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
229 ao_data.format = AF_FORMAT_U16_LE;
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
230 break;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
231 case AUDIO_U16MSB :
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14123
diff changeset
232 ao_data.format = AF_FORMAT_U16_BE;
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
233 break;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
234 default:
13383
c1955840883d mp_msg transition of unmaintained audio output drivers.
ivo
parents: 12908
diff changeset
235 mp_msg(MSGT_AO,MSGL_WARN,MSGTR_AO_SDL_UnsupportedAudioFmt, obtained.format);
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
236 return 0;
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
237 }
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
238
8027
b9da278e4c92 verbose can be negative
arpi
parents: 7908
diff changeset
239 mp_msg(MSGT_AO,MSGL_V,"SDL: buf size = %d\n",obtained.size);
7908
dc96a3eb9fab Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents: 7897
diff changeset
240 ao_data.buffersize=obtained.size;
13887
48ce49693b3c respect immed uninit flag, initialize ao_data.outburst.
reimar
parents: 13832
diff changeset
241 ao_data.outburst = CHUNK_SIZE;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
242
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
243 /* unsilence audio, if callback is ready */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
244 SDL_PauseAudio(0);
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
245
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
246 return 1;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
247 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
248
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
249 // close audio device
12145
99798c3cdb93 uninit immed flag
alex
parents: 12113
diff changeset
250 static void uninit(int immed){
8027
b9da278e4c92 verbose can be negative
arpi
parents: 7908
diff changeset
251 mp_msg(MSGT_AO,MSGL_V,"SDL: Audio Subsystem shutting down!\n");
13887
48ce49693b3c respect immed uninit flag, initialize ao_data.outburst.
reimar
parents: 13832
diff changeset
252 if (!immed)
14849
d313f591d1a4 aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents: 14264
diff changeset
253 usec_sleep(get_delay() * 1000 * 1000);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
254 SDL_CloseAudio();
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
255 SDL_QuitSubSystem(SDL_INIT_AUDIO);
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
256 av_fifo_free(buffer);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
257 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
258
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
259 // stop playing and empty buffers (for seeking/pause)
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 17245
diff changeset
260 static void reset(void){
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
261
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
262 //printf("SDL: reset called!\n");
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
263
13832
2f8cfe66dbfd Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents: 13383
diff changeset
264 SDL_PauseAudio(1);
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
265 /* Reset ring-buffer state */
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
266 av_fifo_reset(buffer);
13832
2f8cfe66dbfd Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents: 13383
diff changeset
267 SDL_PauseAudio(0);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
268 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
269
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
270 // stop playing, keep buffers (for pause)
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 17245
diff changeset
271 static void audio_pause(void)
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
272 {
1070
e45a029db4c3 Better format info.
atmosfear
parents: 1066
diff changeset
273
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
274 //printf("SDL: audio_pause called!\n");
7897
7674e94baff7 Change SDL_(Un)lockAudio to PauseAudio() (works better)
colin
parents: 7660
diff changeset
275 SDL_PauseAudio(1);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
276
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
277 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
278
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
279 // resume playing, after audio_pause()
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 17245
diff changeset
280 static void audio_resume(void)
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
281 {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
282 //printf("SDL: audio_resume called!\n");
7897
7674e94baff7 Change SDL_(Un)lockAudio to PauseAudio() (works better)
colin
parents: 7660
diff changeset
283 SDL_PauseAudio(0);
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
284 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
285
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
286
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
287 // return: how many bytes can be played without blocking
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 17245
diff changeset
288 static int get_space(void){
29126
f951680cfea2 Simplify: use av_fifo_space
reimar
parents: 28882
diff changeset
289 return av_fifo_space(buffer);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
290 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
291
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
292 // plays 'len' bytes of 'data'
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
293 // it should round it down to outburst*n
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
294 // return: number of bytes played
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
295 static int play(void* data,int len,int flags){
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
296
22104
5ecca25377e0 Support for AOPLAY_FINAL_CHUNK in ao_sdl.c
reimar
parents: 21733
diff changeset
297 if (!(flags & AOPLAY_FINAL_CHUNK))
12151
75fdb659f5bf round len to outburst and increment full_buffers at the correct time, patch by Nehal <nehalmistry at gmx.net>
faust3
parents: 12145
diff changeset
298 len = (len/ao_data.outburst)*ao_data.outburst;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
299 #if 0
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
300 int ret;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
301
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
302 /* Audio locking prohibits call of outputaudio */
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
303 SDL_LockAudio();
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29126
diff changeset
304 // copy audio stream into ring-buffer
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
305 ret = write_buffer(data, len);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
306 SDL_UnlockAudio();
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
307
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
308 return ret;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
309 #else
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
310 return write_buffer(data, len);
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
311 #endif
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
312 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
313
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
314 // return: delay in seconds between first and last sample in buffer
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 17245
diff changeset
315 static float get_delay(void){
28882
15f93fd5cd48 Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents: 28823
diff changeset
316 int buffered = av_fifo_size(buffer); // could be less
13832
2f8cfe66dbfd Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents: 13383
diff changeset
317 return (float)(buffered + ao_data.buffersize)/(float)ao_data.bps;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
318 }