audio_stream_opus.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*************************************************************************/
  2. /* audio_stream_opus.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_opus.h"
  31. const float AudioStreamPlaybackOpus::osrate = 48000.0f;
  32. int AudioStreamPlaybackOpus::_op_read_func(void *_stream, unsigned char *_ptr, int _nbytes) {
  33. FileAccess *fa = (FileAccess *)_stream;
  34. if (fa->eof_reached())
  35. return 0;
  36. uint8_t *dst = (uint8_t *)_ptr;
  37. int read = fa->get_buffer(dst, _nbytes);
  38. return read;
  39. }
  40. int AudioStreamPlaybackOpus::_op_seek_func(void *_stream, opus_int64 _offset, int _whence) {
  41. #ifdef SEEK_SET
  42. FileAccess *fa = (FileAccess *)_stream;
  43. switch (_whence) {
  44. case SEEK_SET: {
  45. fa->seek(_offset);
  46. } break;
  47. case SEEK_CUR: {
  48. fa->seek(fa->get_pos() + _offset);
  49. } break;
  50. case SEEK_END: {
  51. fa->seek_end(_offset);
  52. } break;
  53. default: {
  54. ERR_PRINT("BUG, wtf was whence set to?\n");
  55. }
  56. }
  57. int ret = fa->eof_reached() ? -1 : 0;
  58. return ret;
  59. #else
  60. return -1; // no seeking
  61. #endif
  62. }
  63. int AudioStreamPlaybackOpus::_op_close_func(void *_stream) {
  64. if (!_stream)
  65. return 0;
  66. FileAccess *fa = (FileAccess *)_stream;
  67. if (fa->is_open())
  68. fa->close();
  69. return 0;
  70. }
  71. opus_int64 AudioStreamPlaybackOpus::_op_tell_func(void *_stream) {
  72. FileAccess *_fa = (FileAccess *)_stream;
  73. return (opus_int64)_fa->get_pos();
  74. }
  75. void AudioStreamPlaybackOpus::_clear_stream() {
  76. if (!stream_loaded)
  77. return;
  78. op_free(opus_file);
  79. _close_file();
  80. stream_loaded = false;
  81. stream_channels = 1;
  82. playing = false;
  83. }
  84. void AudioStreamPlaybackOpus::_close_file() {
  85. if (f) {
  86. memdelete(f);
  87. f = NULL;
  88. }
  89. }
  90. Error AudioStreamPlaybackOpus::_load_stream() {
  91. ERR_FAIL_COND_V(!stream_valid, ERR_UNCONFIGURED);
  92. _clear_stream();
  93. if (file == "")
  94. return ERR_INVALID_DATA;
  95. Error err;
  96. f = FileAccess::open(file, FileAccess::READ, &err);
  97. if (err) {
  98. ERR_FAIL_COND_V(err, err);
  99. }
  100. int _err = 0;
  101. opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &_err);
  102. switch (_err) {
  103. case OP_EREAD: { // - Can't read the file.
  104. memdelete(f);
  105. f = NULL;
  106. ERR_FAIL_V(ERR_FILE_CANT_READ);
  107. } break;
  108. case OP_EVERSION: // - Unrecognized version number.
  109. case OP_ENOTFORMAT: // - Stream is not Opus data.
  110. case OP_EIMPL: { // - Stream used non-implemented feature.
  111. memdelete(f);
  112. f = NULL;
  113. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  114. } break;
  115. case OP_EBADLINK: // - Failed to find old data after seeking.
  116. case OP_EBADTIMESTAMP: // - Timestamp failed the validity checks.
  117. case OP_EBADHEADER: { // - Invalid or mising Opus bitstream header.
  118. memdelete(f);
  119. f = NULL;
  120. ERR_FAIL_V(ERR_FILE_CORRUPT);
  121. } break;
  122. case OP_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  123. memdelete(f);
  124. f = NULL;
  125. ERR_FAIL_V(ERR_BUG);
  126. } break;
  127. }
  128. repeats = 0;
  129. stream_loaded = true;
  130. return OK;
  131. }
  132. AudioStreamPlaybackOpus::AudioStreamPlaybackOpus() {
  133. loops = false;
  134. playing = false;
  135. f = NULL;
  136. stream_loaded = false;
  137. stream_valid = false;
  138. repeats = 0;
  139. paused = true;
  140. stream_channels = 0;
  141. current_section = 0;
  142. length = 0;
  143. loop_restart_time = 0;
  144. pre_skip = 0;
  145. _op_callbacks.read = _op_read_func;
  146. _op_callbacks.seek = _op_seek_func;
  147. _op_callbacks.tell = _op_tell_func;
  148. _op_callbacks.close = _op_close_func;
  149. }
  150. Error AudioStreamPlaybackOpus::set_file(const String &p_file) {
  151. file = p_file;
  152. stream_valid = false;
  153. Error err;
  154. f = FileAccess::open(file, FileAccess::READ, &err);
  155. if (err) {
  156. ERR_FAIL_COND_V(err, err);
  157. }
  158. int _err;
  159. opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &_err);
  160. switch (_err) {
  161. case OP_EREAD: { // - Can't read the file.
  162. memdelete(f);
  163. f = NULL;
  164. ERR_FAIL_V(ERR_FILE_CANT_READ);
  165. } break;
  166. case OP_EVERSION: // - Unrecognized version number.
  167. case OP_ENOTFORMAT: // - Stream is not Opus data.
  168. case OP_EIMPL: { // - Stream used non-implemented feature.
  169. memdelete(f);
  170. f = NULL;
  171. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  172. } break;
  173. case OP_EBADLINK: // - Failed to find old data after seeking.
  174. case OP_EBADTIMESTAMP: // - Timestamp failed the validity checks.
  175. case OP_EBADHEADER: { // - Invalid or mising Opus bitstream header.
  176. memdelete(f);
  177. f = NULL;
  178. ERR_FAIL_V(ERR_FILE_CORRUPT);
  179. } break;
  180. case OP_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  181. memdelete(f);
  182. f = NULL;
  183. ERR_FAIL_V(ERR_BUG);
  184. } break;
  185. }
  186. const OpusHead *oinfo = op_head(opus_file, -1);
  187. stream_channels = oinfo->channel_count;
  188. pre_skip = oinfo->pre_skip;
  189. frames_mixed = pre_skip;
  190. ogg_int64_t len = op_pcm_total(opus_file, -1);
  191. if (len < 0) {
  192. length = 0;
  193. } else {
  194. length = (len / osrate);
  195. }
  196. op_free(opus_file);
  197. memdelete(f);
  198. f = NULL;
  199. stream_valid = true;
  200. return OK;
  201. }
  202. void AudioStreamPlaybackOpus::play(float p_from) {
  203. if (playing)
  204. stop();
  205. if (_load_stream() != OK)
  206. return;
  207. frames_mixed = pre_skip;
  208. playing = true;
  209. if (p_from > 0) {
  210. seek_pos(p_from);
  211. }
  212. }
  213. void AudioStreamPlaybackOpus::stop() {
  214. _clear_stream();
  215. playing = false;
  216. }
  217. void AudioStreamPlaybackOpus::seek_pos(float p_time) {
  218. if (!playing) return;
  219. ogg_int64_t pcm_offset = (ogg_int64_t)(p_time * osrate);
  220. bool ok = op_pcm_seek(opus_file, pcm_offset) == 0;
  221. if (!ok) {
  222. ERR_PRINT("Seek time over stream size.");
  223. return;
  224. }
  225. frames_mixed = osrate * p_time;
  226. }
  227. int AudioStreamPlaybackOpus::mix(int16_t *p_bufer, int p_frames) {
  228. if (!playing)
  229. return 0;
  230. int total = p_frames;
  231. while (true) {
  232. int todo = p_frames;
  233. if (todo == 0 || todo < MIN_MIX) {
  234. break;
  235. }
  236. int ret = op_read(opus_file, (opus_int16 *)p_bufer, todo * stream_channels, &current_section);
  237. if (ret < 0) {
  238. playing = false;
  239. ERR_EXPLAIN("Error reading Opus File: " + file);
  240. ERR_BREAK(ret < 0);
  241. } else if (ret == 0) { // end of song, reload?
  242. op_free(opus_file);
  243. _close_file();
  244. f = FileAccess::open(file, FileAccess::READ);
  245. int errv = 0;
  246. opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &errv);
  247. if (errv != 0) {
  248. playing = false;
  249. break; // :(
  250. }
  251. if (!has_loop()) {
  252. playing = false;
  253. repeats = 1;
  254. break;
  255. }
  256. if (loop_restart_time) {
  257. bool ok = op_pcm_seek(opus_file, (loop_restart_time * osrate) + pre_skip) == 0;
  258. if (!ok) {
  259. playing = false;
  260. ERR_PRINT("loop restart time rejected")
  261. }
  262. frames_mixed = (loop_restart_time * osrate) + pre_skip;
  263. } else {
  264. frames_mixed = pre_skip;
  265. }
  266. repeats++;
  267. continue;
  268. }
  269. stream_channels = op_head(opus_file, current_section)->channel_count;
  270. frames_mixed += ret;
  271. p_bufer += ret * stream_channels;
  272. p_frames -= ret;
  273. }
  274. return total - p_frames;
  275. }
  276. float AudioStreamPlaybackOpus::get_length() const {
  277. if (!stream_loaded) {
  278. if (const_cast<AudioStreamPlaybackOpus *>(this)->_load_stream() != OK)
  279. return 0;
  280. }
  281. return length;
  282. }
  283. float AudioStreamPlaybackOpus::get_pos() const {
  284. int32_t frames = int32_t(frames_mixed);
  285. if (frames < 0)
  286. frames = 0;
  287. return double(frames) / osrate;
  288. }
  289. int AudioStreamPlaybackOpus::get_minimum_buffer_size() const {
  290. return MIN_MIX;
  291. }
  292. AudioStreamPlaybackOpus::~AudioStreamPlaybackOpus() {
  293. _clear_stream();
  294. }
  295. RES ResourceFormatLoaderAudioStreamOpus::load(const String &p_path, const String &p_original_path, Error *r_error) {
  296. if (r_error)
  297. *r_error = OK;
  298. AudioStreamOpus *opus_stream = memnew(AudioStreamOpus);
  299. opus_stream->set_file(p_path);
  300. return Ref<AudioStreamOpus>(opus_stream);
  301. }
  302. void ResourceFormatLoaderAudioStreamOpus::get_recognized_extensions(List<String> *p_extensions) const {
  303. p_extensions->push_back("opus");
  304. }
  305. String ResourceFormatLoaderAudioStreamOpus::get_resource_type(const String &p_path) const {
  306. if (p_path.extension().to_lower() == "opus")
  307. return "AudioStreamOpus";
  308. return "";
  309. }
  310. bool ResourceFormatLoaderAudioStreamOpus::handles_type(const String &p_type) const {
  311. return (p_type == "AudioStream" || p_type == "AudioStreamOpus");
  312. }