1 /*************************************************************************/
2 /*  stream_player.h                                                      */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #ifndef STREAM_PLAYER_H
31 #define STREAM_PLAYER_H
32 
33 #include "scene/main/node.h"
34 #include "scene/resources/audio_stream.h"
35 #include "servers/audio/audio_rb_resampler.h"
36 
37 class StreamPlayer : public Node {
38 
39 	OBJ_TYPE(StreamPlayer, Node);
40 
41 	//_THREAD_SAFE_CLASS_
42 
43 	struct InternalStream : public AudioServer::AudioStream {
44 		StreamPlayer *player;
45 		virtual int get_channel_count() const;
46 		virtual void set_mix_rate(int p_rate); //notify the stream of the mix rate
47 		virtual bool mix(int32_t *p_buffer, int p_frames);
48 		virtual void update();
49 	};
50 
51 	InternalStream internal_stream;
52 	Ref<AudioStreamPlayback> playback;
53 	Ref<AudioStream> stream;
54 
55 	int sp_get_channel_count() const;
56 	void sp_set_mix_rate(int p_rate); //notify the stream of the mix rate
57 	bool sp_mix(int32_t *p_buffer, int p_frames);
58 	void sp_update();
59 
60 	int server_mix_rate;
61 
62 	RID stream_rid;
63 	bool paused;
64 	bool autoplay;
65 	bool loops;
66 	float volume;
67 	float loop_point;
68 	int buffering_ms;
69 	volatile bool stop_request;
70 	float resume_pos;
71 
72 	AudioRBResampler resampler;
73 
74 	void _do_stop();
75 
76 	bool _play;
77 	void _set_play(bool p_play);
78 	bool _get_play() const;
79 
80 protected:
81 	void _notification(int p_what);
82 
83 	static void _bind_methods();
84 
85 public:
86 	void set_stream(const Ref<AudioStream> &p_stream);
87 	Ref<AudioStream> get_stream() const;
88 
89 	void play(float p_from_offset = 0);
90 	void stop();
91 	bool is_playing() const;
92 
93 	void set_paused(bool p_paused);
94 	bool is_paused() const;
95 
96 	void set_loop(bool p_enable);
97 	bool has_loop() const;
98 
99 	void set_volume(float p_vol);
100 	float get_volume() const;
101 
102 	void set_loop_restart_time(float p_secs);
103 	float get_loop_restart_time() const;
104 
105 	void set_volume_db(float p_db);
106 	float get_volume_db() const;
107 
108 	String get_stream_name() const;
109 
110 	int get_loop_count() const;
111 
112 	float get_pos() const;
113 	void seek_pos(float p_time);
114 	float get_length() const;
115 	void set_autoplay(bool p_vol);
116 	bool has_autoplay() const;
117 
118 	void set_buffering_msec(int p_msec);
119 	int get_buffering_msec() const;
120 
121 	StreamPlayer();
122 	~StreamPlayer();
123 };
124 
125 #endif // AUDIO_STREAM_PLAYER_H
126