SoundData.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Copyright (c) 2006-2014 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 <cstdlib>
  23. #include <cstring>
  24. // C++
  25. #include <limits>
  26. #include <iostream>
  27. #include <vector>
  28. namespace love
  29. {
  30. namespace sound
  31. {
  32. SoundData::SoundData(Decoder *decoder)
  33. : data(0)
  34. , size(0)
  35. , sampleRate(Decoder::DEFAULT_SAMPLE_RATE)
  36. , bitDepth(0)
  37. , channels(0)
  38. {
  39. size_t bufferSize = 524288; // 0x80000
  40. int decoded = decoder->decode();
  41. while (decoded > 0)
  42. {
  43. // Expand or allocate buffer. Note that realloc may move
  44. // memory to other locations.
  45. if (!data || bufferSize < size + decoded)
  46. {
  47. while (bufferSize < size + decoded)
  48. bufferSize <<= 1;
  49. data = (uint8 *) realloc(data, bufferSize);
  50. }
  51. if (!data)
  52. throw love::Exception("Not enough memory.");
  53. // Copy memory into new part of memory.
  54. memcpy(data + size, decoder->getBuffer(), decoded);
  55. // Overflow check.
  56. if (size > std::numeric_limits<size_t>::max() - decoded)
  57. {
  58. free(data);
  59. throw love::Exception("Not enough memory.");
  60. }
  61. // Keep this up to date.
  62. size += decoded;
  63. decoded = decoder->decode();
  64. }
  65. // Shrink buffer if necessary.
  66. if (data && bufferSize > size)
  67. data = (uint8 *) realloc(data, size);
  68. channels = decoder->getChannels();
  69. bitDepth = decoder->getBitDepth();
  70. sampleRate = decoder->getSampleRate();
  71. }
  72. SoundData::SoundData(int samples, int sampleRate, int bitDepth, int channels)
  73. : data(0)
  74. , size(0)
  75. , sampleRate(0)
  76. , bitDepth(0)
  77. , channels(0)
  78. {
  79. load(samples, sampleRate, bitDepth, channels);
  80. }
  81. SoundData::SoundData(void *d, int samples, int sampleRate, int bitDepth, int channels)
  82. : data(0)
  83. , size(0)
  84. , sampleRate(0)
  85. , bitDepth(0)
  86. , channels(0)
  87. {
  88. load(samples, sampleRate, bitDepth, channels, d);
  89. }
  90. SoundData::~SoundData()
  91. {
  92. if (data != 0)
  93. free(data);
  94. }
  95. void SoundData::load(int samples, int sampleRate, int bitDepth, int channels, void *newData)
  96. {
  97. if (samples <= 0)
  98. throw love::Exception("Invalid sample count: %d", samples);
  99. if (sampleRate <= 0)
  100. throw love::Exception("Invalid sample rate: %d", sampleRate);
  101. if (bitDepth <= 0)
  102. throw love::Exception("Invalid bit depth: %d", bitDepth);
  103. if (channels <= 0)
  104. throw love::Exception("Invalid channel count: %d", channels);
  105. if (data != 0)
  106. {
  107. free(data);
  108. data = 0;
  109. }
  110. size = samples * (bitDepth / 8) * channels;
  111. this->sampleRate = sampleRate;
  112. this->bitDepth = bitDepth;
  113. this->channels = channels;
  114. double realsize = samples;
  115. realsize *= (bitDepth / 8) * channels;
  116. if (realsize > std::numeric_limits<size_t>::max())
  117. throw love::Exception("Data is too big!");
  118. data = (uint8 *) malloc(size);
  119. if (!data)
  120. throw love::Exception("Not enough memory.");
  121. if (newData)
  122. memcpy(data, newData, size);
  123. else
  124. memset(data, bitDepth == 8 ? 128 : 0, size);
  125. }
  126. void *SoundData::getData() const
  127. {
  128. return (void *)data;
  129. }
  130. size_t SoundData::getSize() const
  131. {
  132. return size;
  133. }
  134. int SoundData::getChannels() const
  135. {
  136. return channels;
  137. }
  138. int SoundData::getBitDepth() const
  139. {
  140. return bitDepth;
  141. }
  142. int SoundData::getSampleRate() const
  143. {
  144. return sampleRate;
  145. }
  146. int SoundData::getSampleCount() const
  147. {
  148. return (size/channels)/(bitDepth/8);
  149. }
  150. float SoundData::getDuration() const
  151. {
  152. return float(size) / (channels*sampleRate*bitDepth/8);
  153. }
  154. void SoundData::setSample(int i, float sample)
  155. {
  156. // Check range.
  157. if (i < 0 || (size_t) i >= size/(bitDepth/8))
  158. throw love::Exception("Attempt to set out-of-range sample!");
  159. if (bitDepth == 16)
  160. {
  161. // 16-bit sample values are signed.
  162. int16 *s = (int16 *) data;
  163. s[i] = (int16) (sample * (float) LOVE_INT16_MAX);
  164. }
  165. else
  166. {
  167. // 8-bit sample values are unsigned internally.
  168. data[i] = (uint8) ((sample * 127.0f) + 128.0f);
  169. }
  170. }
  171. float SoundData::getSample(int i) const
  172. {
  173. // Check range.
  174. if (i < 0 || (size_t) i >= size/(bitDepth/8))
  175. throw love::Exception("Attempt to get out-of-range sample!");
  176. if (bitDepth == 16)
  177. {
  178. // 16-bit sample values are signed.
  179. int16 *s = (int16 *) data;
  180. return (float) s[i] / (float) LOVE_INT16_MAX;
  181. }
  182. else
  183. {
  184. // 8-bit sample values are unsigned internally.
  185. return ((float) data[i] - 128.0f) / 127.0f;
  186. }
  187. }
  188. } // sound
  189. } // love