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

Latest commit

 

History

History
115 lines (99 loc) · 3.33 KB

testresample.c

File metadata and controls

115 lines (99 loc) · 3.33 KB
 
Apr 8, 2011
Apr 8, 2011
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
Jan 11, 2009
Jan 11, 2009
12
13
14
#include <stdio.h>
#include "SDL.h"
Jan 14, 2009
Jan 14, 2009
15
16
int
main(int argc, char **argv)
Jan 11, 2009
Jan 11, 2009
17
18
19
20
21
22
23
24
25
26
27
{
SDL_AudioSpec spec;
SDL_AudioCVT cvt;
Uint32 len = 0;
Uint8 *data = NULL;
int cvtfreq = 0;
int bitsize = 0;
int blockalign = 0;
int avgbytes = 0;
SDL_RWops *io = NULL;
Jan 14, 2009
Jan 14, 2009
28
if (argc != 4) {
Jan 11, 2009
Jan 11, 2009
29
30
31
32
33
34
fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
return 1;
}
cvtfreq = atoi(argv[3]);
Jan 14, 2009
Jan 14, 2009
35
if (SDL_Init(SDL_INIT_AUDIO) == -1) {
Jan 11, 2009
Jan 11, 2009
36
37
38
39
fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
return 2;
}
Jan 14, 2009
Jan 14, 2009
40
if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
Jan 11, 2009
Jan 11, 2009
41
42
43
44
45
46
fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
SDL_Quit();
return 3;
}
if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
Jan 14, 2009
Jan 14, 2009
47
spec.format, spec.channels, cvtfreq) == -1) {
Jan 11, 2009
Jan 11, 2009
48
49
50
51
52
53
54
55
fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
SDL_FreeWAV(data);
SDL_Quit();
return 4;
}
cvt.len = len;
cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
Jan 14, 2009
Jan 14, 2009
56
if (cvt.buf == NULL) {
Jan 11, 2009
Jan 11, 2009
57
58
59
60
61
62
63
fprintf(stderr, "Out of memory.\n");
SDL_FreeWAV(data);
SDL_Quit();
return 5;
}
memcpy(cvt.buf, data, len);
Jan 14, 2009
Jan 14, 2009
64
if (SDL_ConvertAudio(&cvt) == -1) {
Jan 11, 2009
Jan 11, 2009
65
66
67
68
69
70
71
72
73
fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
free(cvt.buf);
SDL_FreeWAV(data);
SDL_Quit();
return 6;
}
/* write out a WAV header... */
io = SDL_RWFromFile(argv[2], "wb");
Jan 14, 2009
Jan 14, 2009
74
if (io == NULL) {
Jan 11, 2009
Jan 11, 2009
75
76
77
78
79
80
81
82
83
84
85
fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
free(cvt.buf);
SDL_FreeWAV(data);
SDL_Quit();
return 7;
}
bitsize = SDL_AUDIO_BITSIZE(spec.format);
blockalign = (bitsize / 8) * spec.channels;
avgbytes = cvtfreq * blockalign;
Jan 14, 2009
Jan 14, 2009
86
SDL_WriteLE32(io, 0x46464952); /* RIFF */
Jan 11, 2009
Jan 11, 2009
87
SDL_WriteLE32(io, len * cvt.len_mult + 36);
Jan 14, 2009
Jan 14, 2009
88
89
90
91
92
93
94
95
96
97
98
SDL_WriteLE32(io, 0x45564157); /* WAVE */
SDL_WriteLE32(io, 0x20746D66); /* fmt */
SDL_WriteLE32(io, 16); /* chunk size */
SDL_WriteLE16(io, 1); /* uncompressed */
SDL_WriteLE16(io, spec.channels); /* channels */
SDL_WriteLE32(io, cvtfreq); /* sample rate */
SDL_WriteLE32(io, avgbytes); /* average bytes per second */
SDL_WriteLE16(io, blockalign); /* block align */
SDL_WriteLE16(io, bitsize); /* significant bits per sample */
SDL_WriteLE32(io, 0x61746164); /* data */
SDL_WriteLE32(io, cvt.len_cvt); /* size */
Jan 11, 2009
Jan 11, 2009
99
SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
Jan 11, 2009
Jan 11, 2009
100
Jan 14, 2009
Jan 14, 2009
101
if (SDL_RWclose(io) == -1) {
Jan 11, 2009
Jan 11, 2009
102
103
104
105
106
fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
free(cvt.buf);
SDL_FreeWAV(data);
SDL_Quit();
return 8;
Jan 14, 2009
Jan 14, 2009
107
} // if
Jan 11, 2009
Jan 11, 2009
108
109
110
111
112
free(cvt.buf);
SDL_FreeWAV(data);
SDL_Quit();
return 0;
Jan 14, 2009
Jan 14, 2009
113
} // main
Jan 11, 2009
Jan 11, 2009
114
115
// end of resample_test.c ...