1/**************************************************************************/
2/* audio_stream_generator.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_GENERATOR_H
32#define AUDIO_STREAM_GENERATOR_H
33
34#include "core/templates/ring_buffer.h"
35#include "servers/audio/audio_stream.h"
36
37class AudioStreamGenerator : public AudioStream {
38 GDCLASS(AudioStreamGenerator, AudioStream);
39
40 float mix_rate;
41 float buffer_len;
42
43protected:
44 static void _bind_methods();
45
46public:
47 void set_mix_rate(float p_mix_rate);
48 float get_mix_rate() const;
49
50 void set_buffer_length(float p_seconds);
51 float get_buffer_length() const;
52
53 virtual Ref<AudioStreamPlayback> instantiate_playback() override;
54 virtual String get_stream_name() const override;
55
56 virtual double get_length() const override;
57 virtual bool is_monophonic() const override;
58 AudioStreamGenerator();
59};
60
61class AudioStreamGeneratorPlayback : public AudioStreamPlaybackResampled {
62 GDCLASS(AudioStreamGeneratorPlayback, AudioStreamPlaybackResampled);
63 friend class AudioStreamGenerator;
64 RingBuffer<AudioFrame> buffer;
65 int skips;
66 bool active;
67 float mixed;
68 AudioStreamGenerator *generator = nullptr;
69
70protected:
71 virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
72 virtual float get_stream_sampling_rate() override;
73
74 static void _bind_methods();
75
76public:
77 virtual void start(double p_from_pos = 0.0) override;
78 virtual void stop() override;
79 virtual bool is_playing() const override;
80
81 virtual int get_loop_count() const override; //times it looped
82
83 virtual double get_playback_position() const override;
84 virtual void seek(double p_time) override;
85
86 bool push_frame(const Vector2 &p_frame);
87 bool can_push_buffer(int p_frames) const;
88 bool push_buffer(const PackedVector2Array &p_frames);
89 int get_frames_available() const;
90 int get_skips() const;
91
92 virtual void tag_used_streams() override;
93
94 void clear_buffer();
95
96 AudioStreamGeneratorPlayback();
97};
98
99#endif // AUDIO_STREAM_GENERATOR_H
100