test/testresample.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 05 Jun 2013 22:33:10 -0700
changeset 7286 04087a99e3f4
parent 5535 96594ac5fd1a
child 7444 3b9b7bcee64f
permissions -rw-r--r--
Added testautomation to the Visual Studio 2008 project.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 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
*/
icculus@3017
    12
#include <stdio.h>
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
slouken@3040
    28
    if (argc != 4) {
icculus@3017
    29
        fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
icculus@3017
    30
        return 1;
icculus@3017
    31
    }
icculus@3017
    32
icculus@3017
    33
    cvtfreq = atoi(argv[3]);
icculus@3017
    34
slouken@3040
    35
    if (SDL_Init(SDL_INIT_AUDIO) == -1) {
icculus@3017
    36
        fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
icculus@3017
    37
        return 2;
icculus@3017
    38
    }
icculus@3017
    39
slouken@3040
    40
    if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
icculus@3017
    41
        fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
icculus@3017
    42
        SDL_Quit();
icculus@3017
    43
        return 3;
icculus@3017
    44
    }
icculus@3017
    45
icculus@3017
    46
    if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
slouken@3040
    47
                          spec.format, spec.channels, cvtfreq) == -1) {
icculus@3017
    48
        fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
icculus@3017
    49
        SDL_FreeWAV(data);
icculus@3017
    50
        SDL_Quit();
icculus@3017
    51
        return 4;
icculus@3017
    52
    }
icculus@3017
    53
icculus@3017
    54
    cvt.len = len;
icculus@3017
    55
    cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
slouken@3040
    56
    if (cvt.buf == NULL) {
icculus@3017
    57
        fprintf(stderr, "Out of memory.\n");
icculus@3017
    58
        SDL_FreeWAV(data);
icculus@3017
    59
        SDL_Quit();
icculus@3017
    60
        return 5;
icculus@3017
    61
    }
icculus@3017
    62
    memcpy(cvt.buf, data, len);
icculus@3017
    63
slouken@3040
    64
    if (SDL_ConvertAudio(&cvt) == -1) {
icculus@3017
    65
        fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
icculus@3017
    66
        free(cvt.buf);
icculus@3017
    67
        SDL_FreeWAV(data);
icculus@3017
    68
        SDL_Quit();
icculus@3017
    69
        return 6;
icculus@3017
    70
    }
icculus@3017
    71
icculus@3017
    72
    /* write out a WAV header... */
icculus@3017
    73
    io = SDL_RWFromFile(argv[2], "wb");
slouken@3040
    74
    if (io == NULL) {
icculus@3017
    75
        fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
icculus@3017
    76
        free(cvt.buf);
icculus@3017
    77
        SDL_FreeWAV(data);
icculus@3017
    78
        SDL_Quit();
icculus@3017
    79
        return 7;
icculus@3017
    80
    }
icculus@3017
    81
icculus@3017
    82
    bitsize = SDL_AUDIO_BITSIZE(spec.format);
icculus@3017
    83
    blockalign = (bitsize / 8) * spec.channels;
icculus@3017
    84
    avgbytes = cvtfreq * blockalign;
icculus@3017
    85
slouken@3040
    86
    SDL_WriteLE32(io, 0x46464952);      /* RIFF */
icculus@3017
    87
    SDL_WriteLE32(io, len * cvt.len_mult + 36);
slouken@3040
    88
    SDL_WriteLE32(io, 0x45564157);      /* WAVE */
slouken@3040
    89
    SDL_WriteLE32(io, 0x20746D66);      /* fmt */
slouken@3040
    90
    SDL_WriteLE32(io, 16);      /* chunk size */
slouken@3040
    91
    SDL_WriteLE16(io, 1);       /* uncompressed */
slouken@3040
    92
    SDL_WriteLE16(io, spec.channels);   /* channels */
slouken@3040
    93
    SDL_WriteLE32(io, cvtfreq); /* sample rate */
slouken@3040
    94
    SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
slouken@3040
    95
    SDL_WriteLE16(io, blockalign);      /* block align */
slouken@3040
    96
    SDL_WriteLE16(io, bitsize); /* significant bits per sample */
slouken@3040
    97
    SDL_WriteLE32(io, 0x61746164);      /* data */
slouken@3040
    98
    SDL_WriteLE32(io, cvt.len_cvt);     /* size */
icculus@3018
    99
    SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
icculus@3017
   100
slouken@3040
   101
    if (SDL_RWclose(io) == -1) {
icculus@3017
   102
        fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
icculus@3017
   103
        free(cvt.buf);
icculus@3017
   104
        SDL_FreeWAV(data);
icculus@3017
   105
        SDL_Quit();
icculus@3017
   106
        return 8;
slouken@3040
   107
    }                           // if
icculus@3017
   108
icculus@3017
   109
    free(cvt.buf);
icculus@3017
   110
    SDL_FreeWAV(data);
icculus@3017
   111
    SDL_Quit();
icculus@3017
   112
    return 0;
slouken@3040
   113
}                               // main
icculus@3017
   114
icculus@3017
   115
// end of resample_test.c ...