Source code of Windows XP (NT5)
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.

1023 lines
25 KiB

  1. /******************************************************************************
  2. * SpeakerData.cpp *
  3. *-----------------*
  4. *
  5. *------------------------------------------------------------------------------
  6. * Copyright (c) 1997 Entropic Research Laboratory, Inc.
  7. * Copyright (C) 1998 Entropic, Inc
  8. * Copyright (C) 2000 Microsoft Corporation Date: 03/02/00
  9. * All Rights Reserved
  10. *
  11. ********************************************************************* PACOG ***/
  12. #include "SpeakerData.h"
  13. #include "UnitSearch.h"
  14. #include "clusters.h"
  15. #include "trees.h"
  16. #include "VqTable.h"
  17. #include "BeVersion.h"
  18. #include "vapiio.h"
  19. #include "SynthUnit.h"
  20. #include "BackEnd.h"
  21. #include <assert.h>
  22. #ifdef WIN32
  23. #include <fcntl.h>
  24. #endif
  25. #define MAX_LINE 512
  26. struct UnitSamples
  27. {
  28. char* samples;
  29. int nSamples;
  30. Epoch* epochs;
  31. int nEpochs;
  32. };
  33. CList<CSpeakerData*> CSpeakerData::m_speakers;
  34. CComAutoCriticalSection CSpeakerData::m_critSect;
  35. /*****************************************************************************
  36. * CSpeakerData::ClassFactory *
  37. *----------------------------*
  38. * Description:
  39. *
  40. ******************************************************************* PACOG ***/
  41. CSpeakerData* CSpeakerData::ClassFactory (const char* pszFileName, bool fCheckVersion)
  42. {
  43. CSpeakerData* pSpkrData = 0;
  44. assert (pszFileName);
  45. m_critSect.Lock();
  46. if ((m_speakers.Find(pszFileName, pSpkrData)) != 0)
  47. {
  48. pSpkrData->AddRef();
  49. }
  50. else if (( pSpkrData = new CSpeakerData(pszFileName) ) != 0)
  51. {
  52. m_speakers.PushBack(pszFileName, pSpkrData);
  53. pSpkrData->AddRef();
  54. if (!pSpkrData->Load (fCheckVersion) )
  55. {
  56. delete pSpkrData;
  57. pSpkrData = 0;
  58. }
  59. }
  60. m_critSect.Unlock();
  61. return pSpkrData;
  62. }
  63. /*****************************************************************************
  64. * CSpeakerData::AddRef *
  65. *-------------------------*
  66. * Description:
  67. *
  68. ******************************************************************* PACOG ***/
  69. void CSpeakerData::AddRef()
  70. {
  71. m_iRefCount++;
  72. }
  73. /*****************************************************************************
  74. * CSpeakerData::Release *
  75. *-----------------------*
  76. * Description:
  77. *
  78. ******************************************************************* PACOG ***/
  79. void CSpeakerData::Release()
  80. {
  81. if (--m_iRefCount == 0)
  82. {
  83. delete this;
  84. }
  85. }
  86. /*****************************************************************************
  87. * CSpeakerData::CSpeakerData *
  88. *----------------------------*
  89. * Description:
  90. *
  91. ******************************************************************* PACOG ***/
  92. CSpeakerData::CSpeakerData (const char* pszFileName)
  93. {
  94. strcpy(m_pszFileName, pszFileName);
  95. m_iRefCount = 0;
  96. m_iSampFreq = 0;
  97. m_iFormat = 0;
  98. m_pTrees = 0;
  99. m_pClusters = 0;
  100. m_pVq = 0;
  101. m_ttpPar.baseLine = 0;
  102. m_ttpPar.refLine = 0;
  103. m_ttpPar.topLine = 0;
  104. m_pFileNames = 0;
  105. m_iNumFileNames = 0;
  106. m_pUnits = 0;
  107. m_iNumUnits= 0;
  108. m_dRumTime = 0;
  109. m_fWaveConcat = false;
  110. m_fMSPhoneSet = false;
  111. m_fMSEntropic = false;
  112. }
  113. /*****************************************************************************
  114. * CSpeakerData::~CSpeakerData *
  115. *-----------------------------*
  116. * Description:
  117. *
  118. ******************************************************************* PACOG ***/
  119. CSpeakerData::~CSpeakerData ()
  120. {
  121. m_critSect.Lock();
  122. for (int i = 0; i<m_speakers.Size(); i++)
  123. {
  124. if (m_speakers[i] == this)
  125. {
  126. m_speakers.Remove(i);
  127. }
  128. }
  129. m_critSect.Unlock();
  130. delete m_pVq;
  131. delete m_pTrees;
  132. delete m_pClusters;
  133. FreeSamples();
  134. }
  135. /*****************************************************************************
  136. * CSpeakerData::Load *
  137. *--------------------*
  138. * Description:
  139. *
  140. ******************************************************************* PACOG ***/
  141. int CSpeakerData::Load (bool fCheckVersion)
  142. {
  143. FILE* fin;
  144. char line[MAX_LINE +1];
  145. #ifdef WIN32
  146. _fmode = _O_BINARY;
  147. #endif
  148. fin = fopen (m_pszFileName, "r");
  149. #ifdef WIN32
  150. _fmode = _O_TEXT;
  151. #endif
  152. if (!fin)
  153. {
  154. return 0;
  155. }
  156. if (fCheckVersion)
  157. {
  158. BendVersion bVers;
  159. if (! bVers.CheckVersionString(fin) )
  160. {
  161. goto end;
  162. }
  163. }
  164. if ((m_pTrees = CClustTree::ClassFactory()) == 0)
  165. {
  166. goto end;
  167. }
  168. if (!m_pTrees->LoadFromFile (fin)) {
  169. goto end;
  170. }
  171. if ((m_pClusters = new CClusters) == 0)
  172. {
  173. goto end;
  174. }
  175. if (!m_pClusters->LoadFromFile(fin))
  176. {
  177. goto end;
  178. }
  179. while (fgets (line, MAX_LINE, fin))
  180. {
  181. // Gain table, vq table and ttp params are optional
  182. if (strcmp(line, "#Slm Weights\n") == 0)
  183. {
  184. if (!LoadWeights (fin))
  185. {
  186. goto end;
  187. }
  188. }
  189. else if (strcmp(line, "#Slm New Weights\n") == 0)
  190. {
  191. if (!LoadNewWeights (fin))
  192. {
  193. goto end;
  194. }
  195. }
  196. else if ( strcmp(line, "#Waveform Concatenation\n") == 0)
  197. {
  198. m_fWaveConcat = true;
  199. }
  200. else if ( strcmp(line, "#MS Phone Set\n") == 0)
  201. {
  202. m_fMSPhoneSet = true;
  203. }
  204. else if ( strcmp(line, "#Slm AvgGain Table\n") == 0)
  205. {
  206. if (!m_pClusters->LoadGainTable (fin))
  207. {
  208. goto end;
  209. }
  210. }
  211. else if ( strcmp(line, "#Slm VQ Table\n") == 0)
  212. {
  213. if ((m_pVq = new CVqTable) == 0)
  214. {
  215. goto end;
  216. }
  217. if (!m_pVq->LoadFromFile(fin))
  218. {
  219. goto end;
  220. }
  221. }
  222. else if ( strcmp(line, "#Ttp Params\n") == 0)
  223. {
  224. if (!LoadTtpParam (fin))
  225. {
  226. goto end;
  227. }
  228. }
  229. else if ( strcmp(line, "#Samples\n") == 0)
  230. {
  231. if (!LoadSamples (fin))
  232. {
  233. goto end;
  234. }
  235. }
  236. else if ( strcmp(line, "#File names\n") == 0)
  237. {
  238. if (!LoadFileNames (fin))
  239. {
  240. goto end;
  241. }
  242. }
  243. else
  244. {
  245. goto end;
  246. }
  247. }
  248. return 1;
  249. end:
  250. return 0;
  251. }
  252. /*****************************************************************************
  253. * CSpeakerData::LoadFileNames *
  254. *--------------------------------*
  255. * Description:
  256. *
  257. ******************************************************************* PACOG ***/
  258. int CSpeakerData::LoadFileNames (FILE* fp)
  259. {
  260. char fileName[_MAX_PATH+1];
  261. assert (fp);
  262. if (fscanf (fp, "%d\n", &m_iNumFileNames)!=1)
  263. {
  264. return 0;
  265. }
  266. if ((m_pFileNames = new CDynString[m_iNumFileNames]) == 0)
  267. {
  268. return 0;
  269. }
  270. for (int i=0; i<m_iNumFileNames; i++)
  271. {
  272. if (fscanf (fp, "%s\n", fileName) != 1)
  273. {
  274. return 0;
  275. }
  276. m_pFileNames[i] = fileName;
  277. }
  278. return 1;
  279. }
  280. /*****************************************************************************
  281. * CSpeakerData::LoadTtpParam *
  282. *----------------------------*
  283. * Description:
  284. * Loads the Text-to-Prosody Parameters
  285. ******************************************************************* PACOG ***/
  286. int CSpeakerData::LoadTtpParam (FILE* fin)
  287. {
  288. assert (fin);
  289. if (!fread (&m_ttpPar, sizeof (m_ttpPar), 1, fin))
  290. {
  291. return 0;
  292. }
  293. return 1;
  294. }
  295. /*****************************************************************************
  296. * CSpeakerData::LoadWeights *
  297. *---------------------------*
  298. * Description:
  299. *
  300. ******************************************************************* PACOG ***/
  301. int CSpeakerData::LoadWeights (FILE* fin)
  302. {
  303. assert (fin);
  304. if (!fread (&m_weights, sizeof (WeightsBasic), 1, fin))
  305. {
  306. return 0;
  307. }
  308. m_weights.f0Bdr = 0.2F;
  309. m_weights.phBdr = 0.4F;
  310. return 1;
  311. }
  312. /*****************************************************************************
  313. * CSpeakerData::LoadNewWeights *
  314. *------------------------------*
  315. * Description:
  316. *
  317. ******************************************************************* WD ***/
  318. int CSpeakerData::LoadNewWeights (FILE* fin)
  319. {
  320. assert (fin);
  321. if (!fread (&m_weights, sizeof (m_weights), 1, fin))
  322. {
  323. return 0;
  324. }
  325. return 1;
  326. }
  327. /*****************************************************************************
  328. * CSpeakerData::PreComputeDist *
  329. *------------------------------*
  330. * Description:
  331. *
  332. ******************************************************************* PACOG ***/
  333. void CSpeakerData::PreComputeDist()
  334. {
  335. m_critSect.Lock();
  336. if (m_pClusters)
  337. {
  338. m_pClusters->PreComputeDist(m_weights.dur, m_weights.rms, m_weights.lkl);
  339. }
  340. if (m_pVq)
  341. {
  342. m_pVq->Scale (m_weights.cont);
  343. }
  344. m_critSect.Unlock();
  345. }
  346. /*****************************************************************************
  347. * CSpeakerData::GetTtpParam *
  348. *---------------------------*
  349. * Description:
  350. *
  351. ******************************************************************* PACOG ***/
  352. void CSpeakerData::GetTtpParam (int* piBaseLine, int* piRefLine, int* piTopLine)
  353. {
  354. assert (piBaseLine && piRefLine && piTopLine);
  355. *piBaseLine = m_ttpPar.baseLine;
  356. *piRefLine = m_ttpPar.refLine;
  357. *piTopLine = m_ttpPar.topLine;
  358. }
  359. /*****************************************************************************
  360. * CSpeakerData::LoadSamples *
  361. *------------------------------*
  362. * Description:
  363. *
  364. ******************************************************************* PACOG ***/
  365. int CSpeakerData::LoadSamples (FILE* fin)
  366. {
  367. int sampSize;
  368. assert (fin);
  369. if (!fread (&m_iNumUnits, sizeof(m_iNumUnits), 1, fin))
  370. {
  371. return 0;
  372. }
  373. if (!fread (&m_iFormat, sizeof(m_iFormat), 1, fin))
  374. {
  375. return 0;
  376. }
  377. sampSize = VapiIO::SizeOf (m_iFormat);
  378. if (!fread (&m_iSampFreq, sizeof(m_iSampFreq), 1, fin))
  379. {
  380. return 0;
  381. }
  382. if ((m_pUnits = new UnitSamples[m_iNumUnits]) == 0)
  383. {
  384. return 0;
  385. }
  386. for (int i=0; i<m_iNumUnits; i++)
  387. {
  388. if (!fread (&m_pUnits[i].nSamples, sizeof (int), 1, fin)) {
  389. return 0;
  390. }
  391. if ((m_pUnits[i].samples = new char[m_pUnits[i].nSamples * sampSize]) == 0)
  392. {
  393. return 0;
  394. }
  395. if (fread (m_pUnits[i].samples, sampSize, m_pUnits[i].nSamples, fin) != (unsigned)m_pUnits[i].nSamples)
  396. {
  397. return 0;
  398. }
  399. if (!fread (&m_pUnits[i].nEpochs, sizeof (int), 1, fin))
  400. {
  401. return 0;
  402. }
  403. if ((m_pUnits[i].epochs = new Epoch[m_pUnits[i].nEpochs]) == 0)
  404. {
  405. return 0;
  406. }
  407. if (fread (m_pUnits[i].epochs , sizeof(Epoch), m_pUnits[i].nEpochs, fin) != (unsigned)m_pUnits[i].nEpochs)
  408. {
  409. return 0;
  410. }
  411. }
  412. return 1;
  413. }
  414. /*****************************************************************************
  415. * CSpeakerData::FreeSamples *
  416. *------------------------------*
  417. * Description:
  418. *
  419. ******************************************************************* PACOG ***/
  420. void CSpeakerData::FreeSamples ()
  421. {
  422. if (m_pUnits)
  423. {
  424. for (int i=0; i<m_iNumUnits; i++)
  425. {
  426. if (m_pUnits[i].samples)
  427. {
  428. delete[] m_pUnits[i].samples;
  429. }
  430. if (m_pUnits[i].epochs)
  431. {
  432. delete[] m_pUnits[i].epochs;
  433. }
  434. }
  435. delete[] m_pUnits;
  436. m_pUnits = NULL;
  437. }
  438. m_iNumUnits = 0;
  439. }
  440. /*****************************************************************************
  441. * CSpeakerData::GetSampFreq *
  442. *---------------------------*
  443. * Description:
  444. *
  445. ******************************************************************* PACOG ***/
  446. int CSpeakerData::GetSampFreq ()
  447. {
  448. return m_iSampFreq;
  449. }
  450. /*****************************************************************************
  451. * CSpeakerData::GetSampFormat *
  452. *-----------------------------*
  453. * Description:
  454. *
  455. ******************************************************************* PACOG ***/
  456. int CSpeakerData::GetSampFormat ()
  457. {
  458. return m_iFormat;
  459. }
  460. /*****************************************************************************
  461. * CSpeakerData::GetUnit *
  462. *-----------------------*
  463. * Description:
  464. *
  465. ******************************************************************* PACOG ***/
  466. CSynth* CSpeakerData::GetUnit (ChkDescript* pDescript)
  467. {
  468. CSynth* pSynth;
  469. assert (pDescript);
  470. if (pDescript->isFileName)
  471. {
  472. pSynth = UnitFromFile (pDescript);
  473. }
  474. else
  475. {
  476. pSynth = UnitFromMemory (pDescript);
  477. }
  478. return pSynth;
  479. }
  480. /*****************************************************************************
  481. * CSpeakerData::UnitFromFile *
  482. *----------------------------*
  483. * Description:
  484. *
  485. ******************************************************************* PACOG ***/
  486. CSynth* CSpeakerData::UnitFromFile (ChkDescript* chunk)
  487. {
  488. CSynth* pUnit;
  489. short *psSamples = NULL;
  490. int iNumSamples;
  491. Epoch* pEpochs;
  492. int iNumEpochs;
  493. int iSampFreq;
  494. int i;
  495. assert (chunk);
  496. if ( !ReadSamples(chunk->chunk.fileName, chunk->from, chunk->to,
  497. &psSamples, &iNumSamples, &pEpochs, &iNumEpochs, &iSampFreq) )
  498. {
  499. fprintf (stderr, "Error accessing file %s\n", chunk->chunk.fileName);
  500. }
  501. if ((pUnit = new CSynth(iSampFreq)) == 0)
  502. {
  503. return 0;
  504. }
  505. if ((pUnit->m_pdSamples = new double[pUnit->m_iNumSamples]) == 0)
  506. {
  507. return 0;
  508. }
  509. for (i=0; i<pUnit->m_iNumSamples; i++)
  510. {
  511. pUnit->m_pdSamples[i] = psSamples[i];
  512. }
  513. delete []psSamples;
  514. pUnit->m_iNumSamples = iNumSamples;
  515. pUnit->m_pEpochs = pEpochs;
  516. pUnit->m_iNumEpochs = iNumEpochs;
  517. //Override sampling frequency and sample format
  518. // m_iSampFreq = iSampFreq;
  519. // m_iSampFormat = VAPI_PCM16;
  520. strcpy ( pUnit->m_pszChunkName, chunk->name );
  521. pUnit->m_dF0Ratio = chunk->f0Ratio;
  522. pUnit->m_dGain = chunk->gain;
  523. /* --- use unit duration as target duration
  524. if ( m_fWaveConcat )
  525. {
  526. m_dRumTime += pEpochs[ iNumEpochs - 1 ].time;
  527. pUnit->m_dRunTimeLimit = m_dRumTime;
  528. }
  529. else
  530. {
  531. pUnit->m_dRunTimeLimit = chunk->end;
  532. }
  533. */
  534. pUnit->m_dRunTimeLimit = chunk->end;
  535. return pUnit;
  536. }
  537. /*****************************************************************************
  538. * CSpeakerData::UnitFromMemory *
  539. *------------------------------*
  540. * Description:
  541. *
  542. ******************************************************************* PACOG ***/
  543. CSynth* CSpeakerData::UnitFromMemory (ChkDescript* pChunk)
  544. {
  545. CSynth* pUnit;
  546. UnitSamples* pUSamples;
  547. int iFirstEpoch;
  548. int iLastEpoch;
  549. int firstSamp;
  550. int lastSamp;
  551. double from;
  552. double to;
  553. int i;
  554. assert (pChunk);
  555. if ((pUnit = new CSynth(m_iSampFreq)) == 0)
  556. {
  557. return 0;
  558. }
  559. if (!m_pUnits || pChunk->chunk.chunkIdx<0 || pChunk->chunk.chunkIdx>=m_iNumUnits)
  560. {
  561. return 0;
  562. }
  563. pUSamples = m_pUnits + pChunk->chunk.chunkIdx;
  564. strcpy ( pUnit->m_pszChunkName, pChunk->name );
  565. from = pChunk->from;
  566. to = pChunk->to;
  567. do
  568. {
  569. for (iFirstEpoch=0; iFirstEpoch<pUSamples->nEpochs && from > pUSamples->epochs[iFirstEpoch].time; iFirstEpoch++)
  570. {
  571. //Empty block
  572. }
  573. if (iFirstEpoch && from < pUSamples->epochs[iFirstEpoch].time)
  574. {
  575. iFirstEpoch--;
  576. }
  577. for (iLastEpoch = iFirstEpoch; iLastEpoch<(pUSamples->nEpochs-1) && to > pUSamples->epochs[iLastEpoch].time; iLastEpoch++)
  578. {
  579. //Empty block
  580. }
  581. pUnit->m_iNumEpochs = iLastEpoch - iFirstEpoch +1;
  582. from-= 0.005;
  583. to += 0.005;
  584. }
  585. while (pUnit->m_iNumEpochs < 3 && (iFirstEpoch >0 || iLastEpoch < (pUSamples->nEpochs -1)));
  586. if ((pUnit->m_pEpochs = new Epoch[pUnit->m_iNumEpochs]) == NULL)
  587. {
  588. return 0;
  589. }
  590. memcpy(pUnit->m_pEpochs, pUSamples->epochs + iFirstEpoch, pUnit->m_iNumEpochs * sizeof(*pUnit->m_pEpochs));
  591. firstSamp = (int)(pUnit->m_pEpochs[0].time * m_iSampFreq);
  592. lastSamp = (int)(pUnit->m_pEpochs[pUnit->m_iNumEpochs-1].time * m_iSampFreq + 0.5);
  593. if (lastSamp >= pUSamples->nSamples) {
  594. lastSamp = pUSamples->nSamples -1;
  595. }
  596. pUnit->m_iNumSamples = lastSamp - firstSamp +1;
  597. if ((pUnit->m_pdSamples = new double[pUnit->m_iNumSamples]) == 0)
  598. {
  599. return 0;
  600. }
  601. {
  602. char* pcInPtr;
  603. short* pnBuffer;
  604. // pUSamples->samples is a char*, add correct number of bytes.
  605. pcInPtr = pUSamples->samples + (firstSamp * VapiIO::SizeOf(m_iFormat));
  606. if (m_iFormat != VAPI_PCM16)
  607. {
  608. if ((pnBuffer = new short[pUnit->m_iNumSamples]) == 0)
  609. {
  610. return 0;
  611. }
  612. VapiIO::DataFormatConversion (pcInPtr, m_iFormat, (char*)pnBuffer, VAPI_PCM16, pUnit->m_iNumSamples);
  613. }
  614. else
  615. {
  616. pnBuffer = (short*)pcInPtr;
  617. }
  618. for (i=0; i< pUnit->m_iNumSamples; i++)
  619. {
  620. pUnit->m_pdSamples[i] = (double)pnBuffer[i];
  621. }
  622. if (pnBuffer != (short*)pcInPtr)
  623. {
  624. delete[] pnBuffer;
  625. }
  626. }
  627. for (i=0; i<pUnit->m_iNumEpochs; i++)
  628. {
  629. pUnit->m_pEpochs[i].time -= (firstSamp /(double)m_iSampFreq);
  630. }
  631. pUnit->m_dF0Ratio = pChunk->f0Ratio;
  632. pUnit->m_dGain = pChunk->gain;
  633. /* --- use unit duration as target duration
  634. if ( m_fWaveConcat )
  635. {
  636. m_dRumTime += pUnit->m_pEpochs[pUnit->m_iNumEpochs-1].time;
  637. pUnit->m_dRunTimeLimit = m_dRumTime;
  638. }
  639. else
  640. {
  641. pUnit->m_dRunTimeLimit = pChunk->end;
  642. }
  643. */
  644. pUnit->m_dRunTimeLimit = pChunk->end;
  645. return pUnit;
  646. }
  647. /*****************************************************************************
  648. * CSpeakerData::ReadSamples *
  649. *---------------------------*
  650. * Description:
  651. * Given some basic information about unit required, it reads the unit in
  652. * and gets the epochs and the samples
  653. ******************************************************************* PACOG ***/
  654. int CSpeakerData::ReadSamples (const char* pszPathName, // Base file with directory path
  655. double dFrom, // Starting time of unit
  656. double dTo, // Ending time of unit
  657. short** ppnSamples, // Read samples
  658. int* piNumSamples, // Number of samples read
  659. Epoch** ppEpochs, // Array of Epochs
  660. int* piNumEpochs, // Number of Epochs read
  661. int* piSampFreq) // Sampling Frequence
  662. {
  663. VapiIO* pViof = 0;
  664. int iRetVal = VAPI_IOERR_NOERROR;
  665. int iSampFormat;
  666. Epoch* pEpochBuffer = NULL;
  667. int iNumReadEpochs;
  668. int iFirstEpoch;
  669. int iLastEpoch;
  670. double dStartTime;
  671. char* pcBuffer = 0;
  672. int i;
  673. assert (pszPathName);
  674. assert (dFrom>=0.0);
  675. assert (dTo == -1.0 || dTo >= dFrom);
  676. assert (ppnSamples);
  677. assert (piNumSamples);
  678. assert (ppEpochs);
  679. assert (piNumEpochs);
  680. assert (piSampFreq);
  681. if (( pViof = VapiIO::ClassFactory()) == 0)
  682. {
  683. iRetVal = VAPI_IOERR_MEMORY;
  684. goto error;
  685. }
  686. if ( (iRetVal = pViof->OpenFile (pszPathName, VAPI_IO_READ)) != VAPI_IOERR_NOERROR)
  687. {
  688. goto error;
  689. }
  690. if ( (iRetVal = pViof->Format (piSampFreq, &iSampFormat)) != VAPI_IOERR_NOERROR)
  691. {
  692. goto error;
  693. }
  694. // Read epochs first
  695. if ((iRetVal = pViof->ReadEpochs(&pEpochBuffer, &iNumReadEpochs)) != VAPI_IOERR_NOERROR)
  696. {
  697. goto error;
  698. }
  699. do
  700. {
  701. for (iFirstEpoch = 0; iFirstEpoch < iNumReadEpochs &&
  702. dFrom > pEpochBuffer[iFirstEpoch].time; iFirstEpoch++)
  703. {
  704. //Empty loop
  705. }
  706. if (iFirstEpoch && dFrom < pEpochBuffer[iFirstEpoch].time)
  707. {
  708. iFirstEpoch--;
  709. }
  710. if (iFirstEpoch >= iNumReadEpochs)
  711. {
  712. iFirstEpoch--;
  713. }
  714. for (iLastEpoch = iFirstEpoch; iLastEpoch < iNumReadEpochs &&
  715. dTo > pEpochBuffer[iLastEpoch].time; iLastEpoch++)
  716. {
  717. //Emtpy loop
  718. }
  719. if (iLastEpoch >= iNumReadEpochs)
  720. {
  721. iLastEpoch--;
  722. }
  723. *piNumEpochs = iLastEpoch - iFirstEpoch + 1;
  724. dFrom -= 0.005;
  725. dTo += 0.005;
  726. }
  727. while (*piNumEpochs < 3 && (iFirstEpoch > 0 || iLastEpoch < (iNumReadEpochs -1)));
  728. if ((*ppEpochs = new Epoch[*piNumEpochs]) == 0)
  729. {
  730. iRetVal = VAPI_IOERR_MEMORY;
  731. goto error;
  732. }
  733. memcpy (*ppEpochs, pEpochBuffer + iFirstEpoch, *piNumEpochs * sizeof (**ppEpochs));
  734. dFrom = pEpochBuffer[iFirstEpoch].time;
  735. dTo = pEpochBuffer[iLastEpoch].time;
  736. // CAREFUL! We need to reset the epochs to the starting time
  737. // of the chunk!
  738. dStartTime = ((int)(dFrom * *piSampFreq))/ (double)*piSampFreq;
  739. for (i=0; i<*piNumEpochs; i++)
  740. {
  741. (*ppEpochs)[i].time -= dStartTime;
  742. }
  743. delete[] pEpochBuffer;
  744. pEpochBuffer = NULL;
  745. if ( (iRetVal = pViof->ReadSamples (dFrom, dTo, (void**)&pcBuffer, piNumSamples, 1)) != VAPI_IOERR_NOERROR)
  746. {
  747. goto error;
  748. }
  749. pViof->CloseFile ();
  750. delete pViof;
  751. pViof = 0;
  752. // Convert the samples to output format
  753. if ( (*ppnSamples = (short*)new char [*piNumSamples * VapiIO::SizeOf(VAPI_PCM16)]) == NULL)
  754. {
  755. iRetVal = VAPI_IOERR_MEMORY;
  756. goto error;
  757. }
  758. VapiIO::DataFormatConversion ((char *)pcBuffer, iSampFormat, (char*) *ppnSamples, VAPI_PCM16, *piNumSamples);
  759. delete[] pcBuffer;
  760. return 1;
  761. error:
  762. if (pViof) {
  763. pViof->CloseFile ();
  764. delete pViof;
  765. }
  766. if (pEpochBuffer)
  767. {
  768. delete[] pEpochBuffer;
  769. }
  770. if (pcBuffer)
  771. {
  772. delete[] pcBuffer;
  773. }
  774. return iRetVal;
  775. }
  776. /*****************************************************************************
  777. * CSpeakerData::SetF0Weight *
  778. *--------------------------*
  779. * Description:
  780. *
  781. ******************************************************************* PACOG ***/
  782. void CSpeakerData::SetF0Weight (float fWeight)
  783. {
  784. m_weights.f0 = fWeight;
  785. }
  786. /*****************************************************************************
  787. * CSpeakerData::SetDurWeight *
  788. *---------------------------*
  789. * Description:
  790. *
  791. ******************************************************************* PACOG ***/
  792. void CSpeakerData::SetDurWeight (float fWeight)
  793. {
  794. m_weights.dur = fWeight;
  795. }
  796. /*****************************************************************************
  797. * CSpeakerData::SetRmsWeight *
  798. *---------------------------*
  799. * Description:
  800. *
  801. ******************************************************************* PACOG ***/
  802. void CSpeakerData::SetRmsWeight (float fWeight)
  803. {
  804. m_weights.rms = fWeight;
  805. }
  806. /*****************************************************************************
  807. * CSpeakerData::SetLklWeight *
  808. *---------------------------*
  809. * Description:
  810. *
  811. ******************************************************************* PACOG ***/
  812. void CSpeakerData::SetLklWeight (float fWeight)
  813. {
  814. m_weights.lkl = fWeight;
  815. }
  816. /*****************************************************************************
  817. * CSpeakerData::SetContWeight *
  818. *----------------------------*
  819. * Description:
  820. *
  821. ******************************************************************* PACOG ***/
  822. void CSpeakerData::SetContWeight (float fWeight)
  823. {
  824. m_weights.cont = fWeight;
  825. }
  826. /*****************************************************************************
  827. * CSpeakerData::SetSameWeight *
  828. *----------------------------*
  829. * Description:
  830. *
  831. ******************************************************************* PACOG ***/
  832. void CSpeakerData::SetSameWeight (float fWeight)
  833. {
  834. m_weights.sameSeg = fWeight;
  835. }
  836. /*****************************************************************************
  837. * CSpeakerData::SetPhBdrWeight *
  838. *------------------------------*
  839. * Description:
  840. *
  841. ******************************************************************* WD ***/
  842. void CSpeakerData::SetPhBdrWeight (float fWeight)
  843. {
  844. m_weights.phBdr = fWeight;
  845. }
  846. /*****************************************************************************
  847. * CSpeakerData::SetF0BdrWeight *
  848. *------------------------------*
  849. * Description:
  850. *
  851. ******************************************************************* WD ***/
  852. void CSpeakerData::SetF0BdrWeight (float fWeight)
  853. {
  854. m_weights.f0Bdr = fWeight;
  855. }
  856. /*****************************************************************************
  857. * CSpeakerData::GetWeights *
  858. *--------------------------*
  859. * Description:
  860. *
  861. ******************************************************************* PACOG ***/
  862. Weights CSpeakerData::GetWeights ()
  863. {
  864. return m_weights;
  865. }