audio.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* audio.cpp - audio player
  2. * Copyright (C) 2017-2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <QMediaPlayer>
  18. #include <QFileInfo>
  19. #include <core/node/abstract_node.h>
  20. #include <core/node/abstract_value.h>
  21. #include <util/strings.h>
  22. #include "audio.h"
  23. namespace rainynite::studio {
  24. AudioPlayer::AudioPlayer() :
  25. player(make_unique<QMediaPlayer>())
  26. {}
  27. AudioPlayer::~AudioPlayer() {
  28. if (node_connection.connected())
  29. node_connection.disconnect();
  30. }
  31. void AudioPlayer::set_context(shared_ptr<EditorContext> context) {
  32. ContextListener::set_context(context);
  33. // TODO: detect audio node change
  34. if (node_connection.connected())
  35. node_connection.disconnect();
  36. audio_node = nullptr;
  37. if (auto doc = get_core_context()->get_document_node()) {
  38. if (auto value = doc->get_property("soundtrack")) {
  39. node_connection = value->subscribe([this]() {
  40. update_audio();
  41. });
  42. audio_node = abstract_node_cast(value);
  43. }
  44. }
  45. update_audio();
  46. }
  47. void AudioPlayer::update_audio() {
  48. if (audio_node) {
  49. auto maybe_fname = audio_node->get_property_value<string>("file_path", get_core_context());
  50. auto fname = maybe_fname.value_or("");
  51. if (fname == cached_file)
  52. return;
  53. using util::str;
  54. player->setMedia(QUrl::fromUserInput(
  55. str(fname),
  56. QFileInfo(str(get_context()->get_file_name())).absolutePath()
  57. ));
  58. cached_file = fname;
  59. connect_boost(
  60. get_context()->changed_time(),
  61. [this](auto time) {
  62. update(std::move(time));
  63. }
  64. );
  65. connect_boost(
  66. get_context()->playback_change,
  67. [this](bool playing) {
  68. is_playing = playing;
  69. if (playing)
  70. player->play();
  71. else
  72. player->pause();
  73. }
  74. );
  75. }
  76. }
  77. void AudioPlayer::update(core::Time time) {
  78. player->setPosition(time.get_seconds()*1000);
  79. // TODO: play one frame sample even when not playing
  80. if (is_playing)
  81. player->play();
  82. }
  83. } // namespace rainynite::studio