Simulant  21.12-194
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 class AudioSource {
104 public:
105  AudioSource(Window* window);
106  AudioSource(Stage* stage, StageNode* this_as_node, SoundDriver *driver);
107  virtual ~AudioSource();
108 
109  PlayingSoundPtr play_sound(
110  SoundPtr sound_id,
111  AudioRepeat repeat=AUDIO_REPEAT_NONE,
112  DistanceModel model=DISTANCE_MODEL_DEFAULT
113  );
114 
115  bool stop_sound(PlayingSoundID sound_id);
116 
117  /* The number of sounds this source is currently playing */
118  uint8_t playing_sound_count() const;
119 
120  /* The number of sounds that have finished, but aren't yet
121  * destroyed */
122  uint8_t played_sound_count() const;
123 
124  bool is_sound_playing() const;
125 
126  sig::signal<void ()>& signal_stream_finished() { return signal_stream_finished_; }
127 
128  void update_source(float dt);
129 protected:
130  SoundDriver* _sound_driver() const;
131 
132 public:
133  Stage* stage_ = nullptr;
134  Window* window_ = nullptr;
135  SoundDriver* driver_ = nullptr;
136  StageNode* node_ = nullptr;
137 
138  std::list<PlayingSound::ptr> instances_;
139  sig::signal<void ()> signal_stream_finished_;
140 
141  friend class Sound;
142  friend class PlayingSound;
143 
144  mutable thread::Mutex mutex_;
145  static void source_update_thread();
146 };
147 
148 }
149 #endif // SOUND_H
smlt::AudioSource
Definition: sound.h:103
smlt::Stage
Definition: stage.h:92
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:61
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:100
smlt::sig::signal
Definition: signal.h:319