stream_player.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*************************************************************************/
  2. /* stream_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 "stream_player.h"
  30. void StreamPlayer::_notification(int p_what) {
  31. switch(p_what) {
  32. case NOTIFICATION_ENTER_TREE: {
  33. //set_idle_process(false); //don't annoy
  34. if (stream.is_valid() && autoplay && !get_tree()->is_editor_hint())
  35. play();
  36. } break;
  37. case NOTIFICATION_EXIT_TREE: {
  38. stop(); //wathever it may be doing, stop
  39. } break;
  40. }
  41. }
  42. void StreamPlayer::set_stream(const Ref<AudioStream> &p_stream) {
  43. stop();
  44. if (stream_rid.is_valid())
  45. AudioServer::get_singleton()->free(stream_rid);
  46. stream_rid=RID();
  47. stream=p_stream;
  48. if (!stream.is_null()) {
  49. stream->set_loop(loops);
  50. stream->set_paused(paused);
  51. stream_rid=AudioServer::get_singleton()->audio_stream_create(stream->get_audio_stream());
  52. }
  53. }
  54. Ref<AudioStream> StreamPlayer::get_stream() const {
  55. return stream;
  56. }
  57. void StreamPlayer::play() {
  58. ERR_FAIL_COND(!is_inside_tree());
  59. if (stream.is_null())
  60. return;
  61. if (stream->is_playing())
  62. stop();
  63. stream->play();
  64. AudioServer::get_singleton()->stream_set_active(stream_rid,true);
  65. AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume);
  66. // if (stream->get_update_mode()!=AudioStream::UPDATE_NONE)
  67. // set_idle_process(true);
  68. }
  69. void StreamPlayer::stop() {
  70. if (!is_inside_tree())
  71. return;
  72. if (stream.is_null())
  73. return;
  74. AudioServer::get_singleton()->stream_set_active(stream_rid,false);
  75. stream->stop();
  76. //set_idle_process(false);
  77. }
  78. bool StreamPlayer::is_playing() const {
  79. if (stream.is_null())
  80. return false;
  81. return stream->is_playing();
  82. }
  83. void StreamPlayer::set_loop(bool p_enable) {
  84. loops=p_enable;
  85. if (stream.is_null())
  86. return;
  87. stream->set_loop(loops);
  88. }
  89. bool StreamPlayer::has_loop() const {
  90. return loops;
  91. }
  92. void StreamPlayer::set_volume(float p_vol) {
  93. volume=p_vol;
  94. if (stream_rid.is_valid())
  95. AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume);
  96. }
  97. float StreamPlayer::get_volume() const {
  98. return volume;
  99. }
  100. void StreamPlayer::set_volume_db(float p_db) {
  101. if (p_db<-79)
  102. set_volume(0);
  103. else
  104. set_volume(Math::db2linear(p_db));
  105. }
  106. float StreamPlayer::get_volume_db() const {
  107. if (volume==0)
  108. return -80;
  109. else
  110. return Math::linear2db(volume);
  111. }
  112. String StreamPlayer::get_stream_name() const {
  113. if (stream.is_null())
  114. return "<No Stream>";
  115. return stream->get_name();
  116. }
  117. int StreamPlayer::get_loop_count() const {
  118. if (stream.is_null())
  119. return 0;
  120. return stream->get_loop_count();
  121. }
  122. float StreamPlayer::get_pos() const {
  123. if (stream.is_null())
  124. return 0;
  125. return stream->get_pos();
  126. }
  127. float StreamPlayer::get_length() const {
  128. if (stream.is_null())
  129. return 0;
  130. return stream->get_length();
  131. }
  132. void StreamPlayer::seek_pos(float p_time) {
  133. if (stream.is_null())
  134. return;
  135. return stream->seek_pos(p_time);
  136. }
  137. void StreamPlayer::set_autoplay(bool p_enable) {
  138. autoplay=p_enable;
  139. }
  140. bool StreamPlayer::has_autoplay() const {
  141. return autoplay;
  142. }
  143. void StreamPlayer::set_paused(bool p_paused) {
  144. paused=p_paused;
  145. if (stream.is_valid())
  146. stream->set_paused(p_paused);
  147. }
  148. bool StreamPlayer::is_paused() const {
  149. return paused;
  150. }
  151. void StreamPlayer::_set_play(bool p_play) {
  152. _play=p_play;
  153. if (is_inside_tree()) {
  154. if(_play)
  155. play();
  156. else
  157. stop();
  158. }
  159. }
  160. bool StreamPlayer::_get_play() const{
  161. return _play;
  162. }
  163. void StreamPlayer::_bind_methods() {
  164. ObjectTypeDB::bind_method(_MD("set_stream","stream:Stream"),&StreamPlayer::set_stream);
  165. ObjectTypeDB::bind_method(_MD("get_stream:Stream"),&StreamPlayer::get_stream);
  166. ObjectTypeDB::bind_method(_MD("play"),&StreamPlayer::play);
  167. ObjectTypeDB::bind_method(_MD("stop"),&StreamPlayer::stop);
  168. ObjectTypeDB::bind_method(_MD("is_playing"),&StreamPlayer::is_playing);
  169. ObjectTypeDB::bind_method(_MD("set_paused","paused"),&StreamPlayer::set_paused);
  170. ObjectTypeDB::bind_method(_MD("is_paused"),&StreamPlayer::is_paused);
  171. ObjectTypeDB::bind_method(_MD("set_loop","enabled"),&StreamPlayer::set_loop);
  172. ObjectTypeDB::bind_method(_MD("has_loop"),&StreamPlayer::has_loop);
  173. ObjectTypeDB::bind_method(_MD("set_volume","volume"),&StreamPlayer::set_volume);
  174. ObjectTypeDB::bind_method(_MD("get_volume"),&StreamPlayer::get_volume);
  175. ObjectTypeDB::bind_method(_MD("set_volume_db","db"),&StreamPlayer::set_volume_db);
  176. ObjectTypeDB::bind_method(_MD("get_volume_db"),&StreamPlayer::get_volume_db);
  177. ObjectTypeDB::bind_method(_MD("get_stream_name"),&StreamPlayer::get_stream_name);
  178. ObjectTypeDB::bind_method(_MD("get_loop_count"),&StreamPlayer::get_loop_count);
  179. ObjectTypeDB::bind_method(_MD("get_pos"),&StreamPlayer::get_pos);
  180. ObjectTypeDB::bind_method(_MD("seek_pos","time"),&StreamPlayer::seek_pos);
  181. ObjectTypeDB::bind_method(_MD("set_autoplay","enabled"),&StreamPlayer::set_autoplay);
  182. ObjectTypeDB::bind_method(_MD("has_autoplay"),&StreamPlayer::has_autoplay);
  183. ObjectTypeDB::bind_method(_MD("get_length"),&StreamPlayer::get_length);
  184. ObjectTypeDB::bind_method(_MD("_set_play","play"),&StreamPlayer::_set_play);
  185. ObjectTypeDB::bind_method(_MD("_get_play"),&StreamPlayer::_get_play);
  186. ADD_PROPERTY( PropertyInfo(Variant::OBJECT, "stream/stream", PROPERTY_HINT_RESOURCE_TYPE,"AudioStream"), _SCS("set_stream"), _SCS("get_stream") );
  187. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/play"), _SCS("_set_play"), _SCS("_get_play") );
  188. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/loop"), _SCS("set_loop"), _SCS("has_loop") );
  189. ADD_PROPERTY( PropertyInfo(Variant::REAL, "stream/volume_db", PROPERTY_HINT_RANGE,"-80,24,0.01"), _SCS("set_volume_db"), _SCS("get_volume_db") );
  190. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/autoplay"), _SCS("set_autoplay"), _SCS("has_autoplay") );
  191. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/paused"), _SCS("set_paused"), _SCS("is_paused") );
  192. }
  193. StreamPlayer::StreamPlayer() {
  194. volume=1;
  195. loops=false;
  196. paused=false;
  197. autoplay=false;
  198. _play=false;
  199. }
  200. StreamPlayer::~StreamPlayer() {
  201. if (stream_rid.is_valid())
  202. AudioServer::get_singleton()->free(stream_rid);
  203. }