Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
sound_driver.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 #pragma once
20 
21 #include <cstdint>
22 #include <vector>
23 
24 #include "generic/property.h"
25 #include "math/vec3.h"
26 #include "generic/range_value.h"
27 #include "sound/playing_sound.h"
28 #include "types.h"
29 
30 namespace smlt {
31 
32 typedef uint32_t AudioSourceID;
33 typedef uint32_t AudioBufferID;
34 
35 
36 enum AudioSourceState {
37  AUDIO_SOURCE_STATE_PLAYING,
38  AUDIO_SOURCE_STATE_PAUSED,
39  AUDIO_SOURCE_STATE_STOPPED
40 };
41 
42 enum AudioDataFormat {
43  AUDIO_DATA_FORMAT_MONO8,
44  AUDIO_DATA_FORMAT_MONO16,
45  AUDIO_DATA_FORMAT_STEREO8,
46  AUDIO_DATA_FORMAT_STEREO16,
47 
48  // These formats aren't supported, but are set
49  // when loading files before down conversion
50  AUDIO_DATA_FORMAT_MONO24,
51  AUDIO_DATA_FORMAT_STEREO24
52 };
53 
54 constexpr uint32_t audio_data_format_byte_size(AudioDataFormat format) {
55  return (format == AUDIO_DATA_FORMAT_MONO8) ? 1 :
56  (format == AUDIO_DATA_FORMAT_MONO16) ? 2 :
57  (format == AUDIO_DATA_FORMAT_STEREO8) ? 2 :
58  (format == AUDIO_DATA_FORMAT_STEREO16) ? 4 :
59  (format == AUDIO_DATA_FORMAT_MONO24) ? 3 : 6;
60 }
61 
62 
63 class Window;
64 
65 /* Basically a hacky abstraction over OpenAL with the thinking that the only other drivers will be:
66  *
67  * - Dreamcast
68  * - Dummy
69  *
70  * If that ceases to be the case for whatever reason, then we should probably design a nicer API for this. Perhaps.
71  */
72 class AudioSource;
73 
74 class SoundDriver {
75 public:
76  SoundDriver(Window* window);
77 
78  virtual ~SoundDriver();
79 
80  virtual bool startup();
81  virtual void shutdown();
82 
83  virtual std::vector<AudioSourceID> generate_sources(uint32_t count) = 0;
84  virtual std::vector<AudioBufferID> generate_buffers(uint32_t count) = 0;
85 
86  virtual void destroy_buffers(const std::vector<AudioBufferID>& buffers) = 0;
87  virtual void destroy_sources(const std::vector<AudioSourceID>& sources) = 0;
88 
89  virtual void play_source(AudioSourceID source_id) = 0;
90  virtual void stop_source(AudioSourceID source_id) = 0;
91 
92  virtual void queue_buffers_to_source(AudioSourceID source, uint32_t count, const std::vector<AudioBufferID>& buffers) = 0;
93  virtual std::vector<AudioBufferID> unqueue_buffers_from_source(AudioSourceID source, uint32_t count) = 0;
94  virtual void upload_buffer_data(AudioBufferID buffer, AudioDataFormat format, const uint8_t* data, std::size_t bytes, uint32_t frequency) = 0;
95 
96  virtual AudioSourceState source_state(AudioSourceID source) = 0;
97  virtual int32_t source_buffers_processed_count(AudioSourceID source) const = 0;
98 
99  Property<Window* SoundDriver::*> window = {this, &SoundDriver::window_};
100 
101  /* When called this should set the source to not be affected by distance
102  * this is used when playing background music etc. (i.e. the Window is the source) */
103  virtual void set_source_as_ambient(AudioSourceID id) = 0;
104 
105  virtual void set_listener_properties(const Vec3& position, const Quaternion& rotation, const Vec3& velocity) = 0;
106  virtual void set_source_properties(AudioSourceID id, const Vec3& position, const Vec3& velocity) = 0;
107 
108  virtual void set_source_reference_distance(AudioSourceID id, float dist) = 0;
109  virtual void set_source_gain(AudioSourceID id, RangeValue<0, 1> value) = 0;
110  virtual void set_source_pitch(AudioSourceID id, RangeValue<0, 1> value) = 0;
111 
112  PlayingSoundPtr play_sound(SoundPtr sound, AudioRepeat repeat=AUDIO_REPEAT_NONE);
113 
114 private:
115  virtual bool _startup() = 0;
116  virtual void _shutdown() = 0;
117 
118  Window* window_ = nullptr;
119  AudioSource* global_source_ = nullptr;
120 
121  sig::connection source_update_;
122 };
123 
124 
125 }
smlt::Property
Definition: property.h:202
smlt::sig::Connection
Definition: signal.h:65
smlt::Vec3
Definition: vec3.h:23
smlt::Quaternion
Definition: quaternion.h:24
smlt::AudioSource
Definition: sound.h:103
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:65
smlt::RangeValue
Definition: range_value.h:24
smlt::PlayingSoundPtr
Definition: playing_sound.h:86
smlt::SoundDriver
Definition: sound_driver.h:74