worldParameters.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "worldParameters.h"
  2. #include "world/world.h"
  3. worldParameters::worldParameters()
  4. {
  5. t = f0 = NULL;
  6. pulseLocations = NULL;
  7. tLen = 0;
  8. framePeriod = 2.0;
  9. }
  10. worldParameters::~worldParameters()
  11. {
  12. destroy();
  13. }
  14. void worldParameters::destroy()
  15. {
  16. delete[] t;
  17. delete[] f0;
  18. delete[] pulseLocations;
  19. tLen = 0;
  20. framePeriod = 2.0;
  21. t = f0 = NULL;
  22. pulseLocations = NULL;
  23. }
  24. bool worldParameters::computeWave(double *wave, int waveLength, int fs, double framePeriod)
  25. {
  26. if(!wave || waveLength < 0)
  27. {
  28. return false;
  29. }
  30. destroy();
  31. tLen = getSamplesForDIO(fs, waveLength, framePeriod);
  32. f0 = new float[tLen];
  33. t = new float[tLen];
  34. double *dF0 = new double[tLen];
  35. double *dT = new double[tLen];
  36. pulseLocations = new int[tLen];
  37. this->framePeriod = framePeriod;
  38. // t, f0 は DIO だけで O.K.
  39. dio(wave, waveLength, fs, framePeriod, dT, dF0);
  40. for(int k = 0; k < tLen; k++)
  41. {
  42. f0[k] = dF0[k];
  43. t[k] = dT[k];
  44. }
  45. // PLATINUM 用にパルス位置を計算. platinum_v4 の機能限定版.
  46. int i, j, index;
  47. int fftl = (int)pow(2.0, 1.0+(int)(log(3.0*fs/FLOOR_F0+1) / log(2.0)));
  48. int vuvNum;
  49. vuvNum = 0;
  50. for(i = 1;i < tLen;i++)
  51. {
  52. if(f0[i]!=0.0 && f0[i-1]==0.0) vuvNum++;
  53. }
  54. vuvNum+=vuvNum-1; // 島数の調整 (有声島と無声島)
  55. if(f0[0] == 0) vuvNum++;
  56. if(f0[tLen-1] == 0) vuvNum++;
  57. int stCount, edCount;
  58. int *stList, *edList;
  59. stList = (int *)malloc(sizeof(int) * vuvNum);
  60. edList = (int *)malloc(sizeof(int) * vuvNum);
  61. edCount = 0;
  62. stList[0] = 0;
  63. stCount = 1;
  64. index = 1;
  65. if(f0[0] != 0)
  66. {
  67. for(i = 1;i < tLen;i++)
  68. {
  69. if(f0[i]==0 && f0[i-1]!=0)
  70. {
  71. edList[0] = i-1;
  72. edCount++;
  73. stList[1] = i;
  74. stCount++;
  75. index = i;
  76. }
  77. }
  78. }
  79. edList[vuvNum-1] = tLen-1;
  80. for(i = index;i < tLen;i++)
  81. {
  82. if(f0[i]!=0.0 && f0[i-1]==0.0)
  83. {
  84. edList[edCount++] = i-1;
  85. stList[stCount++] = i;
  86. }
  87. if(f0[i]==0.0 && f0[i-1]!=0.0)
  88. {
  89. edList[edCount++] = i-1;
  90. stList[stCount++] = i;
  91. }
  92. }
  93. int *wedgeList;
  94. wedgeList = (int *)malloc(sizeof(int) * vuvNum);
  95. getWedgeList(wave, waveLength, vuvNum, stList, edList, fs, framePeriod, dF0, wedgeList);
  96. double *signalTime, *f0interpolatedRaw, *totalPhase;
  97. double *fixedF0;
  98. fixedF0 = (double *)malloc(sizeof(double) * tLen);
  99. signalTime = (double *)malloc(sizeof(double) * waveLength);
  100. f0interpolatedRaw = (double *)malloc(sizeof(double) * waveLength);
  101. totalPhase = (double *)malloc(sizeof(double) * waveLength);
  102. for(i = 0;i < tLen;i++) fixedF0[i] = f0[i] == 0 ? DEFAULT_F0 : f0[i];
  103. for(i = 0;i < waveLength;i++) signalTime[i] = (double)i / (double)fs;
  104. interp1(dT, fixedF0, tLen, signalTime, waveLength, f0interpolatedRaw);
  105. totalPhase[0] = f0interpolatedRaw[0]*2*PI/(double)fs;
  106. for(i = 1;i < waveLength;i++) totalPhase[i] = totalPhase[i-1] + f0interpolatedRaw[i]*2*PI/(double)fs;
  107. double *tmpPulseLocations;
  108. tmpPulseLocations = (double *)malloc(sizeof(double) * waveLength);
  109. int pCount;
  110. pCount = getPulseLocations(wave, waveLength, totalPhase, vuvNum, stList, edList, fs, framePeriod, wedgeList, tmpPulseLocations);
  111. pulseLocations[0] = 0;
  112. for(i = 1;i < tLen;i++)
  113. {
  114. int tmpIndex;
  115. double tmp;
  116. double tmpValue = 100000.0; // safeGuard
  117. for(j = 0;j < pCount; j++)
  118. {
  119. tmp = fabs(tmpPulseLocations[j] - (double)i * framePeriod / 1000.0);
  120. if(tmp < tmpValue)
  121. {
  122. tmpValue = tmp;
  123. tmpIndex = j;
  124. }
  125. index = 1 + (int)(0.5 + tmpPulseLocations[tmpIndex] * fs);
  126. }
  127. pulseLocations[i] = index;
  128. }
  129. free(fixedF0);
  130. free(tmpPulseLocations);
  131. free(totalPhase);
  132. free(f0interpolatedRaw);
  133. free(signalTime);
  134. free(wedgeList);
  135. free(edList);
  136. free(stList);
  137. delete[] dT;
  138. delete[] dF0;
  139. return true;
  140. }
  141. bool worldParameters::writeParameters(const char *path)
  142. {
  143. if(!t || !f0 || !pulseLocations || tLen <= 0)
  144. {
  145. return false;
  146. }
  147. FILE *fp = fopen(path, "wb");
  148. if(fp == NULL)
  149. {
  150. return false;
  151. }
  152. fwrite(&tLen, sizeof(int), 1, fp);
  153. fwrite(&framePeriod, sizeof(float), 1, fp);
  154. fwrite(t, sizeof(float), tLen, fp);
  155. fwrite(f0, sizeof(float), tLen, fp);
  156. fwrite(pulseLocations, sizeof(int), tLen, fp);
  157. fclose(fp);
  158. return true;
  159. }
  160. bool worldParameters::readParameters(const char *path)
  161. {
  162. FILE *fp = fopen(path, "rb");
  163. if(fp == NULL)
  164. {
  165. return false;
  166. }
  167. int c;
  168. bool ret = false;
  169. destroy();
  170. c = fread(&tLen, sizeof(int), 1, fp);
  171. c += fread(&framePeriod, sizeof(float), 1, fp);
  172. t = new float[tLen];
  173. f0 = new float[tLen];
  174. pulseLocations = new int[tLen];
  175. c += fread(t, sizeof(float), tLen, fp);
  176. c += fread(f0, sizeof(float), tLen, fp);
  177. c += fread(pulseLocations, sizeof(int), tLen, fp);
  178. if(c == 3 * tLen + 2)
  179. {
  180. ret = true;
  181. }
  182. else
  183. {
  184. tLen = 0;
  185. }
  186. fclose(fp);
  187. return ret;
  188. }
  189. bool worldParameters::getParameters(float *f0, float *t, int *pulseLocations, int fs, double beginTime, int timeLength, double framePeriod)
  190. {
  191. int beginIndex = (int)(beginTime / framePeriod * 1000.0);
  192. int endIndex = beginIndex + timeLength;
  193. int index, i, tmp;
  194. // 開始時刻がマイナスの可能性があることに注意.
  195. for(i = 0, index = beginIndex; index < 0; i++, index++)
  196. {
  197. f0[i] = 0;
  198. t[i] = 0;
  199. pulseLocations[i] = 0;
  200. }
  201. tmp = index;
  202. // データをコピー.
  203. for(; index < endIndex; i++, index++)
  204. {
  205. int tmpIndex = (int)((double)index * framePeriod / this->framePeriod);
  206. if(tmpIndex >= tLen)
  207. {
  208. break;
  209. }
  210. f0[i] = this->f0[tmpIndex];
  211. t[i] = this->t[tmpIndex] - this->t[tmp];
  212. pulseLocations[i] = this->pulseLocations[tmpIndex] - (int)(beginTime * fs);
  213. }
  214. // 指定された長さほどにデータが無い場合.
  215. for(; index < endIndex; i++, index++)
  216. {
  217. f0[i] = 0;
  218. t[i] = 0;
  219. pulseLocations[i] = 0;
  220. }
  221. return true;
  222. }