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.

343 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 "ResourceFile.h"
  33. #else
  34. #include "../StandardHeader.h"
  35. #include "../waverom/ResourceFile.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. void setWaveLater(const String& waveName)
  66. {
  67. sWave = waveName;
  68. loadWave = true;
  69. }
  70. void actuallySetWave()
  71. {
  72. if (sWave == "asym_saw") {
  73. daTable = (uint16*) Wavetables::asym_saw;
  74. tableSize = Wavetables::asym_saw_size / 2;
  75. } else if (sWave == "bass_tone") {
  76. daTable = (uint16*) Wavetables::bass_tone;
  77. tableSize = Wavetables::bass_tone_size / 2;
  78. } else if (sWave == "buzz_1") {
  79. daTable = (uint16*) Wavetables::buzz_1;
  80. tableSize = Wavetables::buzz_1_size / 2;
  81. } else if (sWave == "buzz_2") {
  82. daTable = (uint16*) Wavetables::buzz_2;
  83. tableSize = Wavetables::buzz_2_size / 2;
  84. } else if (sWave == "dark_strings") {
  85. daTable = (uint16*) Wavetables::dark_strings;
  86. tableSize = Wavetables::dark_strings_size / 2;
  87. } else if (sWave == "deep_ring_1") {
  88. daTable = (uint16*) Wavetables::deep_ring_1;
  89. tableSize = Wavetables::deep_ring_1_size / 2;
  90. } else if (sWave == "deep_ring_2") {
  91. daTable = (uint16*) Wavetables::deep_ring_2;
  92. tableSize = Wavetables::deep_ring_2_size / 2;
  93. } else if (sWave == "epiano_tone") {
  94. daTable = (uint16*) Wavetables::epiano_tone;
  95. tableSize = Wavetables::epiano_tone_size / 2;
  96. } else if (sWave == "ghost_1") {
  97. daTable = (uint16*) Wavetables::ghost_1;
  98. tableSize = Wavetables::ghost_1_size / 2;
  99. } else if (sWave == "ghost_2") {
  100. daTable = (uint16*) Wavetables::ghost_2;
  101. tableSize = Wavetables::ghost_2_size / 2;
  102. } else if (sWave == "ghost_3") {
  103. daTable = (uint16*) Wavetables::ghost_3;
  104. tableSize = Wavetables::ghost_3_size / 2;
  105. } else if (sWave == "ghost_4") {
  106. daTable = (uint16*) Wavetables::ghost_4;
  107. tableSize = Wavetables::ghost_4_size / 2;
  108. } else if (sWave == "grind_1") {
  109. daTable = (uint16*) Wavetables::grind_1;
  110. tableSize = Wavetables::grind_1_size / 2;
  111. } else if (sWave == "grind_2") {
  112. daTable = (uint16*) Wavetables::grind_2;
  113. tableSize = Wavetables::grind_2_size / 2;
  114. } else if (sWave == "more_strings") {
  115. daTable = (uint16*) Wavetables::more_strings;
  116. tableSize = Wavetables::more_strings_size / 2;
  117. } else if (sWave == "multi_pulse") {
  118. daTable = (uint16*) Wavetables::multi_pulse;
  119. tableSize = Wavetables::multi_pulse_size / 2;
  120. } else if (sWave == "one_string") {
  121. daTable = (uint16*) Wavetables::one_string;
  122. tableSize = Wavetables::one_string_size / 2;
  123. } else if (sWave == "organ_1") {
  124. daTable = (uint16*) Wavetables::organ_1;
  125. tableSize = Wavetables::organ_1_size / 2;
  126. } else if (sWave == "organ_2") {
  127. daTable = (uint16*) Wavetables::organ_2;
  128. tableSize = Wavetables::organ_2_size / 2;
  129. } else if (sWave == "phasing_sqr") {
  130. daTable = (uint16*) Wavetables::phasing_sqr;
  131. tableSize = Wavetables::phasing_sqr_size / 2;
  132. } else if (sWave == "pulse") {
  133. daTable = (uint16*) Wavetables::pulse;
  134. tableSize = Wavetables::pulse_size / 2;
  135. } else if (sWave == "saw") {
  136. daTable = (uint16*) Wavetables::saw;
  137. tableSize = Wavetables::saw_size / 2;
  138. } else if (sWave == "sharp_1") {
  139. daTable = (uint16*) Wavetables::sharp_1;
  140. tableSize = Wavetables::sharp_1_size / 2;
  141. } else if (sWave == "sharp_2") {
  142. daTable = (uint16*) Wavetables::sharp_2;
  143. tableSize = Wavetables::sharp_2_size / 2;
  144. } else if (sWave == "sine") {
  145. daTable = (uint16*) Wavetables::sine;
  146. tableSize = Wavetables::sine_size / 2;
  147. } else if (sWave == "soft_1") {
  148. daTable = (uint16*) Wavetables::soft_1;
  149. tableSize = Wavetables::soft_1_size / 2;
  150. } else if (sWave == "soft_2") {
  151. daTable = (uint16*) Wavetables::soft_2;
  152. tableSize = Wavetables::soft_2_size / 2;
  153. } else if (sWave == "soft_3") {
  154. daTable = (uint16*) Wavetables::soft_3;
  155. tableSize = Wavetables::soft_3_size / 2;
  156. } else if (sWave == "soft_4") {
  157. daTable = (uint16*) Wavetables::soft_4;
  158. tableSize = Wavetables::soft_4_size / 2;
  159. } else if (sWave == "square") {
  160. daTable = (uint16*) Wavetables::square;
  161. tableSize = Wavetables::square_size / 2;
  162. } else if (sWave == "strings_1") {
  163. daTable = (uint16*) Wavetables::strings_1;
  164. tableSize = Wavetables::strings_1_size / 2;
  165. } else if (sWave == "strings_2") {
  166. daTable = (uint16*) Wavetables::strings_2;
  167. tableSize = Wavetables::strings_2_size / 2;
  168. } else if (sWave == "string_fuzz") {
  169. daTable = (uint16*) Wavetables::string_fuzz;
  170. tableSize = Wavetables::string_fuzz_size / 2;
  171. } else if (sWave == "syn_choir_1") {
  172. daTable = (uint16*) Wavetables::syn_choir_1;
  173. tableSize = Wavetables::syn_choir_1_size / 2;
  174. } else if (sWave == "syn_choir_2") {
  175. daTable = (uint16*) Wavetables::syn_choir_2;
  176. tableSize = Wavetables::syn_choir_2_size / 2;
  177. } else if (sWave == "syn_choir_3") {
  178. daTable = (uint16*) Wavetables::syn_choir_3;
  179. tableSize = Wavetables::syn_choir_3_size / 2;
  180. } else if (sWave == "thin_1") {
  181. daTable = (uint16*) Wavetables::thin_1;
  182. tableSize = Wavetables::thin_1_size / 2;
  183. } else if (sWave == "thin_2") {
  184. daTable = (uint16*) Wavetables::thin_2;
  185. tableSize = Wavetables::thin_2_size / 2;
  186. } else if (sWave == "two_strings") {
  187. daTable = (uint16*) Wavetables::two_strings;
  188. tableSize = Wavetables::two_strings_size / 2;
  189. } else if (sWave == "voice_1") {
  190. daTable = (uint16*) Wavetables::voice_1;
  191. tableSize = Wavetables::voice_1_size / 2;
  192. } else if (sWave == "voice_2") {
  193. daTable = (uint16*) Wavetables::voice_2;
  194. tableSize = Wavetables::voice_2_size / 2;
  195. }
  196. loadWave = false;
  197. #if 0
  198. // TODO - this fails on linux (host directory not plugin one)
  199. // String waveName = (File::getSpecialLocation(File::currentExecutableFile)).getParentDirectory().getFullPathName();
  200. File location = (File::getSpecialLocation(File::userHomeDirectory)).getChildFile(".vex");
  201. if (! location.exists ())
  202. location.createDirectory ();
  203. String waveName;
  204. waveName << location.getFullPathName()
  205. << "/"
  206. << sWave
  207. << ".raw";
  208. DBG( waveName );
  209. File f(waveName);
  210. if (f.existsAsFile ())
  211. {
  212. tableSize = int(f.getSize() / 2);
  213. if (tableSize > 0)
  214. {
  215. M.setSize(0);
  216. f.loadFileAsData(M);
  217. daTable = (uint16*) M.getData();
  218. }
  219. }
  220. else
  221. {
  222. tableSize = int(Wavetables::sine_size / 2);
  223. if (tableSize > 0)
  224. {
  225. M.setSize (0);
  226. M.append (Wavetables::sine, Wavetables::sine_size);
  227. daTable = (uint16*) M.getData();
  228. }
  229. }
  230. loadWave = false;
  231. #endif
  232. }
  233. void reset(float f, double s, OscSet& o)
  234. {
  235. if (loadWave) actuallySetWave();
  236. s = s * 2.0;
  237. f = f * 2.0f * (1.0f + o.phaseIncOffset * 0.01f);
  238. o.cut = float(2.0 * double_Pi * 9000.0 / s);
  239. o.phase = o.phaseOffset * tableSize * 0.5f;
  240. o.phaseInc = float(cycle / (s/f));
  241. o.buf[0] = 0.0f;
  242. o.buf[1] = 0.0f;
  243. o.buf[2] = 0.0f;
  244. o.buf[3] = 0.0f;
  245. }
  246. void setFrequency(float f, double s, OscSet& o)
  247. {
  248. s = s * 2.0;
  249. f = f * 2.0f;
  250. o.phaseInc = float((float)cycle / (s/f));
  251. }
  252. void fillBuffer(float* buffer, int bufferSize, OscSet& o)
  253. {
  254. if (buffer == nullptr || bufferSize == 0)
  255. return;
  256. float tmp;
  257. for (int i = 0; i < bufferSize; ++i)
  258. {
  259. buffer[i] = 0.0f;
  260. int index = roundFloatToInt(o.phase - 0.5f);
  261. float alpha = o.phase - (float)index;
  262. const float conv = 1.0f / 65535.0f;
  263. float sIndex = daTable[index] * conv - 0.5f;
  264. float sIndexp1 = daTable[(index + 1) % tableSize] * conv - 0.5f;
  265. tmp = sIndex + alpha * (sIndexp1 - sIndex);
  266. o.buf[1] = ((tmp - o.buf[1]) * o.cut) + o.buf[1];
  267. o.buf[2] = ((o.buf[1] - o.buf[2]) * o.cut) + o.buf[2];
  268. o.buf[3] = ((o.buf[2] - o.buf[3]) * o.cut) + o.buf[3];
  269. o.buf[0] = ((o.buf[3] - o.buf[0]) * o.cut) + o.buf[0];
  270. tmp = o.buf[0];
  271. buffer[i] += tmp;
  272. o.phase += o.phaseInc;
  273. if (o.phase > (float)tableSize)
  274. o.phase -= (float)tableSize;
  275. index = roundFloatToInt(o.phase - 0.5f);
  276. alpha = o.phase - (float)index;
  277. sIndex = daTable[index] * conv - 0.5f;
  278. sIndexp1 = daTable[(index + 1) % tableSize] * conv - 0.5f;
  279. tmp = sIndex + alpha * (sIndexp1 - sIndex);
  280. o.buf[1] = ((tmp - o.buf[1]) * o.cut) + o.buf[1];
  281. o.buf[2] = ((o.buf[1] - o.buf[2]) * o.cut) + o.buf[2];
  282. o.buf[3] = ((o.buf[2] - o.buf[3]) * o.cut) + o.buf[3];
  283. o.buf[0] = ((o.buf[3] - o.buf[0]) * o.cut) + o.buf[0];
  284. tmp = o.buf[0];
  285. buffer[i] += tmp;
  286. o.phase += o.phaseInc;
  287. if (o.phase > (float)tableSize)
  288. o.phase -= (float)tableSize;
  289. }
  290. }
  291. static int getWaveTableSize();
  292. static String getWaveTableName(const int index);
  293. private:
  294. static const int kWaveTableSize = 41;
  295. static WaveTableNames waveTableNames[kWaveTableSize];
  296. int cycle, tableSize;
  297. uint16* daTable;
  298. //MemoryBlock M;
  299. String sWave;
  300. bool loadWave;
  301. };
  302. #endif // DISTRHO_VEX_WAVE_RENDERER_HEADER_INCLUDED