Mike Reed | 8a8be22 | 2020-08-13 09:14:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #pragma once |
| 8 | |
| 9 | #include "include/core/SkData.h" |
| 10 | #include <memory> |
| 11 | |
| 12 | class SK_API SkAudioPlayer { |
| 13 | public: |
| 14 | virtual ~SkAudioPlayer(); |
| 15 | |
| 16 | // Returns null on failure (possibly unknown format?) |
| 17 | static std::unique_ptr<SkAudioPlayer> Make(sk_sp<SkData>); |
| 18 | |
| 19 | // in seconds |
| 20 | double duration() const { return this->onGetDuration(); } |
| 21 | double time() const { return this->onGetTime(); } // 0...duration() |
| 22 | double setTime(double); // returns actual time |
| 23 | |
| 24 | double normalizedTime() const { return this->time() / this->duration(); } |
| 25 | double setNormalizedTime(double t); |
| 26 | |
| 27 | enum class State { |
| 28 | kPlaying, |
| 29 | kStopped, |
| 30 | kPaused, |
| 31 | }; |
| 32 | State state() const { return fState; } |
| 33 | float volume() const { return fVolume; } // 0...1 |
| 34 | float rate() const { return fRate; } // multiplier (e.g. 1.0 is default) |
| 35 | |
| 36 | State setState(State); // returns actual State |
| 37 | float setRate(float); // returns actual rate |
| 38 | float setVolume(float); // returns actual volume |
| 39 | |
| 40 | void play() { this->setState(State::kPlaying); } |
| 41 | void pause() { this->setState(State::kPaused); } |
| 42 | void stop() { this->setState(State::kStopped); } |
| 43 | |
| 44 | bool isPlaying() const { return this->state() == State::kPlaying; } |
| 45 | bool isPaused() const { return this->state() == State::kPaused; } |
| 46 | bool isStopped() const { return this->state() == State::kStopped; } |
| 47 | |
| 48 | protected: |
| 49 | SkAudioPlayer() {} // only called by subclasses |
| 50 | |
| 51 | virtual double onGetDuration() const = 0; |
| 52 | virtual double onGetTime() const = 0; |
| 53 | virtual double onSetTime(double) = 0; |
| 54 | |
| 55 | virtual State onSetState(State) = 0; |
| 56 | virtual float onSetRate(float) = 0; |
| 57 | virtual float onSetVolume(float) = 0; |
| 58 | |
| 59 | private: |
| 60 | State fState = State::kStopped; |
| 61 | float fRate = 1.0f; |
| 62 | float fVolume = 1.0f; |
| 63 | }; |