audio_stream_opus.cpp 9.9 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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Author: George Marques <[email protected]> */
  12. /* */
  13. /* Permission is hereby granted, free of charge, to any person obtaining */
  14. /* a copy of this software and associated documentation files (the */
  15. /* "Software"), to deal in the Software without restriction, including */
  16. /* without limitation the rights to use, copy, modify, merge, publish, */
  17. /* distribute, sublicense, and/or sell copies of the Software, and to */
  18. /* permit persons to whom the Software is furnished to do so, subject to */
  19. /* the following conditions: */
  20. /* */
  21. /* The above copyright notice and this permission notice shall be */
  22. /* included in all copies or substantial portions of the Software. */
  23. /* */
  24. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  25. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  26. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  27. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  28. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  29. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  30. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  31. /*************************************************************************/
  32. #include "audio_stream_opus.h"
  33. const float AudioStreamPlaybackOpus::osrate = 48000.0f;
  34. int AudioStreamPlaybackOpus::_op_read_func(void *_stream, unsigned char *_ptr, int _nbytes) {
  35. FileAccess *fa = (FileAccess *)_stream;
  36. if (fa->eof_reached())
  37. return 0;
  38. uint8_t *dst = (uint8_t *)_ptr;
  39. int read = fa->get_buffer(dst, _nbytes);
  40. return read;
  41. }
  42. int AudioStreamPlaybackOpus::_op_seek_func(void *_stream, opus_int64 _offset, int _whence) {
  43. #ifdef SEEK_SET
  44. FileAccess *fa = (FileAccess *)_stream;
  45. switch (_whence) {
  46. case SEEK_SET: {
  47. fa->seek(_offset);
  48. } break;
  49. case SEEK_CUR: {
  50. fa->seek(fa->get_position() + _offset);
  51. } break;
  52. case SEEK_END: {
  53. fa->seek_end(_offset);
  54. } break;
  55. default: {
  56. ERR_PRINT("Opus seek function failure: Unexpected value in _whence\n");
  57. }
  58. }
  59. int ret = fa->eof_reached() ? -1 : 0;
  60. return ret;
  61. #else
  62. return -1; // no seeking
  63. #endif
  64. }
  65. int AudioStreamPlaybackOpus::_op_close_func(void *_stream) {
  66. if (!_stream)
  67. return 0;
  68. FileAccess *fa = (FileAccess *)_stream;
  69. if (fa->is_open())
  70. fa->close();
  71. return 0;
  72. }
  73. opus_int64 AudioStreamPlaybackOpus::_op_tell_func(void *_stream) {
  74. FileAccess *_fa = (FileAccess *)_stream;
  75. return (opus_int64)_fa->get_position();
  76. }
  77. void AudioStreamPlaybackOpus::_clear_stream() {
  78. if (!stream_loaded)
  79. return;
  80. op_free(opus_file);
  81. _close_file();
  82. stream_loaded = false;
  83. stream_channels = 1;
  84. playing = false;
  85. }
  86. void AudioStreamPlaybackOpus::_close_file() {
  87. if (f) {
  88. memdelete(f);
  89. f = NULL;
  90. }
  91. }
  92. Error AudioStreamPlaybackOpus::_load_stream() {
  93. ERR_FAIL_COND_V(!stream_valid, ERR_UNCONFIGURED);
  94. _clear_stream();
  95. if (file == "")
  96. return ERR_INVALID_DATA;
  97. Error err;
  98. f = FileAccess::open(file, FileAccess::READ, &err);
  99. if (err) {
  100. ERR_FAIL_COND_V(err, err);
  101. }
  102. int _err = 0;
  103. opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &_err);
  104. switch (_err) {
  105. case OP_EREAD: { // - Can't read the file.
  106. memdelete(f);
  107. f = NULL;
  108. ERR_FAIL_V(ERR_FILE_CANT_READ);
  109. } break;
  110. case OP_EVERSION: // - Unrecognized version number.
  111. case OP_ENOTFORMAT: // - Stream is not Opus data.
  112. case OP_EIMPL: { // - Stream used non-implemented feature.
  113. memdelete(f);
  114. f = NULL;
  115. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  116. } break;
  117. case OP_EBADLINK: // - Failed to find old data after seeking.
  118. case OP_EBADTIMESTAMP: // - Timestamp failed the validity checks.
  119. case OP_EBADHEADER: { // - Invalid or mising Opus bitstream header.
  120. memdelete(f);
  121. f = NULL;
  122. ERR_FAIL_V(ERR_FILE_CORRUPT);
  123. } break;
  124. case OP_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  125. memdelete(f);
  126. f = NULL;
  127. ERR_FAIL_V(ERR_BUG);
  128. } break;
  129. }
  130. repeats = 0;
  131. stream_loaded = true;
  132. return OK;
  133. }
  134. AudioStreamPlaybackOpus::AudioStreamPlaybackOpus() {
  135. loops = false;
  136. playing = false;
  137. f = NULL;
  138. stream_loaded = false;
  139. stream_valid = false;
  140. repeats = 0;
  141. paused = true;
  142. stream_channels = 0;
  143. current_section = 0;
  144. length = 0;
  145. loop_restart_time = 0;
  146. pre_skip = 0;
  147. _op_callbacks.read = _op_read_func;
  148. _op_callbacks.seek = _op_seek_func;
  149. _op_callbacks.tell = _op_tell_func;
  150. _op_callbacks.close = _op_close_func;
  151. }
  152. Error AudioStreamPlaybackOpus::set_file(const String &p_file) {
  153. file = p_file;
  154. stream_valid = false;
  155. Error err;
  156. f = FileAccess::open(file, FileAccess::READ, &err);
  157. if (err) {
  158. ERR_FAIL_COND_V(err, err);
  159. }
  160. int _err;
  161. opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &_err);
  162. switch (_err) {
  163. case OP_EREAD: { // - Can't read the file.
  164. memdelete(f);
  165. f = NULL;
  166. ERR_FAIL_V(ERR_FILE_CANT_READ);
  167. } break;
  168. case OP_EVERSION: // - Unrecognized version number.
  169. case OP_ENOTFORMAT: // - Stream is not Opus data.
  170. case OP_EIMPL: { // - Stream used non-implemented feature.
  171. memdelete(f);
  172. f = NULL;
  173. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  174. } break;
  175. case OP_EBADLINK: // - Failed to find old data after seeking.
  176. case OP_EBADTIMESTAMP: // - Timestamp failed the validity checks.
  177. case OP_EBADHEADER: { // - Invalid or mising Opus bitstream header.
  178. memdelete(f);
  179. f = NULL;
  180. ERR_FAIL_V(ERR_FILE_CORRUPT);
  181. } break;
  182. case OP_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  183. memdelete(f);
  184. f = NULL;
  185. ERR_FAIL_V(ERR_BUG);
  186. } break;
  187. }
  188. const OpusHead *oinfo = op_head(opus_file, -1);
  189. stream_channels = oinfo->channel_count;
  190. pre_skip = oinfo->pre_skip;
  191. frames_mixed = pre_skip;
  192. ogg_int64_t len = op_pcm_total(opus_file, -1);
  193. if (len < 0) {
  194. length = 0;
  195. } else {
  196. length = (len / osrate);
  197. }
  198. op_free(opus_file);
  199. memdelete(f);
  200. f = NULL;
  201. stream_valid = true;
  202. return OK;
  203. }
  204. void AudioStreamPlaybackOpus::play(float p_from) {
  205. if (playing)
  206. stop();
  207. if (_load_stream() != OK)
  208. return;
  209. frames_mixed = pre_skip;
  210. playing = true;
  211. if (p_from > 0) {
  212. seek(p_from);
  213. }
  214. }
  215. void AudioStreamPlaybackOpus::stop() {
  216. _clear_stream();
  217. playing = false;
  218. }
  219. void AudioStreamPlaybackOpus::seek(float p_time) {
  220. if (!playing) return;
  221. ogg_int64_t pcm_offset = (ogg_int64_t)(p_time * osrate);
  222. bool ok = op_pcm_seek(opus_file, pcm_offset) == 0;
  223. if (!ok) {
  224. ERR_PRINT("Seek time over stream size.");
  225. return;
  226. }
  227. frames_mixed = osrate * p_time;
  228. }
  229. int AudioStreamPlaybackOpus::mix(int16_t *p_buffer, int p_frames) {
  230. if (!playing)
  231. return 0;
  232. int total = p_frames;
  233. while (true) {
  234. int todo = p_frames;
  235. if (todo == 0 || todo < MIN_MIX) {
  236. break;
  237. }
  238. int ret = op_read(opus_file, (opus_int16 *)p_buffer, todo * stream_channels, &current_section);
  239. if (ret < 0) {
  240. playing = false;
  241. ERR_EXPLAIN("Error reading Opus File: " + file);
  242. ERR_BREAK(ret < 0);
  243. } else if (ret == 0) { // end of song, reload?
  244. op_free(opus_file);
  245. _close_file();
  246. f = FileAccess::open(file, FileAccess::READ);
  247. int errv = 0;
  248. opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &errv);
  249. if (errv != 0) {
  250. playing = false;
  251. break; // :(
  252. }
  253. if (!has_loop()) {
  254. playing = false;
  255. repeats = 1;
  256. break;
  257. }
  258. if (loop_restart_time) {
  259. bool ok = op_pcm_seek(opus_file, (loop_restart_time * osrate) + pre_skip) == 0;
  260. if (!ok) {
  261. playing = false;
  262. ERR_PRINT("loop restart time rejected")
  263. }
  264. frames_mixed = (loop_restart_time * osrate) + pre_skip;
  265. } else {
  266. frames_mixed = pre_skip;
  267. }
  268. repeats++;
  269. continue;
  270. }
  271. stream_channels = op_head(opus_file, current_section)->channel_count;
  272. frames_mixed += ret;
  273. p_buffer += ret * stream_channels;
  274. p_frames -= ret;
  275. }
  276. return total - p_frames;
  277. }
  278. float AudioStreamPlaybackOpus::get_length() const {
  279. if (!stream_loaded) {
  280. if (const_cast<AudioStreamPlaybackOpus *>(this)->_load_stream() != OK)
  281. return 0;
  282. }
  283. return length;
  284. }
  285. float AudioStreamPlaybackOpus::get_playback_position() const {
  286. int32_t frames = int32_t(frames_mixed);
  287. if (frames < 0)
  288. frames = 0;
  289. return double(frames) / osrate;
  290. }
  291. int AudioStreamPlaybackOpus::get_minimum_buffer_size() const {
  292. return MIN_MIX;
  293. }
  294. AudioStreamPlaybackOpus::~AudioStreamPlaybackOpus() {
  295. _clear_stream();
  296. }
  297. RES ResourceFormatLoaderAudioStreamOpus::load(const String &p_path, const String &p_original_path, Error *r_error) {
  298. if (r_error)
  299. *r_error = OK;
  300. AudioStreamOpus *opus_stream = memnew(AudioStreamOpus);
  301. opus_stream->set_file(p_path);
  302. return Ref<AudioStreamOpus>(opus_stream);
  303. }
  304. void ResourceFormatLoaderAudioStreamOpus::get_recognized_extensions(List<String> *p_extensions) const {
  305. p_extensions->push_back("opus");
  306. }
  307. String ResourceFormatLoaderAudioStreamOpus::get_resource_type(const String &p_path) const {
  308. if (p_path.get_extension().to_lower() == "opus")
  309. return "AudioStreamOpus";
  310. return "";
  311. }
  312. bool ResourceFormatLoaderAudioStreamOpus::handles_type(const String &p_type) const {
  313. return (p_type == "AudioStream" || p_type == "AudioStreamOpus");
  314. }