Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

Sound.cpp 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "resource/Sound.h"
  19. #include "BinReader.h"
  20. Sound::Sound(uint32_t id, const void* data, size_t size) : ResourceImpl{id}
  21. {
  22. BinReader reader(data, size);
  23. uint32_t resourceId = reader.readInt();
  24. assert(resourceId == id);
  25. UNUSED(resourceId);
  26. uint32_t type = reader.readInt();
  27. // There's one file that has type != 18
  28. // I'm going to ignore it till it comes up
  29. if(type != 18)
  30. {
  31. throw runtime_error("Type 18 sounds not supported");
  32. }
  33. uint32_t samplesLen = reader.readInt();
  34. uint16_t unk1 = reader.readShort();
  35. assert(unk1 == 1);
  36. UNUSED(unk1);
  37. uint16_t unk2 = reader.readShort();
  38. assert(unk2 == 1 || unk2 == 2);
  39. UNUSED(unk2);
  40. samplesPerSecond = reader.readInt();
  41. uint32_t totalSamplesPerSecond = reader.readInt();
  42. UNUSED(totalSamplesPerSecond);
  43. numChannels = reader.readShort();
  44. assert(numChannels == 1 || numChannels == 2 || numChannels == 4);
  45. assert(samplesPerSecond * numChannels == totalSamplesPerSecond);
  46. bitsPerSample = reader.readInt();
  47. assert(bitsPerSample == 8 || bitsPerSample == 16);
  48. const uint8_t* samplesPtr = reader.readRaw(samplesLen);
  49. assert(reader.remaining() == 0);
  50. samples.assign(samplesPtr, samplesPtr + samplesLen);
  51. }