1/**************************************************************************/
2/* audio_stream_polyphonic.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#ifndef AUDIO_STREAM_POLYPHONIC_H
32#define AUDIO_STREAM_POLYPHONIC_H
33
34#include "core/templates/local_vector.h"
35#include "servers/audio/audio_stream.h"
36
37class AudioStreamPolyphonic : public AudioStream {
38 GDCLASS(AudioStreamPolyphonic, AudioStream)
39 int polyphony = 32;
40
41 static void _bind_methods();
42
43public:
44 virtual Ref<AudioStreamPlayback> instantiate_playback() override;
45 virtual String get_stream_name() const override;
46 virtual bool is_monophonic() const override;
47
48 void set_polyphony(int p_voices);
49 int get_polyphony() const;
50
51 AudioStreamPolyphonic();
52};
53
54class AudioStreamPlaybackPolyphonic : public AudioStreamPlayback {
55 GDCLASS(AudioStreamPlaybackPolyphonic, AudioStreamPlayback)
56
57 enum {
58 INTERNAL_BUFFER_LEN = 128,
59 ID_MASK = 0xFFFFFFFF,
60 INDEX_SHIFT = 32
61 };
62 struct Stream {
63 SafeFlag active;
64 SafeFlag pending_play;
65 SafeFlag finish_request;
66 float play_offset = 0;
67 float pitch_scale = 1.0;
68 Ref<AudioStream> stream;
69 Ref<AudioStreamPlayback> stream_playback;
70 float prev_volume_db = 0;
71 float volume_db = 0;
72 uint32_t id = 0;
73
74 Stream() :
75 active(false), pending_play(false), finish_request(false) {}
76 };
77
78 LocalVector<Stream> streams;
79 AudioFrame internal_buffer[INTERNAL_BUFFER_LEN];
80
81 bool active = false;
82 uint32_t id_counter = 1;
83
84 _FORCE_INLINE_ Stream *_find_stream(int64_t p_id);
85
86 friend class AudioStreamPolyphonic;
87
88protected:
89 static void _bind_methods();
90
91public:
92 typedef int64_t ID;
93 enum {
94 INVALID_ID = -1
95 };
96
97 virtual void start(double p_from_pos = 0.0) override;
98 virtual void stop() override;
99 virtual bool is_playing() const override;
100
101 virtual int get_loop_count() const override; //times it looped
102
103 virtual double get_playback_position() const override;
104 virtual void seek(double p_time) override;
105
106 virtual void tag_used_streams() override;
107
108 virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
109
110 ID play_stream(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0);
111 void set_stream_volume(ID p_stream_id, float p_volume_db);
112 void set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale);
113 bool is_stream_playing(ID p_stream_id) const;
114 void stop_stream(ID p_stream_id);
115
116 AudioStreamPlaybackPolyphonic();
117};
118
119#endif // AUDIO_STREAM_POLYPHONIC_H
120