test/testresample.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 25 Nov 2015 21:39:28 +0100
changeset 9922 b066d5fd5fa7
parent 9747 58c7e994318a
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Replaced tabs with spaces in test programs.
slouken@5535
     1
/*
slouken@9619
     2
  Copyright (C) 1997-2015 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@3017
    23
    int bitsize = 0;
icculus@3017
    24
    int blockalign = 0;
icculus@3017
    25
    int avgbytes = 0;
icculus@3017
    26
    SDL_RWops *io = NULL;
icculus@3017
    27
philipp@9922
    28
    /* Enable standard application logging */
aschiffler@7639
    29
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    30
slouken@3040
    31
    if (argc != 4) {
aschiffler@7639
    32
        SDL_Log("USAGE: %s in.wav out.wav newfreq\n", argv[0]);
icculus@3017
    33
        return 1;
icculus@3017
    34
    }
icculus@3017
    35
philipp@7444
    36
    cvtfreq = SDL_atoi(argv[3]);
icculus@3017
    37
slouken@3040
    38
    if (SDL_Init(SDL_INIT_AUDIO) == -1) {
aschiffler@7639
    39
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
icculus@3017
    40
        return 2;
icculus@3017
    41
    }
icculus@3017
    42
slouken@3040
    43
    if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
aschiffler@7639
    44
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
icculus@3017
    45
        SDL_Quit();
icculus@3017
    46
        return 3;
icculus@3017
    47
    }
icculus@3017
    48
icculus@3017
    49
    if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
slouken@3040
    50
                          spec.format, spec.channels, cvtfreq) == -1) {
aschiffler@7639
    51
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
icculus@3017
    52
        SDL_FreeWAV(data);
icculus@3017
    53
        SDL_Quit();
icculus@3017
    54
        return 4;
icculus@3017
    55
    }
icculus@3017
    56
icculus@3017
    57
    cvt.len = len;
philipp@7444
    58
    cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
slouken@3040
    59
    if (cvt.buf == NULL) {
aschiffler@7639
    60
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
icculus@3017
    61
        SDL_FreeWAV(data);
icculus@3017
    62
        SDL_Quit();
icculus@3017
    63
        return 5;
icculus@3017
    64
    }
philipp@7444
    65
    SDL_memcpy(cvt.buf, data, len);
icculus@3017
    66
slouken@3040
    67
    if (SDL_ConvertAudio(&cvt) == -1) {
aschiffler@7639
    68
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
philipp@7444
    69
        SDL_free(cvt.buf);
icculus@3017
    70
        SDL_FreeWAV(data);
icculus@3017
    71
        SDL_Quit();
icculus@3017
    72
        return 6;
icculus@3017
    73
    }
icculus@3017
    74
icculus@3017
    75
    /* write out a WAV header... */
icculus@3017
    76
    io = SDL_RWFromFile(argv[2], "wb");
slouken@3040
    77
    if (io == NULL) {
aschiffler@7639
    78
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
philipp@7444
    79
        SDL_free(cvt.buf);
icculus@3017
    80
        SDL_FreeWAV(data);
icculus@3017
    81
        SDL_Quit();
icculus@3017
    82
        return 7;
icculus@3017
    83
    }
icculus@3017
    84
icculus@3017
    85
    bitsize = SDL_AUDIO_BITSIZE(spec.format);
icculus@3017
    86
    blockalign = (bitsize / 8) * spec.channels;
icculus@3017
    87
    avgbytes = cvtfreq * blockalign;
icculus@3017
    88
slouken@3040
    89
    SDL_WriteLE32(io, 0x46464952);      /* RIFF */
icculus@3017
    90
    SDL_WriteLE32(io, len * cvt.len_mult + 36);
slouken@3040
    91
    SDL_WriteLE32(io, 0x45564157);      /* WAVE */
slouken@3040
    92
    SDL_WriteLE32(io, 0x20746D66);      /* fmt */
slouken@3040
    93
    SDL_WriteLE32(io, 16);      /* chunk size */
slouken@3040
    94
    SDL_WriteLE16(io, 1);       /* uncompressed */
slouken@3040
    95
    SDL_WriteLE16(io, spec.channels);   /* channels */
slouken@3040
    96
    SDL_WriteLE32(io, cvtfreq); /* sample rate */
slouken@3040
    97
    SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
slouken@3040
    98
    SDL_WriteLE16(io, blockalign);      /* block align */
slouken@3040
    99
    SDL_WriteLE16(io, bitsize); /* significant bits per sample */
slouken@3040
   100
    SDL_WriteLE32(io, 0x61746164);      /* data */
slouken@3040
   101
    SDL_WriteLE32(io, cvt.len_cvt);     /* size */
icculus@3018
   102
    SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
icculus@3017
   103
slouken@3040
   104
    if (SDL_RWclose(io) == -1) {
aschiffler@7639
   105
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
philipp@7444
   106
        SDL_free(cvt.buf);
icculus@3017
   107
        SDL_FreeWAV(data);
icculus@3017
   108
        SDL_Quit();
icculus@3017
   109
        return 8;
gabomdq@7663
   110
    }                           /* if */
icculus@3017
   111
philipp@7444
   112
    SDL_free(cvt.buf);
icculus@3017
   113
    SDL_FreeWAV(data);
icculus@3017
   114
    SDL_Quit();
icculus@3017
   115
    return 0;
gabomdq@7663
   116
}                               /* main */
icculus@3017
   117
philipp@9747
   118
/* end of testresample.c ... */