Audio plugin host https://kx.studio/carla
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.

348 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2008 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2008 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU Lesser General Public License, as published by the Free Software
  9. Foundation; either version 2 of the License, or (at your option) any later
  10. version.
  11. JUCE and JUCETICE are distributed in the hope that they will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  17. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. Boston, MA 02111-1307 USA
  19. ==============================================================================
  20. @author rockhardbuns
  21. @tweaker Lucio Asnaghi
  22. @tweaker falkTX
  23. ==============================================================================
  24. */
  25. #ifndef DISTRHO_VEX_WAVE_RENDERER_HEADER_INCLUDED
  26. #define DISTRHO_VEX_WAVE_RENDERER_HEADER_INCLUDED
  27. #ifndef CARLA_EXPORT
  28. #define CARLA_EXPORT
  29. #endif
  30. #ifdef CARLA_EXPORT
  31. #include "juce_audio_basics.h"
  32. #include "resources/Resources.h"
  33. #else
  34. #include "../StandardHeader.h"
  35. #include "resources/Resources.h"
  36. #endif
  37. struct OscSet {
  38. float phase;
  39. float phaseOffset;
  40. float phaseInc;
  41. float phaseIncOffset;
  42. float cut;
  43. float buf[4];
  44. };
  45. struct WaveTableNames {
  46. const char* name;
  47. const char* data;
  48. };
  49. class WaveRenderer
  50. {
  51. public:
  52. WaveRenderer()
  53. : cycle(256), //base pitch 86 Hz
  54. tableSize(0),
  55. daTable(nullptr),
  56. sWave("sine"),
  57. loadWave(true)
  58. {
  59. }
  60. ~WaveRenderer()
  61. {
  62. //M.setSize(0);
  63. daTable = nullptr;
  64. }
  65. const String& getCurrentWaveName() const
  66. {
  67. return sWave;
  68. }
  69. void setWaveLater(const String& waveName)
  70. {
  71. sWave = waveName;
  72. loadWave = true;
  73. }
  74. void actuallySetWave()
  75. {
  76. if (sWave == "asym_saw") {
  77. daTable = (uint16*) Wavetables::asym_saw;
  78. tableSize = Wavetables::asym_saw_size / 2;
  79. } else if (sWave == "bass_tone") {
  80. daTable = (uint16*) Wavetables::bass_tone;
  81. tableSize = Wavetables::bass_tone_size / 2;
  82. } else if (sWave == "buzz_1") {
  83. daTable = (uint16*) Wavetables::buzz_1;
  84. tableSize = Wavetables::buzz_1_size / 2;
  85. } else if (sWave == "buzz_2") {
  86. daTable = (uint16*) Wavetables::buzz_2;
  87. tableSize = Wavetables::buzz_2_size / 2;
  88. } else if (sWave == "dark_strings") {
  89. daTable = (uint16*) Wavetables::dark_strings;
  90. tableSize = Wavetables::dark_strings_size / 2;
  91. } else if (sWave == "deep_ring_1") {
  92. daTable = (uint16*) Wavetables::deep_ring_1;
  93. tableSize = Wavetables::deep_ring_1_size / 2;
  94. } else if (sWave == "deep_ring_2") {
  95. daTable = (uint16*) Wavetables::deep_ring_2;
  96. tableSize = Wavetables::deep_ring_2_size / 2;
  97. } else if (sWave == "epiano_tone") {
  98. daTable = (uint16*) Wavetables::epiano_tone;
  99. tableSize = Wavetables::epiano_tone_size / 2;
  100. } else if (sWave == "ghost_1") {
  101. daTable = (uint16*) Wavetables::ghost_1;
  102. tableSize = Wavetables::ghost_1_size / 2;
  103. } else if (sWave == "ghost_2") {
  104. daTable = (uint16*) Wavetables::ghost_2;
  105. tableSize = Wavetables::ghost_2_size / 2;
  106. } else if (sWave == "ghost_3") {
  107. daTable = (uint16*) Wavetables::ghost_3;
  108. tableSize = Wavetables::ghost_3_size / 2;
  109. } else if (sWave == "ghost_4") {
  110. daTable = (uint16*) Wavetables::ghost_4;
  111. tableSize = Wavetables::ghost_4_size / 2;
  112. } else if (sWave == "grind_1") {
  113. daTable = (uint16*) Wavetables::grind_1;
  114. tableSize = Wavetables::grind_1_size / 2;
  115. } else if (sWave == "grind_2") {
  116. daTable = (uint16*) Wavetables::grind_2;
  117. tableSize = Wavetables::grind_2_size / 2;
  118. } else if (sWave == "more_strings") {
  119. daTable = (uint16*) Wavetables::more_strings;
  120. tableSize = Wavetables::more_strings_size / 2;
  121. } else if (sWave == "multi_pulse") {
  122. daTable = (uint16*) Wavetables::multi_pulse;
  123. tableSize = Wavetables::multi_pulse_size / 2;
  124. } else if (sWave == "one_string") {
  125. daTable = (uint16*) Wavetables::one_string;
  126. tableSize = Wavetables::one_string_size / 2;
  127. } else if (sWave == "organ_1") {
  128. daTable = (uint16*) Wavetables::organ_1;
  129. tableSize = Wavetables::organ_1_size / 2;
  130. } else if (sWave == "organ_2") {
  131. daTable = (uint16*) Wavetables::organ_2;
  132. tableSize = Wavetables::organ_2_size / 2;
  133. } else if (sWave == "phasing_sqr") {
  134. daTable = (uint16*) Wavetables::phasing_sqr;
  135. tableSize = Wavetables::phasing_sqr_size / 2;
  136. } else if (sWave == "pulse") {
  137. daTable = (uint16*) Wavetables::pulse;
  138. tableSize = Wavetables::pulse_size / 2;
  139. } else if (sWave == "saw") {
  140. daTable = (uint16*) Wavetables::saw;
  141. tableSize = Wavetables::saw_size / 2;
  142. } else if (sWave == "sharp_1") {
  143. daTable = (uint16*) Wavetables::sharp_1;
  144. tableSize = Wavetables::sharp_1_size / 2;
  145. } else if (sWave == "sharp_2") {
  146. daTable = (uint16*) Wavetables::sharp_2;
  147. tableSize = Wavetables::sharp_2_size / 2;
  148. } else if (sWave == "sine") {
  149. daTable = (uint16*) Wavetables::sine;
  150. tableSize = Wavetables::sine_size / 2;
  151. } else if (sWave == "soft_1") {
  152. daTable = (uint16*) Wavetables::soft_1;
  153. tableSize = Wavetables::soft_1_size / 2;
  154. } else if (sWave == "soft_2") {
  155. daTable = (uint16*) Wavetables::soft_2;
  156. tableSize = Wavetables::soft_2_size / 2;
  157. } else if (sWave == "soft_3") {
  158. daTable = (uint16*) Wavetables::soft_3;
  159. tableSize = Wavetables::soft_3_size / 2;
  160. } else if (sWave == "soft_4") {
  161. daTable = (uint16*) Wavetables::soft_4;
  162. tableSize = Wavetables::soft_4_size / 2;
  163. } else if (sWave == "square") {
  164. daTable = (uint16*) Wavetables::square;
  165. tableSize = Wavetables::square_size / 2;
  166. } else if (sWave == "strings_1") {
  167. daTable = (uint16*) Wavetables::strings_1;
  168. tableSize = Wavetables::strings_1_size / 2;
  169. } else if (sWave == "strings_2") {
  170. daTable = (uint16*) Wavetables::strings_2;
  171. tableSize = Wavetables::strings_2_size / 2;
  172. } else if (sWave == "string_fuzz") {
  173. daTable = (uint16*) Wavetables::string_fuzz;
  174. tableSize = Wavetables::string_fuzz_size / 2;
  175. } else if (sWave == "syn_choir_1") {
  176. daTable = (uint16*) Wavetables::syn_choir_1;
  177. tableSize = Wavetables::syn_choir_1_size / 2;
  178. } else if (sWave == "syn_choir_2") {
  179. daTable = (uint16*) Wavetables::syn_choir_2;
  180. tableSize = Wavetables::syn_choir_2_size / 2;
  181. } else if (sWave == "syn_choir_3") {
  182. daTable = (uint16*) Wavetables::syn_choir_3;
  183. tableSize = Wavetables::syn_choir_3_size / 2;
  184. } else if (sWave == "thin_1") {
  185. daTable = (uint16*) Wavetables::thin_1;
  186. tableSize = Wavetables::thin_1_size / 2;
  187. } else if (sWave == "thin_2") {
  188. daTable = (uint16*) Wavetables::thin_2;
  189. tableSize = Wavetables::thin_2_size / 2;
  190. } else if (sWave == "two_strings") {
  191. daTable = (uint16*) Wavetables::two_strings;
  192. tableSize = Wavetables::two_strings_size / 2;
  193. } else if (sWave == "voice_1") {
  194. daTable = (uint16*) Wavetables::voice_1;
  195. tableSize = Wavetables::voice_1_size / 2;
  196. } else if (sWave == "voice_2") {
  197. daTable = (uint16*) Wavetables::voice_2;
  198. tableSize = Wavetables::voice_2_size / 2;
  199. }
  200. loadWave = false;
  201. #if 0
  202. // TODO - this fails on linux (host directory not plugin one)
  203. // String waveName = (File::getSpecialLocation(File::currentExecutableFile)).getParentDirectory().getFullPathName();
  204. File location = (File::getSpecialLocation(File::userHomeDirectory)).getChildFile(".vex");
  205. if (! location.exists ())
  206. location.createDirectory ();
  207. String waveName;
  208. waveName << location.getFullPathName()
  209. << "/"
  210. << sWave
  211. << ".raw";
  212. DBG( waveName );
  213. File f(waveName);
  214. if (f.existsAsFile ())
  215. {
  216. tableSize = int(f.getSize() / 2);
  217. if (tableSize > 0)
  218. {
  219. M.setSize(0);
  220. f.loadFileAsData(M);
  221. daTable = (uint16*) M.getData();
  222. }
  223. }
  224. else
  225. {
  226. tableSize = int(Wavetables::sine_size / 2);
  227. if (tableSize > 0)
  228. {
  229. M.setSize (0);
  230. M.append (Wavetables::sine, Wavetables::sine_size);
  231. daTable = (uint16*) M.getData();
  232. }
  233. }
  234. loadWave = false;
  235. #endif
  236. }
  237. void reset(float f, double s, OscSet& o)
  238. {
  239. if (loadWave) actuallySetWave();
  240. s = s * 2.0;
  241. f = f * 2.0f * (1.0f + o.phaseIncOffset * 0.01f);
  242. o.cut = float(2.0 * double_Pi * 9000.0 / s);
  243. o.phase = o.phaseOffset * tableSize * 0.5f;
  244. o.phaseInc = float(cycle / (s/f));
  245. o.buf[0] = 0.0f;
  246. o.buf[1] = 0.0f;
  247. o.buf[2] = 0.0f;
  248. o.buf[3] = 0.0f;
  249. }
  250. void setFrequency(float f, double s, OscSet& o)
  251. {
  252. s = s * 2.0;
  253. f = f * 2.0f;
  254. o.phaseInc = float((float)cycle / (s/f));
  255. }
  256. void fillBuffer(float* buffer, int bufferSize, OscSet& o)
  257. {
  258. if (buffer == nullptr || bufferSize == 0)
  259. return;
  260. float tmp;
  261. for (int i = 0; i < bufferSize; ++i)
  262. {
  263. buffer[i] = 0.0f;
  264. int index = roundFloatToInt(o.phase - 0.5f);
  265. float alpha = o.phase - (float)index;
  266. const float conv = 1.0f / 65535.0f;
  267. float sIndex = daTable[index] * conv - 0.5f;
  268. float sIndexp1 = daTable[(index + 1) % tableSize] * conv - 0.5f;
  269. tmp = sIndex + alpha * (sIndexp1 - sIndex);
  270. o.buf[1] = ((tmp - o.buf[1]) * o.cut) + o.buf[1];
  271. o.buf[2] = ((o.buf[1] - o.buf[2]) * o.cut) + o.buf[2];
  272. o.buf[3] = ((o.buf[2] - o.buf[3]) * o.cut) + o.buf[3];
  273. o.buf[0] = ((o.buf[3] - o.buf[0]) * o.cut) + o.buf[0];
  274. tmp = o.buf[0];
  275. buffer[i] += tmp;
  276. o.phase += o.phaseInc;
  277. if (o.phase > (float)tableSize)
  278. o.phase -= (float)tableSize;
  279. index = roundFloatToInt(o.phase - 0.5f);
  280. alpha = o.phase - (float)index;
  281. sIndex = daTable[index] * conv - 0.5f;
  282. sIndexp1 = daTable[(index + 1) % tableSize] * conv - 0.5f;
  283. tmp = sIndex + alpha * (sIndexp1 - sIndex);
  284. o.buf[1] = ((tmp - o.buf[1]) * o.cut) + o.buf[1];
  285. o.buf[2] = ((o.buf[1] - o.buf[2]) * o.cut) + o.buf[2];
  286. o.buf[3] = ((o.buf[2] - o.buf[3]) * o.cut) + o.buf[3];
  287. o.buf[0] = ((o.buf[3] - o.buf[0]) * o.cut) + o.buf[0];
  288. tmp = o.buf[0];
  289. buffer[i] += tmp;
  290. o.phase += o.phaseInc;
  291. if (o.phase > (float)tableSize)
  292. o.phase -= (float)tableSize;
  293. }
  294. }
  295. static int getWaveTableSize();
  296. static String getWaveTableName(const int index);
  297. private:
  298. static const int kWaveTableSize = 41;
  299. static WaveTableNames waveTableNames[kWaveTableSize];
  300. int cycle, tableSize;
  301. uint16* daTable;
  302. //MemoryBlock M;
  303. String sWave;
  304. bool loadWave;
  305. };
  306. #endif // DISTRHO_VEX_WAVE_RENDERER_HEADER_INCLUDED