SoundResource.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. uint32_t version; // Sound file version
  47. uint32_t size;
  48. uint32_t sample_rate;
  49. uint32_t avg_bytes_ps;
  50. uint32_t channels;
  51. uint16_t block_size;
  52. uint16_t bits_ps;
  53. uint8_t sound_type;
  54. };
  55. class SoundResource
  56. {
  57. public:
  58. //-----------------------------------------------------------------------------
  59. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  60. {
  61. File* file = bundle.open(id);
  62. SoundResource* resource = (SoundResource*)allocator.allocate(sizeof(SoundResource));
  63. file->read(&resource->m_header, sizeof(SoundHeader));
  64. size_t size = resource->size();
  65. resource->m_data = (uint8_t*)allocator.allocate(sizeof(uint8_t) * size);
  66. file->read(resource->m_data, size);
  67. bundle.close(file);
  68. return resource;
  69. }
  70. //-----------------------------------------------------------------------------
  71. static void online(void* resource)
  72. {
  73. CE_ASSERT(resource != NULL, "Resource not loaded");
  74. SoundResource* s = (SoundResource*)resource;
  75. SoundBufferId id = device()->sound_renderer()->create_sound_buffer((void*)s->data(), s->size(), s->sample_rate(), s->channels(), s->bits_ps());
  76. s->m_id = id;
  77. }
  78. //-----------------------------------------------------------------------------
  79. static void unload(Allocator& allocator, void* resource)
  80. {
  81. CE_ASSERT(resource != NULL, "Resource not loaded");
  82. allocator.deallocate(((SoundResource*)resource)->m_data);
  83. allocator.deallocate(resource);
  84. }
  85. //-----------------------------------------------------------------------------
  86. static void offline(void* resource)
  87. {
  88. CE_ASSERT(resource != NULL, "Resource not loaded");
  89. SoundResource* s = (SoundResource*)resource;
  90. device()->sound_renderer()->destroy_sound_buffer(s->m_id);
  91. }
  92. public:
  93. uint32_t size() const { return m_header.size; }
  94. uint32_t sample_rate() const { return m_header.sample_rate; }
  95. uint32_t avg_bytes_ps() const { return m_header.avg_bytes_ps; }
  96. uint32_t channels() const { return m_header.channels; }
  97. uint16_t block_size() const { return m_header.block_size; }
  98. uint16_t bits_ps() const { return m_header.bits_ps; }
  99. uint8_t sound_type() const { return m_header.sound_type; }
  100. const uint8_t* data() const { return m_data; }
  101. public:
  102. SoundHeader m_header;
  103. uint8_t* m_data;
  104. SoundBufferId m_id;
  105. };
  106. } // namespace crown