cp_loader_it_instruments.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* cp_loader_it_instruments.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "cp_loader_it.h"
  31. enum EnvFlags {
  32. ENV_ON = 1,
  33. ENV_LOOP = 2,
  34. ENV_SUSLOOP = 4,
  35. ENV_CARRY = 8,
  36. ENV_FILTER = 128
  37. };
  38. void CPLoader_IT::load_envelope(CPEnvelope *p_envelope, bool *p_has_filter_flag) {
  39. uint8_t flags = file->get_byte();
  40. uint8_t points = file->get_byte();
  41. uint8_t begin = file->get_byte();
  42. uint8_t end = file->get_byte();
  43. uint8_t susbegin = file->get_byte();
  44. uint8_t susend = file->get_byte();
  45. p_envelope->reset();
  46. for (int i = 0; i < 25; i++) {
  47. uint8_t height = file->get_byte();
  48. int8_t &signed_height = (int8_t &)height;
  49. uint16_t tick = file->get_word();
  50. if (i >= points)
  51. continue;
  52. p_envelope->add_position(tick, signed_height);
  53. }
  54. p_envelope->set_enabled(flags & ENV_ON);
  55. p_envelope->set_carry_enabled(flags & ENV_CARRY);
  56. p_envelope->set_loop_enabled(flags & ENV_LOOP);
  57. p_envelope->set_loop_begin(begin);
  58. p_envelope->set_loop_end(end);
  59. p_envelope->set_sustain_loop_enabled(flags & ENV_SUSLOOP);
  60. p_envelope->set_sustain_loop_begin(susbegin);
  61. p_envelope->set_sustain_loop_end(susend);
  62. if (p_has_filter_flag)
  63. *p_has_filter_flag = flags & ENV_FILTER;
  64. file->get_byte(); //zerobyte
  65. //fill with stuff if the envelope hass less than 2 points
  66. while (p_envelope->get_node_count() < 2) {
  67. p_envelope->add_position(30 * p_envelope->get_node_count(), p_envelope->get_min() == 0 ? 64 : 0, false);
  68. }
  69. }
  70. CPLoader::Error CPLoader_IT::load_instrument(CPInstrument *p_instrument, int *p_samples) {
  71. char aux_header[4];
  72. file->get_byte_array((uint8_t *)aux_header, 4);
  73. if (aux_header[0] != 'I' ||
  74. aux_header[1] != 'M' ||
  75. aux_header[2] != 'P' ||
  76. aux_header[3] != 'I') {
  77. CP_PRINTERR("IT CPLoader CPInstrument: Failed Identifier");
  78. return FILE_UNRECOGNIZED;
  79. }
  80. // Ignore deprecated 8.3 filename field
  81. for (int i = 0; i < 12; i++)
  82. file->get_byte();
  83. //Ignore zerobyte
  84. file->get_byte(); /* (byte) CPInstrument type (always 0) */
  85. switch (file->get_byte()) { /* New CPNote Action [0,1,2,3] */
  86. case 0: p_instrument->set_NNA_type(CPInstrument::NNA_NOTE_CUT); break;
  87. case 1: p_instrument->set_NNA_type(CPInstrument::NNA_NOTE_CONTINUE); break;
  88. case 2: p_instrument->set_NNA_type(CPInstrument::NNA_NOTE_OFF); break;
  89. case 3: p_instrument->set_NNA_type(CPInstrument::NNA_NOTE_FADE); break;
  90. };
  91. switch (file->get_byte()) { // Duplicate Check Type
  92. case 0: p_instrument->set_DC_type(CPInstrument::DCT_DISABLED); break;
  93. case 1: p_instrument->set_DC_type(CPInstrument::DCT_NOTE); break;
  94. case 2: p_instrument->set_DC_type(CPInstrument::DCT_SAMPLE); break;
  95. case 3: p_instrument->set_DC_type(CPInstrument::DCT_INSTRUMENT); break;
  96. }
  97. switch (file->get_byte()) { //Duplicate Check Action
  98. case 0: p_instrument->set_DC_action(CPInstrument::DCA_NOTE_CUT); break;
  99. case 1: p_instrument->set_DC_action(CPInstrument::DCA_NOTE_OFF); break;
  100. case 2: p_instrument->set_DC_action(CPInstrument::DCA_NOTE_FADE); break;
  101. }
  102. int fade = file->get_word();
  103. //intf("AFADE: %i\n",fade);
  104. if (fade > CPInstrument::MAX_FADEOUT) //needs to be clipped because of horrible modplug doings
  105. fade = CPInstrument::MAX_FADEOUT;
  106. p_instrument->set_volume_fadeout(fade);
  107. p_instrument->set_pan_pitch_separation(file->get_byte());
  108. p_instrument->set_pan_pitch_center(file->get_byte());
  109. p_instrument->set_volume_global_amount(file->get_byte());
  110. uint8_t pan = file->get_byte();
  111. p_instrument->set_pan_default_amount(pan & 0x7F);
  112. p_instrument->set_pan_default_enabled(!(pan & 0x80));
  113. p_instrument->set_volume_random_variation(file->get_byte());
  114. p_instrument->set_pan_random_variation(file->get_byte());
  115. file->get_word(); //empty (version)
  116. uint8_t samples = file->get_byte();
  117. if (p_samples)
  118. *p_samples = samples;
  119. file->get_byte(); //empty
  120. char aux_name[26];
  121. file->get_byte_array((uint8_t *)aux_name, 26);
  122. p_instrument->set_name(aux_name);
  123. uint8_t cutoff = file->get_byte();
  124. p_instrument->set_filter_default_cutoff(cutoff & 0x7F);
  125. p_instrument->set_filter_use_default_cutoff(cutoff & 0x80);
  126. uint8_t resonance = file->get_byte();
  127. p_instrument->set_filter_default_resonance(resonance & 0x7F);
  128. p_instrument->set_filter_use_default_resonance(resonance & 0x80);
  129. file->get_dword(); //MIDI, IGNORED!
  130. /* CPNote -> CPSample table */
  131. for (uint8_t i = 0; i < CPNote::NOTES; i++) {
  132. uint8_t note = file->get_byte();
  133. if (note >= CPNote::NOTES)
  134. note = 0;
  135. p_instrument->set_note_number(i, note);
  136. uint8_t samp = file->get_byte();
  137. if (samp == 0 || samp > 99)
  138. samp = CPNote::EMPTY;
  139. else
  140. samp--;
  141. p_instrument->set_sample_number(i, samp);
  142. }
  143. load_envelope(p_instrument->get_volume_envelope());
  144. load_envelope(p_instrument->get_pan_envelope());
  145. bool use_as_filter;
  146. load_envelope(p_instrument->get_pitch_filter_envelope(), &use_as_filter);
  147. p_instrument->set_pitch_use_as_filter(use_as_filter);
  148. return FILE_OK;
  149. }
  150. CPLoader::Error CPLoader_IT::load_instruments() {
  151. for (int i = 0; i < header.insnum; i++) {
  152. file->seek(0xC0 + header.ordnum + i * 4);
  153. uint32_t final_location = file->get_dword();
  154. file->seek(final_location);
  155. Error err = load_instrument(song->get_instrument(i));
  156. if (err)
  157. return err;
  158. }
  159. return FILE_OK;
  160. if (file->eof_reached() || file->get_error())
  161. return FILE_CORRUPTED;
  162. }