annotate libao2/ao_sdl.c @ 4858:65730cea02e7

add half new input support for gui
author pontscho
date Mon, 25 Feb 2002 13:14:27 +0000
parents b9ee2d8d7279
children 47297acadbfb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
1 /*
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
2 * ao_sdl.c - libao2 SDLlib Audio Output Driver for MPlayer
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
3 *
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
4 * This driver is under the same license as MPlayer.
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
5 * (http://mplayer.sf.net)
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
6 *
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net)
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
8 *
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
9 * Thanks to Arpi for nice ringbuffer-code!
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
10 *
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
11 */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
12
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
13 #include <stdio.h>
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
14 #include <stdlib.h>
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
15
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
16 #include "audio_out.h"
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
17 #include "audio_out_internal.h"
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
18 #include "afmt.h"
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
19
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
20 #include "../libvo/fastmemcpy.h"
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
21
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
22 static ao_info_t info =
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
23 {
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
24 "SDLlib audio output",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
25 "sdl",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
26 "Felix Buenemann <atmosfear@users.sourceforge.net>",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
27 ""
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
28 };
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
29
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
30 LIBAO_EXTERN(sdl)
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
31
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
32
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
33 extern int verbose;
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
34
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
35 // Samplesize used by the SDLlib AudioSpec struct
993
dfa641c44d4a Increased sample size to solve sound problems one some systems.
atmosfear
parents: 985
diff changeset
36 #define SAMPLESIZE 1024
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
37
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
38 // General purpose Ring-buffering routines
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
39
974
0d4e4da7c126 -abs support
arpi_esp
parents: 972
diff changeset
40 #define BUFFSIZE 4096
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
41 #define NUM_BUFS 16
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
42
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
43 static unsigned char *buffer[NUM_BUFS];
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
44
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
45 static unsigned int buf_read=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
46 static unsigned int buf_write=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
47 static unsigned int buf_read_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
48 static unsigned int buf_write_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
49
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
50 static int full_buffers=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
51 static int buffered_bytes=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
52
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
53
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
54 static int write_buffer(unsigned char* data,int len){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
55 int len2=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
56 int x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
57 while(len>0){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
58 if(full_buffers==NUM_BUFS) break;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
59 x=BUFFSIZE-buf_write_pos;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
60 if(x>len) x=len;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
61 memcpy(buffer[buf_write]+buf_write_pos,data+len2,x);
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
62 len2+=x; len-=x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
63 buffered_bytes+=x; buf_write_pos+=x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
64 if(buf_write_pos>=BUFFSIZE){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
65 // block is full, find next!
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
66 buf_write=(buf_write+1)%NUM_BUFS;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
67 ++full_buffers;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
68 buf_write_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
69 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
70 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
71 return len2;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
72 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
73
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
74 static int read_buffer(unsigned char* data,int len){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
75 int len2=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
76 int x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
77 while(len>0){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
78 if(full_buffers==0) break; // no more data buffered!
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
79 x=BUFFSIZE-buf_read_pos;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
80 if(x>len) x=len;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
81 memcpy(data+len2,buffer[buf_read]+buf_read_pos,x);
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
82 len2+=x; len-=x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
83 buffered_bytes-=x; buf_read_pos+=x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
84 if(buf_read_pos>=BUFFSIZE){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
85 // block is empty, find next!
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
86 buf_read=(buf_read+1)%NUM_BUFS;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
87 --full_buffers;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
88 buf_read_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
89 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
90 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
91 return len2;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
92 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
93
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
94 // end ring buffer stuff
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
95
1238
e7d98f8f9459 Corrected SDL include path.
atmos4
parents: 1189
diff changeset
96 #include <SDL.h>
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 #if defined(sun) && defined(__svr4__)
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
99 /* setenv is missing on solaris */
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
100 static void setenv(const char *name, const char *val, int _xx)
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
101 {
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
102 int len = strlen(name) + strlen(val) + 2;
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
103 char *env = malloc(len);
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
104
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
105 if (env != NULL) {
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
106 strcpy(env, name);
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
107 strcat(env, "=");
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
108 strcat(env, val);
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
109 putenv(env);
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
110 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
111 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
112 #endif
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
113
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
114
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
115 // to set/get/query special features/parameters
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
116 static int control(int cmd,int arg){
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
117 return -1;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
118 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
119
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
120 // SDL Callback function
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
121 void outputaudio(void *unused, Uint8 *stream, int len) {
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
122 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
123 //if(!full_buffers) printf("SDL: Buffer underrun!\n");
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
124
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
125 read_buffer(stream, len);
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
126 //printf("SDL: Full Buffers: %i\n", full_buffers);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
127 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
128
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
129 // open & setup audio device
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
130 // return: 1=success 0=fail
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
131 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
132
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
133 /* SDL Audio Specifications */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
134 SDL_AudioSpec aspec;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
135
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
136 int i;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
137 /* Allocate ring-buffer memory */
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
138 for(i=0;i<NUM_BUFS;i++) buffer[i]=(unsigned char *) malloc(BUFFSIZE);
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
139
1070
e45a029db4c3 Better format info.
atmosfear
parents: 1066
diff changeset
140 printf("SDL: Samplerate: %iHz Channels: %s Format %s\n", rate, (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format));
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
141
1189
7c6bcb281966 sdl_adriver liquidated, ao_subdevice used
al3x
parents: 1091
diff changeset
142 if(ao_subdevice) {
7c6bcb281966 sdl_adriver liquidated, ao_subdevice used
al3x
parents: 1091
diff changeset
143 setenv("SDL_AUDIODRIVER", ao_subdevice, 1);
7c6bcb281966 sdl_adriver liquidated, ao_subdevice used
al3x
parents: 1091
diff changeset
144 printf("SDL: using %s audio driver\n", ao_subdevice);
7c6bcb281966 sdl_adriver liquidated, ao_subdevice used
al3x
parents: 1091
diff changeset
145 }
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
146
3137
b9ee2d8d7279 Audio delay bugfix
anders
parents: 3095
diff changeset
147 ao_data.bps=channels*rate;
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
148 if(format != AFMT_U8 && format != AFMT_S8)
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
149 ao_data.bps*=2;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
150
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
151 /* The desired audio format (see SDL_AudioSpec) */
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
152 switch(format) {
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
153 case AFMT_U8:
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
154 aspec.format = AUDIO_U8;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
155 break;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
156 case AFMT_S16_LE:
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
157 aspec.format = AUDIO_S16LSB;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
158 break;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
159 case AFMT_S16_BE:
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
160 aspec.format = AUDIO_S16MSB;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
161 break;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
162 case AFMT_S8:
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
163 aspec.format = AUDIO_S8;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
164 break;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
165 case AFMT_U16_LE:
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
166 aspec.format = AUDIO_U16LSB;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
167 break;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
168 case AFMT_U16_BE:
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
169 aspec.format = AUDIO_U16MSB;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
170 break;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
171 default:
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
172 printf("SDL: Unsupported audio format: 0x%x.\n", format);
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
173 return 0;
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
174 }
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
175
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
176 /* The desired audio frequency in samples-per-second. */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
177 aspec.freq = rate;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
178
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
179 /* Number of channels (mono/stereo) */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
180 aspec.channels = channels;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
181
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
182 /* 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
183 aspec.samples = SAMPLESIZE;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
184
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
185 /* 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
186 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
187 aspec.callback = outputaudio;
966
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 /* 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
190 aspec.userdata = NULL;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
191
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
192 /* initialize the SDL Audio system */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
193 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) {
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
194 printf("SDL: Initializing of SDL Audio failed: %s.\n", SDL_GetError());
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
195 return 0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
196 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
197
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
198 /* Open the audio device and start playing sound! */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
199 if(SDL_OpenAudio(&aspec, NULL) < 0) {
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
200 printf("SDL: Unable to open audio: %s\n", SDL_GetError());
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
201 return(0);
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
202 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
203
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
204 if(verbose) printf("SDL: buf size = %d\n",aspec.size);
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
205 if(ao_data.buffersize==-1) ao_data.buffersize=aspec.size;
974
0d4e4da7c126 -abs support
arpi_esp
parents: 972
diff changeset
206
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
207 /* unsilence audio, if callback is ready */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
208 SDL_PauseAudio(0);
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
209
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
210 return 1;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
211 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
212
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
213 // close audio device
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
214 static void uninit(){
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
215 if(verbose) printf("SDL: Audio Subsystem shutting down!\n");
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
216 SDL_CloseAudio();
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
217 SDL_QuitSubSystem(SDL_INIT_AUDIO);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
218 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
219
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
220 // stop playing and empty buffers (for seeking/pause)
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
221 static void reset(){
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
222
1091
06fd776f6b59 Lock callback on pause, unlock on resume.
atmosfear
parents: 1070
diff changeset
223 //printf("SDL: reset called!\n");
1066
aecab161d8d6 Better audio format handling.
atmosfear
parents: 1038
diff changeset
224
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
225 /* Reset ring-buffer state */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
226 buf_read=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
227 buf_write=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
228 buf_read_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
229 buf_write_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
230
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
231 full_buffers=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
232 buffered_bytes=0;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
233
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
234 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
235
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
236 // stop playing, keep buffers (for pause)
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
237 static void audio_pause()
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
238 {
1070
e45a029db4c3 Better format info.
atmosfear
parents: 1066
diff changeset
239
1091
06fd776f6b59 Lock callback on pause, unlock on resume.
atmosfear
parents: 1070
diff changeset
240 //printf("SDL: audio_pause called!\n");
06fd776f6b59 Lock callback on pause, unlock on resume.
atmosfear
parents: 1070
diff changeset
241 SDL_LockAudio();
1070
e45a029db4c3 Better format info.
atmosfear
parents: 1066
diff changeset
242
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
243 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
244
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
245 // resume playing, after audio_pause()
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
246 static void audio_resume()
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
247 {
1091
06fd776f6b59 Lock callback on pause, unlock on resume.
atmosfear
parents: 1070
diff changeset
248 //printf("SDL: audio_resume called!\n");
06fd776f6b59 Lock callback on pause, unlock on resume.
atmosfear
parents: 1070
diff changeset
249 SDL_UnlockAudio();
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
250 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
251
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
252
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
253 // return: how many bytes can be played without blocking
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
254 static int get_space(){
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
255 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
256 }
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 // plays 'len' bytes of 'data'
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
259 // it should round it down to outburst*n
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
260 // return: number of bytes played
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
261 static int play(void* data,int len,int flags){
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
262
985
95391cf9e994 Microchange =)
atmosfear
parents: 983
diff changeset
263 #if 0
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
264 int ret;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
265
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
266 /* Audio locking prohibits call of outputaudio */
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
267 SDL_LockAudio();
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
268 // copy audio stream into ring-buffer
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
269 ret = write_buffer(data, len);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
270 SDL_UnlockAudio();
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
271
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
272 return ret;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
273 #else
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
274 return write_buffer(data, len);
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
275 #endif
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
276 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
277
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
278 // return: delay in seconds between first and last sample in buffer
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
279 static float get_delay(){
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1238
diff changeset
280 return (float)(buffered_bytes + ao_data.buffersize)/(float)ao_data.bps;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
281 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
282
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
283
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
284
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
285
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
286
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
287