Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_audio_stream_wav.cpp
45993 views
1
/**************************************************************************/
2
/* test_audio_stream_wav.cpp */
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
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_audio_stream_wav)
34
35
#include "core/io/file_access.h"
36
#include "core/io/marshalls.h"
37
#include "core/math/math_defs.h"
38
#include "core/math/math_funcs.h"
39
#include "scene/resources/audio_stream_wav.h"
40
#include "tests/test_utils.h"
41
42
namespace TestAudioStreamWAV {
43
44
// Default wav rate for test cases.
45
constexpr float WAV_RATE = 44100;
46
/* Default wav count for test cases. 1 second of audio is used so that the file can be listened
47
to manually if needed. */
48
constexpr int WAV_COUNT = WAV_RATE;
49
50
float gen_wav(float frequency, float wav_rate, int wav_number) {
51
// formula for generating a sin wave with given frequency.
52
return Math::sin((Math::TAU * frequency / wav_rate) * wav_number);
53
}
54
55
/* Generates a 440Hz sin wave in channel 0 (mono channel or left stereo channel)
56
* and a 261.63Hz wave in channel 1 (right stereo channel).
57
* These waves correspond to the music notes A4 and C4 respectively.
58
*/
59
Vector<uint8_t> gen_pcm8_test(float wav_rate, int wav_count, bool stereo) {
60
Vector<uint8_t> buffer;
61
buffer.resize(stereo ? wav_count * 2 : wav_count);
62
63
uint8_t *write_ptr = buffer.ptrw();
64
for (int i = 0; i < buffer.size(); i++) {
65
float wav;
66
if (stereo) {
67
if (i % 2 == 0) {
68
wav = gen_wav(440, wav_rate, i / 2);
69
} else {
70
wav = gen_wav(261.63, wav_rate, i / 2);
71
}
72
} else {
73
wav = gen_wav(440, wav_rate, i);
74
}
75
76
// Map sin wave to full range of 8-bit values.
77
uint8_t wav_8bit = Math::fast_ftoi(((wav + 1) / 2) * UINT8_MAX);
78
// Unlike the .wav format, AudioStreamWAV expects signed 8-bit wavs.
79
uint8_t wav_8bit_signed = wav_8bit - (INT8_MAX + 1);
80
write_ptr[i] = wav_8bit_signed;
81
}
82
83
return buffer;
84
}
85
86
// Same as gen_pcm8_test but with 16-bit wavs.
87
Vector<uint8_t> gen_pcm16_test(float wav_rate, int wav_count, bool stereo) {
88
Vector<uint8_t> buffer;
89
buffer.resize(stereo ? wav_count * 4 : wav_count * 2);
90
91
uint8_t *write_ptr = buffer.ptrw();
92
for (int i = 0; i < buffer.size() / 2; i++) {
93
float wav;
94
if (stereo) {
95
if (i % 2 == 0) {
96
wav = gen_wav(440, wav_rate, i / 2);
97
} else {
98
wav = gen_wav(261.63, wav_rate, i / 2);
99
}
100
} else {
101
wav = gen_wav(440, wav_rate, i);
102
}
103
104
// Map sin wave to full range of 16-bit values.
105
uint16_t wav_16bit = Math::fast_ftoi(((wav + 1) / 2) * UINT16_MAX);
106
// The .wav format expects wavs larger than 8 bits to be signed.
107
uint16_t wav_16bit_signed = wav_16bit - (INT16_MAX + 1);
108
encode_uint16(wav_16bit_signed, write_ptr + (i * 2));
109
}
110
111
return buffer;
112
}
113
114
void run_test(String file_name, AudioStreamWAV::Format data_format, bool stereo, float wav_rate, float wav_count) {
115
String save_path = TestUtils::get_temp_path(file_name);
116
117
Vector<uint8_t> test_data;
118
if (data_format == AudioStreamWAV::FORMAT_8_BITS) {
119
test_data = gen_pcm8_test(wav_rate, wav_count, stereo);
120
} else {
121
test_data = gen_pcm16_test(wav_rate, wav_count, stereo);
122
}
123
124
Ref<AudioStreamWAV> stream = memnew(AudioStreamWAV);
125
stream->set_mix_rate(wav_rate);
126
CHECK(stream->get_mix_rate() == wav_rate);
127
128
stream->set_format(data_format);
129
CHECK(stream->get_format() == data_format);
130
131
stream->set_stereo(stereo);
132
CHECK(stream->is_stereo() == stereo);
133
134
stream->set_data(test_data);
135
CHECK(stream->get_data() == test_data);
136
137
SUBCASE("Stream length is computed properly") {
138
CHECK(stream->get_length() == doctest::Approx(double(wav_count / wav_rate)));
139
}
140
141
SUBCASE("Stream can be saved as .wav") {
142
REQUIRE(stream->save_to_wav(save_path) == OK);
143
144
Error error;
145
Ref<FileAccess> wav_file = FileAccess::open(save_path, FileAccess::READ, &error);
146
REQUIRE(error == OK);
147
148
Dictionary options;
149
Ref<AudioStreamWAV> loaded_stream = AudioStreamWAV::load_from_file(save_path, options);
150
151
CHECK(loaded_stream->get_format() == stream->get_format());
152
CHECK(loaded_stream->get_loop_mode() == stream->get_loop_mode());
153
CHECK(loaded_stream->get_loop_begin() == stream->get_loop_begin());
154
CHECK(loaded_stream->get_loop_end() == stream->get_loop_end());
155
CHECK(loaded_stream->get_mix_rate() == stream->get_mix_rate());
156
CHECK(loaded_stream->is_stereo() == stream->is_stereo());
157
CHECK(loaded_stream->get_length() == stream->get_length());
158
CHECK(loaded_stream->is_monophonic() == stream->is_monophonic());
159
CHECK(loaded_stream->get_data() == stream->get_data());
160
}
161
}
162
163
TEST_CASE("[Audio][AudioStreamWAV] Mono PCM8 format") {
164
run_test("test_pcm8_mono.wav", AudioStreamWAV::FORMAT_8_BITS, false, WAV_RATE, WAV_COUNT);
165
}
166
167
TEST_CASE("[Audio][AudioStreamWAV] Mono PCM16 format") {
168
run_test("test_pcm16_mono.wav", AudioStreamWAV::FORMAT_16_BITS, false, WAV_RATE, WAV_COUNT);
169
}
170
171
TEST_CASE("[Audio][AudioStreamWAV] Stereo PCM8 format") {
172
run_test("test_pcm8_stereo.wav", AudioStreamWAV::FORMAT_8_BITS, true, WAV_RATE, WAV_COUNT);
173
}
174
175
TEST_CASE("[Audio][AudioStreamWAV] Stereo PCM16 format") {
176
run_test("test_pcm16_stereo.wav", AudioStreamWAV::FORMAT_16_BITS, true, WAV_RATE, WAV_COUNT);
177
}
178
179
TEST_CASE("[Audio][AudioStreamWAV] Alternate mix rate") {
180
run_test("test_pcm16_stereo_38000Hz.wav", AudioStreamWAV::FORMAT_16_BITS, true, 38000, 38000);
181
}
182
183
TEST_CASE("[Audio][AudioStreamWAV] save_to_wav() adds '.wav' file extension automatically") {
184
String save_path = TestUtils::get_temp_path("test_wav_extension");
185
Vector<uint8_t> test_data = gen_pcm8_test(WAV_RATE, WAV_COUNT, false);
186
Ref<AudioStreamWAV> stream = memnew(AudioStreamWAV);
187
stream->set_data(test_data);
188
189
REQUIRE(stream->save_to_wav(save_path) == OK);
190
Error error;
191
Ref<FileAccess> wav_file = FileAccess::open(save_path + ".wav", FileAccess::READ, &error);
192
CHECK(error == OK);
193
}
194
195
TEST_CASE("[Audio][AudioStreamWAV] Default values") {
196
Ref<AudioStreamWAV> stream = memnew(AudioStreamWAV);
197
CHECK(stream->get_format() == AudioStreamWAV::FORMAT_8_BITS);
198
CHECK(stream->get_loop_mode() == AudioStreamWAV::LOOP_DISABLED);
199
CHECK(stream->get_loop_begin() == 0);
200
CHECK(stream->get_loop_end() == 0);
201
CHECK(stream->get_mix_rate() == 44100);
202
CHECK(stream->is_stereo() == false);
203
CHECK(stream->get_length() == 0);
204
CHECK(stream->is_monophonic() == false);
205
CHECK(stream->get_data() == Vector<uint8_t>{});
206
CHECK(stream->get_stream_name() == "");
207
}
208
209
TEST_CASE("[Audio][AudioStreamWAV] Save empty file") {
210
run_test("test_empty.wav", AudioStreamWAV::FORMAT_8_BITS, false, WAV_RATE, 0);
211
}
212
213
TEST_CASE("[Audio][AudioStreamWAV] Saving IMA ADPCM is not supported") {
214
String save_path = TestUtils::get_temp_path("test_adpcm.wav");
215
Ref<AudioStreamWAV> stream = memnew(AudioStreamWAV);
216
stream->set_format(AudioStreamWAV::FORMAT_IMA_ADPCM);
217
ERR_PRINT_OFF;
218
CHECK(stream->save_to_wav(save_path) == ERR_UNAVAILABLE);
219
ERR_PRINT_ON;
220
}
221
222
} // namespace TestAudioStreamWAV
223
224