VoiceShift.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "core/VoiceShift.h"
  2. #include <algorithm>
  3. #include <atomic>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <vector>
  7. #include "core/Effects.h"
  8. #include "core/MixSuite.h"
  9. #include "core/VocalPitch.h"
  10. namespace lynx::voiceshift
  11. {
  12. double pitchSemitones (float v) noexcept { return -12.0 + 24.0 * (double) std::clamp (v, 0.0f, 1.0f); }
  13. double formantSemitones (float v) noexcept { return pitchSemitones (v); }
  14. float semitoneValue (double st) noexcept { return (float) std::clamp ((st + 12.0) / 24.0, 0.0, 1.0); }
  15. Mode modeOf (float v) noexcept { return (Mode) std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * 2.0f), 0, 2); }
  16. double driveAmount (float v) noexcept { return 10.0 * (double) std::clamp (v, 0.0f, 1.0f); }
  17. int midiChannelIndex (float v) noexcept
  18. {
  19. const auto k = (int) std::lround (std::clamp (v, 0.0f, 1.0f) * 128.0f);
  20. return k <= 0 ? -1 : k - 1;
  21. }
  22. float midiChannelValue (int channel) noexcept { return channel < 0 ? 0.0f : (float) std::clamp (channel + 1, 0, 128) / 128.0f; }
  23. int latencyFor (double sampleRate) noexcept { return (int) std::ceil (2.0 * sampleRate / minHz) + 4; }
  24. // [EXEC trial, 1 kHz at −18 dBFS] the drive raises the level by ≈ 1.75 dB per step up to ~5, then saturates
  25. // (H2 ≈ −30 dBc at 5, H3 takes over near 10) and the stage pulls the level back above ~6. A tube curve (Ember's biased
  26. // tanh: even and odd harmonics) after a pre-gain of 1.75 dB per step, with that make-down; 0 = clean.
  27. float driveCurve (float x, double amount) noexcept
  28. {
  29. if (amount <= 0.0)
  30. return x;
  31. const auto pre = (float) std::pow (10.0, 1.75 * amount / 20.0);
  32. const auto down = (float) std::pow (10.0, -1.5 * std::max (0.0, amount - 6.0) / 20.0);
  33. // crossfade from clean over the first step so that 0 → 0.1 does not jump
  34. const auto shaped = 0.5f * mixsuite::ember::shape (mixsuite::ember::Style::tube, 2.0f * pre * x) * down;
  35. const auto w = (float) std::min (1.0, amount);
  36. return x + w * (shaped - x);
  37. }
  38. const EffectInfo& info()
  39. {
  40. static const EffectInfo i { "Voice Shift",
  41. { { "Pitch", 0.5f }, { "Formant", 0.5f }, { "Link", 0.0f }, { "Mode", 0.0f }, { "Drive", 0.0f }, { "Mix", 1.0f },
  42. { "MIDI", 0.0f } } };
  43. return i;
  44. }
  45. std::string paramText (int param, float v)
  46. {
  47. char t[32];
  48. switch (param)
  49. {
  50. case pitch:
  51. case formant: std::snprintf (t, sizeof t, "%+.1f st", pitchSemitones (v)); return t;
  52. case link: return v >= 0.5f ? "On" : "Off";
  53. case mode:
  54. {
  55. static const char* const n[3] { "Transpose", "Quantize", "Robot" };
  56. return n[(int) modeOf (v)];
  57. }
  58. case drive: std::snprintf (t, sizeof t, "%.1f", driveAmount (v)); return t;
  59. case mix: std::snprintf (t, sizeof t, "%.0f%%", 100.0 * std::clamp (v, 0.0f, 1.0f)); return t;
  60. case midi:
  61. {
  62. const auto c = midiChannelIndex (v);
  63. if (c < 0)
  64. return "Off";
  65. std::snprintf (t, sizeof t, "Channel %d", c + 1);
  66. return t;
  67. }
  68. default: std::snprintf (t, sizeof t, "%.2f", (double) v); return t;
  69. }
  70. }
  71. namespace
  72. {
  73. class Shifter final : public Effect
  74. {
  75. public:
  76. Shifter() : dry ((std::size_t) (2 * dryRing), 0.0f) {}
  77. void prepare (double sampleRate) noexcept override
  78. {
  79. sr = std::clamp (sampleRate > 0.0 ? sampleRate : 44100.0, 8000.0, 192000.0);
  80. hop = std::max (16, (int) std::lround (128.0 * sr / 44100.0));
  81. det.prepare (sr, minHz, maxHz, hop);
  82. det.setThreshold (0.15f);
  83. latency = std::min (latencyFor (sr), dryRing - 1);
  84. psola.prepare (sr, sr / minHz, latency);
  85. psola.setMarkLag (det.windowCentre());
  86. dcA = (float) std::exp (-2.0 * 3.14159265358979323846 * 10.0 / sr);
  87. reset();
  88. }
  89. void reset() noexcept override
  90. {
  91. det.reset();
  92. psola.reset();
  93. std::fill (dry.begin(), dry.end(), 0.0f);
  94. dryPos = 0;
  95. sung = -1.0;
  96. wanted = shift = 0.0;
  97. unvoicedFor = 1e9;
  98. target = -1;
  99. dcX[0] = dcX[1] = dcY[0] = dcY[1] = 0.0f;
  100. }
  101. void setHeldNotes (const NoteMask* channels, int count) noexcept override
  102. {
  103. held = channels;
  104. heldCount = count;
  105. }
  106. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  107. {
  108. // round 137: the highest note held on the MIDI channel replaces the Pitch knob (relative to C4) / the robot note
  109. int midiNote = -1;
  110. if (const auto ch = midiChannelIndex (p[midi]); ch >= 0 && held != nullptr && ch < heldCount)
  111. for (int k = 127; k >= 0 && midiNote < 0; --k)
  112. if ((held[ch][(std::size_t) (k >> 6)] >> (k & 63)) & 1u)
  113. midiNote = k;
  114. meterMidi.store (midiNote < 0 ? 0.0f : (float) midiNote, std::memory_order_relaxed);
  115. const auto robotAt = midiNote >= 0 ? (double) midiNote : robotNote;
  116. const auto pitchSt = midiNote >= 0 ? (modeOf (p[mode]) == Mode::robot ? 0.0 : std::clamp ((double) midiNote - robotNote, -24.0, 24.0))
  117. : pitchSemitones (p[pitch]);
  118. const auto formantSt = formantSemitones (p[formant]);
  119. const auto linked = p[link] >= 0.5f;
  120. const auto md = modeOf (p[mode]);
  121. const auto amount = driveAmount (p[drive]);
  122. const auto wet = std::clamp (p[mix], 0.0f, 1.0f);
  123. const auto stereo = r != nullptr && r != l;
  124. // Quantize: "as quickly as possible" — a 2 ms glide between notes, only to avoid clicks
  125. const auto glide = 1.0 - std::exp (-1.0 / (0.002 * sr));
  126. const auto fShift = std::exp2 (formantSt / 12.0);
  127. for (int i = 0; i < n; ++i)
  128. {
  129. auto clean = [] (float v) { return std::isfinite (v) ? v : 0.0f; };
  130. const float xin[2] { clean (l[i]), clean (stereo ? r[i] : l[i]) };
  131. const auto mid = stereo ? 0.5f * (xin[0] + xin[1]) : xin[0];
  132. if (det.push (mid))
  133. {
  134. const auto& e = det.estimate();
  135. if (e.voiced && e.hz > 0.0)
  136. {
  137. psola.setPitch (e.period, true);
  138. sung = vocal::hzToNote (e.hz);
  139. unvoicedFor = 0.0;
  140. if (md == Mode::quantize)
  141. {
  142. target = vocal::nearestNote (sung, 0xfff, target, 0.03);
  143. wanted = (double) target - sung;
  144. }
  145. else if (md == Mode::robot)
  146. {
  147. target = (int) robotAt;
  148. wanted = robotAt - sung;
  149. }
  150. else
  151. {
  152. target = -1;
  153. wanted = 0.0;
  154. }
  155. }
  156. else
  157. {
  158. psola.setPitch (0.0, false);
  159. unvoicedFor += (double) hop / sr;
  160. if (unvoicedFor > 0.06)
  161. {
  162. sung = -1.0;
  163. target = -1;
  164. }
  165. }
  166. }
  167. shift += (md == Mode::transpose ? 1.0 : glide) * (wanted - shift);
  168. const auto semis = std::clamp (shift + pitchSt, -24.0, 24.0);
  169. const auto ratio = std::exp2 (semis / 12.0);
  170. psola.setShift (ratio, linked ? ratio * fShift : fShift);
  171. float y[2];
  172. psola.process (xin, y, stereo ? 2 : 1);
  173. if (amount > 0.0)
  174. for (int c = 0; c < (stereo ? 2 : 1); ++c)
  175. {
  176. // the tube's asymmetry leaves a DC offset: a 10 Hz high-pass after it
  177. const auto s = driveCurve (y[c], amount);
  178. dcY[c] = s - dcX[c] + dcA * dcY[c];
  179. dcX[c] = s;
  180. y[c] = dcY[c];
  181. }
  182. const auto w = (std::size_t) (dryPos & (dryRing - 1));
  183. const auto rd = (std::size_t) ((dryPos - latency) & (dryRing - 1));
  184. dry[w] = xin[0];
  185. dry[w + dryRing] = xin[1];
  186. const auto d0 = dry[rd], d1 = dry[rd + dryRing];
  187. ++dryPos;
  188. l[i] = d0 + wet * (y[0] - d0);
  189. if (stereo)
  190. r[i] = d1 + wet * (y[1] - d1);
  191. }
  192. meterSung.store ((float) sung, std::memory_order_relaxed);
  193. meterOut.store (sung > 0.0 ? (float) (sung + shift + pitchSt) : -1.0f, std::memory_order_relaxed);
  194. }
  195. float controlOutput (int o) const noexcept override
  196. {
  197. const auto v = (o == 0 ? meterSung : o == 2 ? meterMidi : meterOut).load (std::memory_order_relaxed);
  198. return std::clamp (v, 0.0f, 127.0f) / 127.0f;
  199. }
  200. int latencySamples() const noexcept override { return latency; }
  201. private:
  202. static constexpr int dryRing = 16384;
  203. double sr = 44100.0;
  204. int hop = 128, latency = 1264;
  205. vocal::PitchDetector det;
  206. vocal::Psola psola;
  207. std::vector<float> dry;
  208. std::int64_t dryPos = 0;
  209. double sung = -1.0, wanted = 0.0, shift = 0.0, unvoicedFor = 1e9;
  210. int target = -1;
  211. float dcA = 0.999f, dcX[2] {}, dcY[2] {};
  212. std::atomic<float> meterSung { -1.0f }, meterOut { -1.0f }, meterMidi { 0.0f };
  213. const NoteMask* held = nullptr;
  214. int heldCount = 0;
  215. };
  216. } // namespace
  217. std::unique_ptr<Effect> make()
  218. {
  219. auto e = std::make_unique<Shifter>();
  220. e->prepare (44100.0);
  221. return e;
  222. }
  223. } // namespace lynx::voiceshift