event_player.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*************************************************************************/
  2. /* event_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 "event_player.h"
  30. void EventPlayer::_notification(int p_what) {
  31. switch(p_what) {
  32. case NOTIFICATION_ENTER_TREE: {
  33. //set_idle_process(false); //don't annoy
  34. if (playback.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 EventPlayer::set_stream(const Ref<EventStream> &p_stream) {
  43. stop();
  44. stream=p_stream;
  45. if (stream.is_valid())
  46. playback=stream->instance_playback();
  47. else
  48. playback.unref();
  49. if (playback.is_valid()) {
  50. playback->set_loop(loops);
  51. playback->set_paused(paused);
  52. playback->set_volume(volume);
  53. for(int i=0;i<(MIN(MAX_CHANNELS,stream->get_channel_count()));i++)
  54. playback->set_channel_volume(i,channel_volume[i]);
  55. }
  56. }
  57. Ref<EventStream> EventPlayer::get_stream() const {
  58. return stream;
  59. }
  60. void EventPlayer::play() {
  61. ERR_FAIL_COND(!is_inside_tree());
  62. if (playback.is_null()) {
  63. return;
  64. }
  65. if (playback->is_playing()) {
  66. AudioServer::get_singleton()->lock();
  67. stop();
  68. AudioServer::get_singleton()->unlock();
  69. }
  70. AudioServer::get_singleton()->lock();
  71. playback->play();
  72. AudioServer::get_singleton()->unlock();
  73. }
  74. void EventPlayer::stop() {
  75. if (!is_inside_tree())
  76. return;
  77. if (playback.is_null())
  78. return;
  79. AudioServer::get_singleton()->lock();
  80. playback->stop();
  81. AudioServer::get_singleton()->unlock();
  82. }
  83. bool EventPlayer::is_playing() const {
  84. if (playback.is_null())
  85. return false;
  86. return playback->is_playing();
  87. }
  88. void EventPlayer::set_loop(bool p_enable) {
  89. loops=p_enable;
  90. if (playback.is_null())
  91. return;
  92. playback->set_loop(loops);
  93. }
  94. bool EventPlayer::has_loop() const {
  95. return loops;
  96. }
  97. void EventPlayer::set_volume(float p_volume) {
  98. volume=p_volume;
  99. if (playback.is_valid())
  100. playback->set_volume(volume);
  101. }
  102. float EventPlayer::get_volume() const {
  103. return volume;
  104. }
  105. void EventPlayer::set_volume_db(float p_db) {
  106. if (p_db<-79)
  107. set_volume(0);
  108. else
  109. set_volume(Math::db2linear(p_db));
  110. }
  111. float EventPlayer::get_volume_db() const {
  112. if (volume==0)
  113. return -80;
  114. else
  115. return Math::linear2db(volume);
  116. }
  117. void EventPlayer::set_pitch_scale(float p_pitch_scale) {
  118. pitch_scale=p_pitch_scale;
  119. if (playback.is_valid())
  120. playback->set_pitch_scale(pitch_scale);
  121. }
  122. float EventPlayer::get_pitch_scale() const {
  123. return pitch_scale;
  124. }
  125. void EventPlayer::set_tempo_scale(float p_tempo_scale) {
  126. tempo_scale=p_tempo_scale;
  127. if (playback.is_valid())
  128. playback->set_tempo_scale(tempo_scale);
  129. }
  130. float EventPlayer::get_tempo_scale() const {
  131. return tempo_scale;
  132. }
  133. String EventPlayer::get_stream_name() const {
  134. if (stream.is_null())
  135. return "<No Stream>";
  136. return stream->get_name();
  137. }
  138. int EventPlayer::get_loop_count() const {
  139. if (playback.is_null())
  140. return 0;
  141. return playback->get_loop_count();
  142. }
  143. float EventPlayer::get_pos() const {
  144. if (playback.is_null())
  145. return 0;
  146. return playback->get_pos();
  147. }
  148. float EventPlayer::get_length() const {
  149. if (stream.is_null())
  150. return 0;
  151. return stream->get_length();
  152. }
  153. void EventPlayer::seek_pos(float p_time) {
  154. if (playback.is_null())
  155. return;
  156. return playback->seek_pos(p_time);
  157. }
  158. void EventPlayer::set_autoplay(bool p_enable) {
  159. autoplay=p_enable;
  160. }
  161. bool EventPlayer::has_autoplay() const {
  162. return autoplay;
  163. }
  164. void EventPlayer::set_paused(bool p_paused) {
  165. paused=p_paused;
  166. if (playback.is_valid())
  167. playback->set_paused(p_paused);
  168. }
  169. bool EventPlayer::is_paused() const {
  170. return paused;
  171. }
  172. void EventPlayer::_set_play(bool p_play) {
  173. _play=p_play;
  174. if (is_inside_tree()) {
  175. if(_play)
  176. play();
  177. else
  178. stop();
  179. }
  180. }
  181. bool EventPlayer::_get_play() const{
  182. return _play;
  183. }
  184. void EventPlayer::set_channel_volume(int p_channel,float p_volume) {
  185. ERR_FAIL_INDEX(p_channel,MAX_CHANNELS);
  186. channel_volume[p_channel]=p_volume;
  187. if (playback.is_valid())
  188. playback->set_channel_volume(p_channel,p_volume);
  189. }
  190. float EventPlayer::get_channel_volume(int p_channel) const{
  191. ERR_FAIL_INDEX_V(p_channel,MAX_CHANNELS,0);
  192. return channel_volume[p_channel];
  193. }
  194. float EventPlayer::get_channel_last_note_time(int p_channel) const {
  195. if (playback.is_valid())
  196. return playback->get_last_note_time(p_channel);
  197. return 0;
  198. }
  199. void EventPlayer::_bind_methods() {
  200. ObjectTypeDB::bind_method(_MD("set_stream","stream:EventStream"),&EventPlayer::set_stream);
  201. ObjectTypeDB::bind_method(_MD("get_stream:EventStream"),&EventPlayer::get_stream);
  202. ObjectTypeDB::bind_method(_MD("play"),&EventPlayer::play);
  203. ObjectTypeDB::bind_method(_MD("stop"),&EventPlayer::stop);
  204. ObjectTypeDB::bind_method(_MD("is_playing"),&EventPlayer::is_playing);
  205. ObjectTypeDB::bind_method(_MD("set_paused","paused"),&EventPlayer::set_paused);
  206. ObjectTypeDB::bind_method(_MD("is_paused"),&EventPlayer::is_paused);
  207. ObjectTypeDB::bind_method(_MD("set_loop","enabled"),&EventPlayer::set_loop);
  208. ObjectTypeDB::bind_method(_MD("has_loop"),&EventPlayer::has_loop);
  209. ObjectTypeDB::bind_method(_MD("set_volume","volume"),&EventPlayer::set_volume);
  210. ObjectTypeDB::bind_method(_MD("get_volume"),&EventPlayer::get_volume);
  211. ObjectTypeDB::bind_method(_MD("set_pitch_scale","pitch_scale"),&EventPlayer::set_pitch_scale);
  212. ObjectTypeDB::bind_method(_MD("get_pitch_scale"),&EventPlayer::get_pitch_scale);
  213. ObjectTypeDB::bind_method(_MD("set_tempo_scale","tempo_scale"),&EventPlayer::set_tempo_scale);
  214. ObjectTypeDB::bind_method(_MD("get_tempo_scale"),&EventPlayer::get_tempo_scale);
  215. ObjectTypeDB::bind_method(_MD("set_volume_db","db"),&EventPlayer::set_volume_db);
  216. ObjectTypeDB::bind_method(_MD("get_volume_db"),&EventPlayer::get_volume_db);
  217. ObjectTypeDB::bind_method(_MD("get_stream_name"),&EventPlayer::get_stream_name);
  218. ObjectTypeDB::bind_method(_MD("get_loop_count"),&EventPlayer::get_loop_count);
  219. ObjectTypeDB::bind_method(_MD("get_pos"),&EventPlayer::get_pos);
  220. ObjectTypeDB::bind_method(_MD("seek_pos","time"),&EventPlayer::seek_pos);
  221. ObjectTypeDB::bind_method(_MD("get_length"),&EventPlayer::get_length);
  222. ObjectTypeDB::bind_method(_MD("set_autoplay","enabled"),&EventPlayer::set_autoplay);
  223. ObjectTypeDB::bind_method(_MD("has_autoplay"),&EventPlayer::has_autoplay);
  224. ObjectTypeDB::bind_method(_MD("set_channel_volume","channel","channel_volume"),&EventPlayer::set_channel_volume);
  225. ObjectTypeDB::bind_method(_MD("get_channel_volume","channel"),&EventPlayer::get_channel_volume);
  226. ObjectTypeDB::bind_method(_MD("get_channel_last_note_time","channel"),&EventPlayer::get_channel_last_note_time);
  227. ObjectTypeDB::bind_method(_MD("_set_play","play"),&EventPlayer::_set_play);
  228. ObjectTypeDB::bind_method(_MD("_get_play"),&EventPlayer::_get_play);
  229. ADD_PROPERTY( PropertyInfo(Variant::OBJECT, "stream/stream", PROPERTY_HINT_RESOURCE_TYPE,"EventStream"), _SCS("set_stream"), _SCS("get_stream") );
  230. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/play"), _SCS("_set_play"), _SCS("_get_play") );
  231. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/loop"), _SCS("set_loop"), _SCS("has_loop") );
  232. ADD_PROPERTY( PropertyInfo(Variant::REAL, "stream/volume_db", PROPERTY_HINT_RANGE,"-80,24,0.01"), _SCS("set_volume_db"), _SCS("get_volume_db") );
  233. ADD_PROPERTY( PropertyInfo(Variant::REAL, "stream/pitch_scale", PROPERTY_HINT_RANGE,"0.001,16,0.001"), _SCS("set_pitch_scale"), _SCS("get_pitch_scale") );
  234. ADD_PROPERTY( PropertyInfo(Variant::REAL, "stream/tempo_scale", PROPERTY_HINT_RANGE,"0.001,16,0.001"), _SCS("set_tempo_scale"), _SCS("get_tempo_scale") );
  235. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/autoplay"), _SCS("set_autoplay"), _SCS("has_autoplay") );
  236. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/paused"), _SCS("set_paused"), _SCS("is_paused") );
  237. }
  238. EventPlayer::EventPlayer() {
  239. volume=1;
  240. loops=false;
  241. paused=false;
  242. autoplay=false;
  243. _play=false;
  244. pitch_scale=1.0;
  245. tempo_scale=1.0;
  246. for(int i=0;i<MAX_CHANNELS;i++)
  247. channel_volume[i]=1.0;
  248. }
  249. EventPlayer::~EventPlayer() {
  250. }