test/testresample.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 03 Jun 2013 19:24:18 -0700
changeset 7268 b399d0441810
parent 5535 96594ac5fd1a
child 7444 3b9b7bcee64f
permissions -rw-r--r--
Add missing VS2012 test projects; update VS2010 and VS2012 solutions; update keybord suite for VS compiler warnings
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 #include <stdio.h>
    13 #include "SDL.h"
    14 
    15 int
    16 main(int argc, char **argv)
    17 {
    18     SDL_AudioSpec spec;
    19     SDL_AudioCVT cvt;
    20     Uint32 len = 0;
    21     Uint8 *data = NULL;
    22     int cvtfreq = 0;
    23     int bitsize = 0;
    24     int blockalign = 0;
    25     int avgbytes = 0;
    26     SDL_RWops *io = NULL;
    27 
    28     if (argc != 4) {
    29         fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
    30         return 1;
    31     }
    32 
    33     cvtfreq = atoi(argv[3]);
    34 
    35     if (SDL_Init(SDL_INIT_AUDIO) == -1) {
    36         fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
    37         return 2;
    38     }
    39 
    40     if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
    41         fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
    42         SDL_Quit();
    43         return 3;
    44     }
    45 
    46     if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
    47                           spec.format, spec.channels, cvtfreq) == -1) {
    48         fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
    49         SDL_FreeWAV(data);
    50         SDL_Quit();
    51         return 4;
    52     }
    53 
    54     cvt.len = len;
    55     cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
    56     if (cvt.buf == NULL) {
    57         fprintf(stderr, "Out of memory.\n");
    58         SDL_FreeWAV(data);
    59         SDL_Quit();
    60         return 5;
    61     }
    62     memcpy(cvt.buf, data, len);
    63 
    64     if (SDL_ConvertAudio(&cvt) == -1) {
    65         fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
    66         free(cvt.buf);
    67         SDL_FreeWAV(data);
    68         SDL_Quit();
    69         return 6;
    70     }
    71 
    72     /* write out a WAV header... */
    73     io = SDL_RWFromFile(argv[2], "wb");
    74     if (io == NULL) {
    75         fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
    76         free(cvt.buf);
    77         SDL_FreeWAV(data);
    78         SDL_Quit();
    79         return 7;
    80     }
    81 
    82     bitsize = SDL_AUDIO_BITSIZE(spec.format);
    83     blockalign = (bitsize / 8) * spec.channels;
    84     avgbytes = cvtfreq * blockalign;
    85 
    86     SDL_WriteLE32(io, 0x46464952);      /* RIFF */
    87     SDL_WriteLE32(io, len * cvt.len_mult + 36);
    88     SDL_WriteLE32(io, 0x45564157);      /* WAVE */
    89     SDL_WriteLE32(io, 0x20746D66);      /* fmt */
    90     SDL_WriteLE32(io, 16);      /* chunk size */
    91     SDL_WriteLE16(io, 1);       /* uncompressed */
    92     SDL_WriteLE16(io, spec.channels);   /* channels */
    93     SDL_WriteLE32(io, cvtfreq); /* sample rate */
    94     SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
    95     SDL_WriteLE16(io, blockalign);      /* block align */
    96     SDL_WriteLE16(io, bitsize); /* significant bits per sample */
    97     SDL_WriteLE32(io, 0x61746164);      /* data */
    98     SDL_WriteLE32(io, cvt.len_cvt);     /* size */
    99     SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
   100 
   101     if (SDL_RWclose(io) == -1) {
   102         fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
   103         free(cvt.buf);
   104         SDL_FreeWAV(data);
   105         SDL_Quit();
   106         return 8;
   107     }                           // if
   108 
   109     free(cvt.buf);
   110     SDL_FreeWAV(data);
   111     SDL_Quit();
   112     return 0;
   113 }                               // main
   114 
   115 // end of resample_test.c ...