You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

399 lines
13KB

  1. #include "plugin.hpp"
  2. #include "clouds/dsp/granular_processor.h"
  3. struct Clouds : Module {
  4. enum ParamIds {
  5. FREEZE_PARAM,
  6. MODE_PARAM,
  7. LOAD_PARAM,
  8. POSITION_PARAM,
  9. SIZE_PARAM,
  10. PITCH_PARAM,
  11. IN_GAIN_PARAM,
  12. DENSITY_PARAM,
  13. TEXTURE_PARAM,
  14. BLEND_PARAM,
  15. SPREAD_PARAM,
  16. FEEDBACK_PARAM,
  17. REVERB_PARAM,
  18. NUM_PARAMS
  19. };
  20. enum InputIds {
  21. FREEZE_INPUT,
  22. TRIG_INPUT,
  23. POSITION_INPUT,
  24. SIZE_INPUT,
  25. PITCH_INPUT,
  26. BLEND_INPUT,
  27. IN_L_INPUT,
  28. IN_R_INPUT,
  29. DENSITY_INPUT,
  30. TEXTURE_INPUT,
  31. NUM_INPUTS
  32. };
  33. enum OutputIds {
  34. OUT_L_OUTPUT,
  35. OUT_R_OUTPUT,
  36. NUM_OUTPUTS
  37. };
  38. enum LightIds {
  39. FREEZE_LIGHT,
  40. MIX_GREEN_LIGHT, MIX_RED_LIGHT,
  41. PAN_GREEN_LIGHT, PAN_RED_LIGHT,
  42. FEEDBACK_GREEN_LIGHT, FEEDBACK_RED_LIGHT,
  43. REVERB_GREEN_LIGHT, REVERB_RED_LIGHT,
  44. NUM_LIGHTS
  45. };
  46. dsp::SampleRateConverter<2> inputSrc;
  47. dsp::SampleRateConverter<2> outputSrc;
  48. dsp::DoubleRingBuffer<dsp::Frame<2>, 256> inputBuffer;
  49. dsp::DoubleRingBuffer<dsp::Frame<2>, 256> outputBuffer;
  50. uint8_t* block_mem;
  51. uint8_t* block_ccm;
  52. clouds::GranularProcessor* processor;
  53. bool triggered = false;
  54. dsp::SchmittTrigger freezeTrigger;
  55. bool freeze = false;
  56. dsp::SchmittTrigger blendTrigger;
  57. int blendMode = 0;
  58. clouds::PlaybackMode playback;
  59. int quality = 0;
  60. Clouds() {
  61. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
  62. configParam(POSITION_PARAM, 0.0, 1.0, 0.5, "Grain position");
  63. configParam(SIZE_PARAM, 0.0, 1.0, 0.5, "Grain size");
  64. configParam(PITCH_PARAM, -2.0, 2.0, 0.0, "Grain pitch");
  65. configParam(IN_GAIN_PARAM, 0.0, 1.0, 0.5, "Audio input gain");
  66. configParam(DENSITY_PARAM, 0.0, 1.0, 0.5, "Grain density");
  67. configParam(TEXTURE_PARAM, 0.0, 1.0, 0.5, "Grain texture");
  68. configParam(BLEND_PARAM, 0.0, 1.0, 0.5, "Dry/wet");
  69. configParam(SPREAD_PARAM, 0.0, 1.0, 0.0, "Stereo spread");
  70. configParam(FEEDBACK_PARAM, 0.0, 1.0, 0.0, "Feedback amount");
  71. configParam(REVERB_PARAM, 0.0, 1.0, 0.0, "Reverb amount");
  72. configParam(FREEZE_PARAM, 0.0, 1.0, 0.0, "Freeze");
  73. configParam(MODE_PARAM, 0.0, 1.0, 0.0, "Mode");
  74. configParam(LOAD_PARAM, 0.0, 1.0, 0.0, "Load/save");
  75. const int memLen = 118784;
  76. const int ccmLen = 65536 - 128;
  77. block_mem = new uint8_t[memLen]();
  78. block_ccm = new uint8_t[ccmLen]();
  79. processor = new clouds::GranularProcessor();
  80. memset(processor, 0, sizeof(*processor));
  81. processor->Init(block_mem, memLen, block_ccm, ccmLen);
  82. onReset();
  83. }
  84. ~Clouds() {
  85. delete processor;
  86. delete[] block_mem;
  87. delete[] block_ccm;
  88. }
  89. void process(const ProcessArgs& args) override {
  90. // Get input
  91. dsp::Frame<2> inputFrame = {};
  92. if (!inputBuffer.full()) {
  93. inputFrame.samples[0] = inputs[IN_L_INPUT].getVoltage() * params[IN_GAIN_PARAM].getValue() / 5.0;
  94. inputFrame.samples[1] = inputs[IN_R_INPUT].isConnected() ? inputs[IN_R_INPUT].getVoltage() * params[IN_GAIN_PARAM].getValue() / 5.0 : inputFrame.samples[0];
  95. inputBuffer.push(inputFrame);
  96. }
  97. if (freezeTrigger.process(params[FREEZE_PARAM].getValue())) {
  98. freeze ^= true;
  99. }
  100. if (blendTrigger.process(params[MODE_PARAM].getValue())) {
  101. blendMode = (blendMode + 1) % 4;
  102. }
  103. // Trigger
  104. if (inputs[TRIG_INPUT].getVoltage() >= 1.0) {
  105. triggered = true;
  106. }
  107. // Render frames
  108. if (outputBuffer.empty()) {
  109. clouds::ShortFrame input[32] = {};
  110. // Convert input buffer
  111. {
  112. inputSrc.setRates(args.sampleRate, 32000);
  113. dsp::Frame<2> inputFrames[32];
  114. int inLen = inputBuffer.size();
  115. int outLen = 32;
  116. inputSrc.process(inputBuffer.startData(), &inLen, inputFrames, &outLen);
  117. inputBuffer.startIncr(inLen);
  118. // We might not fill all of the input buffer if there is a deficiency, but this cannot be avoided due to imprecisions between the input and output SRC.
  119. for (int i = 0; i < outLen; i++) {
  120. input[i].l = clamp(inputFrames[i].samples[0] * 32767.0f, -32768.0f, 32767.0f);
  121. input[i].r = clamp(inputFrames[i].samples[1] * 32767.0f, -32768.0f, 32767.0f);
  122. }
  123. }
  124. // Set up processor
  125. processor->set_playback_mode(playback);
  126. processor->set_quality(quality);
  127. processor->Prepare();
  128. clouds::Parameters* p = processor->mutable_parameters();
  129. p->trigger = triggered;
  130. p->gate = triggered;
  131. p->freeze = freeze || (inputs[FREEZE_INPUT].getVoltage() >= 1.0);
  132. p->position = clamp(params[POSITION_PARAM].getValue() + inputs[POSITION_INPUT].getVoltage() / 5.0f, 0.0f, 1.0f);
  133. p->size = clamp(params[SIZE_PARAM].getValue() + inputs[SIZE_INPUT].getVoltage() / 5.0f, 0.0f, 1.0f);
  134. p->pitch = clamp((params[PITCH_PARAM].getValue() + inputs[PITCH_INPUT].getVoltage()) * 12.0f, -48.0f, 48.0f);
  135. p->density = clamp(params[DENSITY_PARAM].getValue() + inputs[DENSITY_INPUT].getVoltage() / 5.0f, 0.0f, 1.0f);
  136. p->texture = clamp(params[TEXTURE_PARAM].getValue() + inputs[TEXTURE_INPUT].getVoltage() / 5.0f, 0.0f, 1.0f);
  137. p->dry_wet = params[BLEND_PARAM].getValue();
  138. p->stereo_spread = params[SPREAD_PARAM].getValue();
  139. p->feedback = params[FEEDBACK_PARAM].getValue();
  140. // TODO
  141. // Why doesn't dry audio get reverbed?
  142. p->reverb = params[REVERB_PARAM].getValue();
  143. float blend = inputs[BLEND_INPUT].getVoltage() / 5.0f;
  144. switch (blendMode) {
  145. case 0:
  146. p->dry_wet += blend;
  147. p->dry_wet = clamp(p->dry_wet, 0.0f, 1.0f);
  148. break;
  149. case 1:
  150. p->stereo_spread += blend;
  151. p->stereo_spread = clamp(p->stereo_spread, 0.0f, 1.0f);
  152. break;
  153. case 2:
  154. p->feedback += blend;
  155. p->feedback = clamp(p->feedback, 0.0f, 1.0f);
  156. break;
  157. case 3:
  158. p->reverb += blend;
  159. p->reverb = clamp(p->reverb, 0.0f, 1.0f);
  160. break;
  161. }
  162. clouds::ShortFrame output[32];
  163. processor->Process(input, output, 32);
  164. // Convert output buffer
  165. {
  166. dsp::Frame<2> outputFrames[32];
  167. for (int i = 0; i < 32; i++) {
  168. outputFrames[i].samples[0] = output[i].l / 32768.0;
  169. outputFrames[i].samples[1] = output[i].r / 32768.0;
  170. }
  171. outputSrc.setRates(32000, args.sampleRate);
  172. int inLen = 32;
  173. int outLen = outputBuffer.capacity();
  174. outputSrc.process(outputFrames, &inLen, outputBuffer.endData(), &outLen);
  175. outputBuffer.endIncr(outLen);
  176. }
  177. triggered = false;
  178. }
  179. // Set output
  180. dsp::Frame<2> outputFrame = {};
  181. if (!outputBuffer.empty()) {
  182. outputFrame = outputBuffer.shift();
  183. outputs[OUT_L_OUTPUT].setVoltage(5.0 * outputFrame.samples[0]);
  184. outputs[OUT_R_OUTPUT].setVoltage(5.0 * outputFrame.samples[1]);
  185. }
  186. // Lights
  187. clouds::Parameters* p = processor->mutable_parameters();
  188. dsp::VuMeter vuMeter;
  189. vuMeter.dBInterval = 6.0;
  190. dsp::Frame<2> lightFrame = p->freeze ? outputFrame : inputFrame;
  191. vuMeter.setValue(fmaxf(fabsf(lightFrame.samples[0]), fabsf(lightFrame.samples[1])));
  192. lights[FREEZE_LIGHT].setBrightness(p->freeze ? 0.75 : 0.0);
  193. lights[MIX_GREEN_LIGHT].setSmoothBrightness(vuMeter.getBrightness(3), args.sampleTime);
  194. lights[PAN_GREEN_LIGHT].setSmoothBrightness(vuMeter.getBrightness(2), args.sampleTime);
  195. lights[FEEDBACK_GREEN_LIGHT].setSmoothBrightness(vuMeter.getBrightness(1), args.sampleTime);
  196. lights[REVERB_GREEN_LIGHT].setBrightness(0.0);
  197. lights[MIX_RED_LIGHT].setBrightness(0.0);
  198. lights[PAN_RED_LIGHT].setBrightness(0.0);
  199. lights[FEEDBACK_RED_LIGHT].setSmoothBrightness(vuMeter.getBrightness(1), args.sampleTime);
  200. lights[REVERB_RED_LIGHT].setSmoothBrightness(vuMeter.getBrightness(0), args.sampleTime);
  201. }
  202. void onReset() override {
  203. freeze = false;
  204. blendMode = 0;
  205. playback = clouds::PLAYBACK_MODE_GRANULAR;
  206. quality = 0;
  207. }
  208. json_t* dataToJson() override {
  209. json_t* rootJ = json_object();
  210. json_object_set_new(rootJ, "playback", json_integer((int) playback));
  211. json_object_set_new(rootJ, "quality", json_integer(quality));
  212. json_object_set_new(rootJ, "blendMode", json_integer(blendMode));
  213. return rootJ;
  214. }
  215. void dataFromJson(json_t* rootJ) override {
  216. json_t* playbackJ = json_object_get(rootJ, "playback");
  217. if (playbackJ) {
  218. playback = (clouds::PlaybackMode) json_integer_value(playbackJ);
  219. }
  220. json_t* qualityJ = json_object_get(rootJ, "quality");
  221. if (qualityJ) {
  222. quality = json_integer_value(qualityJ);
  223. }
  224. json_t* blendModeJ = json_object_get(rootJ, "blendMode");
  225. if (blendModeJ) {
  226. blendMode = json_integer_value(blendModeJ);
  227. }
  228. }
  229. };
  230. struct FreezeLight : YellowLight {
  231. FreezeLight() {
  232. box.size = Vec(28 - 6, 28 - 6);
  233. bgColor = color::BLACK_TRANSPARENT;
  234. }
  235. };
  236. struct CloudsWidget : ModuleWidget {
  237. ParamWidget* blendParam;
  238. ParamWidget* spreadParam;
  239. ParamWidget* feedbackParam;
  240. ParamWidget* reverbParam;
  241. CloudsWidget(Clouds* module) {
  242. setModule(module);
  243. setPanel(Svg::load(asset::plugin(pluginInstance, "res/Clouds.svg")));
  244. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  245. addChild(createWidget<ScrewSilver>(Vec(240, 0)));
  246. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  247. addChild(createWidget<ScrewSilver>(Vec(240, 365)));
  248. addParam(createParam<Rogan3PSRed>(Vec(27, 93), module, Clouds::POSITION_PARAM));
  249. addParam(createParam<Rogan3PSGreen>(Vec(108, 93), module, Clouds::SIZE_PARAM));
  250. addParam(createParam<Rogan3PSWhite>(Vec(190, 93), module, Clouds::PITCH_PARAM));
  251. addParam(createParam<Rogan1PSRed>(Vec(14, 180), module, Clouds::IN_GAIN_PARAM));
  252. addParam(createParam<Rogan1PSRed>(Vec(81, 180), module, Clouds::DENSITY_PARAM));
  253. addParam(createParam<Rogan1PSGreen>(Vec(146, 180), module, Clouds::TEXTURE_PARAM));
  254. blendParam = createParam<Rogan1PSWhite>(Vec(213, 180), module, Clouds::BLEND_PARAM);
  255. addParam(blendParam);
  256. spreadParam = createParam<Rogan1PSRed>(Vec(213, 180), module, Clouds::SPREAD_PARAM);
  257. spreadParam->hide();
  258. addParam(spreadParam);
  259. feedbackParam = createParam<Rogan1PSGreen>(Vec(213, 180), module, Clouds::FEEDBACK_PARAM);
  260. feedbackParam->hide();
  261. addParam(feedbackParam);
  262. reverbParam = createParam<Rogan1PSBlue>(Vec(213, 180), module, Clouds::REVERB_PARAM);
  263. reverbParam->hide();
  264. addParam(reverbParam);
  265. addParam(createParam<CKD6>(Vec(12, 43), module, Clouds::FREEZE_PARAM));
  266. addParam(createParam<TL1105>(Vec(211, 50), module, Clouds::MODE_PARAM));
  267. addParam(createParam<TL1105>(Vec(239, 50), module, Clouds::LOAD_PARAM));
  268. addInput(createInput<PJ301MPort>(Vec(15, 274), module, Clouds::FREEZE_INPUT));
  269. addInput(createInput<PJ301MPort>(Vec(58, 274), module, Clouds::TRIG_INPUT));
  270. addInput(createInput<PJ301MPort>(Vec(101, 274), module, Clouds::POSITION_INPUT));
  271. addInput(createInput<PJ301MPort>(Vec(144, 274), module, Clouds::SIZE_INPUT));
  272. addInput(createInput<PJ301MPort>(Vec(188, 274), module, Clouds::PITCH_INPUT));
  273. addInput(createInput<PJ301MPort>(Vec(230, 274), module, Clouds::BLEND_INPUT));
  274. addInput(createInput<PJ301MPort>(Vec(15, 317), module, Clouds::IN_L_INPUT));
  275. addInput(createInput<PJ301MPort>(Vec(58, 317), module, Clouds::IN_R_INPUT));
  276. addInput(createInput<PJ301MPort>(Vec(101, 317), module, Clouds::DENSITY_INPUT));
  277. addInput(createInput<PJ301MPort>(Vec(144, 317), module, Clouds::TEXTURE_INPUT));
  278. addOutput(createOutput<PJ301MPort>(Vec(188, 317), module, Clouds::OUT_L_OUTPUT));
  279. addOutput(createOutput<PJ301MPort>(Vec(230, 317), module, Clouds::OUT_R_OUTPUT));
  280. addChild(createLight<FreezeLight>(Vec(12 + 3, 43 + 3), module, Clouds::FREEZE_LIGHT));
  281. addChild(createLight<MediumLight<GreenRedLight>>(Vec(82.5, 53), module, Clouds::MIX_GREEN_LIGHT));
  282. addChild(createLight<MediumLight<GreenRedLight>>(Vec(114.5, 53), module, Clouds::PAN_GREEN_LIGHT));
  283. addChild(createLight<MediumLight<GreenRedLight>>(Vec(145.5, 53), module, Clouds::FEEDBACK_GREEN_LIGHT));
  284. addChild(createLight<MediumLight<GreenRedLight>>(Vec(177.5, 53), module, Clouds::REVERB_GREEN_LIGHT));
  285. }
  286. void step() override {
  287. Clouds* module = dynamic_cast<Clouds*>(this->module);
  288. if (module) {
  289. blendParam->visible = (module->blendMode == 0);
  290. spreadParam->visible = (module->blendMode == 1);
  291. feedbackParam->visible = (module->blendMode == 2);
  292. reverbParam->visible = (module->blendMode == 3);
  293. }
  294. ModuleWidget::step();
  295. }
  296. void appendContextMenu(Menu* menu) override {
  297. Clouds* module = dynamic_cast<Clouds*>(this->module);
  298. assert(module);
  299. menu->addChild(new MenuSeparator);
  300. menu->addChild(createMenuLabel("Blend knob"));
  301. static const std::vector<std::string> blendLabels = {
  302. "Wet/dry",
  303. "Spread",
  304. "Feedback",
  305. "Reverb",
  306. };
  307. for (int i = 0; i < (int) blendLabels.size(); i++) {
  308. menu->addChild(createCheckMenuItem(blendLabels[i],
  309. [=]() {return module->blendMode == i;},
  310. [=]() {module->blendMode = i;}
  311. ));
  312. }
  313. menu->addChild(new MenuSeparator);
  314. menu->addChild(createMenuLabel("Alternate mode"));
  315. static const std::vector<std::string> playbackLabels = {
  316. "Granular",
  317. "Pitch-shifter/time-stretcher",
  318. "Looping delay",
  319. "Spectral madness",
  320. };
  321. for (int i = 0; i < (int) playbackLabels.size(); i++) {
  322. menu->addChild(createCheckMenuItem(playbackLabels[i],
  323. [=]() {return module->playback == i;},
  324. [=]() {module->playback = (clouds::PlaybackMode) i;}
  325. ));
  326. }
  327. menu->addChild(new MenuSeparator);
  328. menu->addChild(createMenuLabel("Quality"));
  329. static const std::vector<std::string> qualityLabels = {
  330. "1s 32kHz 16-bit stereo",
  331. "2s 32kHz 16-bit mono",
  332. "4s 16kHz 8-bit µ-law stereo",
  333. "8s 16kHz 8-bit µ-law mono",
  334. };
  335. for (int i = 0; i < (int) qualityLabels.size(); i++) {
  336. menu->addChild(createCheckMenuItem(qualityLabels[i],
  337. [=]() {return module->quality == i;},
  338. [=]() {module->quality = i;}
  339. ));
  340. }
  341. }
  342. };
  343. Model* modelClouds = createModel<Clouds, CloudsWidget>("Clouds");