audio_stream_ogg_vorbis.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 == 0 || 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_EXPLAIN("Error reading OGG Vorbis File: " + file);
  93. ERR_BREAK(ret < 0);
  94. } else if (ret == 0) { // end of song, reload?
  95. ov_clear(&vf);
  96. _close_file();
  97. if (!has_loop()) {
  98. playing = false;
  99. repeats = 1;
  100. break;
  101. }
  102. f = FileAccess::open(file, FileAccess::READ);
  103. int errv = ov_open_callbacks(f, &vf, NULL, 0, _ov_callbacks);
  104. if (errv != 0) {
  105. playing = false;
  106. break; // :(
  107. }
  108. if (loop_restart_time) {
  109. bool ok = ov_time_seek(&vf, loop_restart_time) == 0;
  110. if (!ok) {
  111. playing = false;
  112. //ERR_EXPLAIN("loop restart time rejected");
  113. ERR_PRINT("loop restart time rejected")
  114. }
  115. frames_mixed = stream_srate * loop_restart_time;
  116. } else {
  117. frames_mixed = 0;
  118. }
  119. repeats++;
  120. continue;
  121. }
  122. ret /= stream_channels;
  123. ret /= sizeof(int16_t);
  124. frames_mixed += ret;
  125. p_buffer += ret * stream_channels;
  126. p_frames -= ret;
  127. }
  128. return total - p_frames;
  129. }
  130. void AudioStreamPlaybackOGGVorbis::play(float p_from) {
  131. if (playing)
  132. stop();
  133. if (_load_stream() != OK)
  134. return;
  135. frames_mixed = 0;
  136. playing = true;
  137. if (p_from > 0) {
  138. seek(p_from);
  139. }
  140. }
  141. void AudioStreamPlaybackOGGVorbis::_close_file() {
  142. if (f) {
  143. memdelete(f);
  144. f = NULL;
  145. }
  146. }
  147. bool AudioStreamPlaybackOGGVorbis::is_playing() const {
  148. return playing;
  149. }
  150. void AudioStreamPlaybackOGGVorbis::stop() {
  151. _clear_stream();
  152. playing = false;
  153. //_clear();
  154. }
  155. float AudioStreamPlaybackOGGVorbis::get_playback_position() const {
  156. int32_t frames = int32_t(frames_mixed);
  157. if (frames < 0)
  158. frames = 0;
  159. return double(frames) / stream_srate;
  160. }
  161. void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
  162. if (!playing)
  163. return;
  164. bool ok = ov_time_seek(&vf, p_time) == 0;
  165. ERR_FAIL_COND(!ok);
  166. frames_mixed = stream_srate * p_time;
  167. }
  168. String AudioStreamPlaybackOGGVorbis::get_stream_name() const {
  169. return "";
  170. }
  171. void AudioStreamPlaybackOGGVorbis::set_loop(bool p_enable) {
  172. loops = p_enable;
  173. }
  174. bool AudioStreamPlaybackOGGVorbis::has_loop() const {
  175. return loops;
  176. }
  177. int AudioStreamPlaybackOGGVorbis::get_loop_count() const {
  178. return repeats;
  179. }
  180. Error AudioStreamPlaybackOGGVorbis::set_file(const String &p_file) {
  181. file = p_file;
  182. stream_valid = false;
  183. Error err;
  184. f = FileAccess::open(file, FileAccess::READ, &err);
  185. if (err) {
  186. ERR_FAIL_COND_V(err, err);
  187. }
  188. int errv = ov_open_callbacks(f, &vf, NULL, 0, _ov_callbacks);
  189. switch (errv) {
  190. case OV_EREAD: { // - A read from media returned an error.
  191. memdelete(f);
  192. f = NULL;
  193. ERR_FAIL_V(ERR_FILE_CANT_READ);
  194. } break;
  195. case OV_EVERSION: // - Vorbis version mismatch.
  196. case OV_ENOTVORBIS: { // - Bitstream is not Vorbis data.
  197. memdelete(f);
  198. f = NULL;
  199. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  200. } break;
  201. case OV_EBADHEADER: { // - Invalid Vorbis bitstream header.
  202. memdelete(f);
  203. f = NULL;
  204. ERR_FAIL_V(ERR_FILE_CORRUPT);
  205. } break;
  206. case OV_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  207. memdelete(f);
  208. f = NULL;
  209. ERR_FAIL_V(ERR_BUG);
  210. } break;
  211. }
  212. const vorbis_info *vinfo = ov_info(&vf, -1);
  213. stream_channels = vinfo->channels;
  214. stream_srate = vinfo->rate;
  215. length = ov_time_total(&vf, -1);
  216. ov_clear(&vf);
  217. memdelete(f);
  218. f = NULL;
  219. stream_valid = true;
  220. return OK;
  221. }
  222. Error AudioStreamPlaybackOGGVorbis::_load_stream() {
  223. ERR_FAIL_COND_V(!stream_valid, ERR_UNCONFIGURED);
  224. _clear_stream();
  225. if (file == "")
  226. return ERR_INVALID_DATA;
  227. Error err;
  228. f = FileAccess::open(file, FileAccess::READ, &err);
  229. if (err) {
  230. ERR_FAIL_COND_V(err, err);
  231. }
  232. int errv = ov_open_callbacks(f, &vf, NULL, 0, _ov_callbacks);
  233. switch (errv) {
  234. case OV_EREAD: { // - A read from media returned an error.
  235. memdelete(f);
  236. f = NULL;
  237. ERR_FAIL_V(ERR_FILE_CANT_READ);
  238. } break;
  239. case OV_EVERSION: // - Vorbis version mismatch.
  240. case OV_ENOTVORBIS: { // - Bitstream is not Vorbis data.
  241. memdelete(f);
  242. f = NULL;
  243. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  244. } break;
  245. case OV_EBADHEADER: { // - Invalid Vorbis bitstream header.
  246. memdelete(f);
  247. f = NULL;
  248. ERR_FAIL_V(ERR_FILE_CORRUPT);
  249. } break;
  250. case OV_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  251. memdelete(f);
  252. f = NULL;
  253. ERR_FAIL_V(ERR_BUG);
  254. } break;
  255. }
  256. repeats = 0;
  257. stream_loaded = true;
  258. return OK;
  259. }
  260. float AudioStreamPlaybackOGGVorbis::get_length() const {
  261. if (!stream_loaded) {
  262. if (const_cast<AudioStreamPlaybackOGGVorbis *>(this)->_load_stream() != OK)
  263. return 0;
  264. }
  265. return length;
  266. }
  267. void AudioStreamPlaybackOGGVorbis::_clear_stream() {
  268. if (!stream_loaded)
  269. return;
  270. ov_clear(&vf);
  271. _close_file();
  272. stream_loaded = false;
  273. //stream_channels=1;
  274. playing = false;
  275. }
  276. void AudioStreamPlaybackOGGVorbis::set_paused(bool p_paused) {
  277. paused = p_paused;
  278. }
  279. bool AudioStreamPlaybackOGGVorbis::is_paused() const {
  280. return paused;
  281. }
  282. AudioStreamPlaybackOGGVorbis::AudioStreamPlaybackOGGVorbis() {
  283. loops = false;
  284. playing = false;
  285. _ov_callbacks.read_func = _ov_read_func;
  286. _ov_callbacks.seek_func = _ov_seek_func;
  287. _ov_callbacks.close_func = _ov_close_func;
  288. _ov_callbacks.tell_func = _ov_tell_func;
  289. f = NULL;
  290. stream_loaded = false;
  291. stream_valid = false;
  292. repeats = 0;
  293. paused = true;
  294. stream_channels = 0;
  295. stream_srate = 0;
  296. current_section = 0;
  297. length = 0;
  298. loop_restart_time = 0;
  299. }
  300. AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
  301. _clear_stream();
  302. }
  303. RES ResourceFormatLoaderAudioStreamOGGVorbis::load(const String &p_path, const String &p_original_path, Error *r_error) {
  304. if (r_error)
  305. *r_error = OK;
  306. AudioStreamOGGVorbis *ogg_stream = memnew(AudioStreamOGGVorbis);
  307. ogg_stream->set_file(p_path);
  308. return Ref<AudioStreamOGGVorbis>(ogg_stream);
  309. }
  310. void ResourceFormatLoaderAudioStreamOGGVorbis::get_recognized_extensions(List<String> *p_extensions) const {
  311. p_extensions->push_back("ogg");
  312. }
  313. String ResourceFormatLoaderAudioStreamOGGVorbis::get_resource_type(const String &p_path) const {
  314. if (p_path.get_extension().to_lower() == "ogg")
  315. return "AudioStreamOGGVorbis";
  316. return "";
  317. }
  318. bool ResourceFormatLoaderAudioStreamOGGVorbis::handles_type(const String &p_type) const {
  319. return (p_type == "AudioStream" || p_type == "AudioStreamOGG" || p_type == "AudioStreamOGGVorbis");
  320. }