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.

628 lines
28KB

  1. #include <mutex>
  2. #include <chrono>
  3. #include <thread>
  4. #include <condition_variable>
  5. #include "plugin.hpp"
  6. #include <audio.hpp>
  7. #include <context.hpp>
  8. namespace rack {
  9. namespace core {
  10. template <int NUM_AUDIO_INPUTS, int NUM_AUDIO_OUTPUTS>
  11. struct AudioPort : audio::Port {
  12. Module* module;
  13. dsp::DoubleRingBuffer<dsp::Frame<NUM_AUDIO_INPUTS>, 32768> engineInputBuffer;
  14. dsp::DoubleRingBuffer<dsp::Frame<NUM_AUDIO_OUTPUTS>, 32768> engineOutputBuffer;
  15. dsp::SampleRateConverter<NUM_AUDIO_INPUTS> inputSrc;
  16. dsp::SampleRateConverter<NUM_AUDIO_OUTPUTS> outputSrc;
  17. // Port variable caches
  18. int deviceNumInputs = 0;
  19. int deviceNumOutputs = 0;
  20. float deviceSampleRate = 0.f;
  21. int requestedEngineFrames = 0;
  22. AudioPort(Module* module) {
  23. this->module = module;
  24. maxOutputs = NUM_AUDIO_INPUTS;
  25. maxInputs = NUM_AUDIO_OUTPUTS;
  26. inputSrc.setQuality(6);
  27. outputSrc.setQuality(6);
  28. }
  29. void setMaster(bool master = true) {
  30. if (master) {
  31. APP->engine->setMasterModule(module);
  32. }
  33. else {
  34. // Unset master only if module is currently master
  35. if (isMaster())
  36. APP->engine->setMasterModule(NULL);
  37. }
  38. }
  39. bool isMaster() {
  40. return APP->engine->getMasterModule() == module;
  41. }
  42. void processInput(const float* input, int inputStride, int frames) override {
  43. deviceNumInputs = std::min(getNumInputs(), NUM_AUDIO_OUTPUTS);
  44. deviceNumOutputs = std::min(getNumOutputs(), NUM_AUDIO_INPUTS);
  45. deviceSampleRate = getSampleRate();
  46. // DEBUG("%p: new device block ____________________________", this);
  47. // Claim master module if there is none
  48. if (!APP->engine->getMasterModule()) {
  49. setMaster();
  50. }
  51. bool isMasterCached = isMaster();
  52. // Set sample rate of engine if engine sample rate is "auto".
  53. if (isMasterCached) {
  54. APP->engine->setSuggestedSampleRate(deviceSampleRate);
  55. }
  56. float engineSampleRate = APP->engine->getSampleRate();
  57. float sampleRateRatio = engineSampleRate / deviceSampleRate;
  58. // DEBUG("%p: %d block, engineOutputBuffer still has %d", this, frames, (int) engineOutputBuffer.size());
  59. // Consider engine buffers "too full" if they contain a bit more than the audio device's number of frames, converted to engine sample rate.
  60. int maxEngineFrames = (int) std::ceil(frames * sampleRateRatio * 2.0) - 1;
  61. // If the engine output buffer is too full, clear it to keep latency low. No need to clear if master because it's always cleared below.
  62. if (!isMasterCached && (int) engineOutputBuffer.size() > maxEngineFrames) {
  63. engineOutputBuffer.clear();
  64. // DEBUG("%p: clearing engine output", this);
  65. }
  66. if (deviceNumInputs > 0) {
  67. // Always clear engine output if master
  68. if (isMasterCached) {
  69. engineOutputBuffer.clear();
  70. }
  71. // Set up sample rate converter
  72. outputSrc.setRates(deviceSampleRate, engineSampleRate);
  73. outputSrc.setChannels(deviceNumInputs);
  74. int inputFrames = frames;
  75. int outputFrames = engineOutputBuffer.capacity();
  76. outputSrc.process(input, inputStride, &inputFrames, (float*) engineOutputBuffer.endData(), NUM_AUDIO_OUTPUTS, &outputFrames);
  77. engineOutputBuffer.endIncr(outputFrames);
  78. // Request exactly as many frames as we have in the engine output buffer.
  79. requestedEngineFrames = engineOutputBuffer.size();
  80. }
  81. else {
  82. // Upper bound on number of frames so that `audioOutputFrames >= frames` when processOutput() is called.
  83. requestedEngineFrames = std::max((int) std::ceil(frames * sampleRateRatio) - (int) engineInputBuffer.size(), 0);
  84. }
  85. }
  86. void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames) override {
  87. // Step engine
  88. if (isMaster() && requestedEngineFrames > 0) {
  89. // DEBUG("%p: %d block, stepping %d", this, frames, requestedEngineFrames);
  90. APP->engine->stepBlock(requestedEngineFrames);
  91. }
  92. }
  93. void processOutput(float* output, int outputStride, int frames) override {
  94. // bool isMasterCached = isMaster();
  95. float engineSampleRate = APP->engine->getSampleRate();
  96. float sampleRateRatio = engineSampleRate / deviceSampleRate;
  97. if (deviceNumOutputs > 0) {
  98. // Set up sample rate converter
  99. inputSrc.setRates(engineSampleRate, deviceSampleRate);
  100. inputSrc.setChannels(deviceNumOutputs);
  101. // Convert engine input -> audio output
  102. int inputFrames = engineInputBuffer.size();
  103. int outputFrames = frames;
  104. inputSrc.process((const float*) engineInputBuffer.startData(), NUM_AUDIO_INPUTS, &inputFrames, output, outputStride, &outputFrames);
  105. engineInputBuffer.startIncr(inputFrames);
  106. // Clamp output samples
  107. for (int i = 0; i < outputFrames; i++) {
  108. for (int j = 0; j < deviceNumOutputs; j++) {
  109. float v = output[i * outputStride + j];
  110. v = clamp(v, -1.f, 1.f);
  111. output[i * outputStride + j] = v;
  112. }
  113. }
  114. // Fill the rest of the audio output buffer with zeros
  115. for (int i = outputFrames; i < frames; i++) {
  116. for (int j = 0; j < deviceNumOutputs; j++) {
  117. output[i * outputStride + j] = 0.f;
  118. }
  119. }
  120. }
  121. // DEBUG("%p: %d block, engineInputBuffer left %d", this, frames, (int) engineInputBuffer.size());
  122. // If the engine input buffer is too full, clear it to keep latency low.
  123. int maxEngineFrames = (int) std::ceil(frames * sampleRateRatio * 2.0) - 1;
  124. if ((int) engineInputBuffer.size() > maxEngineFrames) {
  125. engineInputBuffer.clear();
  126. // DEBUG("%p: clearing engine input", this);
  127. }
  128. // DEBUG("%p %s:\tframes %d requestedEngineFrames %d\toutputBuffer %d engineInputBuffer %d\t", this, isMasterCached ? "master" : "secondary", frames, requestedEngineFrames, engineOutputBuffer.size(), engineInputBuffer.size());
  129. }
  130. void onStartStream() override {
  131. engineInputBuffer.clear();
  132. engineOutputBuffer.clear();
  133. // DEBUG("onStartStream");
  134. }
  135. void onStopStream() override {
  136. deviceNumInputs = 0;
  137. deviceNumOutputs = 0;
  138. deviceSampleRate = 0.f;
  139. engineInputBuffer.clear();
  140. engineOutputBuffer.clear();
  141. // We can be in an Engine write-lock here (e.g. onReset() calls this indirectly), so use non-locking master module API.
  142. // setMaster(false);
  143. if (APP->engine->getMasterModule() == module)
  144. APP->engine->setMasterModule_NoLock(NULL);
  145. // DEBUG("onStopStream");
  146. }
  147. };
  148. template <int NUM_AUDIO_INPUTS, int NUM_AUDIO_OUTPUTS>
  149. struct Audio : Module {
  150. static constexpr int NUM_INPUT_LIGHTS = (NUM_AUDIO_INPUTS > 2) ? (NUM_AUDIO_INPUTS / 2) : 0;
  151. static constexpr int NUM_OUTPUT_LIGHTS = (NUM_AUDIO_OUTPUTS > 2) ? (NUM_AUDIO_OUTPUTS / 2) : 0;
  152. enum ParamIds {
  153. ENUMS(LEVEL_PARAM, NUM_AUDIO_INPUTS == 2),
  154. NUM_PARAMS
  155. };
  156. enum InputIds {
  157. ENUMS(AUDIO_INPUTS, NUM_AUDIO_INPUTS),
  158. NUM_INPUTS
  159. };
  160. enum OutputIds {
  161. ENUMS(AUDIO_OUTPUTS, NUM_AUDIO_OUTPUTS),
  162. NUM_OUTPUTS
  163. };
  164. enum LightIds {
  165. ENUMS(INPUT_LIGHTS, NUM_INPUT_LIGHTS * 2),
  166. ENUMS(OUTPUT_LIGHTS, NUM_OUTPUT_LIGHTS * 2),
  167. ENUMS(VU_LIGHTS, (NUM_AUDIO_INPUTS == 2) ? (2 * 6) : 0),
  168. NUM_LIGHTS
  169. };
  170. AudioPort<NUM_AUDIO_INPUTS, NUM_AUDIO_OUTPUTS> port;
  171. dsp::RCFilter dcFilters[NUM_AUDIO_INPUTS];
  172. bool dcFilterEnabled = false;
  173. dsp::ClockDivider lightDivider;
  174. // For each pair of inputs/outputs
  175. float inputClipTimers[(NUM_AUDIO_INPUTS > 0) ? NUM_INPUT_LIGHTS : 0] = {};
  176. float outputClipTimers[(NUM_AUDIO_INPUTS > 0) ? NUM_OUTPUT_LIGHTS : 0] = {};
  177. dsp::VuMeter2 vuMeter[(NUM_AUDIO_INPUTS == 2) ? 2 : 0];
  178. Audio() : port(this) {
  179. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
  180. if (NUM_AUDIO_INPUTS == 2)
  181. configParam(LEVEL_PARAM, 0.f, 2.f, 1.f, "Level", " dB", -10, 40);
  182. for (int i = 0; i < NUM_AUDIO_INPUTS; i++)
  183. configInput(AUDIO_INPUTS + i, string::f("To \"device output %d\"", i + 1));
  184. for (int i = 0; i < NUM_AUDIO_OUTPUTS; i++)
  185. configOutput(AUDIO_OUTPUTS + i, string::f("From \"device input %d\"", i + 1));
  186. for (int i = 0; i < NUM_INPUT_LIGHTS; i++)
  187. configLight(INPUT_LIGHTS + 2 * i, string::f("Device output %d/%d status", 2 * i + 1, 2 * i + 2));
  188. for (int i = 0; i < NUM_OUTPUT_LIGHTS; i++)
  189. configLight(OUTPUT_LIGHTS + 2 * i, string::f("Device input %d/%d status", 2 * i + 1, 2 * i + 2));
  190. lightDivider.setDivision(512);
  191. float sampleTime = APP->engine->getSampleTime();
  192. for (int i = 0; i < NUM_AUDIO_INPUTS; i++) {
  193. dcFilters[i].setCutoffFreq(10.f * sampleTime);
  194. }
  195. onReset();
  196. }
  197. ~Audio() {
  198. // Close stream here before destructing AudioPort, so processBuffer() etc are not called on another thread while destructing.
  199. port.setDriverId(-1);
  200. }
  201. void onReset() override {
  202. port.setDriverId(-1);
  203. if (NUM_AUDIO_INPUTS == 2)
  204. dcFilterEnabled = true;
  205. else
  206. dcFilterEnabled = false;
  207. }
  208. void onSampleRateChange(const SampleRateChangeEvent& e) override {
  209. port.engineInputBuffer.clear();
  210. port.engineOutputBuffer.clear();
  211. for (int i = 0; i < NUM_AUDIO_INPUTS; i++) {
  212. dcFilters[i].setCutoffFreq(10.f * e.sampleTime);
  213. }
  214. }
  215. void process(const ProcessArgs& args) override {
  216. const float clipTime = 0.25f;
  217. // Push inputs to buffer
  218. if (port.deviceNumOutputs > 0) {
  219. dsp::Frame<NUM_AUDIO_INPUTS> inputFrame = {};
  220. for (int i = 0; i < port.deviceNumOutputs; i++) {
  221. // Get input
  222. float v = 0.f;
  223. if (inputs[AUDIO_INPUTS + i].isConnected())
  224. v = inputs[AUDIO_INPUTS + i].getVoltageSum() / 10.f;
  225. // Normalize right input to left on Audio-2
  226. else if (i == 1 && NUM_AUDIO_INPUTS == 2)
  227. v = inputFrame.samples[0];
  228. // Apply DC filter
  229. if (dcFilterEnabled) {
  230. dcFilters[i].process(v);
  231. v = dcFilters[i].highpass();
  232. }
  233. // Detect clipping
  234. if (NUM_AUDIO_INPUTS > 2) {
  235. if (std::fabs(v) >= 1.f)
  236. inputClipTimers[i / 2] = clipTime;
  237. }
  238. inputFrame.samples[i] = v;
  239. }
  240. // Audio-2: Apply gain from knob
  241. if (NUM_AUDIO_INPUTS == 2) {
  242. float gain = std::pow(params[LEVEL_PARAM].getValue(), 2.f);
  243. for (int i = 0; i < NUM_AUDIO_INPUTS; i++) {
  244. inputFrame.samples[i] *= gain;
  245. }
  246. }
  247. if (!port.engineInputBuffer.full()) {
  248. port.engineInputBuffer.push(inputFrame);
  249. }
  250. // Audio-2: VU meter process
  251. if (NUM_AUDIO_INPUTS == 2) {
  252. for (int i = 0; i < NUM_AUDIO_INPUTS; i++) {
  253. vuMeter[i].process(args.sampleTime, inputFrame.samples[i]);
  254. }
  255. }
  256. }
  257. else {
  258. // Audio-2: Clear VU meter
  259. if (NUM_AUDIO_INPUTS == 2) {
  260. for (int i = 0; i < NUM_AUDIO_INPUTS; i++) {
  261. vuMeter[i].reset();
  262. }
  263. }
  264. }
  265. // Pull outputs from buffer
  266. if (!port.engineOutputBuffer.empty()) {
  267. dsp::Frame<NUM_AUDIO_OUTPUTS> outputFrame = port.engineOutputBuffer.shift();
  268. for (int i = 0; i < NUM_AUDIO_OUTPUTS; i++) {
  269. float v = outputFrame.samples[i];
  270. outputs[AUDIO_OUTPUTS + i].setVoltage(10.f * v);
  271. // Detect clipping
  272. if (NUM_AUDIO_OUTPUTS > 2) {
  273. if (std::fabs(v) >= 1.f)
  274. outputClipTimers[i / 2] = clipTime;
  275. }
  276. }
  277. }
  278. else {
  279. // Zero outputs
  280. for (int i = 0; i < NUM_AUDIO_OUTPUTS; i++) {
  281. outputs[AUDIO_OUTPUTS + i].setVoltage(0.f);
  282. }
  283. }
  284. // Lights
  285. if (lightDivider.process()) {
  286. float lightTime = args.sampleTime * lightDivider.getDivision();
  287. // Audio-2: VU meter
  288. if (NUM_AUDIO_INPUTS == 2) {
  289. for (int i = 0; i < NUM_AUDIO_INPUTS; i++) {
  290. lights[VU_LIGHTS + i * 6 + 0].setBrightness(vuMeter[i].getBrightness(-3, 0));
  291. lights[VU_LIGHTS + i * 6 + 1].setBrightness(vuMeter[i].getBrightness(-6, -3));
  292. lights[VU_LIGHTS + i * 6 + 2].setBrightness(vuMeter[i].getBrightness(-12, -6));
  293. lights[VU_LIGHTS + i * 6 + 3].setBrightness(vuMeter[i].getBrightness(-24, -12));
  294. lights[VU_LIGHTS + i * 6 + 4].setBrightness(vuMeter[i].getBrightness(-36, -24));
  295. lights[VU_LIGHTS + i * 6 + 5].setBrightness(vuMeter[i].getBrightness(-48, -36));
  296. }
  297. }
  298. // Audio-8 and Audio-16: pair state lights
  299. else {
  300. // Turn on light if at least one port is enabled in the nearby pair.
  301. for (int i = 0; i < NUM_AUDIO_INPUTS / 2; i++) {
  302. bool active = port.deviceNumOutputs >= 2 * i + 1;
  303. bool clip = inputClipTimers[i] > 0.f;
  304. if (clip)
  305. inputClipTimers[i] -= lightTime;
  306. lights[INPUT_LIGHTS + i * 2 + 0].setBrightness(active && !clip);
  307. lights[INPUT_LIGHTS + i * 2 + 1].setBrightness(active && clip);
  308. }
  309. for (int i = 0; i < NUM_AUDIO_OUTPUTS / 2; i++) {
  310. bool active = port.deviceNumInputs >= 2 * i + 1;
  311. bool clip = outputClipTimers[i] > 0.f;
  312. if (clip)
  313. outputClipTimers[i] -= lightTime;
  314. lights[OUTPUT_LIGHTS + i * 2 + 0].setBrightness(active & !clip);
  315. lights[OUTPUT_LIGHTS + i * 2 + 1].setBrightness(active & clip);
  316. }
  317. }
  318. }
  319. }
  320. json_t* dataToJson() override {
  321. json_t* rootJ = json_object();
  322. json_object_set_new(rootJ, "audio", port.toJson());
  323. json_object_set_new(rootJ, "dcFilter", json_boolean(dcFilterEnabled));
  324. return rootJ;
  325. }
  326. void dataFromJson(json_t* rootJ) override {
  327. json_t* audioJ = json_object_get(rootJ, "audio");
  328. if (audioJ)
  329. port.fromJson(audioJ);
  330. json_t* dcFilterJ = json_object_get(rootJ, "dcFilter");
  331. if (dcFilterJ)
  332. dcFilterEnabled = json_boolean_value(dcFilterJ);
  333. }
  334. };
  335. /** For Audio-2 module. */
  336. struct Audio2Display : LedDisplay {
  337. AudioDeviceMenuChoice* deviceChoice;
  338. LedDisplaySeparator* deviceSeparator;
  339. void setAudioPort(audio::Port* port) {
  340. math::Vec pos;
  341. deviceChoice = createWidget<AudioDeviceMenuChoice>(math::Vec());
  342. deviceChoice->box.size.x = box.size.x;
  343. deviceChoice->port = port;
  344. addChild(deviceChoice);
  345. pos = deviceChoice->box.getBottomLeft();
  346. deviceSeparator = createWidget<LedDisplaySeparator>(pos);
  347. deviceSeparator->box.size.x = box.size.x;
  348. addChild(deviceSeparator);
  349. }
  350. void drawLayer(const DrawArgs& args, int layer) override {
  351. if (layer == 1) {
  352. static const std::vector<float> posY = {
  353. mm2px(28.899 - 13.039),
  354. mm2px(34.196 - 13.039),
  355. mm2px(39.494 - 13.039),
  356. mm2px(44.791 - 13.039),
  357. mm2px(50.089 - 13.039),
  358. mm2px(55.386 - 13.039),
  359. };
  360. static const std::vector<std::string> texts = {
  361. " 0", "-3", "-6", "-12", "-24", "-36",
  362. };
  363. std::string fontPath = asset::system("res/fonts/Nunito-Bold.ttf");
  364. std::shared_ptr<Font> font = APP->window->loadFont(fontPath);
  365. if (!font)
  366. return;
  367. nvgSave(args.vg);
  368. nvgFontFaceId(args.vg, font->handle);
  369. nvgFontSize(args.vg, 11);
  370. nvgTextLetterSpacing(args.vg, 0.0);
  371. nvgTextAlign(args.vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
  372. nvgFillColor(args.vg, nvgRGB(99, 99, 99));
  373. for (int i = 0; i < 6; i++) {
  374. nvgText(args.vg, 36.0, posY[i], texts[i].c_str(), NULL);
  375. }
  376. nvgRestore(args.vg);
  377. }
  378. LedDisplay::drawLayer(args, layer);
  379. }
  380. };
  381. template <int NUM_AUDIO_INPUTS, int NUM_AUDIO_OUTPUTS>
  382. struct AudioWidget : ModuleWidget {
  383. typedef Audio<NUM_AUDIO_INPUTS, NUM_AUDIO_OUTPUTS> TAudio;
  384. AudioWidget(TAudio* module) {
  385. setModule(module);
  386. if (NUM_AUDIO_INPUTS == 8 && NUM_AUDIO_OUTPUTS == 8) {
  387. setPanel(Svg::load(asset::system("res/Core/Audio8.svg")));
  388. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  389. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  390. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  391. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  392. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.81, 57.929)), module, TAudio::AUDIO_INPUTS + 0));
  393. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(19.359, 57.929)), module, TAudio::AUDIO_INPUTS + 1));
  394. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(30.909, 57.929)), module, TAudio::AUDIO_INPUTS + 2));
  395. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(42.459, 57.929)), module, TAudio::AUDIO_INPUTS + 3));
  396. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.81, 74.286)), module, TAudio::AUDIO_INPUTS + 4));
  397. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(19.359, 74.286)), module, TAudio::AUDIO_INPUTS + 5));
  398. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(30.909, 74.286)), module, TAudio::AUDIO_INPUTS + 6));
  399. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(42.459, 74.286)), module, TAudio::AUDIO_INPUTS + 7));
  400. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.81, 96.859)), module, TAudio::AUDIO_OUTPUTS + 0));
  401. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(19.359, 96.859)), module, TAudio::AUDIO_OUTPUTS + 1));
  402. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(30.909, 96.859)), module, TAudio::AUDIO_OUTPUTS + 2));
  403. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(42.459, 96.859)), module, TAudio::AUDIO_OUTPUTS + 3));
  404. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.81, 113.115)), module, TAudio::AUDIO_OUTPUTS + 4));
  405. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(19.359, 113.115)), module, TAudio::AUDIO_OUTPUTS + 5));
  406. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(30.909, 113.115)), module, TAudio::AUDIO_OUTPUTS + 6));
  407. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(42.459, 113.115)), module, TAudio::AUDIO_OUTPUTS + 7));
  408. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(13.54, 52.168)), module, TAudio::INPUT_LIGHTS + 2 * 0));
  409. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(36.774, 52.168)), module, TAudio::INPUT_LIGHTS + 2 * 1));
  410. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(13.54, 68.53)), module, TAudio::INPUT_LIGHTS + 2 * 2));
  411. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(36.774, 68.53)), module, TAudio::INPUT_LIGHTS + 2 * 3));
  412. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(13.54, 90.791)), module, TAudio::OUTPUT_LIGHTS + 2 * 0));
  413. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(36.638, 90.791)), module, TAudio::OUTPUT_LIGHTS + 2 * 1));
  414. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(13.54, 107.097)), module, TAudio::OUTPUT_LIGHTS + 2 * 2));
  415. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(36.638, 107.097)), module, TAudio::OUTPUT_LIGHTS + 2 * 3));
  416. AudioDisplay* display = createWidget<AudioDisplay>(mm2px(Vec(0.0, 13.039)));
  417. display->box.size = mm2px(Vec(50.8, 29.021));
  418. display->setAudioPort(module ? &module->port : NULL);
  419. addChild(display);
  420. }
  421. else if (NUM_AUDIO_INPUTS == 16 && NUM_AUDIO_OUTPUTS == 16) {
  422. setPanel(Svg::load(asset::system("res/Core/Audio16.svg")));
  423. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  424. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  425. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  426. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  427. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.815, 57.929)), module, TAudio::AUDIO_INPUTS + 0));
  428. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(19.364, 57.929)), module, TAudio::AUDIO_INPUTS + 1));
  429. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(30.914, 57.929)), module, TAudio::AUDIO_INPUTS + 2));
  430. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(42.464, 57.929)), module, TAudio::AUDIO_INPUTS + 3));
  431. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(54.015, 57.929)), module, TAudio::AUDIO_INPUTS + 4));
  432. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(65.565, 57.914)), module, TAudio::AUDIO_INPUTS + 5));
  433. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(77.114, 57.914)), module, TAudio::AUDIO_INPUTS + 6));
  434. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(88.664, 57.914)), module, TAudio::AUDIO_INPUTS + 7));
  435. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.815, 74.276)), module, TAudio::AUDIO_INPUTS + 8));
  436. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(19.364, 74.276)), module, TAudio::AUDIO_INPUTS + 9));
  437. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(30.914, 74.276)), module, TAudio::AUDIO_INPUTS + 10));
  438. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(42.464, 74.276)), module, TAudio::AUDIO_INPUTS + 11));
  439. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(54.015, 74.291)), module, TAudio::AUDIO_INPUTS + 12));
  440. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(65.565, 74.276)), module, TAudio::AUDIO_INPUTS + 13));
  441. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(77.114, 74.276)), module, TAudio::AUDIO_INPUTS + 14));
  442. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(88.664, 74.276)), module, TAudio::AUDIO_INPUTS + 15));
  443. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.815, 96.859)), module, TAudio::AUDIO_OUTPUTS + 0));
  444. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(19.364, 96.859)), module, TAudio::AUDIO_OUTPUTS + 1));
  445. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(30.914, 96.859)), module, TAudio::AUDIO_OUTPUTS + 2));
  446. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(42.464, 96.859)), module, TAudio::AUDIO_OUTPUTS + 3));
  447. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(54.015, 96.859)), module, TAudio::AUDIO_OUTPUTS + 4));
  448. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(65.565, 96.859)), module, TAudio::AUDIO_OUTPUTS + 5));
  449. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(77.114, 96.859)), module, TAudio::AUDIO_OUTPUTS + 6));
  450. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(88.664, 96.859)), module, TAudio::AUDIO_OUTPUTS + 7));
  451. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.815, 113.115)), module, TAudio::AUDIO_OUTPUTS + 8));
  452. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(19.364, 113.115)), module, TAudio::AUDIO_OUTPUTS + 9));
  453. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(30.914, 113.115)), module, TAudio::AUDIO_OUTPUTS + 10));
  454. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(42.464, 113.115)), module, TAudio::AUDIO_OUTPUTS + 11));
  455. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(54.015, 113.115)), module, TAudio::AUDIO_OUTPUTS + 12));
  456. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(65.565, 113.115)), module, TAudio::AUDIO_OUTPUTS + 13));
  457. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(77.114, 113.115)), module, TAudio::AUDIO_OUTPUTS + 14));
  458. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(88.664, 113.115)), module, TAudio::AUDIO_OUTPUTS + 15));
  459. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(13.545, 52.168)), module, TAudio::INPUT_LIGHTS + 2 * 0));
  460. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(36.779, 52.168)), module, TAudio::INPUT_LIGHTS + 2 * 1));
  461. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(59.745, 52.168)), module, TAudio::INPUT_LIGHTS + 2 * 2));
  462. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(82.98, 52.168)), module, TAudio::INPUT_LIGHTS + 2 * 3));
  463. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(13.545, 68.53)), module, TAudio::INPUT_LIGHTS + 2 * 4));
  464. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(36.779, 68.53)), module, TAudio::INPUT_LIGHTS + 2 * 5));
  465. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(59.745, 68.53)), module, TAudio::INPUT_LIGHTS + 2 * 6));
  466. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(82.98, 68.53)), module, TAudio::INPUT_LIGHTS + 2 * 7));
  467. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(13.545, 90.791)), module, TAudio::OUTPUT_LIGHTS + 2 * 0));
  468. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(36.644, 90.791)), module, TAudio::OUTPUT_LIGHTS + 2 * 1));
  469. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(59.745, 90.791)), module, TAudio::OUTPUT_LIGHTS + 2 * 2));
  470. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(82.844, 90.791)), module, TAudio::OUTPUT_LIGHTS + 2 * 3));
  471. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(13.545, 107.097)), module, TAudio::OUTPUT_LIGHTS + 2 * 4));
  472. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(36.644, 107.097)), module, TAudio::OUTPUT_LIGHTS + 2 * 5));
  473. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(59.745, 107.097)), module, TAudio::OUTPUT_LIGHTS + 2 * 6));
  474. addChild(createLightCentered<SmallLight<GreenRedLight>>(mm2px(Vec(82.844, 107.097)), module, TAudio::OUTPUT_LIGHTS + 2 * 7));
  475. AudioDisplay* display = createWidget<AudioDisplay>(mm2px(Vec(0.0, 13.039)));
  476. display->box.size = mm2px(Vec(96.52, 29.021));
  477. display->setAudioPort(module ? &module->port : NULL);
  478. addChild(display);
  479. }
  480. else if (NUM_AUDIO_INPUTS == 2 && NUM_AUDIO_OUTPUTS == 2) {
  481. setPanel(Svg::load(asset::system("res/Core/Audio2.svg")));
  482. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  483. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  484. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  485. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  486. addParam(createParamCentered<RoundLargeBlackKnob>(mm2px(Vec(12.869, 77.362)), module, TAudio::LEVEL_PARAM));
  487. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.285, 96.859)), module, TAudio::AUDIO_INPUTS + 0));
  488. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(18.122, 96.859)), module, TAudio::AUDIO_INPUTS + 1));
  489. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.285, 113.115)), module, TAudio::AUDIO_OUTPUTS + 0));
  490. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(18.122, 113.115)), module, TAudio::AUDIO_OUTPUTS + 1));
  491. Audio2Display* display = createWidget<Audio2Display>(mm2px(Vec(0.0, 13.039)));
  492. display->box.size = mm2px(Vec(25.4, 47.726));
  493. display->setAudioPort(module ? &module->port : NULL);
  494. addChild(display);
  495. addChild(createLightCentered<SmallSimpleLight<RedLight>>(mm2px(Vec(6.691, 28.899)), module, TAudio::VU_LIGHTS + 6 * 0 + 0));
  496. addChild(createLightCentered<SmallSimpleLight<RedLight>>(mm2px(Vec(18.709, 28.899)), module, TAudio::VU_LIGHTS + 6 * 1 + 0));
  497. addChild(createLightCentered<SmallSimpleLight<YellowLight>>(mm2px(Vec(6.691, 34.196)), module, TAudio::VU_LIGHTS + 6 * 0 + 1));
  498. addChild(createLightCentered<SmallSimpleLight<YellowLight>>(mm2px(Vec(18.709, 34.196)), module, TAudio::VU_LIGHTS + 6 * 1 + 1));
  499. addChild(createLightCentered<SmallSimpleLight<GreenLight>>(mm2px(Vec(6.691, 39.494)), module, TAudio::VU_LIGHTS + 6 * 0 + 2));
  500. addChild(createLightCentered<SmallSimpleLight<GreenLight>>(mm2px(Vec(18.709, 39.494)), module, TAudio::VU_LIGHTS + 6 * 1 + 2));
  501. addChild(createLightCentered<SmallSimpleLight<GreenLight>>(mm2px(Vec(6.691, 44.791)), module, TAudio::VU_LIGHTS + 6 * 0 + 3));
  502. addChild(createLightCentered<SmallSimpleLight<GreenLight>>(mm2px(Vec(18.709, 44.791)), module, TAudio::VU_LIGHTS + 6 * 1 + 3));
  503. addChild(createLightCentered<SmallSimpleLight<GreenLight>>(mm2px(Vec(6.691, 50.089)), module, TAudio::VU_LIGHTS + 6 * 0 + 4));
  504. addChild(createLightCentered<SmallSimpleLight<GreenLight>>(mm2px(Vec(18.709, 50.089)), module, TAudio::VU_LIGHTS + 6 * 1 + 4));
  505. addChild(createLightCentered<SmallSimpleLight<GreenLight>>(mm2px(Vec(6.691, 55.386)), module, TAudio::VU_LIGHTS + 6 * 0 + 5));
  506. addChild(createLightCentered<SmallSimpleLight<GreenLight>>(mm2px(Vec(18.709, 55.386)), module, TAudio::VU_LIGHTS + 6 * 1 + 5));
  507. // AudioButton example
  508. // AudioButton* audioButton_ADAT = createWidget<AudioButton_ADAT>(Vec(0, 0));
  509. // audioButton_ADAT->setAudioPort(module ? &module->port : NULL);
  510. // addChild(audioButton_ADAT);
  511. // AudioButton* audioButton_USB_B = createWidget<AudioButton_USB_B>(Vec(0, 40));
  512. // audioButton_USB_B->setAudioPort(module ? &module->port : NULL);
  513. // addChild(audioButton_USB_B);
  514. }
  515. }
  516. void appendContextMenu(Menu* menu) override {
  517. TAudio* module = dynamic_cast<TAudio*>(this->module);
  518. menu->addChild(new MenuSeparator);
  519. menu->addChild(createBoolMenuItem("Master audio module", "",
  520. [=]() {return module->port.isMaster();},
  521. [=](bool master) {module->port.setMaster(master);}
  522. ));
  523. menu->addChild(createBoolPtrMenuItem("DC blocker", "", &module->dcFilterEnabled));
  524. }
  525. };
  526. Model* modelAudio2 = createModel<Audio<2, 2>, AudioWidget<2, 2>>("AudioInterface2");
  527. Model* modelAudio8 = createModel<Audio<8, 8>, AudioWidget<8, 8>>("AudioInterface");
  528. Model* modelAudio16 = createModel<Audio<16, 16>, AudioWidget<16, 16>>("AudioInterface16");
  529. } // namespace core
  530. } // namespace rack