audio_stream_ogg_vorbis.cpp 8.5 KB

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