ResamplingReader.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author: Ben Allison [email protected]
  6. ** Created:
  7. **/
  8. #include "main.h"
  9. #include "ResamplingReader.h"
  10. ResamplingReader::ResamplingReader(Resampler *_resampler, CommonReader *_reader, size_t inputFrameSize)
  11. : resampler(_resampler), reader(_reader),
  12. bufferValid(0),
  13. readState(READING)
  14. {
  15. bufferAlloc = inputFrameSize * 1024; // enough room for 1024 samples
  16. buffer = (__int8 *)calloc(bufferAlloc, sizeof(__int8));
  17. }
  18. ResamplingReader::~ResamplingReader()
  19. {
  20. free(buffer);
  21. delete resampler;
  22. delete reader;
  23. }
  24. size_t ResamplingReader::ReadAudio(void *outputBuffer, size_t sizeBytes)
  25. {
  26. size_t origSize = sizeBytes;
  27. __int8 *origBuffer = (__int8 *)outputBuffer;
  28. size_t bytesResampled = 0;
  29. read_again:
  30. // First, read from the file decoder
  31. switch (readState)
  32. {
  33. case READING:
  34. {
  35. size_t bytesToRead = bufferAlloc - bufferValid;
  36. if (bytesToRead)
  37. {
  38. int decode_killswitch=0, decode_error;
  39. size_t bytesRead = reader->ReadAudio(buffer + (bufferAlloc - bytesToRead), bytesToRead, &decode_killswitch, &decode_error);
  40. bufferValid += bytesRead;
  41. if (bytesRead == 0)
  42. {
  43. readState = ENDOFFILE;
  44. }
  45. }
  46. }
  47. break;
  48. case ENDOFFILE:
  49. resampler->Flush();
  50. readState = FLUSHING;
  51. }
  52. // now, resample
  53. size_t inputBytes = bufferValid;
  54. size_t bytesDone;
  55. bytesDone = resampler->Convert(buffer, &inputBytes, outputBuffer, sizeBytes);
  56. bytesResampled += bytesDone;
  57. // if we didn't use all of our input buffer, then we'll copy what's left to the beginning
  58. if (inputBytes)
  59. memmove(buffer, buffer + (bufferValid - inputBytes), inputBytes);
  60. // mark the number of bytes of data still valid in the input buffer
  61. bufferValid = inputBytes;
  62. if (!bytesDone && readState == FLUSHING)
  63. readState = DONE;
  64. if (bytesResampled != origSize && readState != DONE) // if we didn't provide enough data to fill the output buffer
  65. {
  66. sizeBytes = origSize - bytesResampled;
  67. outputBuffer = origBuffer + bytesResampled;
  68. goto read_again;
  69. }
  70. return bytesResampled;
  71. }
  72. #define CBCLASS ResamplingReader
  73. START_DISPATCH;
  74. CB(IFC_AUDIOSTREAM_READAUDIO, ReadAudio)
  75. END_DISPATCH;