/* * Copyright 2020 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "include/core/SkData.h" #include "modules/audioplayer/SkAudioPlayer.h" #include namespace { class SFMLAudioPlayer final : public SkAudioPlayer { public: explicit SFMLAudioPlayer(sk_sp data) : fData(std::move(data)) { fMusic.openFromMemory(fData->data(), fData->size()); } private: double onGetDuration() const override { return static_cast(fMusic.getDuration().asSeconds()); } double onGetTime() const override { return static_cast(fMusic.getPlayingOffset().asSeconds()); } double onSetTime(double t) override { fMusic.setPlayingOffset(sf::seconds(static_cast(t))); return this->onGetTime(); } State onSetState(State state) override { switch (state) { case State::kPlaying: fMusic.play(); break; case State::kStopped: fMusic.stop(); break; case State::kPaused : fMusic.pause(); break; } return state; } float onSetRate(float r) override { fMusic.setPitch(r); return r; } float onSetVolume(float v) override { fMusic.setVolume(v * 100); return v; } const sk_sp fData; sf::Music fMusic; }; } // namespace std::unique_ptr SkAudioPlayer::Make(sk_sp src) { auto player = std::make_unique(std::move(src)); return player->duration() > 0 ? std::move(player) : nullptr; }