resource_importer_wav.h 5.8 KB

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