1
0

ExtendedReader.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "ExtendedReader.h"
  10. #include "../nu/AutoCharFn.h"
  11. ExtendedReader::ExtendedReader(OpenFunc _open, OpenWFunc _openW, OpenFunc _openFloat, OpenWFunc _openWFloat, GetDataFunc _getData, CloseFunc _close, SetTimeFunc _setTime)
  12. : open(_open), openFloat(_openFloat), openW(_openW), openWFloat(_openWFloat), getData(_getData), close(_close), setTime(_setTime), handle(0)
  13. {}
  14. bool ExtendedReader::Open(const wchar_t *filename, AudioParameters *parameters)
  15. {
  16. if (parameters->flags & AUDIOPARAMETERS_FLOAT)
  17. {
  18. if (this->openWFloat)
  19. handle = this->openWFloat(filename, (int *) & parameters->sizeBytes, (int *) & parameters->bitsPerSample, (int *) & parameters->channels, (int *) & parameters->sampleRate);
  20. else if (this->openFloat)
  21. handle = this->openFloat(AutoCharFn(filename), (int *) & parameters->sizeBytes, (int *) & parameters->bitsPerSample, (int *) & parameters->channels, (int *) & parameters->sampleRate);
  22. }
  23. else
  24. {
  25. if (this->openW)
  26. handle = this->openW(filename, (int *) & parameters->sizeBytes, (int *) & parameters->bitsPerSample, (int *) & parameters->channels, (int *) & parameters->sampleRate);
  27. else
  28. handle = this->open(AutoCharFn(filename), (int *) & parameters->sizeBytes, (int *) & parameters->bitsPerSample, (int *) & parameters->channels, (int *) & parameters->sampleRate);
  29. }
  30. if (handle > 0)
  31. return true;
  32. else
  33. {
  34. if (handle == -1)
  35. parameters->errorCode = API_DECODEFILE_FAIL_NO_WARN;
  36. else
  37. parameters->errorCode = API_DECODEFILE_FAILURE;
  38. return false;
  39. }
  40. }
  41. size_t ExtendedReader::ReadAudio(void *buffer, size_t sizeBytes)
  42. {
  43. int killswitch = 0;
  44. return this->getData(handle, buffer, sizeBytes, &killswitch);
  45. }
  46. size_t ExtendedReader::ReadAudio_kill(void *buffer, size_t sizeBytes, int *killswitch, int *error)
  47. {
  48. *error=0;
  49. intptr_t ret = this->getData(handle, buffer, sizeBytes, killswitch);
  50. if (ret < 0)
  51. {
  52. *error=1;
  53. return 0;
  54. }
  55. else
  56. return static_cast<size_t>(ret);
  57. }
  58. BOOL ExtendedReader::SeekToTimeMs(int millisecs)
  59. {
  60. if (!setTime)
  61. return FALSE;
  62. return this->setTime(handle, millisecs);
  63. }
  64. int ExtendedReader::CanSeek()
  65. {
  66. return setTime?1:0;
  67. }
  68. ExtendedReader::~ExtendedReader()
  69. {
  70. if (handle)
  71. this->close(handle);
  72. }
  73. #define CBCLASS ExtendedReader
  74. START_DISPATCH;
  75. CB(IFC_AUDIOSTREAM_READAUDIO, ReadAudio)
  76. CB(IFC_AUDIOSTREAM_READAUDIO2, ReadAudio_kill)
  77. CB(IFC_AUDIOSTREAM_SEEKTOTIMEMS, SeekToTimeMs)
  78. CB(IFC_AUDIOSTREAM_CANSEEK, CanSeek)
  79. END_DISPATCH;