1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*************************************************************************/
/* audio_stream_wav.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 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_STREAM_WAV_H
#define AUDIO_STREAM_WAV_H
#include "servers/audio/audio_stream.h"
class AudioStreamWAV;
class AudioStreamPlaybackWAV : public AudioStreamPlayback {
GDCLASS(AudioStreamPlaybackWAV, AudioStreamPlayback);
enum {
MIX_FRAC_BITS = 13,
MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
};
struct IMA_ADPCM_State {
int16_t step_index = 0;
int32_t predictor = 0;
/* values at loop point */
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;
} ima_adpcm[2];
int64_t offset = 0;
int sign = 1;
bool active = false;
friend class AudioStreamWAV;
Ref<AudioStreamWAV> base;
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);
public:
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 int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
virtual void tag_used_streams() override;
AudioStreamPlaybackWAV();
};
class AudioStreamWAV : public AudioStream {
GDCLASS(AudioStreamWAV, AudioStream);
RES_BASE_EXTENSION("sample")
public:
enum Format {
FORMAT_8_BITS,
FORMAT_16_BITS,
FORMAT_IMA_ADPCM
};
// Keep the ResourceImporterWAV `edit/loop_mode` enum hint in sync with these options.
enum LoopMode {
LOOP_DISABLED,
LOOP_FORWARD,
LOOP_PINGPONG,
LOOP_BACKWARD
};
private:
friend class AudioStreamPlaybackWAV;
enum {
DATA_PAD = 16 //padding for interpolation
};
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;
protected:
static void _bind_methods();
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;
virtual double get_length() const override; //if supported, otherwise return 0
virtual bool is_monophonic() const override;
void set_data(const Vector<uint8_t> &p_data);
Vector<uint8_t> get_data() const;
Error save_to_wav(const String &p_path);
virtual Ref<AudioStreamPlayback> instantiate_playback() override;
virtual String get_stream_name() const override;
AudioStreamWAV();
~AudioStreamWAV();
};
VARIANT_ENUM_CAST(AudioStreamWAV::Format)
VARIANT_ENUM_CAST(AudioStreamWAV::LoopMode)
#endif // AUDIO_STREAM_WAV_H
|