2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_stream_sample.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2019-01-01 11:53:14 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-02-03 01:51:26 +00:00
|
|
|
#ifndef AUDIOSTREAMSAMPLE_H
|
|
|
|
#define AUDIOSTREAMSAMPLE_H
|
|
|
|
|
|
|
|
#include "servers/audio/audio_stream.h"
|
|
|
|
|
|
|
|
class AudioStreamSample;
|
|
|
|
|
|
|
|
class AudioStreamPlaybackSample : public AudioStreamPlayback {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
GDCLASS(AudioStreamPlaybackSample, AudioStreamPlayback)
|
2017-02-03 01:51:26 +00:00
|
|
|
enum {
|
2017-03-05 15:44:50 +00:00
|
|
|
MIX_FRAC_BITS = 13,
|
|
|
|
MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
|
|
|
|
MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
|
2017-02-03 01:51:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IMA_ADPCM_State {
|
|
|
|
|
|
|
|
int16_t step_index;
|
|
|
|
int32_t predictor;
|
|
|
|
/* values at loop point */
|
|
|
|
int16_t loop_step_index;
|
|
|
|
int32_t loop_predictor;
|
|
|
|
int32_t last_nibble;
|
|
|
|
int32_t loop_pos;
|
|
|
|
int32_t window_ofs;
|
|
|
|
} ima_adpcm[2];
|
|
|
|
|
|
|
|
int64_t offset;
|
|
|
|
int sign;
|
|
|
|
bool active;
|
2017-03-05 15:44:50 +00:00
|
|
|
friend class AudioStreamSample;
|
2017-02-03 01:51:26 +00:00
|
|
|
Ref<AudioStreamSample> base;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class Depth, bool is_stereo, bool is_ima_adpcm>
|
|
|
|
void do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &offset, int32_t &increment, uint32_t amount, IMA_ADPCM_State *ima_adpcm);
|
2017-02-03 01:51:26 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
|
|
|
virtual void start(float p_from_pos = 0.0);
|
2017-02-03 01:51:26 +00:00
|
|
|
virtual void stop();
|
|
|
|
virtual bool is_playing() const;
|
|
|
|
|
|
|
|
virtual int get_loop_count() const; //times it looped
|
|
|
|
|
2017-09-21 03:31:36 +00:00
|
|
|
virtual float get_playback_position() const;
|
|
|
|
virtual void seek(float p_time);
|
2017-02-03 01:51:26 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
|
2017-02-03 01:51:26 +00:00
|
|
|
|
|
|
|
AudioStreamPlaybackSample();
|
|
|
|
};
|
|
|
|
|
|
|
|
class AudioStreamSample : public AudioStream {
|
2017-03-05 15:44:50 +00:00
|
|
|
GDCLASS(AudioStreamSample, AudioStream)
|
2017-06-15 22:44:11 +00:00
|
|
|
RES_BASE_EXTENSION("sample")
|
2017-02-03 01:51:26 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
enum Format {
|
|
|
|
FORMAT_8_BITS,
|
|
|
|
FORMAT_16_BITS,
|
|
|
|
FORMAT_IMA_ADPCM
|
|
|
|
};
|
|
|
|
|
|
|
|
enum LoopMode {
|
|
|
|
LOOP_DISABLED,
|
|
|
|
LOOP_FORWARD,
|
2018-10-28 18:37:14 +00:00
|
|
|
LOOP_PING_PONG,
|
|
|
|
LOOP_BACKWARD
|
2017-02-03 01:51:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2017-03-05 15:44:50 +00:00
|
|
|
friend class AudioStreamPlaybackSample;
|
2017-02-03 01:51:26 +00:00
|
|
|
|
|
|
|
enum {
|
2017-03-05 15:44:50 +00:00
|
|
|
DATA_PAD = 16 //padding for interpolation
|
2017-02-03 01:51:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Format format;
|
|
|
|
LoopMode loop_mode;
|
|
|
|
bool stereo;
|
|
|
|
int loop_begin;
|
|
|
|
int loop_end;
|
|
|
|
int mix_rate;
|
|
|
|
void *data;
|
|
|
|
uint32_t data_bytes;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
protected:
|
2017-02-03 01:51:26 +00:00
|
|
|
static void _bind_methods();
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2017-02-03 01:51:26 +00:00
|
|
|
public:
|
|
|
|
void set_format(Format p_format);
|
|
|
|
Format get_format() const;
|
|
|
|
|
|
|
|
void set_loop_mode(LoopMode p_loop_mode);
|
|
|
|
LoopMode get_loop_mode() const;
|
|
|
|
|
|
|
|
void set_loop_begin(int p_frame);
|
|
|
|
int get_loop_begin() const;
|
|
|
|
|
|
|
|
void set_loop_end(int p_frame);
|
|
|
|
int get_loop_end() const;
|
|
|
|
|
|
|
|
void set_mix_rate(int p_hz);
|
|
|
|
int get_mix_rate() const;
|
|
|
|
|
|
|
|
void set_stereo(bool p_enable);
|
|
|
|
bool is_stereo() const;
|
|
|
|
|
2018-01-22 19:35:33 +00:00
|
|
|
virtual float get_length() const; //if supported, otherwise return 0
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void set_data(const PoolVector<uint8_t> &p_data);
|
2017-02-03 01:51:26 +00:00
|
|
|
PoolVector<uint8_t> get_data() const;
|
|
|
|
|
2019-03-12 14:48:52 +00:00
|
|
|
Error save_to_wav(const String &p_path);
|
2018-01-18 15:03:53 +00:00
|
|
|
|
2017-02-03 01:51:26 +00:00
|
|
|
virtual Ref<AudioStreamPlayback> instance_playback();
|
|
|
|
virtual String get_stream_name() const;
|
|
|
|
|
|
|
|
AudioStreamSample();
|
|
|
|
~AudioStreamSample();
|
|
|
|
};
|
|
|
|
|
|
|
|
VARIANT_ENUM_CAST(AudioStreamSample::Format)
|
|
|
|
VARIANT_ENUM_CAST(AudioStreamSample::LoopMode)
|
|
|
|
|
|
|
|
#endif // AUDIOSTREAMSample_H
|