resource_importer_wav.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************/
  2. /* resource_importer_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 RESOURCE_IMPORTER_WAV_H
  31. #define RESOURCE_IMPORTER_WAV_H
  32. #include "core/io/resource_importer.h"
  33. class ResourceImporterWAV : public ResourceImporter {
  34. GDCLASS(ResourceImporterWAV, ResourceImporter);
  35. public:
  36. virtual String get_importer_name() const override;
  37. virtual String get_visible_name() const override;
  38. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  39. virtual String get_save_extension() const override;
  40. virtual String get_resource_type() const override;
  41. virtual int get_preset_count() const override;
  42. virtual String get_preset_name(int p_idx) const override;
  43. virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const override;
  44. virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const override;
  45. static void _compress_ima_adpcm(const Vector<float> &p_data, Vector<uint8_t> &dst_data) {
  46. static const int16_t _ima_adpcm_step_table[89] = {
  47. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  48. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  49. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  50. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  51. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  52. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  53. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  54. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  55. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  56. };
  57. static const int8_t _ima_adpcm_index_table[16] = {
  58. -1, -1, -1, -1, 2, 4, 6, 8,
  59. -1, -1, -1, -1, 2, 4, 6, 8
  60. };
  61. int datalen = p_data.size();
  62. int datamax = datalen;
  63. if (datalen & 1) {
  64. datalen++;
  65. }
  66. dst_data.resize(datalen / 2 + 4);
  67. uint8_t *w = dst_data.ptrw();
  68. int i, step_idx = 0, prev = 0;
  69. uint8_t *out = w;
  70. const float *in = p_data.ptr();
  71. // Initial value is zero.
  72. *(out++) = 0;
  73. *(out++) = 0;
  74. // Table index initial value.
  75. *(out++) = 0;
  76. // Unused.
  77. *(out++) = 0;
  78. for (i = 0; i < datalen; i++) {
  79. int step, diff, vpdiff, mask;
  80. uint8_t nibble;
  81. int16_t xm_sample;
  82. if (i >= datamax) {
  83. xm_sample = 0;
  84. } else {
  85. xm_sample = CLAMP(in[i] * 32767.0, -32768, 32767);
  86. }
  87. diff = (int)xm_sample - prev;
  88. nibble = 0;
  89. step = _ima_adpcm_step_table[step_idx];
  90. vpdiff = step >> 3;
  91. if (diff < 0) {
  92. nibble = 8;
  93. diff = -diff;
  94. }
  95. mask = 4;
  96. while (mask) {
  97. if (diff >= step) {
  98. nibble |= mask;
  99. diff -= step;
  100. vpdiff += step;
  101. }
  102. step >>= 1;
  103. mask >>= 1;
  104. }
  105. if (nibble & 8) {
  106. prev -= vpdiff;
  107. } else {
  108. prev += vpdiff;
  109. }
  110. prev = CLAMP(prev, -32768, 32767);
  111. step_idx += _ima_adpcm_index_table[nibble];
  112. step_idx = CLAMP(step_idx, 0, 88);
  113. if (i & 1) {
  114. *out |= nibble << 4;
  115. out++;
  116. } else {
  117. *out = nibble;
  118. }
  119. }
  120. }
  121. virtual Error import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) override;
  122. ResourceImporterWAV();
  123. };
  124. #endif // RESOURCE_IMPORTER_WAV_H