SoundResource.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "Resource.h"
  26. #include "Bundle.h"
  27. #include "Allocator.h"
  28. #include "File.h"
  29. #include "Device.h"
  30. #include "SoundRenderer.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. struct SoundType
  35. {
  36. enum Enum
  37. {
  38. WAV = 0,
  39. OGG = 1
  40. };
  41. };
  42. const uint32_t SOUND_VERSION = 1;
  43. //-----------------------------------------------------------------------------
  44. struct SoundHeader
  45. {
  46. SoundBufferId sb;
  47. uint32_t version; // Sound file version
  48. uint32_t size;
  49. uint32_t sample_rate;
  50. uint32_t avg_bytes_ps;
  51. uint32_t channels;
  52. uint16_t block_size;
  53. uint16_t bits_ps;
  54. uint8_t sound_type;
  55. };
  56. struct SoundResource
  57. {
  58. public:
  59. //-----------------------------------------------------------------------------
  60. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  61. {
  62. File* file = bundle.open(id);
  63. const size_t file_size = file->size();
  64. void* res = allocator.allocate(file_size);
  65. file->read(res, file_size);
  66. bundle.close(file);
  67. return res;
  68. }
  69. //-----------------------------------------------------------------------------
  70. static void online(void* resource)
  71. {
  72. CE_ASSERT_NOT_NULL(resource);
  73. SoundResource* sr = (SoundResource*)resource;
  74. SoundHeader* h = (SoundHeader*) sr;
  75. h->sb = device()->sound_renderer()->create_sound_buffer((void*)sr->data(), sr->size(), sr->sample_rate(), sr->channels(), sr->bits_ps());
  76. }
  77. //-----------------------------------------------------------------------------
  78. static void unload(Allocator& allocator, void* resource)
  79. {
  80. CE_ASSERT_NOT_NULL(resource);
  81. allocator.deallocate(resource);
  82. }
  83. //-----------------------------------------------------------------------------
  84. static void offline(void* resource)
  85. {
  86. CE_ASSERT_NOT_NULL(resource);
  87. SoundHeader* s = (SoundHeader*) resource;
  88. device()->sound_renderer()->destroy_sound_buffer(s->sb);
  89. }
  90. public:
  91. uint32_t size() const
  92. {
  93. return ((SoundHeader*) this)->size;
  94. }
  95. uint32_t sample_rate() const
  96. {
  97. return ((SoundHeader*) this)->sample_rate;
  98. }
  99. uint32_t avg_bytes_ps() const
  100. {
  101. return ((SoundHeader*) this)->avg_bytes_ps;
  102. }
  103. uint32_t channels() const
  104. {
  105. return ((SoundHeader*) this)->channels;
  106. }
  107. uint16_t block_size() const
  108. {
  109. return ((SoundHeader*) this)->block_size;
  110. }
  111. uint16_t bits_ps() const
  112. {
  113. return ((SoundHeader*) this)->bits_ps;
  114. }
  115. uint8_t sound_type() const
  116. {
  117. return ((SoundHeader*) this)->sound_type;
  118. }
  119. const char* data() const
  120. {
  121. return ((char*) this) + sizeof(SoundHeader);
  122. }
  123. SoundBufferId sound_buffer() const
  124. {
  125. return ((SoundHeader*) this)->sb;
  126. }
  127. private:
  128. // Disable construction
  129. SoundResource();
  130. };
  131. } // namespace crown