audio_stream_ogg_vorbis.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/os/file_access.h"
  32. void AudioStreamPlaybackOGGVorbis::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  33. ERR_FAIL_COND(!active);
  34. int todo = p_frames;
  35. int start_buffer = 0;
  36. while (todo && active) {
  37. float *buffer = (float *)p_buffer;
  38. if (start_buffer > 0) {
  39. buffer = (buffer + start_buffer * 2);
  40. }
  41. int mixed = stb_vorbis_get_samples_float_interleaved(ogg_stream, 2, buffer, todo * 2);
  42. if (vorbis_stream->channels == 1 && mixed > 0) {
  43. //mix mono to stereo
  44. for (int i = start_buffer; i < start_buffer + mixed; i++) {
  45. p_buffer[i].r = p_buffer[i].l;
  46. }
  47. }
  48. todo -= mixed;
  49. frames_mixed += mixed;
  50. if (todo) {
  51. //end of file!
  52. bool is_not_empty = mixed > 0 || stb_vorbis_stream_length_in_samples(ogg_stream) > 0;
  53. if (vorbis_stream->loop && is_not_empty) {
  54. //loop
  55. seek(vorbis_stream->loop_offset);
  56. loops++;
  57. // we still have buffer to fill, start from this element in the next iteration.
  58. start_buffer = p_frames - todo;
  59. } else {
  60. for (int i = p_frames - todo; i < p_frames; i++) {
  61. p_buffer[i] = AudioFrame(0, 0);
  62. }
  63. active = false;
  64. todo = 0;
  65. }
  66. }
  67. }
  68. }
  69. float AudioStreamPlaybackOGGVorbis::get_stream_sampling_rate() {
  70. return vorbis_stream->sample_rate;
  71. }
  72. void AudioStreamPlaybackOGGVorbis::start(float p_from_pos) {
  73. active = true;
  74. seek(p_from_pos);
  75. loops = 0;
  76. _begin_resample();
  77. }
  78. void AudioStreamPlaybackOGGVorbis::stop() {
  79. active = false;
  80. }
  81. bool AudioStreamPlaybackOGGVorbis::is_playing() const {
  82. return active;
  83. }
  84. int AudioStreamPlaybackOGGVorbis::get_loop_count() const {
  85. return loops;
  86. }
  87. float AudioStreamPlaybackOGGVorbis::get_playback_position() const {
  88. return float(frames_mixed) / vorbis_stream->sample_rate;
  89. }
  90. void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
  91. if (!active) {
  92. return;
  93. }
  94. if (p_time >= vorbis_stream->get_length()) {
  95. p_time = 0;
  96. }
  97. frames_mixed = uint32_t(vorbis_stream->sample_rate * p_time);
  98. stb_vorbis_seek(ogg_stream, frames_mixed);
  99. }
  100. AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
  101. if (ogg_alloc.alloc_buffer) {
  102. stb_vorbis_close(ogg_stream);
  103. AudioServer::get_singleton()->audio_data_free(ogg_alloc.alloc_buffer);
  104. }
  105. }
  106. Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {
  107. Ref<AudioStreamPlaybackOGGVorbis> ovs;
  108. ERR_FAIL_COND_V_MSG(data == nullptr, ovs,
  109. "This AudioStreamOGGVorbis does not have an audio file assigned "
  110. "to it. AudioStreamOGGVorbis should not be created from the "
  111. "inspector or with `.new()`. Instead, load an audio file.");
  112. ovs.instance();
  113. ovs->vorbis_stream = Ref<AudioStreamOGGVorbis>(this);
  114. ovs->ogg_alloc.alloc_buffer = (char *)AudioServer::get_singleton()->audio_data_alloc(decode_mem_size);
  115. ovs->ogg_alloc.alloc_buffer_length_in_bytes = decode_mem_size;
  116. ovs->frames_mixed = 0;
  117. ovs->active = false;
  118. ovs->loops = 0;
  119. int error;
  120. ovs->ogg_stream = stb_vorbis_open_memory((const unsigned char *)data, data_len, &error, &ovs->ogg_alloc);
  121. if (!ovs->ogg_stream) {
  122. AudioServer::get_singleton()->audio_data_free(ovs->ogg_alloc.alloc_buffer);
  123. ovs->ogg_alloc.alloc_buffer = nullptr;
  124. ERR_FAIL_COND_V(!ovs->ogg_stream, Ref<AudioStreamPlaybackOGGVorbis>());
  125. }
  126. return ovs;
  127. }
  128. String AudioStreamOGGVorbis::get_stream_name() const {
  129. return ""; //return stream_name;
  130. }
  131. void AudioStreamOGGVorbis::clear_data() {
  132. if (data) {
  133. AudioServer::get_singleton()->audio_data_free(data);
  134. data = nullptr;
  135. data_len = 0;
  136. }
  137. }
  138. void AudioStreamOGGVorbis::set_data(const PoolVector<uint8_t> &p_data) {
  139. int src_data_len = p_data.size();
  140. uint32_t alloc_try = 1024;
  141. PoolVector<char> alloc_mem;
  142. PoolVector<char>::Write w;
  143. stb_vorbis *ogg_stream = nullptr;
  144. stb_vorbis_alloc ogg_alloc;
  145. // Vorbis comments may be up to UINT32_MAX, but that's arguably pretty rare.
  146. // Let's go with 2^30 so we don't risk going out of bounds.
  147. const uint32_t MAX_TEST_MEM = 1 << 30;
  148. while (alloc_try < MAX_TEST_MEM) {
  149. alloc_mem.resize(alloc_try);
  150. w = alloc_mem.write();
  151. ogg_alloc.alloc_buffer = w.ptr();
  152. ogg_alloc.alloc_buffer_length_in_bytes = alloc_try;
  153. PoolVector<uint8_t>::Read src_datar = p_data.read();
  154. int error;
  155. ogg_stream = stb_vorbis_open_memory((const unsigned char *)src_datar.ptr(), src_data_len, &error, &ogg_alloc);
  156. if (!ogg_stream && error == VORBIS_outofmem) {
  157. w.release();
  158. alloc_try *= 2;
  159. } else {
  160. ERR_FAIL_COND_MSG(alloc_try == MAX_TEST_MEM, "Failed allocating memory for OGG Vorbis stream.");
  161. ERR_FAIL_COND_MSG(!ogg_stream, "OGG Vorbis decoding failed. Check that your data is a valid OGG Vorbis audio stream.");
  162. stb_vorbis_info info = stb_vorbis_get_info(ogg_stream);
  163. channels = info.channels;
  164. sample_rate = info.sample_rate;
  165. decode_mem_size = alloc_try;
  166. //does this work? (it's less mem..)
  167. //decode_mem_size = ogg_alloc.alloc_buffer_length_in_bytes + info.setup_memory_required + info.temp_memory_required + info.max_frame_size;
  168. length = stb_vorbis_stream_length_in_seconds(ogg_stream);
  169. stb_vorbis_close(ogg_stream);
  170. // free any existing data
  171. clear_data();
  172. data = AudioServer::get_singleton()->audio_data_alloc(src_data_len, src_datar.ptr());
  173. data_len = src_data_len;
  174. break;
  175. }
  176. }
  177. ERR_FAIL_COND_MSG(alloc_try == MAX_TEST_MEM, vformat("Couldn't set vorbis data even with an alloc buffer of %d bytes, report bug.", MAX_TEST_MEM));
  178. }
  179. PoolVector<uint8_t> AudioStreamOGGVorbis::get_data() const {
  180. PoolVector<uint8_t> vdata;
  181. if (data_len && data) {
  182. vdata.resize(data_len);
  183. {
  184. PoolVector<uint8_t>::Write w = vdata.write();
  185. memcpy(w.ptr(), data, data_len);
  186. }
  187. }
  188. return vdata;
  189. }
  190. void AudioStreamOGGVorbis::set_loop(bool p_enable) {
  191. loop = p_enable;
  192. }
  193. bool AudioStreamOGGVorbis::has_loop() const {
  194. return loop;
  195. }
  196. void AudioStreamOGGVorbis::set_loop_offset(float p_seconds) {
  197. loop_offset = p_seconds;
  198. }
  199. float AudioStreamOGGVorbis::get_loop_offset() const {
  200. return loop_offset;
  201. }
  202. float AudioStreamOGGVorbis::get_length() const {
  203. return length;
  204. }
  205. void AudioStreamOGGVorbis::_bind_methods() {
  206. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamOGGVorbis::set_data);
  207. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamOGGVorbis::get_data);
  208. ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamOGGVorbis::set_loop);
  209. ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamOGGVorbis::has_loop);
  210. ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamOGGVorbis::set_loop_offset);
  211. ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamOGGVorbis::get_loop_offset);
  212. ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_data", "get_data");
  213. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  214. ADD_PROPERTY(PropertyInfo(Variant::REAL, "loop_offset"), "set_loop_offset", "get_loop_offset");
  215. }
  216. AudioStreamOGGVorbis::AudioStreamOGGVorbis() {
  217. data = nullptr;
  218. data_len = 0;
  219. length = 0;
  220. sample_rate = 1;
  221. channels = 1;
  222. loop_offset = 0;
  223. decode_mem_size = 0;
  224. loop = false;
  225. }
  226. AudioStreamOGGVorbis::~AudioStreamOGGVorbis() {
  227. clear_data();
  228. }