SoundData.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) 2006-2011 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "SoundData.h"
  21. // C
  22. #include <climits>
  23. #include <cstdlib>
  24. #include <cstring>
  25. // STL
  26. #include <iostream>
  27. #include <vector>
  28. namespace love
  29. {
  30. namespace sound
  31. {
  32. SoundData::SoundData(Decoder * decoder)
  33. : data(0), size(0), sampleRate(Decoder::DEFAULT_SAMPLE_RATE), bits(0), channels(0)
  34. {
  35. int decoded = decoder->decode();
  36. while(decoded > 0)
  37. {
  38. // Expand or allocate buffer. Note that realloc may move
  39. // memory to other locations.
  40. data = (char*)realloc(data, size + decoder->getSize());
  41. if (!data)
  42. throw love::Exception("Not enough memory."); // I know, I know, little memory, creating objects..
  43. // Copy memory into new part of memory.
  44. memcpy(data + size, decoder->getBuffer(), decoded);
  45. // Keep this up to date.
  46. size += decoded;
  47. decoded = decoder->decode();
  48. }
  49. channels = decoder->getChannels();
  50. bits = decoder->getBits();
  51. sampleRate = decoder->getSampleRate();
  52. }
  53. SoundData::SoundData(int samples, int sampleRate, int bits, int channels)
  54. : data(0), size(samples*(bits/8)*channels), sampleRate(sampleRate), bits(bits), channels(channels)
  55. {
  56. double realsize = samples;
  57. realsize *= (bits/8)*channels;
  58. if (realsize > INT_MAX)
  59. throw love::Exception("Data is too big!");
  60. data = (char*)malloc(size);
  61. if (!data)
  62. throw love::Exception("Not enough memory.");
  63. }
  64. SoundData::SoundData(void * d, int samples, int sampleRate, int bits, int channels)
  65. : data(0), size(samples*(bits/8)*channels), sampleRate(sampleRate), bits(bits), channels(channels)
  66. {
  67. double realsize = samples;
  68. realsize *= (bits/8)*channels;
  69. if (realsize > INT_MAX)
  70. throw love::Exception("Data is too big!");
  71. data = (char*)malloc(size);
  72. if (!data)
  73. throw love::Exception("Not enough memory.");
  74. memcpy(data, d, size);
  75. }
  76. SoundData::~SoundData()
  77. {
  78. if(data != 0)
  79. free(data);
  80. }
  81. void * SoundData::getData() const
  82. {
  83. return (void*)data;
  84. }
  85. int SoundData::getSize() const
  86. {
  87. return (int)size;
  88. }
  89. int SoundData::getChannels() const
  90. {
  91. return channels;
  92. }
  93. int SoundData::getBits() const
  94. {
  95. return bits;
  96. }
  97. int SoundData::getSampleRate() const
  98. {
  99. return sampleRate;
  100. }
  101. void SoundData::setSample(int i, float sample)
  102. {
  103. // Check range.
  104. if(i < 0 || i >= size/(bits/8))
  105. return;
  106. if(bits == 16)
  107. {
  108. short * s = (short *)data;
  109. s[i] = (short)(sample*(float)SHRT_MAX);
  110. return;
  111. }
  112. else
  113. {
  114. data[i] = (char)(sample*(float)CHAR_MAX);
  115. return;
  116. }
  117. }
  118. float SoundData::getSample(int i) const
  119. {
  120. // Check range.
  121. if(i < 0 || i >= size/(bits/8))
  122. return 0;
  123. if(bits == 16)
  124. {
  125. short * s = (short *)data;
  126. return (float)s[i]/(float)SHRT_MAX;
  127. }
  128. else
  129. {
  130. return (float)data[i]/(float)CHAR_MAX;
  131. }
  132. }
  133. } // sound
  134. } // love