test_audio_stream_wav.h 9.4 KB

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