LiveInput.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. // External audio input through the mixer (REA_rodada88): the sources an insert can take its input from, when the
  3. // input is monitored, and the block of input the audio thread hands to the renderer.
  4. #include <algorithm>
  5. #include <array>
  6. #include <atomic>
  7. #include <string>
  8. #include <vector>
  9. #include "core/Meters.h"
  10. #include "core/Song.h"
  11. namespace lynx
  12. {
  13. // One choice of an insert's "Audio input source": a stereo pair of device input channels, or one channel.
  14. struct InputSource
  15. {
  16. int channel = -1; // first device input channel (0-based)
  17. bool stereo = false;
  18. std::string name; // "In 1 - In 2" style, from the device's channel names
  19. };
  20. // The choices for a device whose input channels are called `channelNames` (in device order): the stereo pairs
  21. // (1-2, 3-4, ...) first, then every channel alone, as FL lists them. A single-channel device gives one mono entry.
  22. inline std::vector<InputSource> inputSources (const std::vector<std::string>& channelNames)
  23. {
  24. std::vector<InputSource> out;
  25. const auto count = (int) channelNames.size();
  26. for (int c = 0; c + 1 < count; c += 2)
  27. out.push_back ({ c, true, channelNames[(std::size_t) c] + " - " + channelNames[(std::size_t) c + 1] });
  28. for (int c = 0; c < count; ++c)
  29. out.push_back ({ c, false, channelNames[(std::size_t) c] });
  30. return out;
  31. }
  32. // The name of an insert's input (empty when it has none). A channel the device doesn't have (another device
  33. // is open now) still gets a name, numbered from 1.
  34. inline std::string inputSourceName (int channel, bool stereo, const std::vector<std::string>& channelNames)
  35. {
  36. if (channel < 0)
  37. return {};
  38. auto nameOf = [&] (int c) {
  39. return c < (int) channelNames.size() ? channelNames[(std::size_t) c] : "In " + std::to_string (c + 1);
  40. };
  41. return stereo ? nameOf (channel) + " - " + nameOf (channel + 1) : nameOf (channel);
  42. }
  43. // Whether an insert hears its external input now.
  44. inline bool inputMonitored (InputMonitor mode, bool armed, bool playing, bool recording) noexcept
  45. {
  46. switch (mode)
  47. {
  48. case InputMonitor::off: return false;
  49. case InputMonitor::whenArmed: return armed;
  50. case InputMonitor::automatic: return armed && (recording || ! playing);
  51. case InputMonitor::on: return true;
  52. }
  53. return false;
  54. }
  55. // Device input channels an insert's source uses: { first, count } (count 0 = none or not available).
  56. inline std::pair<int, int> inputChannelsOf (const MixerTrack& track, int deviceChannels) noexcept
  57. {
  58. if (track.inputChannel < 0 || track.inputChannel >= deviceChannels)
  59. return { 0, 0 };
  60. return { track.inputChannel, track.inputStereo && track.inputChannel + 1 < deviceChannels ? 2 : 1 };
  61. }
  62. // Shared between the audio callback (writes the block's input pointers, then calls the renderer on the same
  63. // thread), the renderer (reads them) and the UI (arm switches, input meters).
  64. struct LiveInput
  65. {
  66. static constexpr int maxChannels = 64;
  67. // audio thread, valid only during one render call
  68. std::array<const float*, maxChannels> channels {};
  69. int numChannels = 0;
  70. // UI -> audio
  71. std::array<std::atomic<bool>, maxMixerTracks> armed {};
  72. std::atomic<bool> recording { false };
  73. // audio -> UI: peak of each insert's input source, monitored or not
  74. MeterBank meters;
  75. const float* channel (int c, int offset) const noexcept
  76. {
  77. return c >= 0 && c < numChannels && channels[(std::size_t) c] != nullptr ? channels[(std::size_t) c] + offset : nullptr;
  78. }
  79. };
  80. } // namespace lynx