event_player.cpp 9.9 KB

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