2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2023-01-09 10:41:41 -07:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char **argv)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
SDL_AudioSpec spec;
|
2023-01-16 01:45:48 -07:00
|
|
|
SDL_AudioStream *stream;
|
|
|
|
Uint8 *dst_buf = NULL;
|
2015-06-21 09:33:46 -06:00
|
|
|
Uint32 len = 0;
|
|
|
|
Uint8 *data = NULL;
|
|
|
|
int cvtfreq = 0;
|
2017-01-08 14:18:49 -07:00
|
|
|
int cvtchans = 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
int bitsize = 0;
|
|
|
|
int blockalign = 0;
|
|
|
|
int avgbytes = 0;
|
|
|
|
SDL_RWops *io = NULL;
|
2023-01-16 01:45:48 -07:00
|
|
|
int src_samplesize, dst_samplesize;
|
|
|
|
int src_len, dst_len, real_dst_len;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2015-11-25 13:39:28 -07:00
|
|
|
/* Enable standard application logging */
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
2017-01-08 14:18:49 -07:00
|
|
|
if (argc != 5) {
|
|
|
|
SDL_Log("USAGE: %s in.wav out.wav newfreq newchans\n", argv[0]);
|
2015-06-21 09:33:46 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cvtfreq = SDL_atoi(argv[3]);
|
2017-01-08 14:18:49 -07:00
|
|
|
cvtchans = SDL_atoi(argv[4]);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-16 01:45:48 -07:00
|
|
|
|
|
|
|
stream = SDL_CreateAudioStream(spec.format, spec.channels, spec.freq,
|
|
|
|
spec.format, cvtchans, cvtfreq);
|
|
|
|
if (stream == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build audio stream: %s\n", SDL_GetError());
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(data);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2023-01-16 01:45:48 -07:00
|
|
|
src_samplesize = (SDL_AUDIO_BITSIZE(spec.format) / 8) * spec.channels;
|
|
|
|
dst_samplesize = (SDL_AUDIO_BITSIZE(spec.format) / 8) * cvtchans;
|
|
|
|
|
|
|
|
|
|
|
|
src_len = len & ~(src_samplesize - 1);
|
|
|
|
dst_len = dst_samplesize * (src_len / src_samplesize);
|
|
|
|
if (spec.freq < cvtfreq) {
|
|
|
|
const double mult = ((double)cvtfreq) / ((double)spec.freq);
|
|
|
|
dst_len *= (int) SDL_ceil(mult);
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_len = dst_len & ~(dst_samplesize - 1);
|
|
|
|
dst_buf = (Uint8 *)SDL_malloc(dst_len);
|
|
|
|
if (dst_buf == NULL) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(data);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2023-01-16 01:45:48 -07:00
|
|
|
/* Run the audio converter */
|
|
|
|
if (SDL_PutAudioStreamData(stream, data, src_len) < 0 ||
|
|
|
|
SDL_FlushAudioStream(stream) < 0) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
|
2023-01-16 01:45:48 -07:00
|
|
|
SDL_free(dst_buf);
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(data);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
|
|
|
return 6;
|
2023-01-16 01:45:48 -07:00
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:45:48 -07:00
|
|
|
real_dst_len = SDL_GetAudioStreamData(stream, dst_buf, dst_len);
|
|
|
|
if (real_dst_len < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
|
|
|
|
SDL_free(dst_buf);
|
|
|
|
SDL_free(data);
|
|
|
|
SDL_Quit();
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
dst_len = real_dst_len;
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* 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());
|
2023-01-16 01:45:48 -07:00
|
|
|
SDL_free(dst_buf);
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(data);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
2023-01-16 01:45:48 -07:00
|
|
|
return 8;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bitsize = SDL_AUDIO_BITSIZE(spec.format);
|
2017-01-08 14:18:49 -07:00
|
|
|
blockalign = (bitsize / 8) * cvtchans;
|
2015-06-21 09:33:46 -06:00
|
|
|
avgbytes = cvtfreq * blockalign;
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_WriteLE32(io, 0x46464952); /* RIFF */
|
2023-01-16 01:45:48 -07:00
|
|
|
SDL_WriteLE32(io, dst_len + 36);
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_WriteLE32(io, 0x45564157); /* WAVE */
|
|
|
|
SDL_WriteLE32(io, 0x20746D66); /* fmt */
|
|
|
|
SDL_WriteLE32(io, 16); /* chunk size */
|
|
|
|
SDL_WriteLE16(io, SDL_AUDIO_ISFLOAT(spec.format) ? 3 : 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 */
|
2023-01-16 01:45:48 -07:00
|
|
|
SDL_WriteLE32(io, dst_len); /* size */
|
|
|
|
SDL_RWwrite(io, dst_buf, dst_len);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
if (SDL_RWclose(io) == -1) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
|
2023-01-16 01:45:48 -07:00
|
|
|
SDL_free(dst_buf);
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(data);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
2023-01-16 01:45:48 -07:00
|
|
|
return 9;
|
2022-11-30 13:51:59 -07:00
|
|
|
} /* if */
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-01-16 01:45:48 -07:00
|
|
|
SDL_free(dst_buf);
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(data);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
|
|
|
return 0;
|
2022-11-30 13:51:59 -07:00
|
|
|
} /* main */
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
/* end of testresample.c ... */
|