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.

135 lines
3.0KB

  1. #include "Examples.hpp"
  2. #include "waveTable.h"
  3. void waveTable::settableName(char *name)
  4. {
  5. if (strcmp(tableName, name) != 0)
  6. {
  7. words.clear();
  8. buf.clear();
  9. vector<float> tBuf;
  10. vector<std::string> tWords;
  11. words.swap(tWords);
  12. buf.swap(tBuf);
  13. strcpy(tableName, name);
  14. std::string tmp;
  15. std::string line;
  16. std::string finLine;
  17. char pathName [100] = "res/";
  18. strcat(pathName, tableName);
  19. std::string filename = assetPlugin(plugin, pathName);
  20. ifstream myfile (filename);
  21. if (myfile.is_open())
  22. {
  23. while ( getline (myfile,line) )
  24. {
  25. cout << line;
  26. if (!line.empty() && line[line.length()-1] == '\n') {
  27. line.erase(line.length()-1);
  28. }
  29. finLine.append(line);
  30. }
  31. myfile.close();
  32. }
  33. char delim = ',';
  34. stringstream ss;
  35. ss << finLine;
  36. while (std::getline(ss, tmp, ','))
  37. {
  38. words.push_back(tmp);
  39. }
  40. for (int i = 0; i < words.size(); i++)
  41. {
  42. std::string valueS = words.at(i);
  43. int value = std::stoi(valueS);
  44. buf.push_back(value);
  45. }
  46. wasSet = true;
  47. }
  48. }
  49. void waveTable::setindex(float _index)
  50. {
  51. if (wasSet == true)
  52. {
  53. if (_index < buf.size() - 1)
  54. {
  55. rawIndex = _index;
  56. index = int(_index);
  57. if (index > rawIndex && index > 0)
  58. {
  59. index = index-1;
  60. }
  61. }
  62. else
  63. {
  64. /*int j = buf.size();
  65. index = _index%j;*/
  66. index = 0;
  67. rawIndex = 0;
  68. }
  69. if (index > buf.size())
  70. {
  71. index = 0;
  72. rawIndex = 0;
  73. }
  74. }
  75. }
  76. void waveTable::setreset(float _reset)
  77. {
  78. if (_reset != 0)
  79. {
  80. index = 0;
  81. rawIndex = 0;
  82. }
  83. }
  84. float waveTable::getoutput()
  85. {
  86. if (buf.size()>0 && index < buf.size() && wasSet == true)
  87. {
  88. float delta = 0;
  89. float deltaValue = 0;
  90. float lValue = 0;
  91. float cValue = 0;
  92. lValue = buf.at(index);
  93. if (index < buf.size())
  94. {
  95. cValue = buf.at(index + 1);
  96. }
  97. else
  98. {
  99. cValue = buf.at(0);
  100. }
  101. delta = rawIndex - index;
  102. if (cValue > lValue)
  103. {
  104. deltaValue = cValue - lValue;
  105. output = lValue + (delta * deltaValue) ;
  106. }
  107. else
  108. {
  109. deltaValue = lValue - cValue;
  110. output = lValue - (delta * deltaValue) ;
  111. }
  112. }
  113. else
  114. {
  115. output = 0;
  116. }
  117. return output;
  118. }
  119. int waveTable::getbufSize()
  120. {
  121. int bf = 0;
  122. if (wasSet == true)
  123. {
  124. bf = buf.size();
  125. }
  126. return bf;
  127. }