test/testresample.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Oct 2018 15:48:26 -0700
changeset 12285 30c3ffcbed38
parent 11988 8fa59be85dfb
child 12503 806492103856
permissions -rw-r--r--
Update documentation to note that this hint works on Android too (thanks Trent!)
slouken@5535
     1
/*
slouken@11811
     2
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
aschiffler@7639
    12
icculus@3017
    13
#include "SDL.h"
icculus@3017
    14
slouken@3040
    15
int
slouken@3040
    16
main(int argc, char **argv)
icculus@3017
    17
{
icculus@3017
    18
    SDL_AudioSpec spec;
icculus@3017
    19
    SDL_AudioCVT cvt;
icculus@3017
    20
    Uint32 len = 0;
icculus@3017
    21
    Uint8 *data = NULL;
icculus@3017
    22
    int cvtfreq = 0;
icculus@10793
    23
    int cvtchans = 0;
icculus@3017
    24
    int bitsize = 0;
icculus@3017
    25
    int blockalign = 0;
icculus@3017
    26
    int avgbytes = 0;
icculus@3017
    27
    SDL_RWops *io = NULL;
icculus@3017
    28
philipp@9922
    29
    /* Enable standard application logging */
aschiffler@7639
    30
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    31
icculus@10793
    32
    if (argc != 5) {
icculus@10793
    33
        SDL_Log("USAGE: %s in.wav out.wav newfreq newchans\n", argv[0]);
icculus@3017
    34
        return 1;
icculus@3017
    35
    }
icculus@3017
    36
philipp@7444
    37
    cvtfreq = SDL_atoi(argv[3]);
icculus@10793
    38
    cvtchans = SDL_atoi(argv[4]);
icculus@3017
    39
slouken@3040
    40
    if (SDL_Init(SDL_INIT_AUDIO) == -1) {
aschiffler@7639
    41
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
icculus@3017
    42
        return 2;
icculus@3017
    43
    }
icculus@3017
    44
slouken@3040
    45
    if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
aschiffler@7639
    46
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
icculus@3017
    47
        SDL_Quit();
icculus@3017
    48
        return 3;
icculus@3017
    49
    }
icculus@3017
    50
icculus@3017
    51
    if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
icculus@10793
    52
                          spec.format, cvtchans, cvtfreq) == -1) {
aschiffler@7639
    53
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
icculus@3017
    54
        SDL_FreeWAV(data);
icculus@3017
    55
        SDL_Quit();
icculus@3017
    56
        return 4;
icculus@3017
    57
    }
icculus@3017
    58
icculus@3017
    59
    cvt.len = len;
philipp@7444
    60
    cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
slouken@3040
    61
    if (cvt.buf == NULL) {
aschiffler@7639
    62
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
icculus@3017
    63
        SDL_FreeWAV(data);
icculus@3017
    64
        SDL_Quit();
icculus@3017
    65
        return 5;
icculus@3017
    66
    }
philipp@7444
    67
    SDL_memcpy(cvt.buf, data, len);
icculus@3017
    68
slouken@3040
    69
    if (SDL_ConvertAudio(&cvt) == -1) {
aschiffler@7639
    70
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
philipp@7444
    71
        SDL_free(cvt.buf);
icculus@3017
    72
        SDL_FreeWAV(data);
icculus@3017
    73
        SDL_Quit();
icculus@3017
    74
        return 6;
icculus@3017
    75
    }
icculus@3017
    76
icculus@3017
    77
    /* write out a WAV header... */
icculus@3017
    78
    io = SDL_RWFromFile(argv[2], "wb");
slouken@3040
    79
    if (io == NULL) {
aschiffler@7639
    80
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
philipp@7444
    81
        SDL_free(cvt.buf);
icculus@3017
    82
        SDL_FreeWAV(data);
icculus@3017
    83
        SDL_Quit();
icculus@3017
    84
        return 7;
icculus@3017
    85
    }
icculus@3017
    86
icculus@3017
    87
    bitsize = SDL_AUDIO_BITSIZE(spec.format);
icculus@10793
    88
    blockalign = (bitsize / 8) * cvtchans;
icculus@3017
    89
    avgbytes = cvtfreq * blockalign;
icculus@3017
    90
slouken@3040
    91
    SDL_WriteLE32(io, 0x46464952);      /* RIFF */
icculus@10803
    92
    SDL_WriteLE32(io, cvt.len_cvt + 36);
slouken@3040
    93
    SDL_WriteLE32(io, 0x45564157);      /* WAVE */
slouken@3040
    94
    SDL_WriteLE32(io, 0x20746D66);      /* fmt */
slouken@3040
    95
    SDL_WriteLE32(io, 16);      /* chunk size */
icculus@11988
    96
    SDL_WriteLE16(io, SDL_AUDIO_ISFLOAT(spec.format) ? 3 : 1);       /* uncompressed */
icculus@10793
    97
    SDL_WriteLE16(io, cvtchans);   /* channels */
slouken@3040
    98
    SDL_WriteLE32(io, cvtfreq); /* sample rate */
slouken@3040
    99
    SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
slouken@3040
   100
    SDL_WriteLE16(io, blockalign);      /* block align */
slouken@3040
   101
    SDL_WriteLE16(io, bitsize); /* significant bits per sample */
slouken@3040
   102
    SDL_WriteLE32(io, 0x61746164);      /* data */
slouken@3040
   103
    SDL_WriteLE32(io, cvt.len_cvt);     /* size */
icculus@3018
   104
    SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
icculus@3017
   105
slouken@3040
   106
    if (SDL_RWclose(io) == -1) {
aschiffler@7639
   107
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
philipp@7444
   108
        SDL_free(cvt.buf);
icculus@3017
   109
        SDL_FreeWAV(data);
icculus@3017
   110
        SDL_Quit();
icculus@3017
   111
        return 8;
gabomdq@7663
   112
    }                           /* if */
icculus@3017
   113
philipp@7444
   114
    SDL_free(cvt.buf);
icculus@3017
   115
    SDL_FreeWAV(data);
icculus@3017
   116
    SDL_Quit();
icculus@3017
   117
    return 0;
gabomdq@7663
   118
}                               /* main */
icculus@3017
   119
philipp@9747
   120
/* end of testresample.c ... */