1/**************************************************************************/
2/* audio_stream_wav.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef AUDIO_STREAM_WAV_H
32#define AUDIO_STREAM_WAV_H
33
34#include "servers/audio/audio_stream.h"
35
36class AudioStreamWAV;
37
38class AudioStreamPlaybackWAV : public AudioStreamPlayback {
39 GDCLASS(AudioStreamPlaybackWAV, AudioStreamPlayback);
40 enum {
41 MIX_FRAC_BITS = 13,
42 MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
43 MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
44 };
45
46 struct IMA_ADPCM_State {
47 int16_t step_index = 0;
48 int32_t predictor = 0;
49 /* values at loop point */
50 int16_t loop_step_index = 0;
51 int32_t loop_predictor = 0;
52 int32_t last_nibble = 0;
53 int32_t loop_pos = 0;
54 int32_t window_ofs = 0;
55 } ima_adpcm[2];
56
57 int64_t offset = 0;
58 int sign = 1;
59 bool active = false;
60 friend class AudioStreamWAV;
61 Ref<AudioStreamWAV> base;
62
63 template <class Depth, bool is_stereo, bool is_ima_adpcm>
64 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);
65
66public:
67 virtual void start(double p_from_pos = 0.0) override;
68 virtual void stop() override;
69 virtual bool is_playing() const override;
70
71 virtual int get_loop_count() const override; //times it looped
72
73 virtual double get_playback_position() const override;
74 virtual void seek(double p_time) override;
75
76 virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
77
78 virtual void tag_used_streams() override;
79
80 AudioStreamPlaybackWAV();
81};
82
83class AudioStreamWAV : public AudioStream {
84 GDCLASS(AudioStreamWAV, AudioStream);
85 RES_BASE_EXTENSION("sample")
86
87public:
88 enum Format {
89 FORMAT_8_BITS,
90 FORMAT_16_BITS,
91 FORMAT_IMA_ADPCM
92 };
93
94 // Keep the ResourceImporterWAV `edit/loop_mode` enum hint in sync with these options.
95 enum LoopMode {
96 LOOP_DISABLED,
97 LOOP_FORWARD,
98 LOOP_PINGPONG,
99 LOOP_BACKWARD
100 };
101
102private:
103 friend class AudioStreamPlaybackWAV;
104
105 enum {
106 DATA_PAD = 16 //padding for interpolation
107 };
108
109 Format format = FORMAT_8_BITS;
110 LoopMode loop_mode = LOOP_DISABLED;
111 bool stereo = false;
112 int loop_begin = 0;
113 int loop_end = 0;
114 int mix_rate = 44100;
115 void *data = nullptr;
116 uint32_t data_bytes = 0;
117
118protected:
119 static void _bind_methods();
120
121public:
122 void set_format(Format p_format);
123 Format get_format() const;
124
125 void set_loop_mode(LoopMode p_loop_mode);
126 LoopMode get_loop_mode() const;
127
128 void set_loop_begin(int p_frame);
129 int get_loop_begin() const;
130
131 void set_loop_end(int p_frame);
132 int get_loop_end() const;
133
134 void set_mix_rate(int p_hz);
135 int get_mix_rate() const;
136
137 void set_stereo(bool p_enable);
138 bool is_stereo() const;
139
140 virtual double get_length() const override; //if supported, otherwise return 0
141
142 virtual bool is_monophonic() const override;
143
144 void set_data(const Vector<uint8_t> &p_data);
145 Vector<uint8_t> get_data() const;
146
147 Error save_to_wav(const String &p_path);
148
149 virtual Ref<AudioStreamPlayback> instantiate_playback() override;
150 virtual String get_stream_name() const override;
151
152 AudioStreamWAV();
153 ~AudioStreamWAV();
154};
155
156VARIANT_ENUM_CAST(AudioStreamWAV::Format)
157VARIANT_ENUM_CAST(AudioStreamWAV::LoopMode)
158
159#endif // AUDIO_STREAM_WAV_H
160