load_ogg.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. SDL_mixer: An audio mixer library based on the SDL library
  3. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  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. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. This is the source needed to decode an Ogg Vorbis into a waveform.
  18. This file by Vaclav Slavik ([email protected]).
  19. */
  20. /* $Id$ */
  21. #ifdef OGG_MUSIC
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "SDL_mutex.h"
  26. #include "SDL_endian.h"
  27. #include "SDL_timer.h"
  28. #include "SDL_mixer.h"
  29. #include "dynamic_ogg.h"
  30. #include "load_ogg.h"
  31. static size_t sdl_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
  32. {
  33. return SDL_RWread((SDL_RWops*)datasource, ptr, size, nmemb);
  34. }
  35. static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
  36. {
  37. return (int)SDL_RWseek((SDL_RWops*)datasource, offset, whence);
  38. }
  39. static int sdl_close_func_freesrc(void *datasource)
  40. {
  41. return SDL_RWclose((SDL_RWops*)datasource);
  42. }
  43. static int sdl_close_func_nofreesrc(void *datasource)
  44. {
  45. SDL_RWseek((SDL_RWops*)datasource, 0, RW_SEEK_SET);
  46. return 0;
  47. }
  48. static long sdl_tell_func(void *datasource)
  49. {
  50. return (long)SDL_RWtell((SDL_RWops*)datasource);
  51. }
  52. /* don't call this directly; use Mix_LoadWAV_RW() for now. */
  53. SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc,
  54. SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
  55. {
  56. OggVorbis_File vf;
  57. ov_callbacks callbacks;
  58. vorbis_info *info;
  59. Uint8 *buf;
  60. int bitstream = -1;
  61. long samplesize;
  62. long samples;
  63. int read, to_read;
  64. int must_close = 1;
  65. int was_error = 1;
  66. if ( (!src) || (!audio_buf) || (!audio_len) ) /* sanity checks. */
  67. goto done;
  68. if ( !Mix_Init(MIX_INIT_OGG) )
  69. goto done;
  70. callbacks.read_func = sdl_read_func;
  71. callbacks.seek_func = sdl_seek_func;
  72. callbacks.tell_func = sdl_tell_func;
  73. callbacks.close_func = freesrc ?
  74. sdl_close_func_freesrc : sdl_close_func_nofreesrc;
  75. if (vorbis.ov_open_callbacks(src, &vf, NULL, 0, callbacks) != 0)
  76. {
  77. SDL_SetError("OGG bitstream is not valid Vorbis stream!");
  78. goto done;
  79. }
  80. must_close = 0;
  81. info = vorbis.ov_info(&vf, -1);
  82. *audio_buf = NULL;
  83. *audio_len = 0;
  84. memset(spec, '\0', sizeof (SDL_AudioSpec));
  85. spec->format = AUDIO_S16;
  86. spec->channels = info->channels;
  87. spec->freq = info->rate;
  88. spec->samples = 4096; /* buffer size */
  89. samples = (long)vorbis.ov_pcm_total(&vf, -1);
  90. *audio_len = spec->size = samples * spec->channels * 2;
  91. *audio_buf = (Uint8 *)SDL_malloc(*audio_len);
  92. if (*audio_buf == NULL)
  93. goto done;
  94. buf = *audio_buf;
  95. to_read = *audio_len;
  96. #ifdef OGG_USE_TREMOR
  97. for (read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream);
  98. read > 0;
  99. read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream))
  100. #else
  101. for (read = vorbis.ov_read(&vf, (char *)buf, to_read, 0/*LE*/, 2/*16bit*/, 1/*signed*/, &bitstream);
  102. read > 0;
  103. read = vorbis.ov_read(&vf, (char *)buf, to_read, 0, 2, 1, &bitstream))
  104. #endif
  105. {
  106. if (read == OV_HOLE || read == OV_EBADLINK)
  107. break; /* error */
  108. to_read -= read;
  109. buf += read;
  110. }
  111. vorbis.ov_clear(&vf);
  112. was_error = 0;
  113. /* Don't return a buffer that isn't a multiple of samplesize */
  114. samplesize = ((spec->format & 0xFF)/8)*spec->channels;
  115. *audio_len &= ~(samplesize-1);
  116. done:
  117. if (freesrc && src && must_close) {
  118. SDL_RWclose(src);
  119. }
  120. if (was_error) {
  121. spec = NULL;
  122. }
  123. return(spec);
  124. } /* Mix_LoadOGG_RW */
  125. /* end of load_ogg.c ... */
  126. #endif