audio_stream_opus.cpp 9.8 KB

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