Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
sound.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef SOUND_H
20 #define SOUND_H
21 
22 #include <vector>
23 #include <list>
24 
25 #include "sound_driver.h"
26 #include "sound/playing_sound.h"
27 
28 #include "generic/managed.h"
29 #include "generic/identifiable.h"
30 
31 #include "signals/signal.h"
32 
33 #include "asset.h"
34 #include "loadable.h"
35 
36 #include "types.h"
37 
38 namespace smlt {
39 
40 class StageNode;
41 class AudioSource;
42 class PlayingSound;
43 
44 class Sound :
45  public RefCounted<Sound>,
46  public generic::Identifiable<SoundID>,
47  public Asset,
48  public Loadable,
49  public ChainNameable<Sound> {
50 
51 public:
52  Sound(SoundID id, AssetManager* asset_manager, SoundDriver* sound_driver);
53 
54  uint32_t sample_rate() const { return sample_rate_; }
55  void set_sample_rate(uint32_t rate) { sample_rate_ = rate; }
56 
57  AudioDataFormat format() { return format_; }
58  void set_format(AudioDataFormat format) { format_ = format; }
59 
60  std::size_t buffer_size() const;
61 
62  uint8_t channels() const { return channels_; }
63  void set_channels(uint8_t ch) { channels_ = ch; }
64 
65  std::shared_ptr<std::istream>& input_stream() { return sound_data_; }
66  void set_input_stream(std::shared_ptr<std::istream> stream) {
67  sound_data_ = stream;
68 
69  stream->seekg(0, std::ios_base::end);
70  int end = stream->tellg();
71  stream->seekg(0, std::ios_base::beg);
72  stream_length_ = end;
73  }
74 
75  std::size_t stream_length() const {
76  return stream_length_;
77  }
78 
79  template<typename Func>
80  void set_playing_sound_init_function(Func&& func) {
81  init_playing_sound_ = func;
82  }
83 
84  SoundDriver* _driver() const { return driver_; }
85 private:
86  void init_source(PlayingSound& source);
87 
88  std::function<void (PlayingSound&)> init_playing_sound_;
89 
90  SoundDriver* driver_ = nullptr;
91  std::shared_ptr<std::istream> sound_data_;
92 
93  uint32_t sample_rate_ = 0;
94  AudioDataFormat format_;
95  uint8_t channels_ = 0;
96  std::size_t stream_length_ = 0;
97 
98  friend class AudioSource;
99  friend class PlayingSound;
100 };
101 
102 
103 typedef sig::signal<void (SoundPtr, AudioRepeat, DistanceModel)> SoundPlayedSignal;
104 
105 
106 class AudioSource {
107  DEFINE_SIGNAL(SoundPlayedSignal, signal_sound_played);
108 
109 public:
110  AudioSource(Window* window);
111  AudioSource(Stage* stage, StageNode* this_as_node, SoundDriver *driver);
112  virtual ~AudioSource();
113 
114  PlayingSoundPtr play_sound(
115  SoundPtr sound_id,
116  AudioRepeat repeat=AUDIO_REPEAT_NONE,
117  DistanceModel model=DISTANCE_MODEL_DEFAULT
118  );
119 
120  bool stop_sound(PlayingSoundID sound_id);
121 
122  /* The number of sounds this source is currently playing */
123  uint8_t playing_sound_count() const;
124 
125  /* The number of sounds that have finished, but aren't yet
126  * destroyed */
127  uint8_t played_sound_count() const;
128 
129  bool is_sound_playing() const;
130 
131  sig::signal<void ()>& signal_stream_finished() { return signal_stream_finished_; }
132 
133  void update_source(float dt);
134 protected:
135  SoundDriver* _sound_driver() const;
136 
137 public:
138  Stage* stage_ = nullptr;
139  Window* window_ = nullptr;
140  SoundDriver* driver_ = nullptr;
141  StageNode* node_ = nullptr;
142 
143  std::list<PlayingSound::ptr> instances_;
144  sig::signal<void ()> signal_stream_finished_;
145 
146  friend class Sound;
147  friend class PlayingSound;
148 
149  mutable thread::Mutex mutex_;
150  static void source_update_thread();
151 };
152 
153 }
154 #endif // SOUND_H
smlt::AudioSource
Definition: sound.h:106
smlt::Stage
Definition: stage.h:80
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:65
smlt::PlayingSound
Definition: playing_sound.h:36
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::UniqueID
Definition: unique_id.h:77
smlt::Loadable
Definition: loadable.h:26
smlt::ChainNameable
Definition: nameable.h:34
smlt::PlayingSoundPtr
Definition: playing_sound.h:86
smlt::StageNode
Definition: stage_node.h:62
smlt::Asset
Definition: asset.h:37
smlt::SoundDriver
Definition: sound_driver.h:74
smlt::thread::Mutex
Definition: mutex.h:25
smlt::Sound
Definition: sound.h:49
smlt::AssetManager
Definition: asset_manager.h:107
smlt::sig::signal
Definition: signal.h:330