1
0

SoundBlock.cpp 973 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "SoundBlock.h"
  2. #include <memory.h>
  3. #include <malloc.h>
  4. SoundBlock::SoundBlock()
  5. {
  6. data = 0;
  7. size = 0;
  8. next = 0;
  9. prev = 0;
  10. start = 0;
  11. used = 0;
  12. }
  13. SoundBlock::~SoundBlock()
  14. {
  15. if (data) free(data);
  16. }
  17. void SoundBlock::SetData(void *new_data, size_t new_size)
  18. {
  19. if (!data) data = malloc(new_size);
  20. else if (new_size > size)
  21. {
  22. data = realloc(data, new_size);
  23. if (!data) data = malloc(new_size);
  24. else size = new_size;
  25. }
  26. if (data)
  27. {
  28. memcpy(data, new_data, new_size);
  29. used = new_size;
  30. }
  31. else
  32. {
  33. used = 0;
  34. }
  35. start = 0;
  36. }
  37. void SoundBlock::Advance(size_t d)
  38. {
  39. used -= d;
  40. start += d;
  41. }
  42. const void *SoundBlock::GetData()
  43. {
  44. return (char*)data + start;
  45. }
  46. size_t SoundBlock::GetDataSize()
  47. {
  48. return used;
  49. }
  50. size_t SoundBlock::Dump(void * out, size_t out_size)
  51. {
  52. const void * src = GetData();
  53. if (out_size > used) out_size = used;
  54. memcpy(out, src, out_size);
  55. Advance(out_size);
  56. return out_size;
  57. }
  58. void SoundBlock::Clear()
  59. {
  60. used = 0;
  61. }