Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
97 lines (87 loc) · 5.41 KB

SDL_OpenAudio.3

File metadata and controls

97 lines (87 loc) · 5.41 KB
 
Sep 14, 2001
Sep 14, 2001
1
.TH "SDL_OpenAudio" "3" "Tue 11 Sep 2001, 22:58" "SDL" "SDL API Reference"
Apr 26, 2001
Apr 26, 2001
2
.SH "NAME"
Dec 29, 2007
Dec 29, 2007
3
SDL_OpenAudio \- Opens the audio device with the desired parameters\&.
Apr 26, 2001
Apr 26, 2001
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
.SH "SYNOPSIS"
.PP
\fB#include "SDL\&.h"
.sp
\fBint \fBSDL_OpenAudio\fP\fR(\fBSDL_AudioSpec *desired, SDL_AudioSpec *obtained\fR);
.SH "DESCRIPTION"
.PP
This function opens the audio device with the \fBdesired\fR parameters, and returns 0 if successful, placing the actual hardware parameters in the structure pointed to by \fBobtained\fR\&. If \fBobtained\fR is NULL, the audio data passed to the callback function will be guaranteed to be in the requested format, and will be automatically converted to the hardware audio format if necessary\&. This function returns -1 if it failed to open the audio device, or couldn\&'t set up the audio thread\&.
.PP
To open the audio device a \fBdesired\fR \fI\fBSDL_AudioSpec\fR\fR must be created\&.
.PP
.nf
\f(CWSDL_AudioSpec *desired;
\&.
\&.
desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));\fR
.fi
.PP
You must then fill this structure with your desired audio specifications\&.
.IP "\fBdesired\fR->\fBfreq\fR" 10The desired audio frequency in samples-per-second\&.
.IP "\fBdesired\fR->\fBformat\fR" 10The desired audio format (see \fI\fBSDL_AudioSpec\fR\fR)
.IP "\fBdesired\fR->\fBsamples\fR" 10The 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
.IP "\fBdesired\fR->\fBcallback\fR" 10This 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 \fI\fBSDL_LockAudio\fP\fR and \fI\fBSDL_UnlockAudio\fP\fR in your code\&. The callback prototype is:
.PP
.nf
\f(CWvoid callback(void *userdata, Uint8 *stream, int len);\fR
.fi
.PP
\fBuserdata\fR is the pointer stored in \fBuserdata\fR field of the \fBSDL_AudioSpec\fR\&. \fBstream\fR is a pointer to the audio buffer you want to fill with information and \fBlen\fR is the length of the audio buffer in bytes\&.
.IP "\fBdesired\fR->\fBuserdata\fR" 10This pointer is passed as the first parameter to the \fBcallback\fP function\&.
.PP
\fBSDL_OpenAudio\fP reads these fields from the \fBdesired\fR \fBSDL_AudioSpec\fR structure pass to the function and attempts to find an audio configuration matching your \fBdesired\fR\&. As mentioned above, if the \fBobtained\fR parameter is \fBNULL\fP then SDL with convert from your \fBdesired\fR audio settings to the hardware settings as it plays\&.
.PP
If \fBobtained\fR is \fBNULL\fP then the \fBdesired\fR \fBSDL_AudioSpec\fR is your working specification, otherwise the \fBobtained\fR \fBSDL_AudioSpec\fR becomes the working specification and the \fBdesirec\fR specification can be deleted\&. The data in the working specification is used when building \fBSDL_AudioCVT\fR\&'s for converting loaded data to the hardware format\&.
.PP
\fBSDL_OpenAudio\fP calculates the \fBsize\fR and \fBsilence\fR fields for both the \fBdesired\fR and \fBobtained\fR specifications\&. The \fBsize\fR field stores the total size of the audio buffer in bytes, while the \fBsilence\fR stores the value used to represent silence in the audio buffer
.PP
The audio device starts out playing \fBsilence\fR when it\&'s opened, and should be enabled for playing by calling \fI\fBSDL_PauseAudio\fP(\fB0\fR)\fR when you are ready for your audio \fBcallback\fR function to be called\&. Since the audio driver may modify the requested \fBsize\fR of the audio buffer, you should allocate any local mixing buffers after you open the audio device\&.
.SH "EXAMPLES"
.PP
.nf
\f(CW/* Prototype of our callback function */
void my_audio_callback(void *userdata, Uint8 *stream, int len);
/* Open the audio device */
SDL_AudioSpec *desired, *obtained;
SDL_AudioSpec *hardware_spec;
/* Allocate a desired SDL_AudioSpec */
desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
/* Allocate space for the obtained SDL_AudioSpec */
obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
/* 22050Hz - FM Radio quality */
desired->freq=22050;
/* 16-bit signed audio */
desired->format=AUDIO_S16LSB;
Sep 14, 2001
Sep 14, 2001
64
65
66
/* Mono */
desired->channels=0;
Apr 26, 2001
Apr 26, 2001
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Large audio buffer reduces risk of dropouts but increases response time */
desired->samples=8192;
/* Our callback function */
desired->callback=my_audio_callback;
desired->userdata=NULL;
/* Open the audio device */
if ( SDL_OpenAudio(desired, obtained) < 0 ){
fprintf(stderr, "Couldn\&'t open audio: %s
", SDL_GetError());
exit(-1);
}
/* desired spec is no longer needed */
free(desired);
hardware_spec=obtained;
\&.
\&.
/* Prepare callback for playing */
\&.
\&.
\&.
/* Start playing */
SDL_PauseAudio(0);\fR
.fi
.PP
.SH "SEE ALSO"
.PP
\fI\fBSDL_AudioSpec\fP\fR, \fI\fBSDL_LockAudio\fP\fR, \fI\fBSDL_UnlockAudio\fP\fR, \fI\fBSDL_PauseAudio\fP\fR
Sep 14, 2001
Sep 14, 2001
97
...\" created by instant / docbook-to-man, Tue 11 Sep 2001, 22:58