Path: blob/master/scene/resources/audio_stream_polyphonic.h
9896 views
/**************************************************************************/1/* audio_stream_polyphonic.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/templates/local_vector.h"33#include "scene/scene_string_names.h"34#include "servers/audio/audio_stream.h"35#include "servers/audio_server.h"3637class AudioStreamPolyphonic : public AudioStream {38GDCLASS(AudioStreamPolyphonic, AudioStream)39int polyphony = 32;4041AudioServer::PlaybackType playback_type;4243static void _bind_methods();4445public:46virtual Ref<AudioStreamPlayback> instantiate_playback() override;47virtual String get_stream_name() const override;48virtual bool is_monophonic() const override;4950void set_polyphony(int p_voices);51int get_polyphony() const;5253virtual bool is_meta_stream() const override { return true; }5455AudioStreamPolyphonic();56};5758class AudioStreamPlaybackPolyphonic : public AudioStreamPlayback {59GDCLASS(AudioStreamPlaybackPolyphonic, AudioStreamPlayback)6061constexpr static uint32_t INTERNAL_BUFFER_LEN = 128;6263struct Stream {64SafeFlag active;65SafeFlag pending_play;66SafeFlag finish_request;67float play_offset = 0;68float pitch_scale = 1.0;69Ref<AudioStream> stream;70Ref<AudioStreamPlayback> stream_playback;71float prev_volume_db = 0;72float volume_db = 0;73uint32_t id = 0;7475Stream() :76active(false), pending_play(false), finish_request(false) {}77};7879LocalVector<Stream> streams;80AudioFrame internal_buffer[INTERNAL_BUFFER_LEN];8182bool active = false;83uint32_t id_counter = 1;8485bool _is_sample = false;86Ref<AudioSamplePlayback> sample_playback;8788_FORCE_INLINE_ Stream *_find_stream(int64_t p_id);89_FORCE_INLINE_ const Stream *_find_stream(int64_t p_id) const;9091friend class AudioStreamPolyphonic;9293protected:94static void _bind_methods();9596public:97typedef int64_t ID;98enum {99INVALID_ID = -1100};101102virtual void start(double p_from_pos = 0.0) override;103virtual void stop() override;104virtual bool is_playing() const override;105106virtual int get_loop_count() const override; //times it looped107108virtual double get_playback_position() const override;109virtual void seek(double p_time) override;110111virtual void tag_used_streams() override;112113virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;114115ID play_stream(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0, AudioServer::PlaybackType p_playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT, const StringName &p_bus = SceneStringName(Master));116void set_stream_volume(ID p_stream_id, float p_volume_db);117void set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale);118bool is_stream_playing(ID p_stream_id) const;119void stop_stream(ID p_stream_id);120121virtual void set_is_sample(bool p_is_sample) override;122virtual bool get_is_sample() const override;123virtual Ref<AudioSamplePlayback> get_sample_playback() const override;124virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;125126private:127#ifndef DISABLE_DEPRECATED128ID _play_stream_bind_compat_91382(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0);129static void _bind_compatibility_methods();130#endif // DISABLE_DEPRECATED131132public:133AudioStreamPlaybackPolyphonic();134};135136137