ExprSynth.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * exprfront.h - header file to a Frontend to ExprTk
  3. *
  4. * Copyright (c) 2016-2017 Orr Dvori
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef EXPRSYNTH_H
  25. #define EXPRSYNTH_H
  26. #include <cmath>
  27. #include <cstddef>
  28. #include <limits>
  29. #include "AutomatableModel.h"
  30. #include "Graph.h"
  31. #include "Instrument.h"
  32. #include "MemoryManager.h"
  33. class ExprFrontData;
  34. class ExprFront
  35. {
  36. public:
  37. typedef float (*ff1data_functor)(void*, float);
  38. ExprFront(const char* expr, int last_func_samples);
  39. ~ExprFront();
  40. bool compile();
  41. inline bool isValid() { return m_valid; }
  42. float evaluate();
  43. bool add_variable(const char* name, float & ref);
  44. bool add_constant(const char* name, float ref);
  45. bool add_cyclic_vector(const char* name, const float* data, size_t length, bool interp = false);
  46. void setIntegrate(const unsigned int* frameCounter, unsigned int sample_rate);
  47. ExprFrontData* getData() { return m_data; }
  48. private:
  49. ExprFrontData *m_data;
  50. bool m_valid;
  51. static const int max_float_integer_mask=(1<<(std::numeric_limits<float>::digits))-1;
  52. };
  53. class WaveSample
  54. {
  55. public:
  56. WaveSample(int length)
  57. {
  58. m_length = length;
  59. m_samples = new float[m_length];
  60. for(int i = 0 ; i < m_length ; ++i)
  61. m_samples[i] = 0;
  62. }
  63. WaveSample(const graphModel * graph)
  64. {
  65. m_length = graph->length();
  66. m_samples = new float[m_length];
  67. memcpy(m_samples, graph->samples(), m_length * sizeof(float));
  68. }
  69. inline void copyFrom(const graphModel * graph)
  70. {
  71. memcpy(m_samples, graph->samples(), m_length * sizeof(float));
  72. }
  73. ~WaveSample()
  74. {
  75. delete [] m_samples;
  76. }
  77. inline void setInterpolate(bool _interpolate) { m_interpolate = _interpolate; }
  78. float *m_samples;
  79. int m_length;
  80. bool m_interpolate;
  81. };
  82. class ExprSynth
  83. {
  84. MM_OPERATORS
  85. public:
  86. ExprSynth(const WaveSample* gW1, const WaveSample* gW2, const WaveSample* gW3, ExprFront* exprO1, ExprFront* exprO2, NotePlayHandle* nph,
  87. const sample_rate_t sample_rate, const FloatModel* pan1, const FloatModel* pan2, float rel_trans);
  88. virtual ~ExprSynth();
  89. void renderOutput(fpp_t frames, sampleFrame* buf );
  90. private:
  91. ExprFront *m_exprO1, *m_exprO2;
  92. const WaveSample *m_W1, *m_W2, *m_W3;
  93. unsigned int m_note_sample;
  94. unsigned int m_note_rel_sample;
  95. float m_note_sample_sec;
  96. float m_note_rel_sec;
  97. float m_frequency;
  98. float m_released;
  99. NotePlayHandle* m_nph;
  100. const sample_rate_t m_sample_rate;
  101. const FloatModel *m_pan1,*m_pan2;
  102. float m_rel_transition;
  103. float m_rel_inc;
  104. } ;
  105. inline float positiveFraction(float x)
  106. {
  107. if (std::isnan(x) || std::isinf(x))
  108. return 0;
  109. if (x<0)
  110. {
  111. x+=static_cast<int>(1-x);
  112. }
  113. return x-static_cast<int>(x);
  114. }
  115. template <typename T>
  116. inline void clearArray(T* arr,unsigned int size)
  117. {
  118. const T* const arr_end = arr + size;
  119. while(arr < arr_end)
  120. {
  121. *arr=0;
  122. ++arr;
  123. }
  124. }
  125. #endif