audio_stream_ogg_vorbis.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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-2016 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 AudioStreamPlaybackOGGVorbis::_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 AudioStreamPlaybackOGGVorbis::_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 AudioStreamPlaybackOGGVorbis::_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 AudioStreamPlaybackOGGVorbis::_ov_tell_func(void *_f) {
  71. //printf("close %p\n",_f);
  72. FileAccess *fa=(FileAccess*)_f;
  73. return fa->get_pos();
  74. }
  75. int AudioStreamPlaybackOGGVorbis::mix(int16_t* p_bufer,int p_frames) {
  76. if (!playing)
  77. return 0;
  78. int total=p_frames;
  79. while (true) {
  80. int todo = p_frames;
  81. if (todo==0 || todo<MIN_MIX) {
  82. break;
  83. }
  84. //printf("to mix %i - mix me %i bytes\n",to_mix,to_mix*stream_channels*sizeof(int16_t));
  85. #ifdef BIG_ENDIAN_ENABLED
  86. long ret=ov_read(&vf,(char*)p_bufer,todo*stream_channels*sizeof(int16_t), 1, 2, 1, &current_section);
  87. #else
  88. long ret=ov_read(&vf,(char*)p_bufer,todo*stream_channels*sizeof(int16_t), 0, 2, 1, &current_section);
  89. #endif
  90. if (ret<0) {
  91. playing = false;
  92. ERR_EXPLAIN("Error reading OGG Vorbis File: "+file);
  93. ERR_BREAK(ret<0);
  94. } else if (ret==0) { // end of song, reload?
  95. ov_clear(&vf);
  96. _close_file();
  97. if (!has_loop()) {
  98. playing=false;
  99. repeats=1;
  100. break;
  101. }
  102. f=FileAccess::open(file,FileAccess::READ);
  103. int errv = ov_open_callbacks(f,&vf,NULL,0,_ov_callbacks);
  104. if (errv!=0) {
  105. playing=false;
  106. break;; // :(
  107. }
  108. if (loop_restart_time) {
  109. bool ok = ov_time_seek(&vf,loop_restart_time)==0;
  110. if (!ok) {
  111. playing=false;
  112. //ERR_EXPLAIN("loop restart time rejected");
  113. ERR_PRINT("loop restart time rejected")
  114. }
  115. frames_mixed=stream_srate*loop_restart_time;
  116. } else {
  117. frames_mixed=0;
  118. }
  119. repeats++;
  120. continue;
  121. }
  122. ret/=stream_channels;
  123. ret/=sizeof(int16_t);
  124. frames_mixed+=ret;
  125. p_bufer+=ret*stream_channels;
  126. p_frames-=ret;
  127. }
  128. return total-p_frames;
  129. }
  130. void AudioStreamPlaybackOGGVorbis::play(float p_from) {
  131. if (playing)
  132. stop();
  133. if (_load_stream()!=OK)
  134. return;
  135. frames_mixed=0;
  136. playing=true;
  137. if (p_from>0) {
  138. seek_pos(p_from);
  139. }
  140. }
  141. void AudioStreamPlaybackOGGVorbis::_close_file() {
  142. if (f) {
  143. memdelete(f);
  144. f=NULL;
  145. }
  146. }
  147. bool AudioStreamPlaybackOGGVorbis::is_playing() const {
  148. return playing;
  149. }
  150. void AudioStreamPlaybackOGGVorbis::stop() {
  151. _clear_stream();
  152. playing=false;
  153. //_clear();
  154. }
  155. float AudioStreamPlaybackOGGVorbis::get_pos() const {
  156. int32_t frames = int32_t(frames_mixed);
  157. if (frames < 0)
  158. frames=0;
  159. return double(frames) / stream_srate;
  160. }
  161. void AudioStreamPlaybackOGGVorbis::seek_pos(float p_time) {
  162. if (!playing)
  163. return;
  164. bool ok = ov_time_seek(&vf,p_time)==0;
  165. ERR_FAIL_COND(!ok);
  166. frames_mixed=stream_srate*p_time;
  167. }
  168. String AudioStreamPlaybackOGGVorbis::get_stream_name() const {
  169. return "";
  170. }
  171. void AudioStreamPlaybackOGGVorbis::set_loop(bool p_enable) {
  172. loops=p_enable;
  173. }
  174. bool AudioStreamPlaybackOGGVorbis::has_loop() const {
  175. return loops;
  176. }
  177. int AudioStreamPlaybackOGGVorbis::get_loop_count() const {
  178. return repeats;
  179. }
  180. Error AudioStreamPlaybackOGGVorbis::set_file(const String& p_file) {
  181. file=p_file;
  182. stream_valid=false;
  183. Error err;
  184. f=FileAccess::open(file,FileAccess::READ,&err);
  185. if (err) {
  186. ERR_FAIL_COND_V( err, err );
  187. }
  188. int errv = ov_open_callbacks(f,&vf,NULL,0,_ov_callbacks);
  189. switch(errv) {
  190. case OV_EREAD: { // - A read from media returned an error.
  191. memdelete(f); f=NULL;
  192. ERR_FAIL_V( ERR_FILE_CANT_READ );
  193. } break;
  194. case OV_EVERSION: // - Vorbis version mismatch.
  195. case OV_ENOTVORBIS: { // - Bitstream is not Vorbis data.
  196. memdelete(f); f=NULL;
  197. ERR_FAIL_V( ERR_FILE_UNRECOGNIZED );
  198. } break;
  199. case OV_EBADHEADER: { // - Invalid Vorbis bitstream header.
  200. memdelete(f); f=NULL;
  201. ERR_FAIL_V( ERR_FILE_CORRUPT );
  202. } break;
  203. case OV_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  204. memdelete(f); f=NULL;
  205. ERR_FAIL_V( ERR_BUG );
  206. } break;
  207. }
  208. const vorbis_info *vinfo=ov_info(&vf,-1);
  209. stream_channels=vinfo->channels;
  210. stream_srate=vinfo->rate;
  211. length = ov_time_total(&vf,-1);
  212. ov_clear(&vf);
  213. memdelete(f);
  214. f=NULL;
  215. stream_valid=true;
  216. return OK;
  217. }
  218. Error AudioStreamPlaybackOGGVorbis::_load_stream() {
  219. ERR_FAIL_COND_V(!stream_valid,ERR_UNCONFIGURED);
  220. _clear_stream();
  221. if (file=="")
  222. return ERR_INVALID_DATA;
  223. Error err;
  224. f=FileAccess::open(file,FileAccess::READ,&err);
  225. if (err) {
  226. ERR_FAIL_COND_V( err, err );
  227. }
  228. int errv = ov_open_callbacks(f,&vf,NULL,0,_ov_callbacks);
  229. switch(errv) {
  230. case OV_EREAD: { // - A read from media returned an error.
  231. memdelete(f); f=NULL;
  232. ERR_FAIL_V( ERR_FILE_CANT_READ );
  233. } break;
  234. case OV_EVERSION: // - Vorbis version mismatch.
  235. case OV_ENOTVORBIS: { // - Bitstream is not Vorbis data.
  236. memdelete(f); f=NULL;
  237. ERR_FAIL_V( ERR_FILE_UNRECOGNIZED );
  238. } break;
  239. case OV_EBADHEADER: { // - Invalid Vorbis bitstream header.
  240. memdelete(f); f=NULL;
  241. ERR_FAIL_V( ERR_FILE_CORRUPT );
  242. } break;
  243. case OV_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption.
  244. memdelete(f); f=NULL;
  245. ERR_FAIL_V( ERR_BUG );
  246. } break;
  247. }
  248. repeats=0;
  249. stream_loaded=true;
  250. return OK;
  251. }
  252. float AudioStreamPlaybackOGGVorbis::get_length() const {
  253. if (!stream_loaded) {
  254. if (const_cast<AudioStreamPlaybackOGGVorbis*>(this)->_load_stream()!=OK)
  255. return 0;
  256. }
  257. return length;
  258. }
  259. void AudioStreamPlaybackOGGVorbis::_clear_stream() {
  260. if (!stream_loaded)
  261. return;
  262. ov_clear(&vf);
  263. _close_file();
  264. stream_loaded=false;
  265. //stream_channels=1;
  266. playing=false;
  267. }
  268. void AudioStreamPlaybackOGGVorbis::set_paused(bool p_paused) {
  269. paused=p_paused;
  270. }
  271. bool AudioStreamPlaybackOGGVorbis::is_paused(bool p_paused) const {
  272. return paused;
  273. }
  274. AudioStreamPlaybackOGGVorbis::AudioStreamPlaybackOGGVorbis() {
  275. loops=false;
  276. playing=false;
  277. _ov_callbacks.read_func=_ov_read_func;
  278. _ov_callbacks.seek_func=_ov_seek_func;
  279. _ov_callbacks.close_func=_ov_close_func;
  280. _ov_callbacks.tell_func=_ov_tell_func;
  281. f = NULL;
  282. stream_loaded=false;
  283. stream_valid=false;
  284. repeats=0;
  285. paused=true;
  286. stream_channels=0;
  287. stream_srate=0;
  288. current_section=0;
  289. length=0;
  290. loop_restart_time=0;
  291. }
  292. AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
  293. _clear_stream();
  294. }
  295. RES ResourceFormatLoaderAudioStreamOGGVorbis::load(const String &p_path, const String& p_original_path, Error *r_error) {
  296. if (r_error)
  297. *r_error=OK;
  298. AudioStreamOGGVorbis *ogg_stream = memnew(AudioStreamOGGVorbis);
  299. ogg_stream->set_file(p_path);
  300. return Ref<AudioStreamOGGVorbis>(ogg_stream);
  301. }
  302. void ResourceFormatLoaderAudioStreamOGGVorbis::get_recognized_extensions(List<String> *p_extensions) const {
  303. p_extensions->push_back("ogg");
  304. }
  305. String ResourceFormatLoaderAudioStreamOGGVorbis::get_resource_type(const String &p_path) const {
  306. if (p_path.extension().to_lower()=="ogg")
  307. return "AudioStreamOGGVorbis";
  308. return "";
  309. }
  310. bool ResourceFormatLoaderAudioStreamOGGVorbis::handles_type(const String& p_type) const {
  311. return (p_type=="AudioStream" || p_type=="AudioStreamOGG" || p_type=="AudioStreamOGGVorbis");
  312. }