test/testresample.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 17 Aug 2016 21:04:50 +0200
changeset 10275 ba7dc3ebb81e
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
Emscripten: Fixed opening previously closed joystick.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9922
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
    11
*/
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    12
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include "SDL.h"
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    15
int
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    16
main(int argc, char **argv)
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
{
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    SDL_AudioSpec spec;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
    SDL_AudioCVT cvt;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    Uint32 len = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    Uint8 *data = NULL;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
    int cvtfreq = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    int bitsize = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    int blockalign = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
    int avgbytes = 0;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
    SDL_RWops *io = NULL;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9747
diff changeset
    28
    /* Enable standard application logging */
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    29
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    30
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    31
    if (argc != 4) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    32
        SDL_Log("USAGE: %s in.wav out.wav newfreq\n", argv[0]);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
        return 1;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
    36
    cvtfreq = SDL_atoi(argv[3]);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    38
    if (SDL_Init(SDL_INIT_AUDIO) == -1) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    39
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
        return 2;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    43
    if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    44
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
        return 3;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    50
                          spec.format, spec.channels, cvtfreq) == -1) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    51
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
        return 4;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    cvt.len = len;
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
    58
    cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    59
    if (cvt.buf == NULL) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    60
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
        return 5;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    }
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
    65
    SDL_memcpy(cvt.buf, data, len);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    67
    if (SDL_ConvertAudio(&cvt) == -1) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    68
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
    69
        SDL_free(cvt.buf);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        return 6;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    /* write out a WAV header... */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    io = SDL_RWFromFile(argv[2], "wb");
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    77
    if (io == NULL) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    78
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
    79
        SDL_free(cvt.buf);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
        return 7;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    bitsize = SDL_AUDIO_BITSIZE(spec.format);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    blockalign = (bitsize / 8) * spec.channels;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    avgbytes = cvtfreq * blockalign;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    89
    SDL_WriteLE32(io, 0x46464952);      /* RIFF */
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    SDL_WriteLE32(io, len * cvt.len_mult + 36);
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    91
    SDL_WriteLE32(io, 0x45564157);      /* WAVE */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    92
    SDL_WriteLE32(io, 0x20746D66);      /* fmt */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    93
    SDL_WriteLE32(io, 16);      /* chunk size */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    94
    SDL_WriteLE16(io, 1);       /* uncompressed */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    95
    SDL_WriteLE16(io, spec.channels);   /* channels */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    96
    SDL_WriteLE32(io, cvtfreq); /* sample rate */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    97
    SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    98
    SDL_WriteLE16(io, blockalign);      /* block align */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    99
    SDL_WriteLE16(io, bitsize); /* significant bits per sample */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
   100
    SDL_WriteLE32(io, 0x61746164);      /* data */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
   101
    SDL_WriteLE32(io, cvt.len_cvt);     /* size */
3018
d706d3170d7d testresample.c: Write out correct size for resampled buffer.
Ryan C. Gordon <icculus@icculus.org>
parents: 3017
diff changeset
   102
    SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
   104
    if (SDL_RWclose(io) == -1) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   105
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
   106
        SDL_free(cvt.buf);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
        return 8;
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7639
diff changeset
   110
    }                           /* if */
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
   112
    SDL_free(cvt.buf);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    return 0;
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7639
diff changeset
   116
}                               /* main */
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
9747
58c7e994318a Fixed comment in test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   118
/* end of testresample.c ... */