test_audio_stream_wav.h 8.6 KB

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