test/testresample.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 23 May 2019 14:19:00 -0700
changeset 12756 4116bf232fe7
parent 12503 806492103856
permissions -rw-r--r--
Added a function to get the current Android SDK version at runtime
     1 /*
     2   Copyright (C) 1997-2019 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 cvtchans = 0;
    24     int bitsize = 0;
    25     int blockalign = 0;
    26     int avgbytes = 0;
    27     SDL_RWops *io = NULL;
    28 
    29     /* Enable standard application logging */
    30     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    31 
    32     if (argc != 5) {
    33         SDL_Log("USAGE: %s in.wav out.wav newfreq newchans\n", argv[0]);
    34         return 1;
    35     }
    36 
    37     cvtfreq = SDL_atoi(argv[3]);
    38     cvtchans = SDL_atoi(argv[4]);
    39 
    40     if (SDL_Init(SDL_INIT_AUDIO) == -1) {
    41         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
    42         return 2;
    43     }
    44 
    45     if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
    46         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
    47         SDL_Quit();
    48         return 3;
    49     }
    50 
    51     if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
    52                           spec.format, cvtchans, cvtfreq) == -1) {
    53         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
    54         SDL_FreeWAV(data);
    55         SDL_Quit();
    56         return 4;
    57     }
    58 
    59     cvt.len = len;
    60     cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
    61     if (cvt.buf == NULL) {
    62         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
    63         SDL_FreeWAV(data);
    64         SDL_Quit();
    65         return 5;
    66     }
    67     SDL_memcpy(cvt.buf, data, len);
    68 
    69     if (SDL_ConvertAudio(&cvt) == -1) {
    70         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
    71         SDL_free(cvt.buf);
    72         SDL_FreeWAV(data);
    73         SDL_Quit();
    74         return 6;
    75     }
    76 
    77     /* write out a WAV header... */
    78     io = SDL_RWFromFile(argv[2], "wb");
    79     if (io == NULL) {
    80         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
    81         SDL_free(cvt.buf);
    82         SDL_FreeWAV(data);
    83         SDL_Quit();
    84         return 7;
    85     }
    86 
    87     bitsize = SDL_AUDIO_BITSIZE(spec.format);
    88     blockalign = (bitsize / 8) * cvtchans;
    89     avgbytes = cvtfreq * blockalign;
    90 
    91     SDL_WriteLE32(io, 0x46464952);      /* RIFF */
    92     SDL_WriteLE32(io, cvt.len_cvt + 36);
    93     SDL_WriteLE32(io, 0x45564157);      /* WAVE */
    94     SDL_WriteLE32(io, 0x20746D66);      /* fmt */
    95     SDL_WriteLE32(io, 16);      /* chunk size */
    96     SDL_WriteLE16(io, SDL_AUDIO_ISFLOAT(spec.format) ? 3 : 1);       /* uncompressed */
    97     SDL_WriteLE16(io, cvtchans);   /* channels */
    98     SDL_WriteLE32(io, cvtfreq); /* sample rate */
    99     SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
   100     SDL_WriteLE16(io, blockalign);      /* block align */
   101     SDL_WriteLE16(io, bitsize); /* significant bits per sample */
   102     SDL_WriteLE32(io, 0x61746164);      /* data */
   103     SDL_WriteLE32(io, cvt.len_cvt);     /* size */
   104     SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
   105 
   106     if (SDL_RWclose(io) == -1) {
   107         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
   108         SDL_free(cvt.buf);
   109         SDL_FreeWAV(data);
   110         SDL_Quit();
   111         return 8;
   112     }                           /* if */
   113 
   114     SDL_free(cvt.buf);
   115     SDL_FreeWAV(data);
   116     SDL_Quit();
   117     return 0;
   118 }                               /* main */
   119 
   120 /* end of testresample.c ... */