testresample.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include "SDL.h"
  11. int
  12. main(int argc, char **argv)
  13. {
  14. SDL_AudioSpec spec;
  15. SDL_AudioCVT cvt;
  16. Uint32 len = 0;
  17. Uint8 *data = NULL;
  18. int cvtfreq = 0;
  19. int bitsize = 0;
  20. int blockalign = 0;
  21. int avgbytes = 0;
  22. SDL_RWops *io = NULL;
  23. /* Enable standard application logging */
  24. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  25. if (argc != 4) {
  26. SDL_Log("USAGE: %s in.wav out.wav newfreq\n", argv[0]);
  27. return 1;
  28. }
  29. cvtfreq = SDL_atoi(argv[3]);
  30. if (SDL_Init(SDL_INIT_AUDIO) == -1) {
  31. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
  32. return 2;
  33. }
  34. if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
  35. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
  36. SDL_Quit();
  37. return 3;
  38. }
  39. if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
  40. spec.format, spec.channels, cvtfreq) == -1) {
  41. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
  42. SDL_FreeWAV(data);
  43. SDL_Quit();
  44. return 4;
  45. }
  46. cvt.len = len;
  47. cvt.buf = (Uint8 *) SDL_malloc(len * cvt.len_mult);
  48. if (cvt.buf == NULL) {
  49. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
  50. SDL_FreeWAV(data);
  51. SDL_Quit();
  52. return 5;
  53. }
  54. SDL_memcpy(cvt.buf, data, len);
  55. if (SDL_ConvertAudio(&cvt) == -1) {
  56. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
  57. SDL_free(cvt.buf);
  58. SDL_FreeWAV(data);
  59. SDL_Quit();
  60. return 6;
  61. }
  62. /* write out a WAV header... */
  63. io = SDL_RWFromFile(argv[2], "wb");
  64. if (io == NULL) {
  65. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
  66. SDL_free(cvt.buf);
  67. SDL_FreeWAV(data);
  68. SDL_Quit();
  69. return 7;
  70. }
  71. bitsize = SDL_AUDIO_BITSIZE(spec.format);
  72. blockalign = (bitsize / 8) * spec.channels;
  73. avgbytes = cvtfreq * blockalign;
  74. SDL_WriteLE32(io, 0x46464952); /* RIFF */
  75. SDL_WriteLE32(io, len * cvt.len_mult + 36);
  76. SDL_WriteLE32(io, 0x45564157); /* WAVE */
  77. SDL_WriteLE32(io, 0x20746D66); /* fmt */
  78. SDL_WriteLE32(io, 16); /* chunk size */
  79. SDL_WriteLE16(io, 1); /* uncompressed */
  80. SDL_WriteLE16(io, spec.channels); /* channels */
  81. SDL_WriteLE32(io, cvtfreq); /* sample rate */
  82. SDL_WriteLE32(io, avgbytes); /* average bytes per second */
  83. SDL_WriteLE16(io, blockalign); /* block align */
  84. SDL_WriteLE16(io, bitsize); /* significant bits per sample */
  85. SDL_WriteLE32(io, 0x61746164); /* data */
  86. SDL_WriteLE32(io, cvt.len_cvt); /* size */
  87. SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
  88. if (SDL_RWclose(io) == -1) {
  89. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
  90. SDL_free(cvt.buf);
  91. SDL_FreeWAV(data);
  92. SDL_Quit();
  93. return 8;
  94. } /* if */
  95. SDL_free(cvt.buf);
  96. SDL_FreeWAV(data);
  97. SDL_Quit();
  98. return 0;
  99. } /* main */
  100. /* end of resample_test.c ... */