🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.9 KiB

  1. /*
  2. Copyright (C) 1997-2023 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 main(int argc, char **argv)
  12. {
  13. SDL_AudioSpec spec;
  14. SDL_AudioCVT cvt;
  15. Uint32 len = 0;
  16. Uint8 *data = NULL;
  17. int cvtfreq = 0;
  18. int cvtchans = 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 != 5) {
  26. SDL_Log("USAGE: %s in.wav out.wav newfreq newchans\n", argv[0]);
  27. return 1;
  28. }
  29. cvtfreq = SDL_atoi(argv[3]);
  30. cvtchans = SDL_atoi(argv[4]);
  31. if (SDL_Init(SDL_INIT_AUDIO) == -1) {
  32. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
  33. return 2;
  34. }
  35. if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
  36. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to load %s: %s\n", argv[1], SDL_GetError());
  37. SDL_Quit();
  38. return 3;
  39. }
  40. if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
  41. spec.format, cvtchans, cvtfreq) == -1) {
  42. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "failed to build CVT: %s\n", SDL_GetError());
  43. SDL_FreeWAV(data);
  44. SDL_Quit();
  45. return 4;
  46. }
  47. cvt.len = len;
  48. cvt.buf = (Uint8 *)SDL_malloc((size_t)len * cvt.len_mult);
  49. if (cvt.buf == NULL) {
  50. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory.\n");
  51. SDL_FreeWAV(data);
  52. SDL_Quit();
  53. return 5;
  54. }
  55. SDL_memcpy(cvt.buf, data, len);
  56. if (SDL_ConvertAudio(&cvt) == -1) {
  57. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Conversion failed: %s\n", SDL_GetError());
  58. SDL_free(cvt.buf);
  59. SDL_FreeWAV(data);
  60. SDL_Quit();
  61. return 6;
  62. }
  63. /* write out a WAV header... */
  64. io = SDL_RWFromFile(argv[2], "wb");
  65. if (io == NULL) {
  66. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
  67. SDL_free(cvt.buf);
  68. SDL_FreeWAV(data);
  69. SDL_Quit();
  70. return 7;
  71. }
  72. bitsize = SDL_AUDIO_BITSIZE(spec.format);
  73. blockalign = (bitsize / 8) * cvtchans;
  74. avgbytes = cvtfreq * blockalign;
  75. SDL_WriteLE32(io, 0x46464952); /* RIFF */
  76. SDL_WriteLE32(io, cvt.len_cvt + 36);
  77. SDL_WriteLE32(io, 0x45564157); /* WAVE */
  78. SDL_WriteLE32(io, 0x20746D66); /* fmt */
  79. SDL_WriteLE32(io, 16); /* chunk size */
  80. SDL_WriteLE16(io, SDL_AUDIO_ISFLOAT(spec.format) ? 3 : 1); /* uncompressed */
  81. SDL_WriteLE16(io, cvtchans); /* channels */
  82. SDL_WriteLE32(io, cvtfreq); /* sample rate */
  83. SDL_WriteLE32(io, avgbytes); /* average bytes per second */
  84. SDL_WriteLE16(io, blockalign); /* block align */
  85. SDL_WriteLE16(io, bitsize); /* significant bits per sample */
  86. SDL_WriteLE32(io, 0x61746164); /* data */
  87. SDL_WriteLE32(io, cvt.len_cvt); /* size */
  88. SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
  89. if (SDL_RWclose(io) == -1) {
  90. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
  91. SDL_free(cvt.buf);
  92. SDL_FreeWAV(data);
  93. SDL_Quit();
  94. return 8;
  95. } /* if */
  96. SDL_free(cvt.buf);
  97. SDL_FreeWAV(data);
  98. SDL_Quit();
  99. return 0;
  100. } /* main */
  101. /* end of testresample.c ... */