SDL  2.0
testresample.c File Reference
#include "SDL.h"
+ Include dependency graph for testresample.c:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

§ main()

int main ( int  argc,
char **  argv 
)

Definition at line 16 of file testresample.c.

References SDL_AudioCVT::buf, SDL_AudioSpec::channels, SDL_AudioSpec::format, SDL_AudioSpec::freq, SDL_AudioCVT::len, SDL_AudioCVT::len_cvt, SDL_AudioCVT::len_mult, NULL, SDL_atoi, SDL_AUDIO_BITSIZE, SDL_BuildAudioCVT, SDL_ConvertAudio, SDL_free(), SDL_FreeWAV, SDL_GetError, SDL_Init, SDL_INIT_AUDIO, SDL_LoadWAV, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_malloc, SDL_memcpy, SDL_Quit, SDL_RWclose, SDL_RWFromFile, SDL_RWwrite, SDL_WriteLE16, SDL_WriteLE32, and spec.

17 {
19  SDL_AudioCVT cvt;
20  Uint32 len = 0;
21  Uint8 *data = NULL;
22  int cvtfreq = 0;
23  int bitsize = 0;
24  int blockalign = 0;
25  int avgbytes = 0;
26  SDL_RWops *io = NULL;
27 
28  /* Enable standard application logging */
30 
31  if (argc != 4) {
32  SDL_Log("USAGE: %s in.wav out.wav newfreq\n", argv[0]);
33  return 1;
34  }
35 
36  cvtfreq = SDL_atoi(argv[3]);
37 
38  if (SDL_Init(SDL_INIT_AUDIO) == -1) {
39  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
40  return 2;
41  }
42 
43  if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
44  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
45  SDL_Quit();
46  return 3;
47  }
48 
49  if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
50  spec.format, spec.channels, cvtfreq) == -1) {
51  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
52  SDL_FreeWAV(data);
53  SDL_Quit();
54  return 4;
55  }
56 
57  cvt.len = len;
58  cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
59  if (cvt.buf == NULL) {
60  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
61  SDL_FreeWAV(data);
62  SDL_Quit();
63  return 5;
64  }
65  SDL_memcpy(cvt.buf, data, len);
66 
67  if (SDL_ConvertAudio(&cvt) == -1) {
68  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
69  SDL_free(cvt.buf);
70  SDL_FreeWAV(data);
71  SDL_Quit();
72  return 6;
73  }
74 
75  /* write out a WAV header... */
76  io = SDL_RWFromFile(argv[2], "wb");
77  if (io == NULL) {
78  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
79  SDL_free(cvt.buf);
80  SDL_FreeWAV(data);
81  SDL_Quit();
82  return 7;
83  }
84 
85  bitsize = SDL_AUDIO_BITSIZE(spec.format);
86  blockalign = (bitsize / 8) * spec.channels;
87  avgbytes = cvtfreq * blockalign;
88 
89  SDL_WriteLE32(io, 0x46464952); /* RIFF */
90  SDL_WriteLE32(io, len * cvt.len_mult + 36);
91  SDL_WriteLE32(io, 0x45564157); /* WAVE */
92  SDL_WriteLE32(io, 0x20746D66); /* fmt */
93  SDL_WriteLE32(io, 16); /* chunk size */
94  SDL_WriteLE16(io, 1); /* uncompressed */
95  SDL_WriteLE16(io, spec.channels); /* channels */
96  SDL_WriteLE32(io, cvtfreq); /* sample rate */
97  SDL_WriteLE32(io, avgbytes); /* average bytes per second */
98  SDL_WriteLE16(io, blockalign); /* block align */
99  SDL_WriteLE16(io, bitsize); /* significant bits per sample */
100  SDL_WriteLE32(io, 0x61746164); /* data */
101  SDL_WriteLE32(io, cvt.len_cvt); /* size */
102  SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
103 
104  if (SDL_RWclose(io) == -1) {
105  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
106  SDL_free(cvt.buf);
107  SDL_FreeWAV(data);
108  SDL_Quit();
109  return 8;
110  } /* if */
111 
112  SDL_free(cvt.buf);
113  SDL_FreeWAV(data);
114  SDL_Quit();
115  return 0;
116 } /* main */
#define SDL_GetError
Uint8 * buf
Definition: SDL_audio.h:206
#define SDL_RWwrite(ctx, ptr, size, n)
Definition: SDL_rwops.h:188
#define SDL_BuildAudioCVT
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
#define SDL_WriteLE16
#define SDL_FreeWAV
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:159
GLenum GLsizei len
#define SDL_LogError
SDL_AudioSpec spec
Definition: loopwave.c:35
#define SDL_Log
#define SDL_RWFromFile
#define SDL_memcpy
#define SDL_ConvertAudio
Uint8 channels
Definition: SDL_audio.h:172
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
void SDL_free(void *mem)
#define SDL_AUDIO_BITSIZE(x)
Definition: SDL_audio.h:75
#define SDL_Quit
#define SDL_LoadWAV(file, spec, audio_buf, audio_len)
Definition: SDL_audio.h:425
#define SDL_atoi
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:143
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
#define SDL_INIT_AUDIO
Definition: SDL.h:76
SDL_AudioFormat format
Definition: SDL_audio.h:171
#define SDL_WriteLE32
#define SDL_Init
#define SDL_malloc