audio_stream_ogg_vorbis.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/io/file_access.h"
  32. #include "core/variant/typed_array.h"
  33. #include "thirdparty/libogg/ogg/ogg.h"
  34. int AudioStreamPlaybackOGGVorbis::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  35. ERR_FAIL_COND_V(!ready, 0);
  36. ERR_FAIL_COND_V(!active, 0);
  37. int todo = p_frames;
  38. int start_buffer = 0;
  39. int frames_mixed_this_step = p_frames;
  40. while (todo && active) {
  41. AudioFrame *buffer = p_buffer;
  42. if (start_buffer > 0) {
  43. buffer = buffer + start_buffer;
  44. }
  45. int mixed = _mix_frames_vorbis(buffer, todo);
  46. if (mixed < 0) {
  47. return 0;
  48. }
  49. todo -= mixed;
  50. frames_mixed += mixed;
  51. start_buffer += mixed;
  52. if (!have_packets_left) {
  53. //end of file!
  54. bool is_not_empty = mixed > 0 || vorbis_stream->get_length() > 0;
  55. if (vorbis_stream->loop && is_not_empty) {
  56. //loop
  57. seek(vorbis_stream->loop_offset);
  58. loops++;
  59. // we still have buffer to fill, start from this element in the next iteration.
  60. start_buffer = p_frames - todo;
  61. } else {
  62. frames_mixed_this_step = p_frames - todo;
  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. return frames_mixed_this_step;
  72. }
  73. int AudioStreamPlaybackOGGVorbis::_mix_frames_vorbis(AudioFrame *p_buffer, int p_frames) {
  74. ERR_FAIL_COND_V(!ready, 0);
  75. if (!have_samples_left) {
  76. ogg_packet *packet = nullptr;
  77. int err;
  78. if (!vorbis_data_playback->next_ogg_packet(&packet)) {
  79. have_packets_left = false;
  80. WARN_PRINT("ran out of packets in stream");
  81. return -1;
  82. }
  83. ERR_FAIL_COND_V_MSG((err = vorbis_synthesis(&block, packet)), 0, "Error during vorbis synthesis " + itos(err));
  84. ERR_FAIL_COND_V_MSG((err = vorbis_synthesis_blockin(&dsp_state, &block)), 0, "Error during vorbis block processing " + itos(err));
  85. have_packets_left = !packet->e_o_s;
  86. }
  87. float **pcm; // Accessed with pcm[channel_idx][sample_idx].
  88. int frames = vorbis_synthesis_pcmout(&dsp_state, &pcm);
  89. if (frames > p_frames) {
  90. frames = p_frames;
  91. have_samples_left = true;
  92. } else {
  93. have_samples_left = false;
  94. }
  95. if (info.channels > 1) {
  96. for (int frame = 0; frame < frames; frame++) {
  97. p_buffer[frame].l = pcm[0][frame];
  98. p_buffer[frame].r = pcm[0][frame];
  99. }
  100. } else {
  101. for (int frame = 0; frame < frames; frame++) {
  102. p_buffer[frame].l = pcm[0][frame];
  103. p_buffer[frame].r = pcm[0][frame];
  104. }
  105. }
  106. vorbis_synthesis_read(&dsp_state, frames);
  107. return frames;
  108. }
  109. float AudioStreamPlaybackOGGVorbis::get_stream_sampling_rate() {
  110. return vorbis_data->get_sampling_rate();
  111. }
  112. bool AudioStreamPlaybackOGGVorbis::_alloc_vorbis() {
  113. vorbis_info_init(&info);
  114. info_is_allocated = true;
  115. vorbis_comment_init(&comment);
  116. comment_is_allocated = true;
  117. ERR_FAIL_COND_V(vorbis_data.is_null(), false);
  118. vorbis_data_playback = vorbis_data->instance_playback();
  119. ogg_packet *packet;
  120. int err;
  121. for (int i = 0; i < 3; i++) {
  122. if (!vorbis_data_playback->next_ogg_packet(&packet)) {
  123. WARN_PRINT("Not enough packets to parse header");
  124. return false;
  125. }
  126. err = vorbis_synthesis_headerin(&info, &comment, packet);
  127. ERR_FAIL_COND_V_MSG(err != 0, false, "Error parsing header");
  128. }
  129. err = vorbis_synthesis_init(&dsp_state, &info);
  130. ERR_FAIL_COND_V_MSG(err != 0, false, "Error initializing dsp state");
  131. dsp_state_is_allocated = true;
  132. err = vorbis_block_init(&dsp_state, &block);
  133. ERR_FAIL_COND_V_MSG(err != 0, false, "Error initializing block");
  134. block_is_allocated = true;
  135. ready = true;
  136. return true;
  137. }
  138. void AudioStreamPlaybackOGGVorbis::start(float p_from_pos) {
  139. ERR_FAIL_COND(!ready);
  140. active = true;
  141. seek(p_from_pos);
  142. loops = 0;
  143. _begin_resample();
  144. }
  145. void AudioStreamPlaybackOGGVorbis::stop() {
  146. active = false;
  147. }
  148. bool AudioStreamPlaybackOGGVorbis::is_playing() const {
  149. return active;
  150. }
  151. int AudioStreamPlaybackOGGVorbis::get_loop_count() const {
  152. return loops;
  153. }
  154. float AudioStreamPlaybackOGGVorbis::get_playback_position() const {
  155. return float(frames_mixed) / vorbis_data->get_sampling_rate();
  156. }
  157. void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
  158. ERR_FAIL_COND(!ready);
  159. ERR_FAIL_COND(vorbis_stream.is_null());
  160. if (!active) {
  161. return;
  162. }
  163. vorbis_synthesis_restart(&dsp_state);
  164. if (p_time >= vorbis_stream->get_length()) {
  165. p_time = 0;
  166. }
  167. frames_mixed = uint32_t(vorbis_data->get_sampling_rate() * p_time);
  168. const int64_t desired_sample = p_time * get_stream_sampling_rate();
  169. if (!vorbis_data_playback->seek_page(desired_sample)) {
  170. WARN_PRINT("seek failed");
  171. return;
  172. }
  173. ogg_packet *packet;
  174. if (!vorbis_data_playback->next_ogg_packet(&packet)) {
  175. WARN_PRINT_ONCE("seeking beyond limits");
  176. return;
  177. }
  178. // The granule position of the page we're seeking through.
  179. int64_t granule_pos = 0;
  180. int headers_remaining = 0;
  181. int samples_in_page = 0;
  182. int err;
  183. while (true) {
  184. if (vorbis_synthesis_idheader(packet)) {
  185. headers_remaining = 3;
  186. }
  187. if (!headers_remaining) {
  188. ERR_FAIL_COND_MSG((err = vorbis_synthesis(&block, packet)), "Error during vorbis synthesis " + itos(err));
  189. ERR_FAIL_COND_MSG((err = vorbis_synthesis_blockin(&dsp_state, &block)), "Error during vorbis block processing " + itos(err));
  190. int samples_out = vorbis_synthesis_pcmout(&dsp_state, nullptr);
  191. ERR_FAIL_COND_MSG((err = vorbis_synthesis_read(&dsp_state, samples_out)), "Error during vorbis read updating " + itos(err));
  192. samples_in_page += samples_out;
  193. } else {
  194. headers_remaining--;
  195. }
  196. if (packet->granulepos != -1 && headers_remaining == 0) {
  197. // This indicates the end of the page.
  198. granule_pos = packet->granulepos;
  199. break;
  200. }
  201. if (packet->e_o_s) {
  202. break;
  203. }
  204. if (!vorbis_data_playback->next_ogg_packet(&packet)) {
  205. // We should get an e_o_s flag before this happens.
  206. WARN_PRINT("Vorbis file ended without warning.");
  207. break;
  208. }
  209. }
  210. int64_t samples_to_burn = samples_in_page - (granule_pos - desired_sample);
  211. if (samples_to_burn > samples_in_page) {
  212. WARN_PRINT("Burning more samples than we have in this page. Check seek algorithm.");
  213. } else if (samples_to_burn < 0) {
  214. WARN_PRINT("Burning negative samples doesn't make sense. Check seek algorithm.");
  215. }
  216. // Seek again, this time we'll burn a specific number of samples instead of all of them.
  217. if (!vorbis_data_playback->seek_page(desired_sample)) {
  218. WARN_PRINT("seek failed");
  219. return;
  220. }
  221. if (!vorbis_data_playback->next_ogg_packet(&packet)) {
  222. WARN_PRINT_ONCE("seeking beyond limits");
  223. return;
  224. }
  225. vorbis_synthesis_restart(&dsp_state);
  226. while (true) {
  227. if (vorbis_synthesis_idheader(packet)) {
  228. headers_remaining = 3;
  229. }
  230. if (!headers_remaining) {
  231. ERR_FAIL_COND_MSG((err = vorbis_synthesis(&block, packet)), "Error during vorbis synthesis " + itos(err));
  232. ERR_FAIL_COND_MSG((err = vorbis_synthesis_blockin(&dsp_state, &block)), "Error during vorbis block processing " + itos(err));
  233. int samples_out = vorbis_synthesis_pcmout(&dsp_state, nullptr);
  234. int read_samples = samples_to_burn > samples_out ? samples_out : samples_to_burn;
  235. ERR_FAIL_COND_MSG((err = vorbis_synthesis_read(&dsp_state, samples_out)), "Error during vorbis read updating " + itos(err));
  236. samples_to_burn -= read_samples;
  237. if (samples_to_burn <= 0) {
  238. break;
  239. }
  240. } else {
  241. headers_remaining--;
  242. }
  243. if (packet->granulepos != -1 && headers_remaining == 0) {
  244. // This indicates the end of the page.
  245. break;
  246. }
  247. if (packet->e_o_s) {
  248. break;
  249. }
  250. if (!vorbis_data_playback->next_ogg_packet(&packet)) {
  251. // We should get an e_o_s flag before this happens.
  252. WARN_PRINT("Vorbis file ended without warning.");
  253. break;
  254. }
  255. }
  256. }
  257. AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
  258. if (block_is_allocated) {
  259. vorbis_block_clear(&block);
  260. }
  261. if (dsp_state_is_allocated) {
  262. vorbis_dsp_clear(&dsp_state);
  263. }
  264. if (comment_is_allocated) {
  265. vorbis_comment_clear(&comment);
  266. }
  267. if (info_is_allocated) {
  268. vorbis_info_clear(&info);
  269. }
  270. }
  271. Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {
  272. Ref<AudioStreamPlaybackOGGVorbis> ovs;
  273. ERR_FAIL_COND_V(packet_sequence.is_null(), nullptr);
  274. ovs.instantiate();
  275. ovs->vorbis_stream = Ref<AudioStreamOGGVorbis>(this);
  276. ovs->vorbis_data = packet_sequence;
  277. ovs->frames_mixed = 0;
  278. ovs->active = false;
  279. ovs->loops = 0;
  280. if (ovs->_alloc_vorbis()) {
  281. return ovs;
  282. }
  283. // Failed to allocate data structures.
  284. return nullptr;
  285. }
  286. String AudioStreamOGGVorbis::get_stream_name() const {
  287. return ""; //return stream_name;
  288. }
  289. void AudioStreamOGGVorbis::maybe_update_info() {
  290. ERR_FAIL_COND(packet_sequence.is_null());
  291. vorbis_info info;
  292. vorbis_comment comment;
  293. int err;
  294. vorbis_info_init(&info);
  295. vorbis_comment_init(&comment);
  296. int packet_count = 0;
  297. Ref<OGGPacketSequencePlayback> packet_sequence_playback = packet_sequence->instance_playback();
  298. for (int i = 0; i < 3; i++) {
  299. ogg_packet *packet;
  300. if (!packet_sequence_playback->next_ogg_packet(&packet)) {
  301. WARN_PRINT("Failed to get header packet");
  302. break;
  303. }
  304. if (i == 0) {
  305. packet->b_o_s = 1;
  306. }
  307. if (i == 0) {
  308. ERR_FAIL_COND(!vorbis_synthesis_idheader(packet));
  309. }
  310. err = vorbis_synthesis_headerin(&info, &comment, packet);
  311. ERR_FAIL_COND_MSG(err != 0, "Error parsing header packet " + itos(i) + ": " + itos(err));
  312. packet_count++;
  313. }
  314. packet_sequence->set_sampling_rate(info.rate);
  315. vorbis_comment_clear(&comment);
  316. vorbis_info_clear(&info);
  317. }
  318. void AudioStreamOGGVorbis::set_packet_sequence(Ref<OGGPacketSequence> p_packet_sequence) {
  319. packet_sequence = p_packet_sequence;
  320. if (packet_sequence.is_valid()) {
  321. maybe_update_info();
  322. }
  323. }
  324. Ref<OGGPacketSequence> AudioStreamOGGVorbis::get_packet_sequence() const {
  325. return packet_sequence;
  326. }
  327. void AudioStreamOGGVorbis::set_loop(bool p_enable) {
  328. loop = p_enable;
  329. }
  330. bool AudioStreamOGGVorbis::has_loop() const {
  331. return loop;
  332. }
  333. void AudioStreamOGGVorbis::set_loop_offset(float p_seconds) {
  334. loop_offset = p_seconds;
  335. }
  336. float AudioStreamOGGVorbis::get_loop_offset() const {
  337. return loop_offset;
  338. }
  339. float AudioStreamOGGVorbis::get_length() const {
  340. ERR_FAIL_COND_V(packet_sequence.is_null(), 0);
  341. return packet_sequence->get_length();
  342. }
  343. bool AudioStreamOGGVorbis::is_monophonic() const {
  344. return false;
  345. }
  346. void AudioStreamOGGVorbis::_bind_methods() {
  347. ClassDB::bind_method(D_METHOD("set_packet_sequence", "packet_sequence"), &AudioStreamOGGVorbis::set_packet_sequence);
  348. ClassDB::bind_method(D_METHOD("get_packet_sequence"), &AudioStreamOGGVorbis::get_packet_sequence);
  349. ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamOGGVorbis::set_loop);
  350. ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamOGGVorbis::has_loop);
  351. ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamOGGVorbis::set_loop_offset);
  352. ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamOGGVorbis::get_loop_offset);
  353. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "packet_sequence", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_packet_sequence", "get_packet_sequence");
  354. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  355. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset");
  356. }
  357. AudioStreamOGGVorbis::AudioStreamOGGVorbis() {}
  358. AudioStreamOGGVorbis::~AudioStreamOGGVorbis() {}