event_player.cpp 9.7 KB

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