test/testresample.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 14 Jul 2013 13:33:54 +0200
changeset 7444 3b9b7bcee64f
parent 5535 96594ac5fd1a
child 7517 965d57022c01
permissions -rw-r--r--
Fixed compiler warnings in test program by using wrapped functions.
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
/*
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3040
diff changeset
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
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
*/
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include <stdio.h>
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
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    28
    if (argc != 4) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
        fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
        return 1;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
    33
    cvtfreq = SDL_atoi(argv[3]);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    35
    if (SDL_Init(SDL_INIT_AUDIO) == -1) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
        fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
        return 2;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    40
    if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
        fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
        return 3;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    47
                          spec.format, spec.channels, cvtfreq) == -1) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
        fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
        return 4;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    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
    55
    cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    56
    if (cvt.buf == NULL) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
        fprintf(stderr, "Out of memory.\n");
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        return 5;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    }
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
    62
    SDL_memcpy(cvt.buf, data, len);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    64
    if (SDL_ConvertAudio(&cvt) == -1) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
        fprintf(stderr, "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
    66
        SDL_free(cvt.buf);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
        return 6;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    /* write out a WAV header... */
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    io = SDL_RWFromFile(argv[2], "wb");
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    74
    if (io == NULL) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
        fprintf(stderr, "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
    76
        SDL_free(cvt.buf);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
        return 7;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    }
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    bitsize = SDL_AUDIO_BITSIZE(spec.format);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    blockalign = (bitsize / 8) * spec.channels;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    avgbytes = cvtfreq * blockalign;
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    86
    SDL_WriteLE32(io, 0x46464952);      /* RIFF */
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    SDL_WriteLE32(io, len * cvt.len_mult + 36);
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    88
    SDL_WriteLE32(io, 0x45564157);      /* WAVE */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    89
    SDL_WriteLE32(io, 0x20746D66);      /* fmt */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    90
    SDL_WriteLE32(io, 16);      /* chunk size */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    91
    SDL_WriteLE16(io, 1);       /* uncompressed */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    92
    SDL_WriteLE16(io, spec.channels);   /* channels */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    93
    SDL_WriteLE32(io, cvtfreq); /* sample rate */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    94
    SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    95
    SDL_WriteLE16(io, blockalign);      /* block align */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    96
    SDL_WriteLE16(io, bitsize); /* significant bits per sample */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    97
    SDL_WriteLE32(io, 0x61746164);      /* data */
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
    98
    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
    99
    SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
   101
    if (SDL_RWclose(io) == -1) {
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        fprintf(stderr, "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
   103
        SDL_free(cvt.buf);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
        SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
        SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        return 8;
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
   107
    }                           // if
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
7444
3b9b7bcee64f Fixed compiler warnings in test program by using wrapped functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 5535
diff changeset
   109
    SDL_free(cvt.buf);
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    SDL_FreeWAV(data);
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    SDL_Quit();
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    return 0;
3040
Sam Lantinga <slouken@libsdl.org>
parents: 3018
diff changeset
   113
}                               // main
3017
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
3272431eeee2 Added testresample.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
// end of resample_test.c ...