1 /*************************************************************************/
2 /*  audio_stream_generator.h                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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/ring_buffer.h"
35 #include "servers/audio/audio_stream.h"
36 
37 class AudioStreamGenerator : public AudioStream {
38 	GDCLASS(AudioStreamGenerator, AudioStream);
39 
40 	float mix_rate;
41 	float buffer_len;
42 
43 protected:
44 	static void _bind_methods();
45 
46 public:
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> instance_playback();
54 	virtual String get_stream_name() const;
55 
56 	virtual float get_length() const;
57 	AudioStreamGenerator();
58 };
59 
60 class AudioStreamGeneratorPlayback : public AudioStreamPlaybackResampled {
61 
62 	GDCLASS(AudioStreamGeneratorPlayback, AudioStreamPlaybackResampled);
63 	friend class AudioStreamGenerator;
64 	RingBuffer<AudioFrame> buffer;
65 	int skips;
66 	bool active;
67 	float mixed;
68 	AudioStreamGenerator *generator;
69 
70 protected:
71 	virtual void _mix_internal(AudioFrame *p_buffer, int p_frames);
72 	virtual float get_stream_sampling_rate();
73 
74 	static void _bind_methods();
75 
76 public:
77 	virtual void start(float p_from_pos = 0.0);
78 	virtual void stop();
79 	virtual bool is_playing() const;
80 
81 	virtual int get_loop_count() const; //times it looped
82 
83 	virtual float get_playback_position() const;
84 	virtual void seek(float p_time);
85 
86 	bool push_frame(const Vector2 &p_frame);
87 	bool can_push_buffer(int p_frames) const;
88 	bool push_buffer(const PoolVector2Array &p_frames);
89 	int get_frames_available() const;
90 	int get_skips() const;
91 
92 	void clear_buffer();
93 
94 	AudioStreamGeneratorPlayback();
95 };
96 #endif // AUDIO_STREAM_GENERATOR_H
97