docs/man3/SDL_ConvertAudio.3
author Sam Lantinga <slouken@libsdl.org>
Wed, 08 Mar 2006 18:30:12 +0000
changeset 1486 9d77fc9d0ace
parent 181 e5bc29de3f0a
child 2283 546f7c1eb755
permissions -rw-r--r--
Re-added C runtime support for Visual C++ 5,6
slouken@181
     1
.TH "SDL_ConvertAudio" "3" "Tue 11 Sep 2001, 22:58" "SDL" "SDL API Reference" 
slouken@0
     2
.SH "NAME"
slouken@0
     3
SDL_ConvertAudio\- Convert audio data to a desired audio format\&.
slouken@0
     4
.SH "SYNOPSIS"
slouken@0
     5
.PP
slouken@0
     6
\fB#include "SDL\&.h"
slouken@0
     7
.sp
slouken@0
     8
\fBint \fBSDL_ConvertAudio\fP\fR(\fBSDL_AudioCVT *cvt\fR);
slouken@0
     9
.SH "DESCRIPTION"
slouken@0
    10
.PP
slouken@0
    11
\fBSDL_ConvertAudio\fP takes one parameter, \fBcvt\fR, which was previously initilized\&. Initilizing a \fI\fBSDL_AudioCVT\fR\fR is a two step process\&. First of all, the structure must be passed to \fI\fBSDL_BuildAudioCVT\fP\fR along with source and destination format parameters\&. Secondly, the \fBcvt\fR->\fBbuf\fR and \fBcvt\fR->\fBlen\fR fields must be setup\&. \fBcvt\fR->\fBbuf\fR should point to the audio data and \fBcvt\fR->\fBlen\fR should be set to the length of the audio data in bytes\&. Remember, the length of the buffer pointed to by \fBbuf\fR show be \fBlen\fR*\fBlen_mult\fR bytes in length\&.
slouken@0
    12
.PP
slouken@0
    13
Once the \fBSDL_AudioCVT\fRstructure is initilized then we can pass it to \fBSDL_ConvertAudio\fP, which will convert the audio data pointer to by \fBcvt\fR->\fBbuf\fR\&. If \fBSDL_ConvertAudio\fP returned \fB0\fR then the conversion was completed successfully, otherwise \fB-1\fR is returned\&.
slouken@0
    14
.PP
slouken@0
    15
If the conversion completed successfully then the converted audio data can be read from \fBcvt\fR->\fBbuf\fR\&. The amount of valid, converted, audio data in the buffer is equal to \fBcvt\fR->\fBlen\fR*\fBcvt\fR->\fBlen_ratio\fR\&.
slouken@0
    16
.SH "EXAMPLES"
slouken@0
    17
.PP
slouken@0
    18
.nf
slouken@0
    19
\f(CW/* Converting some WAV data to hardware format */
slouken@0
    20
void my_audio_callback(void *userdata, Uint8 *stream, int len);
slouken@0
    21
slouken@0
    22
SDL_AudioSpec *desired, *obtained;
slouken@0
    23
SDL_AudioSpec wav_spec;
slouken@0
    24
SDL_AudioCVT  wav_cvt;
slouken@0
    25
Uint32 wav_len;
slouken@0
    26
Uint8 *wav_buf;
slouken@0
    27
int ret;
slouken@0
    28
slouken@0
    29
/* Allocated audio specs */
slouken@0
    30
desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
slouken@0
    31
obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
slouken@0
    32
slouken@0
    33
/* Set desired format */
slouken@0
    34
desired->freq=22050;
slouken@0
    35
desired->format=AUDIO_S16LSB;
slouken@0
    36
desired->samples=8192;
slouken@0
    37
desired->callback=my_audio_callback;
slouken@0
    38
desired->userdata=NULL;
slouken@0
    39
slouken@0
    40
/* Open the audio device */
slouken@0
    41
if ( SDL_OpenAudio(desired, obtained) < 0 ){
slouken@0
    42
  fprintf(stderr, "Couldn\&'t open audio: %s
slouken@0
    43
", SDL_GetError());
slouken@0
    44
  exit(-1);
slouken@0
    45
}
slouken@0
    46
        
slouken@0
    47
free(desired);
slouken@0
    48
slouken@0
    49
/* Load the test\&.wav */
slouken@0
    50
if( SDL_LoadWAV("test\&.wav", &wav_spec, &wav_buf, &wav_len) == NULL ){
slouken@0
    51
  fprintf(stderr, "Could not open test\&.wav: %s
slouken@0
    52
", SDL_GetError());
slouken@0
    53
  SDL_CloseAudio();
slouken@0
    54
  free(obtained);
slouken@0
    55
  exit(-1);
slouken@0
    56
}
slouken@0
    57
                                            
slouken@0
    58
/* Build AudioCVT */
slouken@0
    59
ret = SDL_BuildAudioCVT(&wav_cvt,
slouken@0
    60
                        wav_spec\&.format, wav_spec\&.channels, wav_spec\&.freq,
slouken@0
    61
                        obtained->format, obtained->channels, obtained->freq);
slouken@0
    62
slouken@0
    63
/* Check that the convert was built */
slouken@0
    64
if(ret==-1){
slouken@0
    65
  fprintf(stderr, "Couldn\&'t build converter!
slouken@0
    66
");
slouken@0
    67
  SDL_CloseAudio();
slouken@0
    68
  free(obtained);
slouken@0
    69
  SDL_FreeWAV(wav_buf);
slouken@0
    70
}
slouken@0
    71
slouken@0
    72
/* Setup for conversion */
slouken@0
    73
wav_cvt\&.buf=(Uint8 *)malloc(wav_len*wav_cvt\&.len_mult);
slouken@0
    74
wav_cvt\&.len=wav_len;
slouken@0
    75
memcpy(wav_cvt\&.buf, wav_buf, wav_len);
slouken@0
    76
slouken@0
    77
/* We can delete to original WAV data now */
slouken@0
    78
SDL_FreeWAV(wav_buf);
slouken@0
    79
slouken@0
    80
/* And now we\&'re ready to convert */
slouken@0
    81
SDL_ConvertAudio(&wav_cvt);
slouken@0
    82
slouken@0
    83
/* do whatever */
slouken@0
    84
\&.
slouken@0
    85
\&.
slouken@0
    86
\&.
slouken@0
    87
\&.
slouken@0
    88
slouken@0
    89
\fR
slouken@0
    90
.fi
slouken@0
    91
.PP
slouken@0
    92
.SH "SEE ALSO"
slouken@0
    93
.PP
slouken@0
    94
\fI\fBSDL_BuildAudioCVT\fP\fR, \fI\fBSDL_AudioCVT\fP\fR
slouken@181
    95
...\" created by instant / docbook-to-man, Tue 11 Sep 2001, 22:58