Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/interactive_music/audio_stream_synchronized.h
10277 views
1
/**************************************************************************/
2
/* audio_stream_synchronized.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "servers/audio/audio_stream.h"
34
35
class AudioStreamPlaybackSynchronized;
36
37
class AudioStreamSynchronized : public AudioStream {
38
GDCLASS(AudioStreamSynchronized, AudioStream)
39
OBJ_SAVE_TYPE(AudioStream)
40
41
private:
42
friend class AudioStreamPlaybackSynchronized;
43
44
enum {
45
MAX_STREAMS = 32
46
};
47
48
int stream_count = 0;
49
Ref<AudioStream> audio_streams[MAX_STREAMS];
50
float audio_stream_volume_db[MAX_STREAMS] = {};
51
HashSet<AudioStreamPlaybackSynchronized *> playbacks;
52
53
public:
54
virtual double get_bpm() const override;
55
virtual int get_beat_count() const override;
56
virtual int get_bar_beats() const override;
57
virtual bool has_loop() const override;
58
void set_stream_count(int p_count);
59
int get_stream_count() const;
60
void set_sync_stream(int p_stream_index, Ref<AudioStream> p_stream);
61
Ref<AudioStream> get_sync_stream(int p_stream_index) const;
62
void set_sync_stream_volume(int p_stream_index, float p_db);
63
float get_sync_stream_volume(int p_stream_index) const;
64
65
virtual Ref<AudioStreamPlayback> instantiate_playback() override;
66
virtual String get_stream_name() const override;
67
virtual double get_length() const override;
68
virtual bool is_meta_stream() const override { return true; }
69
70
AudioStreamSynchronized();
71
72
protected:
73
static void _bind_methods();
74
void _validate_property(PropertyInfo &property) const;
75
};
76
77
///////////////////////////////////////
78
79
class AudioStreamPlaybackSynchronized : public AudioStreamPlayback {
80
GDCLASS(AudioStreamPlaybackSynchronized, AudioStreamPlayback)
81
friend class AudioStreamSynchronized;
82
83
private:
84
enum {
85
MIX_BUFFER_SIZE = 128
86
};
87
AudioFrame mix_buffer[MIX_BUFFER_SIZE];
88
89
Ref<AudioStreamSynchronized> stream;
90
Ref<AudioStreamPlayback> playback[AudioStreamSynchronized::MAX_STREAMS];
91
92
int play_order[AudioStreamSynchronized::MAX_STREAMS];
93
94
double stream_todo = 0.0;
95
int fade_index = -1;
96
double fade_volume = 1.0;
97
int play_index = 0;
98
double offset = 0.0;
99
100
int loop_count = 0;
101
102
bool active = false;
103
104
void _update_playback_instances();
105
106
public:
107
virtual void start(double p_from_pos = 0.0) override;
108
virtual void stop() override;
109
virtual bool is_playing() const override;
110
virtual int get_loop_count() const override; // times it looped
111
virtual double get_playback_position() const override;
112
virtual void seek(double p_time) override;
113
virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
114
115
virtual void tag_used_streams() override;
116
117
AudioStreamPlaybackSynchronized();
118
~AudioStreamPlaybackSynchronized();
119
};
120
121