godot/scene/resources/audio_stream_polyphonic.h
Juan Linietsky f18d408c08 Add AudioStreamPolyphonic to make it easier to play polyphonic sound from code
* This new audio stream allows to play multiple sounds and control them over time from code.
* It greatly simplifies tasks such as generative music (music generated from code) or audio.

This new type of stream was added with the goal of fixing audio blending in AnimationPlayer and AnimationTree, but can be used by others for their regular audio needs.

Does not fix anything currently, but should help implement #69758 properly.

Some demo code of how to use this:

```GDScript

var player = $SomeNode as AudioStreamPlayer
player.stream = AudioStreamPolyphonic.new()
var playback = player.get_stream_playback() as AudioStreamPlaybackPolyphonic
var id = playback.play_stream(preload("res://Clip1.ogg"))
await get_tree().create_timer(1).timeout
playback.set_stream_volume(id,-12) # Set volume to half after one second
await get_tree().create_timer(2).timeout
var id2 = playback.play_stream(preload("res://Clip2.ogg")) # 2 seconds later, start another clip
await get_tree().create_timer(1).timeout
playback.stop_stream(id) # 1 second later, kill the first clip
playback.set_stream_pitch_scale(id2,1.5) # Make the second clip go 50% faster

```
2023-01-22 16:22:45 +01:00

123 lines
4.5 KiB
C++

/**************************************************************************/
/* audio_stream_polyphonic.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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_STREAM_POLYPHONIC_H
#define AUDIO_STREAM_POLYPHONIC_H
#include "core/templates/local_vector.h"
#include "servers/audio/audio_stream.h"
class AudioStreamPolyphonic : public AudioStream {
GDCLASS(AudioStreamPolyphonic, AudioStream)
int polyphony = 32;
static void _bind_methods();
public:
virtual Ref<AudioStreamPlayback> instantiate_playback() override;
virtual String get_stream_name() const override;
virtual bool is_monophonic() const override;
void set_polyphony(int p_voices);
int get_polyphony() const;
AudioStreamPolyphonic();
};
class AudioStreamPlaybackPolyphonic : public AudioStreamPlayback {
GDCLASS(AudioStreamPlaybackPolyphonic, AudioStreamPlayback)
enum {
INTERNAL_BUFFER_LEN = 128,
ID_MASK = 0xFFFFFFFF,
INDEX_SHIFT = 32
};
struct Stream {
SafeFlag active;
SafeFlag pending_play;
SafeFlag finish_request;
SafeFlag finalizing;
float play_offset = 0;
float pitch_scale = 1.0;
Ref<AudioStream> stream;
Ref<AudioStreamPlayback> stream_playback;
float prev_volume_db = 0;
float volume_db = 0;
uint32_t id = 0;
Stream() :
active(false), pending_play(false), finish_request(false), finalizing(false) {}
};
LocalVector<Stream> streams;
AudioFrame internal_buffer[INTERNAL_BUFFER_LEN];
bool active = false;
uint32_t id_counter = 1;
_FORCE_INLINE_ Stream *_find_stream(int64_t p_id);
void _check_finalized_streams();
friend class AudioStreamPolyphonic;
protected:
static void _bind_methods();
public:
typedef int64_t ID;
enum {
INVALID_ID = -1
};
virtual void start(double p_from_pos = 0.0) override;
virtual void stop() override;
virtual bool is_playing() const override;
virtual int get_loop_count() const override; //times it looped
virtual double get_playback_position() const override;
virtual void seek(double p_time) override;
virtual void tag_used_streams() override;
virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
ID play_stream(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0);
void set_stream_volume(ID p_stream_id, float p_volume_db);
void set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale);
bool is_stream_playing(ID p_stream_id) const;
void stop_stream(ID p_stream_id);
AudioStreamPlaybackPolyphonic();
};
#endif // AUDIO_STREAM_POLYPHONIC_H