resource_importer_wav.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*************************************************************************/
  2. /* resource_importer_wav.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 RESOURCEIMPORTWAV_H
  31. #define RESOURCEIMPORTWAV_H
  32. #include "core/io/resource_import.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. dst_data.resize(datalen / 2 + 4);
  68. PoolVector<uint8_t>::Write w = dst_data.write();
  69. int i, step_idx = 0, prev = 0;
  70. uint8_t *out = w.ptr();
  71. //int16_t xm_prev=0;
  72. const float *in = p_data.ptr();
  73. /* initial value is zero */
  74. *(out++) = 0;
  75. *(out++) = 0;
  76. /* Table index initial value */
  77. *(out++) = 0;
  78. /* unused */
  79. *(out++) = 0;
  80. for (i = 0; i < datalen; i++) {
  81. int step, diff, vpdiff, mask;
  82. uint8_t nibble;
  83. int16_t xm_sample;
  84. if (i >= datamax)
  85. xm_sample = 0;
  86. else {
  87. xm_sample = CLAMP(in[i] * 32767.0, -32768, 32767);
  88. /*
  89. if (xm_sample==32767 || xm_sample==-32768)
  90. printf("clippy!\n",xm_sample);
  91. */
  92. }
  93. //xm_sample=xm_sample+xm_prev;
  94. //xm_prev=xm_sample;
  95. diff = (int)xm_sample - prev;
  96. nibble = 0;
  97. step = _ima_adpcm_step_table[step_idx];
  98. vpdiff = step >> 3;
  99. if (diff < 0) {
  100. nibble = 8;
  101. diff = -diff;
  102. }
  103. mask = 4;
  104. while (mask) {
  105. if (diff >= step) {
  106. nibble |= mask;
  107. diff -= step;
  108. vpdiff += step;
  109. }
  110. step >>= 1;
  111. mask >>= 1;
  112. };
  113. if (nibble & 8)
  114. prev -= vpdiff;
  115. else
  116. prev += vpdiff;
  117. if (prev > 32767) {
  118. //printf("%i,xms %i, prev %i,diff %i, vpdiff %i, clip up %i\n",i,xm_sample,prev,diff,vpdiff,prev);
  119. prev = 32767;
  120. } else if (prev < -32768) {
  121. //printf("%i,xms %i, prev %i,diff %i, vpdiff %i, clip down %i\n",i,xm_sample,prev,diff,vpdiff,prev);
  122. prev = -32768;
  123. }
  124. step_idx += _ima_adpcm_index_table[nibble];
  125. if (step_idx < 0)
  126. step_idx = 0;
  127. else if (step_idx > 88)
  128. step_idx = 88;
  129. if (i & 1) {
  130. *out |= nibble << 4;
  131. out++;
  132. } else {
  133. *out = nibble;
  134. }
  135. /*dataptr[i]=prev>>8;*/
  136. }
  137. }
  138. 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 = NULL);
  139. ResourceImporterWAV();
  140. };
  141. #endif // RESOURCEIMPORTWAV_H