Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of AudioEffectStream resource and StreamAudio node #44608

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/audio_effect_stream/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SCsub

Import("env")

module_env = env.Clone()
module_env.add_source_files(env.modules_sources, "*.cpp")
109 changes: 109 additions & 0 deletions modules/audio_effect_stream/audio_effect_stream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*************************************************************************/
/* audio_effect_stream.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include "audio_effect_stream.h"

void AudioEffectStream::_bind_methods() {
ClassDB::bind_method(D_METHOD("init"), &AudioEffectStream::init);
ClassDB::bind_method(D_METHOD("is_streaming_active"), &AudioEffectStream::is_streaming_active);
}

Ref<AudioEffectInstance> AudioEffectStream::instance() {
Ref<AudioEffectStreamInstance> ins;
ins.instance();
ins->base = Ref<AudioEffectStream>(this);
current_instance = ins;

return ins;
}

Ref<RingBufferAudioFrame> AudioEffectStream::init(int32_t p_ring_buffer_max_size) {
if (current_instance == 0) {
WARN_PRINT("Streaming should not be set as active before Godot has initialized.");
buffering_active = false;
return nullptr;
}

buffering_active = true;
current_instance->output_ring_buffer.instance();
int32_t higher_power = Math::ceil(Math::log(double(p_ring_buffer_max_size)) / Math::log(2.0));
current_instance->output_ring_buffer->get().resize(higher_power);
current_instance->set_streaming(true);
return current_instance->output_ring_buffer;
}

bool AudioEffectStream::is_streaming_active() const {
return buffering_active;
}

AudioEffectStream::AudioEffectStream() {
}

AudioEffectStream::~AudioEffectStream() {
current_instance.unref();
}

void AudioEffectStreamInstance::init() {
set_streaming(true);
}

void AudioEffectStreamInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
Ref<RingBufferAudioFrame> ring_buffer = output_ring_buffer;

for (int i = 0; i < p_frame_count; i++) {
p_dst_frames[i] = p_src_frames[i];
}

if (!get_streaming() || ring_buffer.is_null() || !ring_buffer->get().size()) {
return;
}

//Add incoming audio frames to the IO ring buffer
ring_buffer->get().write(p_src_frames, p_frame_count);
}

bool AudioEffectStreamInstance::process_silence() const {
return true;
}

AudioEffectStreamInstance::AudioEffectStreamInstance() {
}

AudioEffectStreamInstance::~AudioEffectStreamInstance() {
output_ring_buffer.unref();
}

void AudioEffectStreamInstance::set_streaming(bool val) {
is_streaming = val;
}

bool AudioEffectStreamInstance::get_streaming() const {
return is_streaming;
}
85 changes: 85 additions & 0 deletions modules/audio_effect_stream/audio_effect_stream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*************************************************************************/
/* audio_effect_stream.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef AUDIO_EFFECT_STREAM_OPUS_H
#define AUDIO_EFFECT_STREAM_OPUS_H

#include "core/config/engine.h"
#include "servers/audio_server.h"

#include "ring_buffer_audio_frame.h"
#include "servers/audio/audio_effect.h"

class AudioEffectStream;

class AudioEffectStreamInstance : public AudioEffectInstance {
GDCLASS(AudioEffectStreamInstance, AudioEffectInstance);
friend class AudioEffectStream;
Ref<AudioEffectStream> base;

bool is_streaming = false;

Ref<RingBufferAudioFrame> output_ring_buffer;
enum {
IO_BUFFER_SIZE_MS = 1500
};

public:
void init();
virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override;
virtual bool process_silence() const override;
AudioEffectStreamInstance();
~AudioEffectStreamInstance();
void set_streaming(bool val);
bool get_streaming() const;
};

class AudioEffectStream : public AudioEffect {
GDCLASS(AudioEffectStream, AudioEffect)
friend class AudioEffectStreamInstance;

Ref<AudioEffectStreamInstance> current_instance;

protected:
static void _bind_methods();

public:
virtual Ref<AudioEffectInstance> instance() override;

Ref<RingBufferAudioFrame> init(int32_t p_ring_buffer_max_size);
bool is_streaming_active() const;
AudioEffectStream();
~AudioEffectStream();

private:
bool buffering_active;
};

#endif // AUDIO_EFFECT_STREAM_OPUS_H
9 changes: 9 additions & 0 deletions modules/audio_effect_stream/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# config.py


def can_build(env, platform):
return True


def configure(env):
pass
44 changes: 44 additions & 0 deletions modules/audio_effect_stream/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*************************************************************************/
/* register_types.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include "register_types.h"

#include "audio_effect_stream.h"
#include "core/object/class_db.h"
#include "stream_audio.h"

void register_audio_effect_stream_types() {
ClassDB::register_class<RingBufferAudioFrame>();
ClassDB::register_class<AudioEffectStream>();
ClassDB::register_class<StreamAudio>();
}

void unregister_audio_effect_stream_types() {
}
34 changes: 34 additions & 0 deletions modules/audio_effect_stream/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*************************************************************************/
/* register_types.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#pragma once

void register_audio_effect_stream_types();
void unregister_audio_effect_stream_types();
48 changes: 48 additions & 0 deletions modules/audio_effect_stream/ring_buffer_audio_frame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*************************************************************************/
/* ring_buffer_audio_frame.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef RING_BUFFER_AUDIO_FRAME_H
#define RING_BUFFER_AUDIO_FRAME_H

#include "core/math/audio_frame.h"
#include "core/object/reference.h"
#include "core/templates/vector.h"

struct AudioFrame;

class RingBufferAudioFrame : public Reference {
GDCLASS(RingBufferAudioFrame, Reference);
RingBuffer<AudioFrame> ring;

public:
RingBuffer<AudioFrame> &get() { return ring; }
};

#endif
Loading