summaryrefslogtreecommitdiff
path: root/servers
diff options
context:
space:
mode:
authorreduz <reduzio@gmail.com>2021-08-23 14:53:27 -0300
committerreduz <reduzio@gmail.com>2021-08-23 19:58:40 -0300
commit44d62a9f4b6ac892b1fb9b8998be4162409952e3 (patch)
treecbc5ffa6979314def8adbc229cd94b4adf8afb1c /servers
parent679b9be9d30001e8ffc8087e412097f7394cacdd (diff)
Implement NativeExtension pointer arguments
* Allows calling into native extensions directly with a pointer * Makes it easier to implement some APIs more efficiently * Appears with a "*" in the documentation for the argument. * Implementing the pointer handling is entirely up to the implementation, although the extension API provides some hint. * AudioStream has been implemented as an example, allowing to create NativeExtension based AudioStreams.
Diffstat (limited to 'servers')
-rw-r--r--servers/audio/audio_stream.cpp84
-rw-r--r--servers/audio/audio_stream.h37
-rw-r--r--servers/register_server_types.cpp4
3 files changed, 113 insertions, 12 deletions
diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp
index f3fa857682..5544a09ac0 100644
--- a/servers/audio/audio_stream.cpp
+++ b/servers/audio/audio_stream.cpp
@@ -33,6 +33,63 @@
#include "core/config/project_settings.h"
#include "core/os/os.h"
+void AudioStreamPlayback::start(float p_from_pos) {
+ if (GDVIRTUAL_CALL(_start, p_from_pos)) {
+ return;
+ }
+ ERR_FAIL_MSG("AudioStreamPlayback::start unimplemented!");
+}
+void AudioStreamPlayback::stop() {
+ if (GDVIRTUAL_CALL(_stop)) {
+ return;
+ }
+ ERR_FAIL_MSG("AudioStreamPlayback::stop unimplemented!");
+}
+bool AudioStreamPlayback::is_playing() const {
+ bool ret;
+ if (GDVIRTUAL_CALL(_is_playing, ret)) {
+ return ret;
+ }
+ ERR_FAIL_V_MSG(false, "AudioStreamPlayback::is_playing unimplemented!");
+}
+
+int AudioStreamPlayback::get_loop_count() const {
+ int ret;
+ if (GDVIRTUAL_CALL(_get_loop_count, ret)) {
+ return ret;
+ }
+ return 0;
+}
+
+float AudioStreamPlayback::get_playback_position() const {
+ float ret;
+ if (GDVIRTUAL_CALL(_get_playback_position, ret)) {
+ return ret;
+ }
+ ERR_FAIL_V_MSG(0, "AudioStreamPlayback::get_playback_position unimplemented!");
+}
+void AudioStreamPlayback::seek(float p_time) {
+ if (GDVIRTUAL_CALL(_seek, p_time)) {
+ return;
+ }
+}
+
+void AudioStreamPlayback::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
+ if (GDVIRTUAL_CALL(_mix, p_buffer, p_rate_scale, p_frames)) {
+ return;
+ }
+ WARN_PRINT_ONCE("AudioStreamPlayback::mix unimplemented!");
+}
+
+void AudioStreamPlayback::_bind_methods() {
+ GDVIRTUAL_BIND(_start, "from_pos")
+ GDVIRTUAL_BIND(_stop)
+ GDVIRTUAL_BIND(_is_playing)
+ GDVIRTUAL_BIND(_get_loop_count)
+ GDVIRTUAL_BIND(_get_playback_position)
+ GDVIRTUAL_BIND(_seek, "position")
+ GDVIRTUAL_BIND(_mix, "buffer", "rate_scale", "frames");
+}
//////////////////////////////
void AudioStreamPlaybackResampled::_begin_resample() {
@@ -92,8 +149,34 @@ void AudioStreamPlaybackResampled::mix(AudioFrame *p_buffer, float p_rate_scale,
////////////////////////////////
+Ref<AudioStreamPlayback> AudioStream::instance_playback() {
+ Ref<AudioStreamPlayback> ret;
+ if (GDVIRTUAL_CALL(_instance_playback, ret)) {
+ return ret;
+ }
+ ERR_FAIL_V_MSG(Ref<AudioStreamPlayback>(), "Method must be implemented!");
+}
+String AudioStream::get_stream_name() const {
+ String ret;
+ if (GDVIRTUAL_CALL(_get_stream_name, ret)) {
+ return ret;
+ }
+ return String();
+}
+
+float AudioStream::get_length() const {
+ float ret;
+ if (GDVIRTUAL_CALL(_get_length, ret)) {
+ return ret;
+ }
+ return 0;
+}
+
void AudioStream::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_length"), &AudioStream::get_length);
+ GDVIRTUAL_BIND(_instance_playback);
+ GDVIRTUAL_BIND(_get_stream_name);
+ GDVIRTUAL_BIND(_get_length);
}
////////////////////////////////
@@ -358,3 +441,4 @@ void AudioStreamPlaybackRandomPitch::mix(AudioFrame *p_buffer, float p_rate_scal
AudioStreamPlaybackRandomPitch::~AudioStreamPlaybackRandomPitch() {
random_pitch->playbacks.erase(this);
}
+/////////////////////////////////////////////
diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h
index 0d426f99b2..25f0017211 100644
--- a/servers/audio/audio_stream.h
+++ b/servers/audio/audio_stream.h
@@ -36,20 +36,33 @@
#include "servers/audio/audio_filter_sw.h"
#include "servers/audio_server.h"
+#include "core/object/gdvirtual.gen.inc"
+#include "core/object/script_language.h"
+#include "core/variant/native_ptr.h"
+
class AudioStreamPlayback : public RefCounted {
GDCLASS(AudioStreamPlayback, RefCounted);
+protected:
+ static void _bind_methods();
+ GDVIRTUAL1(_start, float)
+ GDVIRTUAL0(_stop)
+ GDVIRTUAL0RC(bool, _is_playing)
+ GDVIRTUAL0RC(int, _get_loop_count)
+ GDVIRTUAL0RC(float, _get_playback_position)
+ GDVIRTUAL1(_seek, float)
+ GDVIRTUAL3(_mix, GDNativePtr<AudioFrame>, float, int)
public:
- virtual void start(float p_from_pos = 0.0) = 0;
- virtual void stop() = 0;
- virtual bool is_playing() const = 0;
+ virtual void start(float p_from_pos = 0.0);
+ virtual void stop();
+ virtual bool is_playing() const;
- virtual int get_loop_count() const = 0; //times it looped
+ virtual int get_loop_count() const; //times it looped
- virtual float get_playback_position() const = 0;
- virtual void seek(float p_time) = 0;
+ virtual float get_playback_position() const;
+ virtual void seek(float p_time);
- virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) = 0;
+ virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
};
class AudioStreamPlaybackResampled : public AudioStreamPlayback {
@@ -84,11 +97,15 @@ class AudioStream : public Resource {
protected:
static void _bind_methods();
+ GDVIRTUAL0RC(Ref<AudioStreamPlayback>, _instance_playback)
+ GDVIRTUAL0RC(String, _get_stream_name)
+ GDVIRTUAL0RC(float, _get_length)
+
public:
- virtual Ref<AudioStreamPlayback> instance_playback() = 0;
- virtual String get_stream_name() const = 0;
+ virtual Ref<AudioStreamPlayback> instance_playback();
+ virtual String get_stream_name() const;
- virtual float get_length() const = 0; //if supported, otherwise return 0
+ virtual float get_length() const;
};
// Microphone
diff --git a/servers/register_server_types.cpp b/servers/register_server_types.cpp
index 857f112102..8e86957d9b 100644
--- a/servers/register_server_types.cpp
+++ b/servers/register_server_types.cpp
@@ -140,8 +140,8 @@ void register_server_types() {
GDREGISTER_VIRTUAL_CLASS(XRInterface);
GDREGISTER_CLASS(XRPositionalTracker);
- GDREGISTER_VIRTUAL_CLASS(AudioStream);
- GDREGISTER_VIRTUAL_CLASS(AudioStreamPlayback);
+ GDREGISTER_CLASS(AudioStream);
+ GDREGISTER_CLASS(AudioStreamPlayback);
GDREGISTER_VIRTUAL_CLASS(AudioStreamPlaybackResampled);
GDREGISTER_CLASS(AudioStreamMicrophone);
GDREGISTER_CLASS(AudioStreamRandomPitch);