audio_stream_ogg_vorbis.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*************************************************************************/
  2. /* audio_stream_ogg_vorbis.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "audio_stream_ogg_vorbis.h"
  30. size_t AudioStreamOGGVorbis::_ov_read_func(void *p_dst,size_t p_data, size_t p_count, void *_f) {
  31. //printf("read to %p, %i bytes, %i nmemb, %p\n",p_dst,p_data,p_count,_f);
  32. FileAccess *fa=(FileAccess*)_f;
  33. size_t read_total = p_data*p_count;
  34. if (fa->eof_reached())
  35. return 0;
  36. uint8_t *dst=(uint8_t*)p_dst;
  37. int read = fa->get_buffer(dst, read_total);
  38. return read;
  39. }
  40. int AudioStreamOGGVorbis::_ov_seek_func(void *_f,ogg_int64_t offs, int whence) {
  41. //printf("seek to %p, offs %i, whence %i\n",_f,(int)offs,whence);
  42. #ifdef SEEK_SET
  43. //printf("seek set defined\n");
  44. FileAccess *fa=(FileAccess*)_f;
  45. if (whence==SEEK_SET) {
  46. fa->seek(offs);
  47. } else if (whence==SEEK_CUR) {
  48. fa->seek(fa->get_pos()+offs);
  49. } else if (whence==SEEK_END) {
  50. fa->seek_end(offs);
  51. } else {
  52. ERR_PRINT("BUG, wtf was whence set to?\n");
  53. }
  54. int ret=fa->eof_reached()?-1:0;
  55. //printf("returning %i\n",ret);
  56. return ret;
  57. #else
  58. return -1; // no seeking
  59. #endif
  60. }
  61. int AudioStreamOGGVorbis::_ov_close_func(void *_f) {
  62. // printf("close %p\n",_f);
  63. if (!_f)
  64. return 0;
  65. FileAccess *fa=(FileAccess*)_f;
  66. if (fa->is_open())
  67. fa->close();
  68. return 0;
  69. }
  70. long AudioStreamOGGVorbis::_ov_tell_func(void *_f) {
  71. //printf("close %p\n",_f);
  72. FileAccess *fa=(FileAccess*)_f;
  73. return fa->get_pos();
  74. }
  75. bool AudioStreamOGGVorbis::_can_mix() const {
  76. return /*playing &&*/ !paused;
  77. }
  78. void AudioStreamOGGVorbis::update() {
  79. _THREAD_SAFE_METHOD_
  80. if (!playing && !setting_up)
  81. return;
  82. while (true) {
  83. int todo = get_todo();
  84. if (todo==0 || todo<MIN_MIX)
  85. break;
  86. //printf("to mix %i - mix me %i bytes\n",to_mix,to_mix*stream_channels*sizeof(int16_t));
  87. #ifdef BIG_ENDIAN_ENABLED
  88. long ret=ov_read(&vf,(char*)get_write_buffer(),todo*stream_channels*sizeof(int16_t), 1, 2, 1, &current_section);
  89. #else
  90. long ret=ov_read(&vf,(char*)get_write_buffer(),todo*stream_channels*sizeof(int16_t), 0, 2, 1, &current_section);
  91. #endif
  92. if (ret<0) {
  93. playing = false;
  94. setting_up=false;
  95. ERR_EXPLAIN("Error reading OGG Vorbis File: "+file);
  96. ERR_BREAK(ret<0);
  97. } else if (ret==0) { // end of song, reload?
  98. ov_clear(&vf);
  99. _close_file();
  100. if (!has_loop()) {
  101. playing=false;
  102. setting_up=false;
  103. repeats=1;
  104. return;
  105. }
  106. f=FileAccess::open(file,FileAccess::READ);
  107. int errv = ov_open_callbacks(f,&vf,NULL,0,_ov_callbacks);
  108. if (errv!=0) {
  109. playing=false;
  110. setting_up=false;
  111. return; // :(
  112. }
  113. frames_mixed=0;
  114. repeats++;
  115. continue;
  116. }
  117. ret/=stream_channels;
  118. ret/=sizeof(int16_t);
  119. frames_mixed+=ret;
  120. write(ret);
  121. }
  122. }
  123. void AudioStreamOGGVorbis::play() {
  124. _THREAD_SAFE_METHOD_
  125. if (playing)
  126. stop();
  127. if (_load_stream()!=OK)
  128. return;
  129. frames_mixed=0;
  130. playing=false;
  131. setting_up=true;
  132. update();
  133. if (!setting_up)
  134. return;
  135. setting_up=false;
  136. playing=true;
  137. }
  138. void AudioStreamOGGVorbis::_close_file() {
  139. if (f) {
  140. memdelete(f);
  141. f=NULL;
  142. }
  143. }
  144. void AudioStreamOGGVorbis::stop() {
  145. _THREAD_SAFE_METHOD_
  146. _clear_stream();
  147. playing=false;
  148. _clear();
  149. }
  150. AudioStreamOGGVorbis::UpdateMode AudioStreamOGGVorbis::get_update_mode() const {
  151. return UPDATE_THREAD;
  152. }
  153. bool AudioStreamOGGVorbis::is_playing() const {
  154. return playing || (get_total() - get_todo() -1 > 0);
  155. }
  156. float AudioStreamOGGVorbis::get_pos() const {
  157. int32_t frames = int32_t(frames_mixed) - (int32_t(get_total()) - get_todo());
  158. if (frames < 0)
  159. frames=0;
  160. return double(frames) / stream_srate;
  161. }
  162. void AudioStreamOGGVorbis::seek_pos(float p_time) {
  163. _THREAD_SAFE_METHOD_
  164. if (!playing)
  165. return;
  166. bool ok = ov_time_seek(&vf,p_time*1000)==0;
  167. ERR_FAIL_COND(!ok);
  168. frames_mixed=stream_srate*p_time;
  169. }
  170. String AudioStreamOGGVorbis::get_stream_name() const {
  171. return "";
  172. }
  173. void AudioStreamOGGVorbis::set_loop(bool p_enable) {
  174. loops=p_enable;
  175. }
  176. bool AudioStreamOGGVorbis::has_loop() const {
  177. return loops;
  178. }
  179. int AudioStreamOGGVorbis::get_loop_count() const {
  180. return repeats;
  181. }
  182. void AudioStreamOGGVorbis::set_file(const String& p_file) {
  183. file=p_file;
  184. }
  185. Error AudioStreamOGGVorbis::_load_stream() {
  186. _clear_stream();
  187. if (file=="")
  188. return ERR_INVALID_DATA;
  189. Error err;
  190. f=FileAccess::open(file,FileAccess::READ,&err);
  191. if (err) {
  192. ERR_FAIL_COND_V( err, err );
  193. }
  194. int errv = ov_open_callbacks(f,&vf,NULL,0,_ov_callbacks);
  195. const vorbis_info *vinfo=ov_info(&vf,-1);
  196. stream_channels=vinfo->channels;
  197. stream_srate=vinfo->rate;
  198. Error serr = _setup(stream_channels,stream_srate);
  199. if (serr) {
  200. _close_file();
  201. ERR_FAIL_V( ERR_INVALID_DATA );
  202. }
  203. switch(errv) {
  204. case OV_EREAD: { // - A read from media returned an error.
  205. _close_file();
  206. ERR_FAIL_V( ERR_FILE_CANT_READ );
  207. } break;
  208. case OV_EVERSION: // - Vorbis version mismatch.
  209. case OV_ENOTVORBIS: { // - Bitstream is not Vorbis data.
  210. _close_file();
  211. ERR_FAIL_V( ERR_FILE_UNRECOGNIZED );
  212. } break;
  213. case OV_EBADHEADER: { // - Invalid Vorbis bitstream header.
  214. _close_file();
  215. ERR_FAIL_V( ERR_FILE_CORRUPT );
  216. } break;
  217. case OV_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  218. _close_file();
  219. ERR_FAIL_V( ERR_BUG );
  220. } break;
  221. }
  222. ogg_int64_t len = ov_time_total(&vf,-1);
  223. length=len/1000.0;
  224. repeats=0;
  225. stream_loaded=true;
  226. return OK;
  227. }
  228. float AudioStreamOGGVorbis::get_length() const {
  229. if (!stream_loaded) {
  230. if (const_cast<AudioStreamOGGVorbis*>(this)->_load_stream()!=OK)
  231. return 0;
  232. }
  233. return length;
  234. }
  235. void AudioStreamOGGVorbis::_clear_stream() {
  236. if (!stream_loaded)
  237. return;
  238. ov_clear(&vf);
  239. _close_file();
  240. stream_loaded=false;
  241. stream_channels=1;
  242. playing=false;
  243. }
  244. void AudioStreamOGGVorbis::set_paused(bool p_paused) {
  245. paused=p_paused;
  246. }
  247. bool AudioStreamOGGVorbis::is_paused(bool p_paused) const {
  248. return paused;
  249. }
  250. AudioStreamOGGVorbis::AudioStreamOGGVorbis() {
  251. loops=false;
  252. playing=false;
  253. _ov_callbacks.read_func=_ov_read_func;
  254. _ov_callbacks.seek_func=_ov_seek_func;
  255. _ov_callbacks.close_func=_ov_close_func;
  256. _ov_callbacks.tell_func=_ov_tell_func;
  257. f = NULL;
  258. stream_loaded=false;
  259. repeats=0;
  260. setting_up=false;
  261. paused=true;
  262. stream_channels=0;
  263. stream_srate=0;
  264. current_section=0;
  265. length=0;
  266. }
  267. AudioStreamOGGVorbis::~AudioStreamOGGVorbis() {
  268. _clear_stream();
  269. }
  270. RES ResourceFormatLoaderAudioStreamOGGVorbis::load(const String &p_path,const String& p_original_path) {
  271. AudioStreamOGGVorbis *ogg_stream = memnew(AudioStreamOGGVorbis);
  272. ogg_stream->set_file(p_path);
  273. return Ref<AudioStreamOGGVorbis>(ogg_stream);
  274. }
  275. void ResourceFormatLoaderAudioStreamOGGVorbis::get_recognized_extensions(List<String> *p_extensions) const {
  276. p_extensions->push_back("ogg");
  277. }
  278. String ResourceFormatLoaderAudioStreamOGGVorbis::get_resource_type(const String &p_path) const {
  279. if (p_path.extension().to_lower()=="ogg")
  280. return "AudioStreamOGGVorbis";
  281. return "";
  282. }
  283. bool ResourceFormatLoaderAudioStreamOGGVorbis::handles_type(const String& p_type) const {
  284. return (p_type=="AudioStream" || p_type=="AudioStreamOGG" || p_type=="AudioStreamOGGVorbis");
  285. }