| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/local_vector.h" |
| 34 |
#include "scene/scene_string_names.h" |
| 35 |
#include "servers/audio/audio_server.h" |
| 36 |
#include "servers/audio/audio_stream.h" |
| 37 |
|
| 38 |
class AudioStreamPolyphonic : public AudioStream { |
| 39 |
GDCLASS(AudioStreamPolyphonic, AudioStream) |
| 40 |
int polyphony = 32; |
| 41 |
|
| 42 |
AudioServer::PlaybackType playback_type; |
| 43 |
|
| 44 |
static void _bind_methods(); |
| 45 |
|
| 46 |
public: |
| 47 |
virtual Ref<AudioStreamPlayback> instantiate_playback() override; |
| 48 |
virtual String get_stream_name() const override; |
| 49 |
virtual bool is_monophonic() const override; |
| 50 |
|
| 51 |
void set_polyphony(int p_voices); |
| 52 |
int get_polyphony() const; |
| 53 |
|
| 54 |
virtual bool is_meta_stream() const override { return true; } |
| 55 |
|
| 56 |
AudioStreamPolyphonic(); |
| 57 |
}; |
| 58 |
|
| 59 |
class AudioStreamPlaybackPolyphonic : public AudioStreamPlayback { |
| 60 |
GDCLASS(AudioStreamPlaybackPolyphonic, AudioStreamPlayback) |
| 61 |
|
| 62 |
constexpr static uint32_t INTERNAL_BUFFER_LEN = 128; |
| 63 |
|
| 64 |
struct Stream { |
| 65 |
SafeFlag active; |
| 66 |
SafeFlag pending_play; |
| 67 |
SafeFlag finish_request; |
| 68 |
float play_offset = 0; |
| 69 |
float pitch_scale = 1.0; |
| 70 |
Ref<AudioStream> stream; |
| 71 |
Ref<AudioStreamPlayback> stream_playback; |
| 72 |
float prev_volume_db = 0; |
| 73 |
float volume_db = 0; |
| 74 |
uint32_t id = 0; |
| 75 |
|
| 76 |
Stream() : |
| 77 |
active(false), pending_play(false), finish_request(false) {} |
| 78 |
}; |
| 79 |
|
| 80 |
LocalVector<Stream> streams; |
| 81 |
AudioFrame internal_buffer[INTERNAL_BUFFER_LEN]; |
| 82 |
|
| 83 |
bool active = false; |
| 84 |
uint32_t id_counter = 1; |
| 85 |
|
| 86 |
bool _is_sample = false; |
| 87 |
Ref<AudioSamplePlayback> sample_playback; |
| 88 |
|
| 89 |
_FORCE_INLINE_ Stream *_find_stream(int64_t p_id); |
| 90 |
_FORCE_INLINE_ const Stream *_find_stream(int64_t p_id) const; |
| 91 |
|
| 92 |
friend class AudioStreamPolyphonic; |
| 93 |
|
| 94 |
protected: |
| 95 |
static void _bind_methods(); |
| 96 |
|
| 97 |
public: |
| 98 |
typedef int64_t ID; |
| 99 |
enum { |
| 100 |
INVALID_ID = -1 |
| 101 |
}; |
| 102 |
|
| 103 |
virtual void start(double p_from_pos = 0.0) override; |
| 104 |
virtual void stop() override; |
| 105 |
virtual bool is_playing() const override; |
| 106 |
|
| 107 |
virtual int get_loop_count() const override; |
| 108 |
|
| 109 |
virtual double get_playback_position() const override; |
| 110 |
virtual void seek(double p_time) override; |
| 111 |
|
| 112 |
virtual void tag_used_streams() override; |
| 113 |
|
| 114 |
virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override; |
| 115 |
|
| 116 |
ID play_stream(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0, AudioServer::PlaybackType p_playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT, const StringName &p_bus = SceneStringName(Master)); |
| 117 |
void set_stream_volume(ID p_stream_id, float p_volume_db); |
| 118 |
void set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale); |
| 119 |
bool is_stream_playing(ID p_stream_id) const; |
| 120 |
void stop_stream(ID p_stream_id); |
| 121 |
|
| 122 |
virtual void set_is_sample(bool p_is_sample) override; |
| 123 |
virtual bool get_is_sample() const override; |
| 124 |
virtual Ref<AudioSamplePlayback> get_sample_playback() const override; |
| 125 |
virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override; |
| 126 |
|
| 127 |
private: |
| 128 |
#ifndef DISABLE_DEPRECATED |
| 129 |
ID _play_stream_bind_compat_91382(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0); |
| 130 |
static void _bind_compatibility_methods(); |
| 131 |
#endif |
| 132 |
|
| 133 |
public: |
| 134 |
AudioStreamPlaybackPolyphonic(); |
| 135 |
}; |