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.

261 lines
5.9 KiB

  1. //==========================================================================
  2. //
  3. // tsmain.cpp --
  4. //
  5. // Copyright (c) 2000 Microsoft, Corp.
  6. // Copyright (c) 1998 Entropic, Inc.
  7. //
  8. //==========================================================================
  9. #include "tsm.h"
  10. #include "getopt.h"
  11. #include "vapiio.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <iostream.h>
  15. #define SYNTAX fprintf(stderr,"Usage: tsm [-t (timescale)] infile [outfile]\n")
  16. struct TInput
  17. {
  18. char input[MAX_PATH+1];
  19. char output[MAX_PATH+1];
  20. double from;
  21. double to;
  22. double timeScale;
  23. };
  24. /*
  25. *
  26. */
  27. static int ProcessCommandLine (int argc, char *argv[], TInput* pInInfo);
  28. //--------------------------------------------------------------------------
  29. //
  30. // MAIN
  31. //
  32. //--------------------------------------------------------------------------
  33. int main(int argc, char **argv)
  34. {
  35. TInput inInfo;
  36. CTsm* tsm;
  37. VapiIO* pOutputFile;
  38. int iSampFreq;
  39. int iFormat;
  40. short* pnSamples;
  41. int iNumSamples;
  42. double* pfBuffer = NULL;
  43. short* pnBuffer = NULL;
  44. int firstSample;
  45. int frameLen;
  46. int frameShift;
  47. int lag;
  48. int lastProcSamp;
  49. int nSamp;
  50. int fileEnd;
  51. if (!ProcessCommandLine (argc, argv, &inInfo))
  52. {
  53. SYNTAX;
  54. return 1;
  55. }
  56. if (VapiIO::ReadVapiFile(inInfo.input, &pnSamples, &iNumSamples, &iSampFreq, &iFormat,
  57. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) != VAPI_IOERR_NOERROR)
  58. {
  59. cerr << "Error reading input file " <<inInfo.input << endl;
  60. return 1;
  61. }
  62. if ((tsm = new CTsm(inInfo.timeScale, iSampFreq, &frameLen, &frameShift)) == NULL)
  63. {
  64. cerr << "Error initializing tsm" << endl;
  65. return 1;
  66. }
  67. if ( (pfBuffer = new double[frameLen]) == NULL)
  68. {
  69. cerr << "Memory error" << endl;
  70. return 1;
  71. }
  72. if ( (pnBuffer = new short[frameLen]) == NULL)
  73. {
  74. cerr << "Memory error" << endl;
  75. return 1;
  76. }
  77. // Open output file, and prepare for writing
  78. if ((pOutputFile = VapiIO::ClassFactory()) == NULL)
  79. {
  80. cerr << "Memory error" << endl;
  81. return 1;
  82. }
  83. if (pOutputFile->OpenFile (inInfo.output, VAPI_IO_WRITE) != VAPI_IOERR_NOERROR)
  84. {
  85. cerr << "Error opening output file " << inInfo.output << endl;
  86. return 1;
  87. }
  88. if (pOutputFile->WriteFormat( iSampFreq, VAPI_PCM16) != VAPI_IOERR_NOERROR)
  89. {
  90. cerr << "Error formating output file " << inInfo.output << endl;
  91. return 1;
  92. }
  93. if (pOutputFile->CreateChunk( "data") != VAPI_IOERR_NOERROR)
  94. {
  95. cerr << "Error formating output file " << inInfo.output << endl;
  96. return 1;
  97. }
  98. // First frame to process
  99. for (int i=0; i<frameLen; i++)
  100. {
  101. pfBuffer[i] = (double) pnSamples[i];
  102. }
  103. lastProcSamp = tsm->FirstFrame(pfBuffer);
  104. pOutputFile->WriteToChunk ((char *)pnSamples, lastProcSamp * VapiIO::SizeOf(VAPI_PCM16));
  105. // LOOP OVER FRAMES
  106. fileEnd = 0;
  107. firstSample = 0;
  108. while (!fileEnd)
  109. {
  110. for (i=0; i<frameLen; i++)
  111. {
  112. if (firstSample + i < iNumSamples)
  113. {
  114. pfBuffer[i] = pnSamples[firstSample + i];
  115. }
  116. else
  117. {
  118. frameLen = i;
  119. fileEnd = 1;
  120. break;
  121. }
  122. }
  123. if (fileEnd)
  124. {
  125. lastProcSamp = tsm->LastFrame (pfBuffer, frameLen, &lag, &nSamp);
  126. }
  127. else
  128. {
  129. lastProcSamp = tsm->AddFrame (pfBuffer, &lag, &nSamp);
  130. }
  131. for (i = 0; i< nSamp; i++)
  132. {
  133. pnBuffer[i] = (short)pfBuffer[i+lag];
  134. }
  135. pOutputFile->WriteToChunk ((char*)pnBuffer, nSamp * VapiIO::SizeOf(VAPI_PCM16));
  136. firstSample += frameShift;
  137. }
  138. // Flush the remaining samples
  139. if (frameLen > lastProcSamp)
  140. {
  141. for (i = 0; i< frameLen - lastProcSamp; i++)
  142. {
  143. pnBuffer[i] = (short)pfBuffer[i+lastProcSamp];
  144. }
  145. pOutputFile->WriteToChunk ((char*)pnBuffer, frameLen - lastProcSamp);
  146. }
  147. pOutputFile->CloseChunk ();
  148. pOutputFile->CloseFile ();
  149. delete tsm;
  150. delete[] pfBuffer;
  151. delete[] pnBuffer;
  152. delete[] pnSamples;
  153. return 0;
  154. }
  155. //--------------------------------------------------------------------------
  156. //
  157. // ProcessCommandLine
  158. //
  159. //--------------------------------------------------------------------------
  160. int ProcessCommandLine (int argc, char *argv[], TInput* pInInfo)
  161. {
  162. int iCh;
  163. CGetOpt getOpt;
  164. memset (pInInfo, 0 ,sizeof(*pInInfo));
  165. strcpy (pInInfo->output, "-");
  166. pInInfo->to = -1.0;
  167. pInInfo->timeScale = 1.0;
  168. getOpt.Init(argc, argv, "s:t:");
  169. while ((iCh = getOpt.NextOption()) != EOF)
  170. {
  171. switch (iCh)
  172. {
  173. case 's':
  174. {
  175. int iNumMatch=sscanf(getOpt.OptArg(),"%lf:%lf",&(pInInfo->from), &(pInInfo->to));
  176. if (!iNumMatch)
  177. {
  178. iNumMatch=sscanf(getOpt.OptArg(),":%lf",&(pInInfo->to));
  179. if (!iNumMatch || iNumMatch==EOF)
  180. {
  181. fprintf(stderr,"Invalid parameters with the option -s");
  182. exit(1);
  183. }
  184. }
  185. }
  186. break;
  187. case 't':
  188. pInInfo->timeScale = atof(getOpt.OptArg());
  189. if (pInInfo->timeScale<0.0)
  190. {
  191. fprintf (stderr, "timescale must be > 0.0\n");
  192. return 0;
  193. }
  194. break;
  195. default:
  196. return 0;
  197. }
  198. }
  199. int optind = getOpt.OptInd();
  200. if ((argc-optind) == 0) {
  201. return 0;
  202. }
  203. strcpy (pInInfo->input, argv[optind++]);
  204. if (argc-optind == 1) {
  205. strcpy (pInInfo->output, argv[optind++]);
  206. } else if (argc-optind) {
  207. return 0;
  208. }
  209. return 1;
  210. }