test/testresample.c
author Edward Rudd <urkle@outoforder.cc>
Sat, 12 Oct 2013 11:21:40 -0400
changeset 7807 73da2747c268
parent 7663 53fe1b64eb2d
child 8149 681eb46b8ac4
permissions -rw-r--r--
added simple Drag & drop test
     1 /*
     2   Copyright (C) 1997-2013 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 
    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 	/* Enable standard application logging */
    29     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    30 
    31     if (argc != 4) {
    32         SDL_Log("USAGE: %s in.wav out.wav newfreq\n", argv[0]);
    33         return 1;
    34     }
    35 
    36     cvtfreq = SDL_atoi(argv[3]);
    37 
    38     if (SDL_Init(SDL_INIT_AUDIO) == -1) {
    39         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
    40         return 2;
    41     }
    42 
    43     if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
    44         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
    45         SDL_Quit();
    46         return 3;
    47     }
    48 
    49     if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
    50                           spec.format, spec.channels, cvtfreq) == -1) {
    51         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
    52         SDL_FreeWAV(data);
    53         SDL_Quit();
    54         return 4;
    55     }
    56 
    57     cvt.len = len;
    58     cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
    59     if (cvt.buf == NULL) {
    60         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
    61         SDL_FreeWAV(data);
    62         SDL_Quit();
    63         return 5;
    64     }
    65     SDL_memcpy(cvt.buf, data, len);
    66 
    67     if (SDL_ConvertAudio(&cvt) == -1) {
    68         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
    69         SDL_free(cvt.buf);
    70         SDL_FreeWAV(data);
    71         SDL_Quit();
    72         return 6;
    73     }
    74 
    75     /* write out a WAV header... */
    76     io = SDL_RWFromFile(argv[2], "wb");
    77     if (io == NULL) {
    78         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
    79         SDL_free(cvt.buf);
    80         SDL_FreeWAV(data);
    81         SDL_Quit();
    82         return 7;
    83     }
    84 
    85     bitsize = SDL_AUDIO_BITSIZE(spec.format);
    86     blockalign = (bitsize / 8) * spec.channels;
    87     avgbytes = cvtfreq * blockalign;
    88 
    89     SDL_WriteLE32(io, 0x46464952);      /* RIFF */
    90     SDL_WriteLE32(io, len * cvt.len_mult + 36);
    91     SDL_WriteLE32(io, 0x45564157);      /* WAVE */
    92     SDL_WriteLE32(io, 0x20746D66);      /* fmt */
    93     SDL_WriteLE32(io, 16);      /* chunk size */
    94     SDL_WriteLE16(io, 1);       /* uncompressed */
    95     SDL_WriteLE16(io, spec.channels);   /* channels */
    96     SDL_WriteLE32(io, cvtfreq); /* sample rate */
    97     SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
    98     SDL_WriteLE16(io, blockalign);      /* block align */
    99     SDL_WriteLE16(io, bitsize); /* significant bits per sample */
   100     SDL_WriteLE32(io, 0x61746164);      /* data */
   101     SDL_WriteLE32(io, cvt.len_cvt);     /* size */
   102     SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
   103 
   104     if (SDL_RWclose(io) == -1) {
   105         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
   106         SDL_free(cvt.buf);
   107         SDL_FreeWAV(data);
   108         SDL_Quit();
   109         return 8;
   110     }                           /* if */
   111 
   112     SDL_free(cvt.buf);
   113     SDL_FreeWAV(data);
   114     SDL_Quit();
   115     return 0;
   116 }                               /* main */
   117 
   118 /* end of resample_test.c ... */