EdisonScript167Tests.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // --- Edison's audio scripts (core/EdisonScript.h) - round 167c (REA_rodada167c.md §2)
  2. #include "core/EdisonScript.h"
  3. namespace edscript167
  4. {
  5. lynx::audioedit::Document ramp (int frames, int channels = 2, double rate = 44100.0)
  6. {
  7. lynx::audioedit::Document d;
  8. d.audio = lynx::audioedit::silence (channels, rate, frames);
  9. for (int c = 0; c < channels; ++c)
  10. for (int i = 0; i < frames; ++i)
  11. d.audio.channels[(std::size_t) c][(std::size_t) i] = (float) (i % 100) / 100.0f * (c == 0 ? 1.0f : -1.0f);
  12. return d;
  13. }
  14. // runs a script to its end, answering every form with its own defaults changed by `set` (name → value)
  15. lynx::audioedit::script::Outcome runAll (const std::string& src, lynx::audioedit::Document& doc, lynx::audioedit::Range sel,
  16. const std::map<std::string, double>& set = {}, int* forms = nullptr,
  17. const lynx::audioedit::script::Host& host = {})
  18. {
  19. using namespace lynx::audioedit::script;
  20. std::vector<Form> answers;
  21. for (int round = 0; round < 8; ++round)
  22. {
  23. auto copy = doc;
  24. auto out = run (src, copy, sel, answers, host);
  25. if (out.status != Outcome::Status::needsForm)
  26. {
  27. if (out.status == Outcome::Status::done)
  28. doc = std::move (copy);
  29. if (forms != nullptr)
  30. *forms = (int) answers.size();
  31. return out;
  32. }
  33. auto f = out.form;
  34. for (auto& c : f.controls)
  35. if (const auto it = set.find (c.name); it != set.end())
  36. c.value = it->second;
  37. answers.push_back (f);
  38. }
  39. return {};
  40. }
  41. } // namespace edscript167
  42. TEST (edscript167_preview_style_and_selection)
  43. {
  44. using namespace lynx::audioedit;
  45. // FL's Amp.pyscript shape: createDialog() + apply(form); the selection's frames, both ends included
  46. const std::string amp = "from enveditor import *\n"
  47. "def createDialog():\n"
  48. " form=ScriptDialog('Amp', 'Simple amplification.')\n"
  49. " form.AddInputKnob('Volume', 1, 0, 2)\n"
  50. " return form\n"
  51. "def apply(form):\n"
  52. " EditorSample.AmpFromTo(Editor.SelectionStartS, Editor.SelectionEndS, form.GetInputValue('Volume'))\n";
  53. auto doc = edscript167::ramp (1000);
  54. const auto before = doc.audio.channels[0];
  55. auto copy = doc;
  56. const auto first = script::run (amp, copy, Range { 100, 200 }, {}, {});
  57. CHECK (first.status == script::Outcome::Status::needsForm && first.form.title == "Amp" && first.form.controls.size() == 1);
  58. CHECK (first.form.controls[0].type == script::Control::Type::knob && first.form.controls[0].value == 1.0 && first.form.controls[0].maximum == 2.0);
  59. int forms = 0;
  60. const auto out = edscript167::runAll (amp, doc, Range { 100, 200 }, { { "Volume", 2.0 } }, &forms);
  61. CHECK (out.status == script::Outcome::Status::done && forms == 1);
  62. CHECK (doc.audio.channels[0][150] == before[150] * 2.0f && doc.audio.channels[0][99] == before[99] && doc.audio.channels[0][200] == before[200]);
  63. // no selection: the whole sample
  64. auto all = edscript167::ramp (500);
  65. edscript167::runAll (amp, all, Range {}, { { "Volume", 0.0 } });
  66. CHECK (all.audio.channels[0][499] == 0.0f && all.audio.channels[1][1] == 0.0f);
  67. }
  68. TEST (edscript167_execute_style_regions_and_messages)
  69. {
  70. using namespace lynx::audioedit;
  71. // FL's Play to end.pyscript shape: a form at the top level (Execute), regions, Utils
  72. const std::string src = "from enveditor import *\n"
  73. "form=ScriptDialog('Play To End','Converts markers.')\n"
  74. "form.AddInputCombo('Reset','No,Yes',0)\n"
  75. "if form.Execute():\n"
  76. " for i in range(EditorSample.RegionCount):\n"
  77. " r=EditorSample.GetRegion(i)\n"
  78. " r.SampleEnd = EditorSample.Length-1 if form.GetInputValue('Reset')==0 else r.SampleStart+9\n"
  79. " Utils.ShowMessage('done %d' % EditorSample.RegionCount)\n";
  80. auto doc = edscript167::ramp (1000);
  81. addMarker (doc, 100, "a");
  82. addMarker (doc, 400, "b");
  83. auto out = edscript167::runAll (src, doc, Range {});
  84. CHECK (out.status == script::Outcome::Status::done && out.messages.size() == 1 && out.messages[0] == "done 2");
  85. CHECK (doc.markers.size() == 2 && doc.markers[0].isRegion() && doc.markers[0].end == 1000 && doc.markers[1].end == 1000);
  86. auto doc2 = edscript167::ramp (1000);
  87. addMarker (doc2, 100, "a");
  88. edscript167::runAll (src, doc2, Range {}, { { "Reset", 1.0 } });
  89. CHECK (doc2.markers[0].start == 100 && doc2.markers[0].end == 110);
  90. // a marker reads as a region ending past the sample; AddRegion / DeleteRegion; Info, KeyNum
  91. const std::string regions = "from enveditor import *\n"
  92. "assert EditorSample.GetRegion(0).SampleEnd > EditorSample.Length\n"
  93. "i = EditorSample.AddRegion('loop', 10, 19)\n"
  94. "r = EditorSample.GetRegion(i)\n"
  95. "r.Info = 'x'\n"
  96. "r.KeyNum = 60\n"
  97. "EditorSample.DeleteRegion(EditorSample.RegionCount - 1)\n";
  98. auto doc3 = edscript167::ramp (1000);
  99. addMarker (doc3, 500, "m");
  100. out = edscript167::runAll (regions, doc3, Range {});
  101. CHECK (out.status == script::Outcome::Status::done);
  102. CHECK (doc3.markers.size() == 1 && doc3.markers[0].name == "loop" && doc3.markers[0].start == 10 && doc3.markers[0].end == 20
  103. && doc3.markers[0].info == "x" && doc3.markers[0].keyNum == 60);
  104. }
  105. TEST (edscript167_sample_api)
  106. {
  107. using namespace lynx::audioedit;
  108. // FL's generator / FX script idioms: an empty editor gets a length, temp samples, paste modes, rate changes
  109. const std::string gen = "from enveditor import *\n"
  110. "if EditorSample.Length<=0:\n"
  111. " EditorSample.Length = round(EditorSample.MSToSamples(1000))\n"
  112. "EditorSample.SineFromTo(Editor.SelectionStartS, Editor.SelectionEndS, 441, 0.25, 0.5)\n"
  113. "t = Sample()\n"
  114. "t.NumChans = 1\n"
  115. "t.Length = 100\n"
  116. "t.SineFromTo(0, 99, 441, 0.25, 0.25)\n"
  117. "EditorSample.PasteFromTo(t, 0, 99, 2)\n";
  118. Document doc;
  119. doc.audio = silence (2, 44100.0, 0);
  120. auto out = edscript167::runAll (gen, doc, Range {});
  121. CHECK (out.status == script::Outcome::Status::done);
  122. CHECK (doc.audio.frames() == 44100 && doc.audio.numChannels() == 2);
  123. // the sine starts at its phase (0.25 cycle = peak): 0.5 + the pasted 0.25 at frame 0, 0.5 after the paste
  124. CHECK (std::abs (doc.audio.channels[0][0] - 0.75f) < 1e-4f && std::abs (doc.audio.channels[1][0] - 0.75f) < 1e-4f);
  125. CHECK (std::abs (doc.audio.channels[0][100] - 0.5f) < 1e-4f); // 441 Hz: 100 frames = one cycle
  126. // Destructoid's way: a copy at a lower rate, processed, back to the editor's format; same length
  127. const std::string fx = "from enveditor import *\n"
  128. "t = Sample()\n"
  129. "t.SampleRate = EditorSample.SampleRate\n"
  130. "t.NumChans = EditorSample.NumChans\n"
  131. "t.PasteFromTo(EditorSample, 0, 0, 0)\n"
  132. "assert t.Length == EditorSample.Length\n"
  133. "t.SampleRate = 11025\n"
  134. "assert abs(t.Length - EditorSample.Length / 4) <= 1\n"
  135. "t.NormalizeFormat(EditorSample, NF_All)\n"
  136. "assert abs(t.Length - EditorSample.Length) <= 4 and t.SampleRate == EditorSample.SampleRate\n"
  137. "EditorSample.ReverseFromTo(0, 9)\n"
  138. "EditorSample.DeleteFromTo(0, 9)\n"
  139. "EditorSample.InsertSilence(0, 4)\n"
  140. "EditorSample.NormalizeFromTo(0, EditorSample.Length - 1, 0.5)\n"
  141. "EditorSample.ConvertToMono(1)\n";
  142. auto d2 = edscript167::ramp (4000);
  143. out = edscript167::runAll (fx, d2, Range {});
  144. CHECK (out.status == script::Outcome::Status::done);
  145. CHECK (d2.audio.frames() == 4000 - 10 + 5 && d2.audio.numChannels() == 1 && d2.audio.channels[0][0] == 0.0f);
  146. CHECK (std::abs (peak (d2.audio, Range { 0, d2.audio.frames() }) - 0.5f) < 1e-5f);
  147. // errors come back with Python's message; the document is untouched
  148. auto d3 = edscript167::ramp (100);
  149. auto copy = d3;
  150. out = script::run ("from enveditor import *\nEditorSample.GetRegion(3)\n", copy, Range {}, {}, {});
  151. CHECK (out.status == script::Outcome::Status::failed && out.error.find ("IndexError") != std::string::npos);
  152. out = script::run ("x = = 1\n", copy, Range {}, {}, {});
  153. CHECK (out.status == script::Outcome::Status::failed && ! out.error.empty());
  154. // Python's adjacent literals join (FL's Limiter.pyscript: 'Gain doesn''t apply'); other strings stay as written
  155. out = edscript167::runAll (R"PY(from enveditor import *
  156. f = ScriptDialog('Lim','doesn''t' "a" "b's")
  157. assert 'x''y' == 'xy' and 'a' 'b' 'c' == 'abc' and "q'" + '#' == "q'#" # 'z''w'
  158. assert '''t''' + 'u' == 'tu' and 'it\'s' 'x' == "it'sx" and len('\\' 'n') == 2
  159. )PY",
  160. d3, Range {});
  161. CHECK (out.status == script::Outcome::Status::done);
  162. if (out.status != script::Outcome::Status::done)
  163. std::printf (" %s\n", out.error.c_str());
  164. // LoadFromClipboard through the host
  165. script::Host host;
  166. host.clipboard = [] (Audio& a) {
  167. a = silence (1, 22050.0, 10);
  168. a.channels[0][3] = 0.5f;
  169. return true;
  170. };
  171. out = edscript167::runAll ("from enveditor import *\nc = Sample()\nc.LoadFromClipboard()\nassert c.Length == 10 and c.SampleRate == 22050\nEditorSample.SetSampleAt(0, 0, c.GetSampleAt(3, 0))\n", d3,
  172. Range {}, {}, nullptr, host);
  173. CHECK (out.status == script::Outcome::Status::done && d3.audio.channels[0][0] == 0.5f);
  174. }
  175. TEST (edscript167_builtins_and_form_memory)
  176. {
  177. using namespace lynx::audioedit;
  178. // LYNX's own scripts all run (their defaults) on a short stereo sample and on an empty editor
  179. for (const auto& b : script::builtins())
  180. {
  181. auto doc = edscript167::ramp (2000);
  182. addMarker (doc, 100);
  183. addMarker (doc, 900);
  184. const auto out = edscript167::runAll (b.source, doc, Range { 0, 1000 });
  185. CHECK (out.status == script::Outcome::Status::done);
  186. if (out.status != script::Outcome::Status::done)
  187. std::printf (" builtin %s/%s: %s\n", b.folder, b.name, out.error.c_str());
  188. }
  189. // restoreFormValues: the last confirmed values come back for the same script
  190. script::Form f;
  191. f.title = "Amp";
  192. script::Control c;
  193. c.type = script::Control::Type::knob;
  194. c.name = "Volume";
  195. c.value = 1.7;
  196. c.maximum = 2.0;
  197. f.controls.push_back (c);
  198. script::remember ("amp.pyscript", f);
  199. f.controls[0].value = 1.0;
  200. script::restore ("amp.pyscript", f);
  201. CHECK (f.controls[0].value == 1.7);
  202. f.controls[0].value = 1.0;
  203. f.restoreValues = false;
  204. script::restore ("amp.pyscript", f);
  205. CHECK (f.controls[0].value == 1.0);
  206. }