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
duration()20     double duration() const { return this->onGetDuration(); }
time()21     double time() const { return this->onGetTime(); }   // 0...duration()
22     double setTime(double);                             // returns actual time
23 
normalizedTime()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     };
state()32     State state() const  { return fState; }
volume()33     float volume() const { return fVolume; }    // 0...1
rate()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 
play()40     void play()  { this->setState(State::kPlaying); }
pause()41     void pause() { this->setState(State::kPaused); }
stop()42     void stop()  { this->setState(State::kStopped); }
43 
isPlaying()44     bool isPlaying() const { return this->state() == State::kPlaying; }
isPaused()45     bool isPaused() const  { return this->state() == State::kPaused; }
isStopped()46     bool isStopped() const { return this->state() == State::kStopped; }
47 
48 protected:
SkAudioPlayer()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 };
64