audio_stream_opus.cpp 9.6 KB

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