SDL_OpenAudio.3
资源名称:NETVIDEO.rar [点击查看]
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:6k
源码类别:
流媒体/Mpeg4/MP4
开发平台:
Visual C++
- .TH "SDL_OpenAudio" "3" "Tue 11 Sep 2001, 22:58" "SDL" "SDL API Reference"
- .SH "NAME"
- SDL_OpenAudio- Opens the audio device with the desired parameters&.
- .SH "SYNOPSIS"
- .PP
- fB#include "SDL&.h"
- .sp
- fBint fBSDL_OpenAudiofPfR(fBSDL_AudioSpec *desired, SDL_AudioSpec *obtainedfR);
- .SH "DESCRIPTION"
- .PP
- This function opens the audio device with the fBdesiredfR parameters, and returns 0 if successful, placing the actual hardware parameters in the structure pointed to by fBobtainedfR&. If fBobtainedfR 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 fBdesiredfR fIfBSDL_AudioSpecfRfR 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 "fBdesiredfR->fBfreqfR" 10The desired audio frequency in samples-per-second&.
- .IP "fBdesiredfR->fBformatfR" 10The desired audio format (see fIfBSDL_AudioSpecfRfR)
- .IP "fBdesiredfR->fBsamplesfR" 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 "fBdesiredfR->fBcallbackfR" 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 fIfBSDL_LockAudiofPfR and fIfBSDL_UnlockAudiofPfR in your code&. The callback prototype is:
- .PP
- .nf
- f(CWvoid callback(void *userdata, Uint8 *stream, int len);fR
- .fi
- .PP
- fBuserdatafR is the pointer stored in fBuserdatafR field of the fBSDL_AudioSpecfR&. fBstreamfR is a pointer to the audio buffer you want to fill with information and fBlenfR is the length of the audio buffer in bytes&.
- .IP "fBdesiredfR->fBuserdatafR" 10This pointer is passed as the first parameter to the fBcallbackfP function&.
- .PP
- fBSDL_OpenAudiofP reads these fields from the fBdesiredfR fBSDL_AudioSpecfR structure pass to the function and attempts to find an audio configuration matching your fBdesiredfR&. As mentioned above, if the fBobtainedfR parameter is fBNULLfP then SDL with convert from your fBdesiredfR audio settings to the hardware settings as it plays&.
- .PP
- If fBobtainedfR is fBNULLfP then the fBdesiredfR fBSDL_AudioSpecfR is your working specification, otherwise the fBobtainedfR fBSDL_AudioSpecfR becomes the working specification and the fBdesirecfR specification can be deleted&. The data in the working specification is used when building fBSDL_AudioCVTfR&'s for converting loaded data to the hardware format&.
- .PP
- fBSDL_OpenAudiofP calculates the fBsizefR and fBsilencefR fields for both the fBdesiredfR and fBobtainedfR specifications&. The fBsizefR field stores the total size of the audio buffer in bytes, while the fBsilencefR stores the value used to represent silence in the audio buffer
- .PP
- The audio device starts out playing fBsilencefR when it&'s opened, and should be enabled for playing by calling fIfBSDL_PauseAudiofP(fB0fR)fR when you are ready for your audio fBcallbackfR function to be called&. Since the audio driver may modify the requested fBsizefR 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;
- /* Mono */
- desired->channels=0;
- /* 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
- fIfBSDL_AudioSpecfPfR, fIfBSDL_LockAudiofPfR, fIfBSDL_UnlockAudiofPfR, fIfBSDL_PauseAudiofPfR
- ..." created by instant / docbook-to-man, Tue 11 Sep 2001, 22:58