SDL  2.0
testresample.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
3 
4  This software is provided 'as-is', without any express or implied
5  warranty. In no event will the authors be held liable for any damages
6  arising from the use of this software.
7 
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it
10  freely.
11 */
12 
13 #include "SDL.h"
14 
15 int
16 main(int argc, char **argv)
17 {
19  SDL_AudioCVT cvt;
20  Uint32 len = 0;
21  Uint8 *data = NULL;
22  int cvtfreq = 0;
23  int cvtchans = 0;
24  int bitsize = 0;
25  int blockalign = 0;
26  int avgbytes = 0;
27  SDL_RWops *io = NULL;
28 
29  /* Enable standard application logging */
31 
32  if (argc != 5) {
33  SDL_Log("USAGE: %s in.wav out.wav newfreq newchans\n", argv[0]);
34  return 1;
35  }
36 
37  cvtfreq = SDL_atoi(argv[3]);
38  cvtchans = SDL_atoi(argv[4]);
39 
40  if (SDL_Init(SDL_INIT_AUDIO) == -1) {
41  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
42  return 2;
43  }
44 
45  if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
46  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
47  SDL_Quit();
48  return 3;
49  }
50 
52  spec.format, cvtchans, cvtfreq) == -1) {
53  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
55  SDL_Quit();
56  return 4;
57  }
58 
59  cvt.len = len;
60  cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
61  if (cvt.buf == NULL) {
62  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
64  SDL_Quit();
65  return 5;
66  }
67  SDL_memcpy(cvt.buf, data, len);
68 
69  if (SDL_ConvertAudio(&cvt) == -1) {
70  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
71  SDL_free(cvt.buf);
73  SDL_Quit();
74  return 6;
75  }
76 
77  /* write out a WAV header... */
78  io = SDL_RWFromFile(argv[2], "wb");
79  if (io == NULL) {
80  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
81  SDL_free(cvt.buf);
83  SDL_Quit();
84  return 7;
85  }
86 
87  bitsize = SDL_AUDIO_BITSIZE(spec.format);
88  blockalign = (bitsize / 8) * cvtchans;
89  avgbytes = cvtfreq * blockalign;
90 
91  SDL_WriteLE32(io, 0x46464952); /* RIFF */
92  SDL_WriteLE32(io, cvt.len_cvt + 36);
93  SDL_WriteLE32(io, 0x45564157); /* WAVE */
94  SDL_WriteLE32(io, 0x20746D66); /* fmt */
95  SDL_WriteLE32(io, 16); /* chunk size */
96  SDL_WriteLE16(io, SDL_AUDIO_ISFLOAT(spec.format) ? 3 : 1); /* uncompressed */
97  SDL_WriteLE16(io, cvtchans); /* channels */
98  SDL_WriteLE32(io, cvtfreq); /* sample rate */
99  SDL_WriteLE32(io, avgbytes); /* average bytes per second */
100  SDL_WriteLE16(io, blockalign); /* block align */
101  SDL_WriteLE16(io, bitsize); /* significant bits per sample */
102  SDL_WriteLE32(io, 0x61746164); /* data */
103  SDL_WriteLE32(io, cvt.len_cvt); /* size */
104  SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
105 
106  if (SDL_RWclose(io) == -1) {
107  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
108  SDL_free(cvt.buf);
109  SDL_FreeWAV(data);
110  SDL_Quit();
111  return 8;
112  } /* if */
113 
114  SDL_free(cvt.buf);
115  SDL_FreeWAV(data);
116  SDL_Quit();
117  return 0;
118 } /* main */
119 
120 /* end of testresample.c ... */
SDL.h
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_AudioSpec::channels
Uint8 channels
Definition: SDL_audio.h:182
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_BuildAudioCVT
#define SDL_BuildAudioCVT
Definition: SDL_dynapi_overrides.h:88
NULL
#define NULL
Definition: begin_code.h:167
SDL_WriteLE16
#define SDL_WriteLE16
Definition: SDL_dynapi_overrides.h:364
SDL_AudioCVT::buf
Uint8 * buf
Definition: SDL_audio.h:232
SDL_AudioSpec::format
SDL_AudioFormat format
Definition: SDL_audio.h:181
SDL_AudioSpec
Definition: SDL_audio.h:178
SDL_FreeWAV
#define SDL_FreeWAV
Definition: SDL_dynapi_overrides.h:87
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_AudioCVT::len_mult
int len_mult
Definition: SDL_audio.h:235
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_AUDIO_ISFLOAT
#define SDL_AUDIO_ISFLOAT(x)
Definition: SDL_audio.h:76
SDL_RWwrite
#define SDL_RWwrite
Definition: SDL_dynapi_overrides.h:724
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2929
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
SDL_ConvertAudio
#define SDL_ConvertAudio
Definition: SDL_dynapi_overrides.h:89
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_RWFromFile
#define SDL_RWFromFile
Definition: SDL_dynapi_overrides.h:351
SDL_LoadWAV
#define SDL_LoadWAV(file, spec, audio_buf, audio_len)
Definition: SDL_audio.h:484
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
SDL_AUDIO_BITSIZE
#define SDL_AUDIO_BITSIZE(x)
Definition: SDL_audio.h:75
main
int main(int argc, char **argv)
Definition: testresample.c:16
SDL_AudioSpec::freq
int freq
Definition: SDL_audio.h:180
spec
SDL_AudioSpec spec
Definition: loopwave.c:31
SDL_atoi
#define SDL_atoi
Definition: SDL_dynapi_overrides.h:410
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_AudioCVT::len
int len
Definition: SDL_audio.h:233
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_RWclose
#define SDL_RWclose
Definition: SDL_dynapi_overrides.h:725
SDL_WriteLE32
#define SDL_WriteLE32
Definition: SDL_dynapi_overrides.h:366
SDL_AudioCVT::len_cvt
int len_cvt
Definition: SDL_audio.h:234
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
SDL_AudioCVT
A structure to hold a set of audio conversion filters and buffers.
Definition: SDL_audio.h:226
SDL_RWops
Definition: SDL_rwops.h:52
SDL_INIT_AUDIO
#define SDL_INIT_AUDIO
Definition: SDL.h:79
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179