audio_stream_ogg_vorbis.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*************************************************************************/
  2. /* audio_stream_ogg_vorbis.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "audio_stream_ogg_vorbis.h"
  31. size_t AudioStreamPlaybackOGGVorbis::_ov_read_func(void *p_dst, size_t p_data, size_t p_count, void *_f) {
  32. //printf("read to %p, %i bytes, %i nmemb, %p\n",p_dst,p_data,p_count,_f);
  33. FileAccess *fa = (FileAccess *)_f;
  34. size_t read_total = p_data * p_count;
  35. if (fa->eof_reached())
  36. return 0;
  37. uint8_t *dst = (uint8_t *)p_dst;
  38. int read = fa->get_buffer(dst, read_total);
  39. return read;
  40. }
  41. int AudioStreamPlaybackOGGVorbis::_ov_seek_func(void *_f, ogg_int64_t offs, int whence) {
  42. //printf("seek to %p, offs %i, whence %i\n",_f,(int)offs,whence);
  43. #ifdef SEEK_SET
  44. //printf("seek set defined\n");
  45. FileAccess *fa = (FileAccess *)_f;
  46. if (whence == SEEK_SET) {
  47. fa->seek(offs);
  48. } else if (whence == SEEK_CUR) {
  49. fa->seek(fa->get_position() + offs);
  50. } else if (whence == SEEK_END) {
  51. fa->seek_end(offs);
  52. } else {
  53. ERR_PRINT("Vorbis seek function failure: Unexpected value in _whence\n");
  54. }
  55. int ret = fa->eof_reached() ? -1 : 0;
  56. //printf("returning %i\n",ret);
  57. return ret;
  58. #else
  59. return -1; // no seeking
  60. #endif
  61. }
  62. int AudioStreamPlaybackOGGVorbis::_ov_close_func(void *_f) {
  63. //printf("close %p\n",_f);
  64. if (!_f)
  65. return 0;
  66. FileAccess *fa = (FileAccess *)_f;
  67. if (fa->is_open())
  68. fa->close();
  69. return 0;
  70. }
  71. long AudioStreamPlaybackOGGVorbis::_ov_tell_func(void *_f) {
  72. //printf("close %p\n",_f);
  73. FileAccess *fa = (FileAccess *)_f;
  74. return fa->get_position();
  75. }
  76. int AudioStreamPlaybackOGGVorbis::mix(int16_t *p_buffer, int p_frames) {
  77. if (!playing)
  78. return 0;
  79. int total = p_frames;
  80. while (true) {
  81. int todo = p_frames;
  82. if (todo < MIN_MIX) {
  83. break;
  84. }
  85. #ifdef BIG_ENDIAN_ENABLED
  86. long ret = ov_read(&vf, (char *)p_buffer, todo * stream_channels * sizeof(int16_t), 1, 2, 1, &current_section);
  87. #else
  88. long ret = ov_read(&vf, (char *)p_buffer, todo * stream_channels * sizeof(int16_t), 0, 2, 1, &current_section);
  89. #endif
  90. if (ret < 0) {
  91. playing = false;
  92. ERR_BREAK_MSG(ret < 0, "Error reading OGG Vorbis file: " + file + ".");
  93. } else if (ret == 0) { // end of song, reload?
  94. ov_clear(&vf);
  95. _close_file();
  96. if (!has_loop()) {
  97. playing = false;
  98. repeats = 1;
  99. break;
  100. }
  101. f = FileAccess::open(file, FileAccess::READ);
  102. int errv = ov_open_callbacks(f, &vf, NULL, 0, _ov_callbacks);
  103. if (errv != 0) {
  104. playing = false;
  105. break; // :(
  106. }
  107. if (loop_restart_time) {
  108. bool ok = ov_time_seek(&vf, loop_restart_time) == 0;
  109. if (!ok) {
  110. playing = false;
  111. ERR_PRINT("Loop restart time rejected");
  112. }
  113. frames_mixed = stream_srate * loop_restart_time;
  114. } else {
  115. frames_mixed = 0;
  116. }
  117. repeats++;
  118. continue;
  119. }
  120. ret /= stream_channels;
  121. ret /= sizeof(int16_t);
  122. frames_mixed += ret;
  123. p_buffer += ret * stream_channels;
  124. p_frames -= ret;
  125. }
  126. return total - p_frames;
  127. }
  128. void AudioStreamPlaybackOGGVorbis::play(float p_from) {
  129. if (playing)
  130. stop();
  131. if (_load_stream() != OK)
  132. return;
  133. frames_mixed = 0;
  134. playing = true;
  135. if (p_from > 0) {
  136. seek(p_from);
  137. }
  138. }
  139. void AudioStreamPlaybackOGGVorbis::_close_file() {
  140. if (f) {
  141. memdelete(f);
  142. f = NULL;
  143. }
  144. }
  145. bool AudioStreamPlaybackOGGVorbis::is_playing() const {
  146. return playing;
  147. }
  148. void AudioStreamPlaybackOGGVorbis::stop() {
  149. _clear_stream();
  150. playing = false;
  151. //_clear();
  152. }
  153. float AudioStreamPlaybackOGGVorbis::get_playback_position() const {
  154. int32_t frames = int32_t(frames_mixed);
  155. if (frames < 0)
  156. frames = 0;
  157. return double(frames) / stream_srate;
  158. }
  159. void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
  160. if (!playing)
  161. return;
  162. bool ok = ov_time_seek(&vf, p_time) == 0;
  163. ERR_FAIL_COND(!ok);
  164. frames_mixed = stream_srate * p_time;
  165. }
  166. String AudioStreamPlaybackOGGVorbis::get_stream_name() const {
  167. return "";
  168. }
  169. void AudioStreamPlaybackOGGVorbis::set_loop(bool p_enable) {
  170. loops = p_enable;
  171. }
  172. bool AudioStreamPlaybackOGGVorbis::has_loop() const {
  173. return loops;
  174. }
  175. int AudioStreamPlaybackOGGVorbis::get_loop_count() const {
  176. return repeats;
  177. }
  178. Error AudioStreamPlaybackOGGVorbis::set_file(const String &p_file) {
  179. file = p_file;
  180. stream_valid = false;
  181. Error err;
  182. f = FileAccess::open(file, FileAccess::READ, &err);
  183. ERR_FAIL_COND_V_MSG(err, err, "Cannot open file '" + p_file + "'.");
  184. int errv = ov_open_callbacks(f, &vf, NULL, 0, _ov_callbacks);
  185. switch (errv) {
  186. case OV_EREAD: { // - A read from media returned an error.
  187. memdelete(f);
  188. f = NULL;
  189. ERR_FAIL_V(ERR_FILE_CANT_READ);
  190. } break;
  191. case OV_EVERSION: // - Vorbis version mismatch.
  192. case OV_ENOTVORBIS: { // - Bitstream is not Vorbis data.
  193. memdelete(f);
  194. f = NULL;
  195. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  196. } break;
  197. case OV_EBADHEADER: { // - Invalid Vorbis bitstream header.
  198. memdelete(f);
  199. f = NULL;
  200. ERR_FAIL_V(ERR_FILE_CORRUPT);
  201. } break;
  202. case OV_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  203. memdelete(f);
  204. f = NULL;
  205. ERR_FAIL_V(ERR_BUG);
  206. } break;
  207. }
  208. const vorbis_info *vinfo = ov_info(&vf, -1);
  209. stream_channels = vinfo->channels;
  210. stream_srate = vinfo->rate;
  211. length = ov_time_total(&vf, -1);
  212. ov_clear(&vf);
  213. memdelete(f);
  214. f = NULL;
  215. stream_valid = true;
  216. return OK;
  217. }
  218. Error AudioStreamPlaybackOGGVorbis::_load_stream() {
  219. ERR_FAIL_COND_V(!stream_valid, ERR_UNCONFIGURED);
  220. _clear_stream();
  221. if (file == "")
  222. return ERR_INVALID_DATA;
  223. Error err;
  224. f = FileAccess::open(file, FileAccess::READ, &err);
  225. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + file + "'.");
  226. int errv = ov_open_callbacks(f, &vf, NULL, 0, _ov_callbacks);
  227. switch (errv) {
  228. case OV_EREAD: { // - A read from media returned an error.
  229. memdelete(f);
  230. f = NULL;
  231. ERR_FAIL_V(ERR_FILE_CANT_READ);
  232. } break;
  233. case OV_EVERSION: // - Vorbis version mismatch.
  234. case OV_ENOTVORBIS: { // - Bitstream is not Vorbis data.
  235. memdelete(f);
  236. f = NULL;
  237. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  238. } break;
  239. case OV_EBADHEADER: { // - Invalid Vorbis bitstream header.
  240. memdelete(f);
  241. f = NULL;
  242. ERR_FAIL_V(ERR_FILE_CORRUPT);
  243. } break;
  244. case OV_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  245. memdelete(f);
  246. f = NULL;
  247. ERR_FAIL_V(ERR_BUG);
  248. } break;
  249. }
  250. repeats = 0;
  251. stream_loaded = true;
  252. return OK;
  253. }
  254. float AudioStreamPlaybackOGGVorbis::get_length() const {
  255. if (!stream_loaded) {
  256. if (const_cast<AudioStreamPlaybackOGGVorbis *>(this)->_load_stream() != OK)
  257. return 0;
  258. }
  259. return length;
  260. }
  261. void AudioStreamPlaybackOGGVorbis::_clear_stream() {
  262. if (!stream_loaded)
  263. return;
  264. ov_clear(&vf);
  265. _close_file();
  266. stream_loaded = false;
  267. //stream_channels=1;
  268. playing = false;
  269. }
  270. void AudioStreamPlaybackOGGVorbis::set_paused(bool p_paused) {
  271. paused = p_paused;
  272. }
  273. bool AudioStreamPlaybackOGGVorbis::is_paused() const {
  274. return paused;
  275. }
  276. AudioStreamPlaybackOGGVorbis::AudioStreamPlaybackOGGVorbis() {
  277. loops = false;
  278. playing = false;
  279. _ov_callbacks.read_func = _ov_read_func;
  280. _ov_callbacks.seek_func = _ov_seek_func;
  281. _ov_callbacks.close_func = _ov_close_func;
  282. _ov_callbacks.tell_func = _ov_tell_func;
  283. f = NULL;
  284. stream_loaded = false;
  285. stream_valid = false;
  286. repeats = 0;
  287. paused = true;
  288. stream_channels = 0;
  289. stream_srate = 0;
  290. current_section = 0;
  291. length = 0;
  292. loop_restart_time = 0;
  293. }
  294. AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
  295. _clear_stream();
  296. }
  297. RES ResourceFormatLoaderAudioStreamOGGVorbis::load(const String &p_path, const String &p_original_path, Error *r_error) {
  298. if (r_error)
  299. *r_error = OK;
  300. AudioStreamOGGVorbis *ogg_stream = memnew(AudioStreamOGGVorbis);
  301. ogg_stream->set_file(p_path);
  302. return Ref<AudioStreamOGGVorbis>(ogg_stream);
  303. }
  304. void ResourceFormatLoaderAudioStreamOGGVorbis::get_recognized_extensions(List<String> *p_extensions) const {
  305. p_extensions->push_back("ogg");
  306. }
  307. String ResourceFormatLoaderAudioStreamOGGVorbis::get_resource_type(const String &p_path) const {
  308. if (p_path.get_extension().to_lower() == "ogg")
  309. return "AudioStreamOGGVorbis";
  310. return "";
  311. }
  312. bool ResourceFormatLoaderAudioStreamOGGVorbis::handles_type(const String &p_type) const {
  313. return (p_type == "AudioStream" || p_type == "AudioStreamOGG" || p_type == "AudioStreamOGGVorbis");
  314. }