editor_sample_import_plugin.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*************************************************************************/
  2. /* editor_sample_import_plugin.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 "editor_sample_import_plugin.h"
  31. #include "editor/editor_dir_dialog.h"
  32. #include "editor/editor_file_dialog.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/property_editor.h"
  36. #include "io/marshalls.h"
  37. #include "io/resource_saver.h"
  38. #include "os/file_access.h"
  39. #include "scene/resources/sample.h"
  40. class _EditorSampleImportOptions : public Object {
  41. OBJ_TYPE(_EditorSampleImportOptions, Object);
  42. public:
  43. enum CompressMode {
  44. COMPRESS_MODE_DISABLED,
  45. COMPRESS_MODE_RAM,
  46. COMPRESS_MODE_DISK
  47. };
  48. enum CompressBitrate {
  49. COMPRESS_64,
  50. COMPRESS_96,
  51. COMPRESS_128,
  52. COMPRESS_192
  53. };
  54. bool force_8_bit;
  55. bool force_mono;
  56. bool force_rate;
  57. float force_rate_hz;
  58. bool edit_trim;
  59. bool edit_normalize;
  60. bool edit_loop;
  61. CompressMode compress_mode;
  62. CompressBitrate compress_bitrate;
  63. bool _set(const StringName &p_name, const Variant &p_value) {
  64. String n = p_name;
  65. if (n == "force/8_bit")
  66. force_8_bit = p_value;
  67. else if (n == "force/mono")
  68. force_mono = p_value;
  69. else if (n == "force/max_rate")
  70. force_rate = p_value;
  71. else if (n == "force/max_rate_hz")
  72. force_rate_hz = p_value;
  73. else if (n == "edit/trim")
  74. edit_trim = p_value;
  75. else if (n == "edit/normalize")
  76. edit_normalize = p_value;
  77. else if (n == "edit/loop")
  78. edit_loop = p_value;
  79. else if (n == "compress/mode")
  80. compress_mode = CompressMode(int(p_value));
  81. else if (n == "compress/bitrate")
  82. compress_bitrate = CompressBitrate(int(p_value));
  83. else
  84. return false;
  85. return true;
  86. }
  87. bool _get(const StringName &p_name, Variant &r_ret) const {
  88. String n = p_name;
  89. if (n == "force/8_bit")
  90. r_ret = force_8_bit;
  91. else if (n == "force/mono")
  92. r_ret = force_mono;
  93. else if (n == "force/max_rate")
  94. r_ret = force_rate;
  95. else if (n == "force/max_rate_hz")
  96. r_ret = force_rate_hz;
  97. else if (n == "edit/trim")
  98. r_ret = edit_trim;
  99. else if (n == "edit/normalize")
  100. r_ret = edit_normalize;
  101. else if (n == "edit/loop")
  102. r_ret = edit_loop;
  103. else if (n == "compress/mode")
  104. r_ret = compress_mode;
  105. else if (n == "compress/bitrate")
  106. r_ret = compress_bitrate;
  107. else
  108. return false;
  109. return true;
  110. }
  111. void _get_property_list(List<PropertyInfo> *p_list) const {
  112. p_list->push_back(PropertyInfo(Variant::BOOL, "force/8_bit"));
  113. p_list->push_back(PropertyInfo(Variant::BOOL, "force/mono"));
  114. p_list->push_back(PropertyInfo(Variant::BOOL, "force/max_rate"));
  115. p_list->push_back(PropertyInfo(Variant::REAL, "force/max_rate_hz", PROPERTY_HINT_EXP_RANGE, "11025,192000,1"));
  116. p_list->push_back(PropertyInfo(Variant::BOOL, "edit/trim"));
  117. p_list->push_back(PropertyInfo(Variant::BOOL, "edit/normalize"));
  118. p_list->push_back(PropertyInfo(Variant::BOOL, "edit/loop"));
  119. p_list->push_back(PropertyInfo(Variant::INT, "compress/mode", PROPERTY_HINT_ENUM, "Disabled,RAM (Ima-ADPCM)"));
  120. //p_list->push_back(PropertyInfo(Variant::INT,"compress/bitrate",PROPERTY_HINT_ENUM,"64,96,128,192"));
  121. }
  122. static void _bind_methods() {
  123. ADD_SIGNAL(MethodInfo("changed"));
  124. }
  125. _EditorSampleImportOptions() {
  126. force_8_bit = false;
  127. force_mono = false;
  128. force_rate = true;
  129. force_rate_hz = 44100;
  130. edit_trim = true;
  131. edit_normalize = true;
  132. edit_loop = false;
  133. compress_mode = COMPRESS_MODE_RAM;
  134. compress_bitrate = COMPRESS_128;
  135. }
  136. };
  137. class EditorSampleImportDialog : public ConfirmationDialog {
  138. OBJ_TYPE(EditorSampleImportDialog, ConfirmationDialog);
  139. EditorSampleImportPlugin *plugin;
  140. LineEdit *import_path;
  141. LineEdit *save_path;
  142. EditorFileDialog *file_select;
  143. EditorDirDialog *save_select;
  144. ConfirmationDialog *error_dialog;
  145. PropertyEditor *option_editor;
  146. _EditorSampleImportOptions *options;
  147. public:
  148. void _choose_files(const Vector<String> &p_path) {
  149. String files;
  150. for (int i = 0; i < p_path.size(); i++) {
  151. if (i > 0)
  152. files += ",";
  153. files += p_path[i];
  154. }
  155. /*
  156. if (p_path.size()) {
  157. String srctex=p_path[0];
  158. String ipath = EditorImportDB::get_singleton()->find_source_path(srctex);
  159. if (ipath!="")
  160. save_path->set_text(ipath.get_base_dir());
  161. }*/
  162. import_path->set_text(files);
  163. }
  164. void _choose_save_dir(const String &p_path) {
  165. save_path->set_text(p_path);
  166. }
  167. void _browse() {
  168. file_select->popup_centered_ratio();
  169. }
  170. void _browse_target() {
  171. save_select->popup_centered_ratio();
  172. }
  173. void popup_import(const String &p_path) {
  174. popup_centered(Size2(400, 400) * EDSCALE);
  175. if (p_path != "") {
  176. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(p_path);
  177. ERR_FAIL_COND(!rimd.is_valid());
  178. save_path->set_text(p_path.get_base_dir());
  179. List<String> opts;
  180. rimd->get_options(&opts);
  181. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  182. options->_set(E->get(), rimd->get_option(E->get()));
  183. }
  184. String src = "";
  185. for (int i = 0; i < rimd->get_source_count(); i++) {
  186. if (i > 0)
  187. src += ",";
  188. src += EditorImportPlugin::expand_source_path(rimd->get_source_path(i));
  189. }
  190. import_path->set_text(src);
  191. }
  192. }
  193. void _import() {
  194. Vector<String> samples = import_path->get_text().split(",");
  195. if (samples.size() == 0) {
  196. error_dialog->set_text(TTR("No samples to import!"));
  197. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  198. }
  199. if (save_path->get_text().strip_edges() == "") {
  200. error_dialog->set_text(TTR("Target path is empty."));
  201. error_dialog->popup_centered_minsize();
  202. return;
  203. }
  204. if (!save_path->get_text().begins_with("res://")) {
  205. error_dialog->set_text(TTR("Target path must be a complete resource path."));
  206. error_dialog->popup_centered_minsize();
  207. return;
  208. }
  209. if (!DirAccess::exists(save_path->get_text())) {
  210. error_dialog->set_text(TTR("Target path must exist."));
  211. error_dialog->popup_centered_minsize();
  212. return;
  213. }
  214. for (int i = 0; i < samples.size(); i++) {
  215. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  216. List<PropertyInfo> pl;
  217. options->_get_property_list(&pl);
  218. for (List<PropertyInfo>::Element *E = pl.front(); E; E = E->next()) {
  219. Variant v;
  220. String opt = E->get().name;
  221. options->_get(opt, v);
  222. imd->set_option(opt, v);
  223. }
  224. imd->add_source(EditorImportPlugin::validate_source_path(samples[i]));
  225. String dst = save_path->get_text();
  226. if (dst == "") {
  227. error_dialog->set_text(TTR("Save path is empty!"));
  228. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  229. }
  230. dst = dst.plus_file(samples[i].get_file().basename() + ".smp");
  231. Error err = plugin->import(dst, imd);
  232. }
  233. hide();
  234. }
  235. void _notification(int p_what) {
  236. if (p_what == NOTIFICATION_ENTER_TREE) {
  237. option_editor->edit(options);
  238. }
  239. }
  240. static void _bind_methods() {
  241. ObjectTypeDB::bind_method("_choose_files", &EditorSampleImportDialog::_choose_files);
  242. ObjectTypeDB::bind_method("_choose_save_dir", &EditorSampleImportDialog::_choose_save_dir);
  243. ObjectTypeDB::bind_method("_import", &EditorSampleImportDialog::_import);
  244. ObjectTypeDB::bind_method("_browse", &EditorSampleImportDialog::_browse);
  245. ObjectTypeDB::bind_method("_browse_target", &EditorSampleImportDialog::_browse_target);
  246. // ADD_SIGNAL( MethodInfo("imported",PropertyInfo(Variant::OBJECT,"scene")) );
  247. }
  248. EditorSampleImportDialog(EditorSampleImportPlugin *p_plugin) {
  249. plugin = p_plugin;
  250. set_title(TTR("Import Audio Samples"));
  251. VBoxContainer *vbc = memnew(VBoxContainer);
  252. add_child(vbc);
  253. set_child_rect(vbc);
  254. HBoxContainer *hbc = memnew(HBoxContainer);
  255. vbc->add_margin_child(TTR("Source Sample(s):"), hbc);
  256. import_path = memnew(LineEdit);
  257. import_path->set_h_size_flags(SIZE_EXPAND_FILL);
  258. hbc->add_child(import_path);
  259. Button *import_choose = memnew(Button);
  260. import_choose->set_text(" .. ");
  261. hbc->add_child(import_choose);
  262. import_choose->connect("pressed", this, "_browse");
  263. hbc = memnew(HBoxContainer);
  264. vbc->add_margin_child(TTR("Target Path:"), hbc);
  265. save_path = memnew(LineEdit);
  266. save_path->set_h_size_flags(SIZE_EXPAND_FILL);
  267. hbc->add_child(save_path);
  268. Button *save_choose = memnew(Button);
  269. save_choose->set_text(" .. ");
  270. hbc->add_child(save_choose);
  271. save_choose->connect("pressed", this, "_browse_target");
  272. file_select = memnew(EditorFileDialog);
  273. file_select->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  274. add_child(file_select);
  275. file_select->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  276. file_select->connect("files_selected", this, "_choose_files");
  277. file_select->add_filter("*.wav ; MS Waveform");
  278. save_select = memnew(EditorDirDialog);
  279. add_child(save_select);
  280. // save_select->set_mode(EditorFileDialog::MODE_OPEN_DIR);
  281. save_select->connect("dir_selected", this, "_choose_save_dir");
  282. get_ok()->connect("pressed", this, "_import");
  283. get_ok()->set_text(TTR("Import"));
  284. error_dialog = memnew(ConfirmationDialog);
  285. add_child(error_dialog);
  286. error_dialog->get_ok()->set_text(TTR("Accept"));
  287. // error_dialog->get_cancel()->hide();
  288. set_hide_on_ok(false);
  289. options = memnew(_EditorSampleImportOptions);
  290. option_editor = memnew(PropertyEditor);
  291. option_editor->hide_top_label();
  292. vbc->add_margin_child(TTR("Options:"), option_editor, true);
  293. }
  294. ~EditorSampleImportDialog() {
  295. memdelete(options);
  296. }
  297. };
  298. String EditorSampleImportPlugin::get_name() const {
  299. return "sample";
  300. }
  301. String EditorSampleImportPlugin::get_visible_name() const {
  302. return TTR("Audio Sample");
  303. }
  304. void EditorSampleImportPlugin::import_dialog(const String &p_from) {
  305. dialog->popup_import(p_from);
  306. }
  307. Error EditorSampleImportPlugin::import(const String &p_path, const Ref<ResourceImportMetadata> &p_from) {
  308. ERR_FAIL_COND_V(p_from->get_source_count() != 1, ERR_INVALID_PARAMETER);
  309. Ref<ResourceImportMetadata> from = p_from;
  310. String src_path = EditorImportPlugin::expand_source_path(from->get_source_path(0));
  311. Ref<Sample> smp = ResourceLoader::load(src_path);
  312. ERR_FAIL_COND_V(smp.is_null(), ERR_CANT_OPEN);
  313. float rate = smp->get_mix_rate();
  314. bool is16 = smp->get_format() == Sample::FORMAT_PCM16;
  315. int chans = smp->is_stereo() ? 2 : 1;
  316. int len = smp->get_length();
  317. Sample::LoopFormat loop = smp->get_loop_format();
  318. int loop_beg = smp->get_loop_begin();
  319. int loop_end = smp->get_loop_end();
  320. print_line("Input Sample: ");
  321. print_line("\tlen: " + itos(len));
  322. print_line("\tchans: " + itos(chans));
  323. print_line("\t16bits: " + itos(is16));
  324. print_line("\trate: " + itos(rate));
  325. print_line("\tloop: " + itos(loop));
  326. print_line("\tloop begin: " + itos(loop_beg));
  327. print_line("\tloop end: " + itos(loop_end));
  328. Vector<float> data;
  329. data.resize(len * chans);
  330. {
  331. DVector<uint8_t> src_data = smp->get_data();
  332. DVector<uint8_t>::Read sr = src_data.read();
  333. for (int i = 0; i < len * chans; i++) {
  334. float s = 0;
  335. if (is16) {
  336. int16_t i16 = decode_uint16(&sr[i * 2]);
  337. s = i16 / 32767.0;
  338. } else {
  339. int8_t i8 = sr[i];
  340. s = i8 / 127.0;
  341. }
  342. data[i] = s;
  343. }
  344. }
  345. //apply frequency limit
  346. bool limit_rate = from->get_option("force/max_rate");
  347. int limit_rate_hz = from->get_option("force/max_rate_hz");
  348. if (limit_rate && rate > limit_rate_hz) {
  349. //resampleeee!!!
  350. int new_data_len = len * limit_rate_hz / rate;
  351. Vector<float> new_data;
  352. new_data.resize(new_data_len * chans);
  353. for (int c = 0; c < chans; c++) {
  354. for (int i = 0; i < new_data_len; i++) {
  355. //simple cubic interpolation should be enough.
  356. float pos = float(i) * len / new_data_len;
  357. float mu = pos - Math::floor(pos);
  358. int ipos = int(Math::floor(pos));
  359. float y0 = data[MAX(0, ipos - 1) * chans + c];
  360. float y1 = data[ipos * chans + c];
  361. float y2 = data[MIN(len - 1, ipos + 1) * chans + c];
  362. float y3 = data[MIN(len - 1, ipos + 2) * chans + c];
  363. float mu2 = mu * mu;
  364. float a0 = y3 - y2 - y0 + y1;
  365. float a1 = y0 - y1 - a0;
  366. float a2 = y2 - y0;
  367. float a3 = y1;
  368. float res = (a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3);
  369. new_data[i * chans + c] = res;
  370. }
  371. }
  372. if (loop) {
  373. loop_beg = loop_beg * new_data_len / len;
  374. loop_end = loop_end * new_data_len / len;
  375. }
  376. data = new_data;
  377. rate = limit_rate_hz;
  378. len = new_data_len;
  379. }
  380. bool normalize = from->get_option("edit/normalize");
  381. if (normalize) {
  382. float max = 0;
  383. for (int i = 0; i < data.size(); i++) {
  384. float amp = Math::abs(data[i]);
  385. if (amp > max)
  386. max = amp;
  387. }
  388. if (max > 0) {
  389. float mult = 1.0 / max;
  390. for (int i = 0; i < data.size(); i++) {
  391. data[i] *= mult;
  392. }
  393. }
  394. }
  395. bool trim = from->get_option("edit/trim");
  396. if (trim && !loop) {
  397. int first = 0;
  398. int last = (len * chans) - 1;
  399. bool found = false;
  400. float limit = Math::db2linear(-30);
  401. for (int i = 0; i < data.size(); i++) {
  402. float amp = Math::abs(data[i]);
  403. if (!found && amp > limit) {
  404. first = i;
  405. found = true;
  406. }
  407. if (found && amp > limit) {
  408. last = i;
  409. }
  410. }
  411. first /= chans;
  412. last /= chans;
  413. if (first < last) {
  414. Vector<float> new_data;
  415. new_data.resize((last - first + 1) * chans);
  416. for (int i = first * chans; i <= last * chans; i++) {
  417. new_data[i - first * chans] = data[i];
  418. }
  419. data = new_data;
  420. len = data.size() / chans;
  421. }
  422. }
  423. bool make_loop = from->get_option("edit/loop");
  424. if (make_loop && !loop) {
  425. loop = Sample::LOOP_FORWARD;
  426. loop_beg = 0;
  427. loop_end = len;
  428. }
  429. int compression = from->get_option("compress/mode");
  430. bool force_mono = from->get_option("force/mono");
  431. if (force_mono && chans == 2) {
  432. Vector<float> new_data;
  433. new_data.resize(data.size() / 2);
  434. for (int i = 0; i < len; i++) {
  435. new_data[i] = (data[i * 2 + 0] + data[i * 2 + 1]) / 2.0;
  436. }
  437. data = new_data;
  438. chans = 1;
  439. }
  440. bool force_8_bit = from->get_option("force/8_bit");
  441. if (force_8_bit) {
  442. is16 = false;
  443. }
  444. DVector<uint8_t> dst_data;
  445. Sample::Format dst_format;
  446. if (compression == _EditorSampleImportOptions::COMPRESS_MODE_RAM) {
  447. dst_format = Sample::FORMAT_IMA_ADPCM;
  448. if (chans == 1) {
  449. _compress_ima_adpcm(data, dst_data);
  450. } else {
  451. print_line("INTERLEAAVE!");
  452. //byte interleave
  453. Vector<float> left;
  454. Vector<float> right;
  455. int tlen = data.size() / 2;
  456. left.resize(tlen);
  457. right.resize(tlen);
  458. for (int i = 0; i < tlen; i++) {
  459. left[i] = data[i * 2 + 0];
  460. right[i] = data[i * 2 + 1];
  461. }
  462. DVector<uint8_t> bleft;
  463. DVector<uint8_t> bright;
  464. _compress_ima_adpcm(left, bleft);
  465. _compress_ima_adpcm(right, bright);
  466. int dl = bleft.size();
  467. dst_data.resize(dl * 2);
  468. DVector<uint8_t>::Write w = dst_data.write();
  469. DVector<uint8_t>::Read rl = bleft.read();
  470. DVector<uint8_t>::Read rr = bright.read();
  471. for (int i = 0; i < dl; i++) {
  472. w[i * 2 + 0] = rl[i];
  473. w[i * 2 + 1] = rr[i];
  474. }
  475. }
  476. // print_line("compressing ima-adpcm, resulting buffersize is "+itos(dst_data.size())+" from "+itos(data.size()));
  477. } else {
  478. dst_format = is16 ? Sample::FORMAT_PCM16 : Sample::FORMAT_PCM8;
  479. dst_data.resize(data.size() * (is16 ? 2 : 1));
  480. {
  481. DVector<uint8_t>::Write w = dst_data.write();
  482. int ds = data.size();
  483. for (int i = 0; i < ds; i++) {
  484. if (is16) {
  485. int16_t v = CLAMP(data[i] * 32767, -32768, 32767);
  486. encode_uint16(v, &w[i * 2]);
  487. } else {
  488. int8_t v = CLAMP(data[i] * 127, -128, 127);
  489. w[i] = v;
  490. }
  491. }
  492. }
  493. }
  494. Ref<Sample> target;
  495. if (ResourceCache::has(p_path)) {
  496. target = Ref<Sample>(ResourceCache::get(p_path)->cast_to<Sample>());
  497. } else {
  498. target = smp;
  499. }
  500. target->create(dst_format, chans == 2 ? true : false, len);
  501. target->set_data(dst_data);
  502. target->set_mix_rate(rate);
  503. target->set_loop_format(loop);
  504. target->set_loop_begin(loop_beg);
  505. target->set_loop_end(loop_end);
  506. from->set_source_md5(0, FileAccess::get_md5(src_path));
  507. from->set_editor(get_name());
  508. target->set_import_metadata(from);
  509. Error err = ResourceSaver::save(p_path, smp);
  510. return err;
  511. }
  512. void EditorSampleImportPlugin::_compress_ima_adpcm(const Vector<float> &p_data, DVector<uint8_t> &dst_data) {
  513. /*p_sample_data->data = (void*)malloc(len);
  514. xm_s8 *dataptr=(xm_s8*)p_sample_data->data;*/
  515. static const int16_t _ima_adpcm_step_table[89] = {
  516. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  517. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  518. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  519. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  520. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  521. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  522. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  523. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  524. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  525. };
  526. static const int8_t _ima_adpcm_index_table[16] = {
  527. -1, -1, -1, -1, 2, 4, 6, 8,
  528. -1, -1, -1, -1, 2, 4, 6, 8
  529. };
  530. int datalen = p_data.size();
  531. int datamax = datalen;
  532. if (datalen & 1)
  533. datalen++;
  534. dst_data.resize(datalen / 2 + 4);
  535. DVector<uint8_t>::Write w = dst_data.write();
  536. int i, step_idx = 0, prev = 0;
  537. uint8_t *out = w.ptr();
  538. //int16_t xm_prev=0;
  539. const float *in = p_data.ptr();
  540. /* initial value is zero */
  541. *(out++) = 0;
  542. *(out++) = 0;
  543. /* Table index initial value */
  544. *(out++) = 0;
  545. /* unused */
  546. *(out++) = 0;
  547. for (i = 0; i < datalen; i++) {
  548. int step, diff, vpdiff, mask;
  549. uint8_t nibble;
  550. int16_t xm_sample;
  551. if (i >= datamax)
  552. xm_sample = 0;
  553. else {
  554. xm_sample = CLAMP(in[i] * 32767.0, -32768, 32767);
  555. //if (xm_sample==32767 || xm_sample==-32768)
  556. // printf("clippy!\n",xm_sample);
  557. }
  558. // xm_sample=xm_sample+xm_prev;
  559. // xm_prev=xm_sample;
  560. diff = (int)xm_sample - prev;
  561. nibble = 0;
  562. step = _ima_adpcm_step_table[step_idx];
  563. vpdiff = step >> 3;
  564. if (diff < 0) {
  565. nibble = 8;
  566. diff = -diff;
  567. }
  568. mask = 4;
  569. while (mask) {
  570. if (diff >= step) {
  571. nibble |= mask;
  572. diff -= step;
  573. vpdiff += step;
  574. }
  575. step >>= 1;
  576. mask >>= 1;
  577. };
  578. if (nibble & 8)
  579. prev -= vpdiff;
  580. else
  581. prev += vpdiff;
  582. if (prev > 32767) {
  583. //printf("%i,xms %i, prev %i,diff %i, vpdiff %i, clip up %i\n",i,xm_sample,prev,diff,vpdiff,prev);
  584. prev = 32767;
  585. } else if (prev < -32768) {
  586. //printf("%i,xms %i, prev %i,diff %i, vpdiff %i, clip down %i\n",i,xm_sample,prev,diff,vpdiff,prev);
  587. prev = -32768;
  588. }
  589. step_idx += _ima_adpcm_index_table[nibble];
  590. if (step_idx < 0)
  591. step_idx = 0;
  592. else if (step_idx > 88)
  593. step_idx = 88;
  594. if (i & 1) {
  595. *out |= nibble << 4;
  596. out++;
  597. } else {
  598. *out = nibble;
  599. }
  600. /*dataptr[i]=prev>>8;*/
  601. }
  602. }
  603. EditorSampleImportPlugin *EditorSampleImportPlugin::singleton = NULL;
  604. void EditorSampleImportPlugin::import_from_drop(const Vector<String> &p_drop, const String &p_dest_path) {
  605. Vector<String> files;
  606. for (int i = 0; i < p_drop.size(); i++) {
  607. String ext = p_drop[i].extension().to_lower();
  608. if (ext == "wav") {
  609. files.push_back(p_drop[i]);
  610. }
  611. }
  612. if (files.size()) {
  613. import_dialog();
  614. dialog->_choose_files(files);
  615. dialog->_choose_save_dir(p_dest_path);
  616. }
  617. }
  618. void EditorSampleImportPlugin::reimport_multiple_files(const Vector<String> &p_list) {
  619. if (p_list.size() == 0)
  620. return;
  621. Vector<String> sources;
  622. for (int i = 0; i < p_list.size(); i++) {
  623. int idx;
  624. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->find_file(p_list[i], &idx);
  625. if (efsd) {
  626. for (int j = 0; j < efsd->get_source_count(idx); j++) {
  627. String file = expand_source_path(efsd->get_source_file(idx, j));
  628. if (sources.find(file) == -1) {
  629. sources.push_back(file);
  630. }
  631. }
  632. }
  633. }
  634. if (sources.size()) {
  635. dialog->popup_import(p_list[0]);
  636. dialog->_choose_files(sources);
  637. dialog->_choose_save_dir(p_list[0].get_base_dir());
  638. }
  639. }
  640. bool EditorSampleImportPlugin::can_reimport_multiple_files() const {
  641. return true;
  642. }
  643. EditorSampleImportPlugin::EditorSampleImportPlugin(EditorNode *p_editor) {
  644. singleton = this;
  645. dialog = memnew(EditorSampleImportDialog(this));
  646. p_editor->get_gui_base()->add_child(dialog);
  647. }
  648. Vector<uint8_t> EditorSampleExportPlugin::custom_export(String &p_path, const Ref<EditorExportPlatform> &p_platform) {
  649. if (EditorImportExport::get_singleton()->sample_get_action() == EditorImportExport::SAMPLE_ACTION_NONE || p_path.extension().to_lower() != "wav") {
  650. return Vector<uint8_t>();
  651. }
  652. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  653. imd->add_source(EditorImportPlugin::validate_source_path(p_path));
  654. imd->set_option("force/8_bit", false);
  655. imd->set_option("force/mono", false);
  656. imd->set_option("force/max_rate", true);
  657. imd->set_option("force/max_rate_hz", EditorImportExport::get_singleton()->sample_get_max_hz());
  658. imd->set_option("edit/trim", EditorImportExport::get_singleton()->sample_get_trim());
  659. imd->set_option("edit/normalize", false);
  660. imd->set_option("edit/loop", false);
  661. imd->set_option("compress/mode", 1);
  662. String savepath = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/smpconv.smp");
  663. Error err = EditorSampleImportPlugin::singleton->import(savepath, imd);
  664. ERR_FAIL_COND_V(err != OK, Vector<uint8_t>());
  665. p_path = p_path.basename() + ".converted.smp";
  666. return FileAccess::get_file_as_array(savepath);
  667. }
  668. EditorSampleExportPlugin::EditorSampleExportPlugin() {
  669. }