2017-03-05 15:47:28 +01:00
|
|
|
/**************************************************************************/
|
2022-07-23 16:34:36 +02:00
|
|
|
/* audio_stream_wav.h */
|
2017-03-05 15:47:28 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2022-07-23 16:34:36 +02:00
|
|
|
#ifndef AUDIO_STREAM_WAV_H
|
|
|
|
#define AUDIO_STREAM_WAV_H
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2024-04-17 01:33:29 +02:00
|
|
|
#define QOA_IMPLEMENTATION
|
|
|
|
#define QOA_NO_STDIO
|
|
|
|
|
2017-02-03 02:51:26 +01:00
|
|
|
#include "servers/audio/audio_stream.h"
|
2024-04-17 01:33:29 +02:00
|
|
|
#include "thirdparty/misc/qoa.h"
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2022-07-23 16:34:36 +02:00
|
|
|
class AudioStreamWAV;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2022-07-23 16:34:36 +02:00
|
|
|
class AudioStreamPlaybackWAV : public AudioStreamPlayback {
|
|
|
|
GDCLASS(AudioStreamPlaybackWAV, AudioStreamPlayback);
|
2017-02-03 02:51:26 +01:00
|
|
|
enum {
|
|
|
|
MIX_FRAC_BITS = 13,
|
|
|
|
MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
|
|
|
|
MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IMA_ADPCM_State {
|
2021-02-09 18:24:36 +01:00
|
|
|
int16_t step_index = 0;
|
|
|
|
int32_t predictor = 0;
|
2017-02-03 02:51:26 +01:00
|
|
|
/* values at loop point */
|
2021-02-09 18:24:36 +01:00
|
|
|
int16_t loop_step_index = 0;
|
|
|
|
int32_t loop_predictor = 0;
|
|
|
|
int32_t last_nibble = 0;
|
|
|
|
int32_t loop_pos = 0;
|
|
|
|
int32_t window_ofs = 0;
|
2017-02-03 02:51:26 +01:00
|
|
|
} ima_adpcm[2];
|
|
|
|
|
2024-04-17 01:33:29 +02:00
|
|
|
struct QOA_State {
|
|
|
|
qoa_desc *desc = nullptr;
|
|
|
|
uint32_t data_ofs = 0;
|
|
|
|
uint32_t frame_len = 0;
|
|
|
|
int16_t *dec = nullptr;
|
|
|
|
uint32_t dec_len = 0;
|
|
|
|
int64_t cache_pos = -1;
|
|
|
|
int16_t cache[2] = { 0, 0 };
|
|
|
|
int16_t cache_r[2] = { 0, 0 };
|
|
|
|
} qoa;
|
|
|
|
|
2021-02-09 18:24:36 +01:00
|
|
|
int64_t offset = 0;
|
|
|
|
int sign = 1;
|
|
|
|
bool active = false;
|
2022-07-23 16:34:36 +02:00
|
|
|
friend class AudioStreamWAV;
|
|
|
|
Ref<AudioStreamWAV> base;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2024-04-17 01:33:29 +02:00
|
|
|
template <typename Depth, bool is_stereo, bool is_ima_adpcm, bool is_qoa>
|
|
|
|
void do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &p_offset, int32_t &p_increment, uint32_t p_amount, IMA_ADPCM_State *p_ima_adpcm, QOA_State *p_qoa);
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
2022-09-22 15:54:15 +02:00
|
|
|
virtual void start(double p_from_pos = 0.0) override;
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual void stop() override;
|
|
|
|
virtual bool is_playing() const override;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual int get_loop_count() const override; //times it looped
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
virtual double get_playback_position() const override;
|
|
|
|
virtual void seek(double p_time) override;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2021-08-27 19:27:58 +02:00
|
|
|
virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2022-07-21 01:00:58 +02:00
|
|
|
virtual void tag_used_streams() override;
|
|
|
|
|
2022-07-23 16:34:36 +02:00
|
|
|
AudioStreamPlaybackWAV();
|
2024-04-17 01:33:29 +02:00
|
|
|
~AudioStreamPlaybackWAV();
|
2017-02-03 02:51:26 +01:00
|
|
|
};
|
|
|
|
|
2022-07-23 16:34:36 +02:00
|
|
|
class AudioStreamWAV : public AudioStream {
|
|
|
|
GDCLASS(AudioStreamWAV, AudioStream);
|
2017-06-16 00:44:11 +02:00
|
|
|
RES_BASE_EXTENSION("sample")
|
2017-02-03 02:51:26 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
enum Format {
|
|
|
|
FORMAT_8_BITS,
|
|
|
|
FORMAT_16_BITS,
|
2024-04-17 01:33:29 +02:00
|
|
|
FORMAT_IMA_ADPCM,
|
|
|
|
FORMAT_QOA,
|
2017-02-03 02:51:26 +01:00
|
|
|
};
|
|
|
|
|
2022-03-15 16:09:39 +01:00
|
|
|
// Keep the ResourceImporterWAV `edit/loop_mode` enum hint in sync with these options.
|
2017-02-03 02:51:26 +01:00
|
|
|
enum LoopMode {
|
|
|
|
LOOP_DISABLED,
|
|
|
|
LOOP_FORWARD,
|
2021-10-15 15:25:00 +02:00
|
|
|
LOOP_PINGPONG,
|
2018-10-28 19:37:14 +01:00
|
|
|
LOOP_BACKWARD
|
2017-02-03 02:51:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2022-07-23 16:34:36 +02:00
|
|
|
friend class AudioStreamPlaybackWAV;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
|
|
|
enum {
|
|
|
|
DATA_PAD = 16 //padding for interpolation
|
|
|
|
};
|
|
|
|
|
2021-02-09 18:24:36 +01:00
|
|
|
Format format = FORMAT_8_BITS;
|
|
|
|
LoopMode loop_mode = LOOP_DISABLED;
|
|
|
|
bool stereo = false;
|
|
|
|
int loop_begin = 0;
|
|
|
|
int loop_end = 0;
|
|
|
|
int mix_rate = 44100;
|
|
|
|
void *data = nullptr;
|
|
|
|
uint32_t data_bytes = 0;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2017-02-03 02:51:26 +01: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;
|
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
virtual double get_length() const override; //if supported, otherwise return 0
|
2018-01-22 20:35:33 +01:00
|
|
|
|
2021-08-28 06:51:03 +02:00
|
|
|
virtual bool is_monophonic() const override;
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
void set_data(const Vector<uint8_t> &p_data);
|
|
|
|
Vector<uint8_t> get_data() const;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2019-03-12 15:48:52 +01:00
|
|
|
Error save_to_wav(const String &p_path);
|
2018-01-18 16:03:53 +01:00
|
|
|
|
2022-07-21 01:00:58 +02:00
|
|
|
virtual Ref<AudioStreamPlayback> instantiate_playback() override;
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual String get_stream_name() const override;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2022-07-23 16:34:36 +02:00
|
|
|
AudioStreamWAV();
|
|
|
|
~AudioStreamWAV();
|
2017-02-03 02:51:26 +01:00
|
|
|
};
|
|
|
|
|
2022-07-23 16:34:36 +02:00
|
|
|
VARIANT_ENUM_CAST(AudioStreamWAV::Format)
|
|
|
|
VARIANT_ENUM_CAST(AudioStreamWAV::LoopMode)
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2022-07-23 16:34:36 +02:00
|
|
|
#endif // AUDIO_STREAM_WAV_H
|