skia2/modules/audioplayer/SkAudioPlayer_sfml.cpp
Florin Malita 59a3c3be94 SFML [1] backed for SkAudioPlayer/Linux
For now this relies on system libs and requires explicit opt-in
(skia_use_sfml=true).

[1] https://www.sfml-dev.org/

TBR=
Change-Id: Iff89efdb4494f79530d0d41dee80ff38d4e75671
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/310065
Reviewed-by: Florin Malita <fmalita@google.com>
Commit-Queue: Florin Malita <fmalita@google.com>
2020-08-18 17:58:55 +00:00

69 lines
1.6 KiB
C++

/*
* 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 <SFML/Audio.hpp>
namespace {
class SFMLAudioPlayer final : public SkAudioPlayer {
public:
explicit SFMLAudioPlayer(sk_sp<SkData> data)
: fData(std::move(data))
{
fMusic.openFromMemory(fData->data(), fData->size());
}
private:
double onGetDuration() const override {
return static_cast<double>(fMusic.getDuration().asSeconds());
}
double onGetTime() const override {
return static_cast<double>(fMusic.getPlayingOffset().asSeconds());
}
double onSetTime(double t) override {
fMusic.setPlayingOffset(sf::seconds(static_cast<float>(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<SkData> fData;
sf::Music fMusic;
};
} // namespace
std::unique_ptr<SkAudioPlayer> SkAudioPlayer::Make(sk_sp<SkData> src) {
auto player = std::make_unique<SFMLAudioPlayer>(std::move(src));
return player->duration() > 0 ? std::move(player) : nullptr;
}