Debian Codegraph @

tests/scene/test_audio_stream_wav.h

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