audio_stream_ogg_vorbis.cpp 10 KB

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