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.

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