testresample.c
Go to the documentation of this file.
/*
Copyright (C) 1997-2018 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.
*/
#include "SDL.h"
int
main(int argc, char **argv)
{
SDL_AudioSpec spec;
SDL_AudioCVT cvt;
Uint32 len = 0;
Uint8 *data = NULL;
int cvtfreq = 0;
int cvtchans = 0;
int bitsize = 0;
int blockalign = 0;
int avgbytes = 0;
SDL_RWops *io = NULL;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
if (argc != 5) {
SDL_Log("USAGE: %s in.wav out.wav newfreq newchans\n", argv[0]);
return 1;
}
cvtfreq = SDL_atoi(argv[3]);
cvtchans = SDL_atoi(argv[4]);
if (SDL_Init(SDL_INIT_AUDIO) == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
return 2;
}
if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
SDL_Quit();
return 3;
}
if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
spec.format, cvtchans, cvtfreq) == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
SDL_FreeWAV(data);
SDL_Quit();
return 4;
}
cvt.len = len;
cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
if (cvt.buf == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
SDL_FreeWAV(data);
SDL_Quit();
return 5;
}
SDL_memcpy(cvt.buf, data, len);
if (SDL_ConvertAudio(&cvt) == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
SDL_free(cvt.buf);
SDL_FreeWAV(data);
SDL_Quit();
return 6;
}
/* write out a WAV header... */
io = SDL_RWFromFile(argv[2], "wb");
if (io == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
SDL_free(cvt.buf);
SDL_FreeWAV(data);
SDL_Quit();
return 7;
}
bitsize = SDL_AUDIO_BITSIZE(spec.format);
blockalign = (bitsize / 8) * cvtchans;
avgbytes = cvtfreq * blockalign;
SDL_WriteLE32(io, 0x46464952); /* RIFF */
SDL_WriteLE32(io, cvt.len_cvt + 36);
SDL_WriteLE32(io, 0x45564157); /* WAVE */
SDL_WriteLE32(io, 0x20746D66); /* fmt */
SDL_WriteLE32(io, 16); /* chunk size */
SDL_WriteLE16(io, 1); /* uncompressed */
SDL_WriteLE16(io, cvtchans); /* 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 */
SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
if (SDL_RWclose(io) == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
SDL_free(cvt.buf);
SDL_FreeWAV(data);
SDL_Quit();
return 8;
} /* if */
SDL_free(cvt.buf);
SDL_FreeWAV(data);
SDL_Quit();
return 0;
} /* main */
/* end of testresample.c ... */
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_AudioCVT::len
int len
Definition: SDL_audio.h:232
SDL_AudioCVT::buf
Uint8 * buf
Definition: SDL_audio.h:231
SDL_RWwrite
#define SDL_RWwrite(ctx, ptr, size, n)
Definition: SDL_rwops.h:188
SDL_BuildAudioCVT
#define SDL_BuildAudioCVT
Definition: SDL_dynapi_overrides.h:88
SDL_RWops
Definition: SDL_rwops.h:52
SDL_AudioSpec
Definition: SDL_audio.h:177
SDL_WriteLE16
#define SDL_WriteLE16
Definition: SDL_dynapi_overrides.h:364
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_FreeWAV
#define SDL_FreeWAV
Definition: SDL_dynapi_overrides.h:87
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
SDL_AudioCVT::len_mult
int len_mult
Definition: SDL_audio.h:234
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
main
int main(int argc, char **argv)
Definition: testresample.c:16
SDL_AudioCVT
A structure to hold a set of audio conversion filters and buffers.
Definition: SDL_audio.h:225
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
spec
SDL_AudioSpec spec
Definition: loopwave.c:31
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_RWFromFile
#define SDL_RWFromFile
Definition: SDL_dynapi_overrides.h:351
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
SDL_AudioSpec::freq
int freq
Definition: SDL_audio.h:179
SDL_ConvertAudio
#define SDL_ConvertAudio
Definition: SDL_dynapi_overrides.h:89
SDL_AudioSpec::channels
Uint8 channels
Definition: SDL_audio.h:181
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_AUDIO_BITSIZE
#define SDL_AUDIO_BITSIZE(x)
Definition: SDL_audio.h:75
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
SDL_LoadWAV
#define SDL_LoadWAV(file, spec, audio_buf, audio_len)
Definition: SDL_audio.h:450
SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_atoi
#define SDL_atoi
Definition: SDL_dynapi_overrides.h:410
SDL_AudioCVT::len_cvt
int len_cvt
Definition: SDL_audio.h:233
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
NULL
#define NULL
Definition: begin_code.h:164
SDL_RWclose
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
SDL_INIT_AUDIO
#define SDL_INIT_AUDIO
Definition: SDL.h:77
SDL_AudioSpec::format
SDL_AudioFormat format
Definition: SDL_audio.h:180
SDL_WriteLE32
#define SDL_WriteLE32
Definition: SDL_dynapi_overrides.h:366
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL.h
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66