annotate libao2/ao_sdl.c @ 1056:d769cd81a215

*** empty log message ***
author gabucino
date Fri, 08 Jun 2001 17:47:23 +0000
parents b36fb1ae4b53
children aecab161d8d6
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"
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
18
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
19 #include "../libvo/fastmemcpy.h"
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
20
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
21 static ao_info_t info =
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
22 {
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
23 "SDLlib audio output",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
24 "sdl",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
25 "Felix Buenemann <atmosfear@users.sourceforge.net>",
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
26 ""
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 LIBAO_EXTERN(sdl)
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
30
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
31 // there are some globals:
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
32 // ao_samplerate
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
33 // ao_channels
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
34 // ao_format
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
35 // ao_bps
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
36 // ao_outburst
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
37 // ao_buffersize
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
38
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
39 extern int verbose;
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
40 /* audio driver to be used by SDLlib */
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
41 char *sdl_adriver;
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
42
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
43 // Samplesize used by the SDLlib AudioSpec struct
993
dfa641c44d4a Increased sample size to solve sound problems one some systems.
atmosfear
parents: 985
diff changeset
44 #define SAMPLESIZE 1024
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
45
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
46 // General purpose Ring-buffering routines
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
47
974
0d4e4da7c126 -abs support
arpi_esp
parents: 972
diff changeset
48 #define BUFFSIZE 4096
0d4e4da7c126 -abs support
arpi_esp
parents: 972
diff changeset
49 #define NUM_BUFS 16
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
50
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
51 static unsigned char *buffer[NUM_BUFS];
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
52
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
53 static unsigned int buf_read=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
54 static unsigned int buf_write=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
55 static unsigned int buf_read_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
56 static unsigned int buf_write_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
57
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
58 static int full_buffers=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
59 static int buffered_bytes=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
60
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
61
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
62 static int write_buffer(unsigned char* data,int len){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
63 int len2=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
64 int x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
65 while(len>0){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
66 if(full_buffers==NUM_BUFS) break;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
67 x=BUFFSIZE-buf_write_pos;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
68 if(x>len) x=len;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
69 memcpy(buffer[buf_write]+buf_write_pos,data+len2,x);
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
70 len2+=x; len-=x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
71 buffered_bytes+=x; buf_write_pos+=x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
72 if(buf_write_pos>=BUFFSIZE){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
73 // block is full, find next!
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
74 buf_write=(buf_write+1)%NUM_BUFS;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
75 ++full_buffers;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
76 buf_write_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
77 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
78 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
79 return len2;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
80 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
81
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
82 static int read_buffer(unsigned char* data,int len){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
83 int len2=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
84 int x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
85 while(len>0){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
86 if(full_buffers==0) break; // no more data buffered!
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
87 x=BUFFSIZE-buf_read_pos;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
88 if(x>len) x=len;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
89 memcpy(data+len2,buffer[buf_read]+buf_read_pos,x);
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
90 len2+=x; len-=x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
91 buffered_bytes-=x; buf_read_pos+=x;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
92 if(buf_read_pos>=BUFFSIZE){
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
93 // block is empty, find next!
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
94 buf_read=(buf_read+1)%NUM_BUFS;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
95 --full_buffers;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
96 buf_read_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
97 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
98 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
99 return len2;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
100 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
101
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
102 // end ring buffer stuff
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
103
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
104 #ifdef __FreeBSD__
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
105 #include <SDL11/SDL.h>
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
106 #else
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
107 #include <SDL/SDL.h>
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
108 #endif
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
109
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
110 #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
111 /* 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
112 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
113 {
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
114 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
115 char *env = malloc(len);
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
116
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
117 if (env != NULL) {
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
118 strcpy(env, name);
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
119 strcat(env, "=");
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
120 strcat(env, val);
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
121 putenv(env);
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
122 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
123 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
124 #endif
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
125
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
126
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
127 // to set/get/query special features/parameters
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
128 static int control(int cmd,int arg){
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
129 return -1;
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
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
132 // SDL Callback function
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
133 void outputaudio(void *unused, Uint8 *stream, int len) {
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
134 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
135 read_buffer(stream, len);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
136 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
137
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
138 // open & setup audio device
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
139 // return: 1=success 0=fail
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
140 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
141
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
142 /* SDL Audio Specifications */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
143 SDL_AudioSpec aspec;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
144
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
145 int i;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
146 /* Allocate ring-buffer memory */
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
147 for(i=0;i<NUM_BUFS;i++) buffer[i]=(unsigned char *) malloc(BUFFSIZE);
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
148
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
149 printf("SDL: Samplerate: %iHz Channels: %s Format %iBit\n", rate, (channels > 1) ? "Stereo" : "Mono", format);
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
150
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
151 if(sdl_adriver) {
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
152 setenv("SDL_AUDIODRIVER", sdl_adriver, 1);
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
153 printf("SDL: using %s audio driver\n", sdl_adriver);
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
154 }
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
155
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
156
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
157 /* The desired audio frequency in samples-per-second. */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
158 aspec.freq = rate;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
159
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
160 /* The desired audio format (see SDL_AudioSpec) */
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
161 aspec.format = (format == 16) ? AUDIO_S16 : AUDIO_U8;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
162
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
163 /* Number of channels (mono/stereo) */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
164 aspec.channels = channels;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
165
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
166 /* 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
167 aspec.samples = SAMPLESIZE;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
168
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
169 /* 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
170 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
171 aspec.callback = outputaudio;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
172
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
173 /* 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
174 aspec.userdata = NULL;
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
175
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
176 /* initialize the SDL Audio system */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
177 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) {
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
178 printf("SDL: Initializing of SDL Audio failed: %s.\n", SDL_GetError());
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
179 return 0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
180 }
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
181
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
182 /* Open the audio device and start playing sound! */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
183 if(SDL_OpenAudio(&aspec, NULL) < 0) {
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
184 printf("SDL: Unable to open audio: %s\n", SDL_GetError());
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
185 return(0);
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
186 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
187
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
188 if(verbose) printf("SDL: buf size = %d\n",aspec.size);
975
65353ed24ac5 -abs support - fixed
arpi_esp
parents: 974
diff changeset
189 if(ao_buffersize==-1) ao_buffersize=aspec.size;
974
0d4e4da7c126 -abs support
arpi_esp
parents: 972
diff changeset
190
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
191 /* unsilence audio, if callback is ready */
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
192 SDL_PauseAudio(0);
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
193
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
194 return 1;
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
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
197 // close audio device
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
198 static void uninit(){
983
cb0e3b29a0dd Several small changes (like driver selection).
atmosfear
parents: 975
diff changeset
199 if(verbose) printf("SDL: Audio Subsystem shutting down!\n");
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
200 SDL_CloseAudio();
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
201 SDL_QuitSubSystem(SDL_INIT_AUDIO);
966
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
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
204 // stop playing and empty buffers (for seeking/pause)
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
205 static void reset(){
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
206
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
207 /* Reset ring-buffer state */
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
208 buf_read=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
209 buf_write=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
210 buf_read_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
211 buf_write_pos=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
212
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
213 full_buffers=0;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
214 buffered_bytes=0;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
215
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
216 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
217
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
218 // 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
219 static void audio_pause()
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
220 {
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
221 // for now, just call reset();
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
222 reset();
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
223 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
224
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
225 // 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
226 static void audio_resume()
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
227 {
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
228 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
229
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 993
diff changeset
230
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
231 // return: how many bytes can be played without blocking
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
232 static int get_space(){
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
233 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos;
966
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
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
236 // plays 'len' bytes of 'data'
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
237 // it should round it down to outburst*n
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
238 // return: number of bytes played
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
239 static int play(void* data,int len,int flags){
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
240
985
95391cf9e994 Microchange =)
atmosfear
parents: 983
diff changeset
241 #if 0
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
242 int ret;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
243
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
244 /* Audio locking prohibits call of outputaudio */
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
245 SDL_LockAudio();
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
246 // copy audio stream into ring-buffer
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
247 ret = write_buffer(data, len);
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
248 SDL_UnlockAudio();
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
249
972
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
250 return ret;
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
251 #else
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
252 return write_buffer(data, len);
b49f89b3ab2e Yea, it worksss!
atmosfear
parents: 966
diff changeset
253 #endif
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
254 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
255
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
256 // return: how many unplayed bytes are in the buffer
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
257 static int get_delay(){
974
0d4e4da7c126 -abs support
arpi_esp
parents: 972
diff changeset
258 return buffered_bytes + ao_buffersize;
966
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
259 }
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
260
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
261
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
262
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
263
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
264
69b4f944ce08 Added support for sdl audio out (buggy pre-alpha).
atmosfear
parents:
diff changeset
265