test/testresample.c
changeset 7639 9406b7dd2f2d
parent 7517 965d57022c01
child 7663 53fe1b64eb2d
equal deleted inserted replaced
7638:783d1cff9b20 7639:9406b7dd2f2d
     7 
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    10   freely.
    11 */
    11 */
    12 #include <stdio.h>
    12 
    13 #include "SDL.h"
    13 #include "SDL.h"
    14 
    14 
    15 int
    15 int
    16 main(int argc, char **argv)
    16 main(int argc, char **argv)
    17 {
    17 {
    23     int bitsize = 0;
    23     int bitsize = 0;
    24     int blockalign = 0;
    24     int blockalign = 0;
    25     int avgbytes = 0;
    25     int avgbytes = 0;
    26     SDL_RWops *io = NULL;
    26     SDL_RWops *io = NULL;
    27 
    27 
       
    28 	/* Enable standard application logging */
       
    29     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
       
    30 
    28     if (argc != 4) {
    31     if (argc != 4) {
    29         fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
    32         SDL_Log("USAGE: %s in.wav out.wav newfreq\n", argv[0]);
    30         return 1;
    33         return 1;
    31     }
    34     }
    32 
    35 
    33     cvtfreq = SDL_atoi(argv[3]);
    36     cvtfreq = SDL_atoi(argv[3]);
    34 
    37 
    35     if (SDL_Init(SDL_INIT_AUDIO) == -1) {
    38     if (SDL_Init(SDL_INIT_AUDIO) == -1) {
    36         fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
    39         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
    37         return 2;
    40         return 2;
    38     }
    41     }
    39 
    42 
    40     if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
    43     if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
    41         fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
    44         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
    42         SDL_Quit();
    45         SDL_Quit();
    43         return 3;
    46         return 3;
    44     }
    47     }
    45 
    48 
    46     if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
    49     if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
    47                           spec.format, spec.channels, cvtfreq) == -1) {
    50                           spec.format, spec.channels, cvtfreq) == -1) {
    48         fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
    51         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
    49         SDL_FreeWAV(data);
    52         SDL_FreeWAV(data);
    50         SDL_Quit();
    53         SDL_Quit();
    51         return 4;
    54         return 4;
    52     }
    55     }
    53 
    56 
    54     cvt.len = len;
    57     cvt.len = len;
    55     cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
    58     cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
    56     if (cvt.buf == NULL) {
    59     if (cvt.buf == NULL) {
    57         fprintf(stderr, "Out of memory.\n");
    60         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
    58         SDL_FreeWAV(data);
    61         SDL_FreeWAV(data);
    59         SDL_Quit();
    62         SDL_Quit();
    60         return 5;
    63         return 5;
    61     }
    64     }
    62     SDL_memcpy(cvt.buf, data, len);
    65     SDL_memcpy(cvt.buf, data, len);
    63 
    66 
    64     if (SDL_ConvertAudio(&cvt) == -1) {
    67     if (SDL_ConvertAudio(&cvt) == -1) {
    65         fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
    68         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
    66         SDL_free(cvt.buf);
    69         SDL_free(cvt.buf);
    67         SDL_FreeWAV(data);
    70         SDL_FreeWAV(data);
    68         SDL_Quit();
    71         SDL_Quit();
    69         return 6;
    72         return 6;
    70     }
    73     }
    71 
    74 
    72     /* write out a WAV header... */
    75     /* write out a WAV header... */
    73     io = SDL_RWFromFile(argv[2], "wb");
    76     io = SDL_RWFromFile(argv[2], "wb");
    74     if (io == NULL) {
    77     if (io == NULL) {
    75         fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
    78         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
    76         SDL_free(cvt.buf);
    79         SDL_free(cvt.buf);
    77         SDL_FreeWAV(data);
    80         SDL_FreeWAV(data);
    78         SDL_Quit();
    81         SDL_Quit();
    79         return 7;
    82         return 7;
    80     }
    83     }
    97     SDL_WriteLE32(io, 0x61746164);      /* data */
   100     SDL_WriteLE32(io, 0x61746164);      /* data */
    98     SDL_WriteLE32(io, cvt.len_cvt);     /* size */
   101     SDL_WriteLE32(io, cvt.len_cvt);     /* size */
    99     SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
   102     SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
   100 
   103 
   101     if (SDL_RWclose(io) == -1) {
   104     if (SDL_RWclose(io) == -1) {
   102         fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
   105         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
   103         SDL_free(cvt.buf);
   106         SDL_free(cvt.buf);
   104         SDL_FreeWAV(data);
   107         SDL_FreeWAV(data);
   105         SDL_Quit();
   108         SDL_Quit();
   106         return 8;
   109         return 8;
   107     }                           // if
   110     }                           // if