audio_stream_ogg_vorbis.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*************************************************************************/
  2. /* audio_stream_ogg_vorbis.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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_ogg_vorbis.h"
  31. #include "os/file_access.h"
  32. #pragma GCC diagnostic push
  33. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  34. #include "thirdparty/misc/stb_vorbis.c"
  35. #pragma GCC diagnostic pop
  36. void AudioStreamPlaybackOGGVorbis::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  37. ERR_FAIL_COND(!active);
  38. int todo = p_frames;
  39. int start_buffer = 0;
  40. while (todo && active) {
  41. float *buffer = (float *)p_buffer;
  42. if (start_buffer > 0) {
  43. buffer = (buffer + start_buffer * 2);
  44. }
  45. int mixed = stb_vorbis_get_samples_float_interleaved(ogg_stream, 2, buffer, todo * 2);
  46. if (vorbis_stream->channels == 1 && mixed > 0) {
  47. //mix mono to stereo
  48. for (int i = start_buffer; i < mixed; i++) {
  49. p_buffer[i].r = p_buffer[i].l;
  50. }
  51. }
  52. todo -= mixed;
  53. frames_mixed += mixed;
  54. if (todo) {
  55. //end of file!
  56. if (vorbis_stream->loop) {
  57. //loop
  58. seek(vorbis_stream->loop_offset);
  59. loops++;
  60. // we still have buffer to fill, start from this element in the next iteration.
  61. start_buffer = p_frames - todo;
  62. } else {
  63. for (int i = p_frames - todo; i < p_frames; i++) {
  64. p_buffer[i] = AudioFrame(0, 0);
  65. }
  66. active = false;
  67. todo = 0;
  68. }
  69. }
  70. }
  71. }
  72. float AudioStreamPlaybackOGGVorbis::get_stream_sampling_rate() {
  73. return vorbis_stream->sample_rate;
  74. }
  75. void AudioStreamPlaybackOGGVorbis::start(float p_from_pos) {
  76. active = true;
  77. seek(p_from_pos);
  78. loops = 0;
  79. _begin_resample();
  80. }
  81. void AudioStreamPlaybackOGGVorbis::stop() {
  82. active = false;
  83. }
  84. bool AudioStreamPlaybackOGGVorbis::is_playing() const {
  85. return active;
  86. }
  87. int AudioStreamPlaybackOGGVorbis::get_loop_count() const {
  88. return loops;
  89. }
  90. float AudioStreamPlaybackOGGVorbis::get_playback_position() const {
  91. return float(frames_mixed) / vorbis_stream->sample_rate;
  92. }
  93. void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
  94. if (!active)
  95. return;
  96. if (p_time >= vorbis_stream->get_length()) {
  97. p_time = 0;
  98. }
  99. frames_mixed = uint32_t(vorbis_stream->sample_rate * p_time);
  100. stb_vorbis_seek(ogg_stream, frames_mixed);
  101. }
  102. AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
  103. if (ogg_alloc.alloc_buffer) {
  104. stb_vorbis_close(ogg_stream);
  105. AudioServer::get_singleton()->audio_data_free(ogg_alloc.alloc_buffer);
  106. }
  107. }
  108. Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {
  109. Ref<AudioStreamPlaybackOGGVorbis> ovs;
  110. ERR_FAIL_COND_V(data == NULL, ovs);
  111. ovs.instance();
  112. ovs->vorbis_stream = Ref<AudioStreamOGGVorbis>(this);
  113. ovs->ogg_alloc.alloc_buffer = (char *)AudioServer::get_singleton()->audio_data_alloc(decode_mem_size);
  114. ovs->ogg_alloc.alloc_buffer_length_in_bytes = decode_mem_size;
  115. ovs->frames_mixed = 0;
  116. ovs->active = false;
  117. ovs->loops = 0;
  118. int error;
  119. ovs->ogg_stream = stb_vorbis_open_memory((const unsigned char *)data, data_len, &error, &ovs->ogg_alloc);
  120. if (!ovs->ogg_stream) {
  121. AudioServer::get_singleton()->audio_data_free(ovs->ogg_alloc.alloc_buffer);
  122. ovs->ogg_alloc.alloc_buffer = NULL;
  123. ERR_FAIL_COND_V(!ovs->ogg_stream, Ref<AudioStreamPlaybackOGGVorbis>());
  124. }
  125. return ovs;
  126. }
  127. String AudioStreamOGGVorbis::get_stream_name() const {
  128. return ""; //return stream_name;
  129. }
  130. void AudioStreamOGGVorbis::clear_data() {
  131. if (data) {
  132. AudioServer::get_singleton()->audio_data_free(data);
  133. data = NULL;
  134. data_len = 0;
  135. }
  136. }
  137. void AudioStreamOGGVorbis::set_data(const PoolVector<uint8_t> &p_data) {
  138. int src_data_len = p_data.size();
  139. #define MAX_TEST_MEM (1 << 20)
  140. uint32_t alloc_try = 1024;
  141. PoolVector<char> alloc_mem;
  142. PoolVector<char>::Write w;
  143. stb_vorbis *ogg_stream = NULL;
  144. stb_vorbis_alloc ogg_alloc;
  145. while (alloc_try < MAX_TEST_MEM) {
  146. alloc_mem.resize(alloc_try);
  147. w = alloc_mem.write();
  148. ogg_alloc.alloc_buffer = w.ptr();
  149. ogg_alloc.alloc_buffer_length_in_bytes = alloc_try;
  150. PoolVector<uint8_t>::Read src_datar = p_data.read();
  151. int error;
  152. ogg_stream = stb_vorbis_open_memory((const unsigned char *)src_datar.ptr(), src_data_len, &error, &ogg_alloc);
  153. if (!ogg_stream && error == VORBIS_outofmem) {
  154. w = PoolVector<char>::Write();
  155. alloc_try *= 2;
  156. } else {
  157. ERR_FAIL_COND(alloc_try == MAX_TEST_MEM);
  158. ERR_FAIL_COND(ogg_stream == NULL);
  159. stb_vorbis_info info = stb_vorbis_get_info(ogg_stream);
  160. channels = info.channels;
  161. sample_rate = info.sample_rate;
  162. decode_mem_size = alloc_try;
  163. //does this work? (it's less mem..)
  164. //decode_mem_size = ogg_alloc.alloc_buffer_length_in_bytes + info.setup_memory_required + info.temp_memory_required + info.max_frame_size;
  165. //print_line("succeeded "+itos(ogg_alloc.alloc_buffer_length_in_bytes)+" setup "+itos(info.setup_memory_required)+" setup temp "+itos(info.setup_temp_memory_required)+" temp "+itos(info.temp_memory_required)+" maxframe"+itos(info.max_frame_size));
  166. length = stb_vorbis_stream_length_in_seconds(ogg_stream);
  167. stb_vorbis_close(ogg_stream);
  168. // free any existing data
  169. clear_data();
  170. data = AudioServer::get_singleton()->audio_data_alloc(src_data_len, src_datar.ptr());
  171. data_len = src_data_len;
  172. break;
  173. }
  174. }
  175. }
  176. PoolVector<uint8_t> AudioStreamOGGVorbis::get_data() const {
  177. PoolVector<uint8_t> vdata;
  178. if (data_len && data) {
  179. vdata.resize(data_len);
  180. {
  181. PoolVector<uint8_t>::Write w = vdata.write();
  182. copymem(w.ptr(), data, data_len);
  183. }
  184. }
  185. return vdata;
  186. }
  187. void AudioStreamOGGVorbis::set_loop(bool p_enable) {
  188. loop = p_enable;
  189. }
  190. bool AudioStreamOGGVorbis::has_loop() const {
  191. return loop;
  192. }
  193. void AudioStreamOGGVorbis::set_loop_offset(float p_seconds) {
  194. loop_offset = p_seconds;
  195. }
  196. float AudioStreamOGGVorbis::get_loop_offset() const {
  197. return loop_offset;
  198. }
  199. float AudioStreamOGGVorbis::get_length() const {
  200. return length;
  201. }
  202. void AudioStreamOGGVorbis::_bind_methods() {
  203. ClassDB::bind_method(D_METHOD("_set_data", "data"), &AudioStreamOGGVorbis::set_data);
  204. ClassDB::bind_method(D_METHOD("_get_data"), &AudioStreamOGGVorbis::get_data);
  205. ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamOGGVorbis::set_loop);
  206. ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamOGGVorbis::has_loop);
  207. ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamOGGVorbis::set_loop_offset);
  208. ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamOGGVorbis::get_loop_offset);
  209. ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  210. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_loop", "has_loop");
  211. ADD_PROPERTY(PropertyInfo(Variant::REAL, "loop_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_loop_offset", "get_loop_offset");
  212. }
  213. AudioStreamOGGVorbis::AudioStreamOGGVorbis() {
  214. data = NULL;
  215. length = 0;
  216. sample_rate = 1;
  217. channels = 1;
  218. loop_offset = 0;
  219. decode_mem_size = 0;
  220. loop = false;
  221. }
  222. AudioStreamOGGVorbis::~AudioStreamOGGVorbis() {
  223. clear_data();
  224. }