sound_resource.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include <stdio.h>
  24. #include <limits.h>
  25. #include <errno.h>
  26. #include "allocator.h"
  27. #include "config.h"
  28. #include "dynamic_string.h"
  29. #include "filesystem.h"
  30. #include "os.h"
  31. #include "sound_resource.h"
  32. #include "string_utils.h"
  33. namespace crown
  34. {
  35. namespace sound_resource
  36. {
  37. //-----------------------------------------------------------------------------
  38. struct WAVHeader
  39. {
  40. char riff[4]; // Should contains 'RIFF'
  41. int32_t chunk_size; // Not Needed
  42. char wave[4]; // Should contains 'WAVE'
  43. char fmt[4]; // Should contains 'fmt '
  44. int32_t fmt_size; // Size of format chunk
  45. int16_t fmt_tag; // Identifies way data is stored, 1 means no compression
  46. int16_t fmt_channels; // Channel, 1 means mono, 2 means stereo
  47. int32_t fmt_sample_rate; // Sample per second
  48. int32_t fmt_avarage; // Avarage bytes per sample
  49. int16_t fmt_block_align; // Block alignment
  50. int16_t fmt_bits_ps; // Number of bits per sample
  51. char data[4]; // Should contains 'data'
  52. int32_t data_size; // Data dimension
  53. };
  54. SoundHeader m_sound_header;
  55. size_t m_sound_data_size = 0;
  56. uint8_t* m_sound_data = NULL;
  57. //-----------------------------------------------------------------------------
  58. size_t compile_if_wav(Filesystem& fs, const char* resource_path)
  59. {
  60. File* in_file = fs.open(resource_path, FOM_READ);
  61. WAVHeader header;
  62. in_file->read((char*)&header, sizeof(WAVHeader));
  63. if (header.riff[0] != 'R' && header.riff[1] != 'I' && header.riff[2] != 'F' && header.riff[3] != 'F')
  64. {
  65. if (header.wave[0] != 'W' && header.wave[1] != 'A' && header.wave[2] != 'V' && header.wave[3] != 'E')
  66. {
  67. if (header.fmt[0] != 'f' && header.fmt[1] != 'm' && header.fmt[2] != 't' && header.fmt[3] != ' ')
  68. {
  69. fs.close(in_file);
  70. return 0;
  71. }
  72. }
  73. }
  74. m_sound_header.version = SOUND_VERSION;
  75. m_sound_header.size = header.data_size;
  76. m_sound_header.sample_rate = header.fmt_sample_rate;
  77. m_sound_header.avg_bytes_ps = header.fmt_avarage;
  78. m_sound_header.channels = header.fmt_channels;
  79. m_sound_header.block_size = header.fmt_block_align;
  80. m_sound_header.bits_ps = header.fmt_bits_ps;
  81. m_sound_header.sound_type = SoundType::WAV;
  82. m_sound_data_size = header.data_size;
  83. m_sound_data = (uint8_t*)default_allocator().allocate(m_sound_data_size);
  84. in_file->read((char*)m_sound_data, m_sound_data_size);
  85. fs.close(in_file);
  86. return sizeof(SoundHeader) + m_sound_data_size;
  87. }
  88. // //-----------------------------------------------------------------------------
  89. // size_t compile_if_ogg(Filesystem& fs, const char* resource_path)
  90. // {
  91. // // Retrieves resource absolute path
  92. // DynamicString s(default_allocator());
  93. // fs.get_absolute_path(resource_path, s);
  94. // const char* abs_path = s.c_str();
  95. // OggVorbis_File ogg_stream;
  96. // bool result = ov_fopen(os::normalize_path(abs_path), &ogg_stream) == 0;
  97. // if (result == false)
  98. // {
  99. // return 0;
  100. // }
  101. // vorbis_info* info = ov_info(&ogg_stream, -1);
  102. // int64_t size = ov_raw_total(&ogg_stream, -1);
  103. // int32_t rate = info->rate;
  104. // int32_t channels = info->channels;
  105. // ov_clear(&ogg_stream);
  106. // File* in_file = fs.open(resource_path, FOM_READ);
  107. // m_sound_header.version = SOUND_VERSION;
  108. // m_sound_header.size = size;
  109. // m_sound_header.sample_rate = rate;
  110. // m_sound_header.block_size = (channels * 16) / 8;
  111. // m_sound_header.avg_bytes_ps = rate * ((channels * 16) / 8);
  112. // m_sound_header.channels = channels;
  113. // m_sound_header.bits_ps = 16;
  114. // m_sound_header.sound_type = SoundType::OGG;
  115. // m_sound_data_size = size;
  116. // m_sound_data = (uint8_t*)default_allocator().allocate(m_sound_data_size);
  117. // in_file->read((char*)m_sound_data, m_sound_data_size);
  118. // fs.close(in_file);
  119. // return sizeof(SoundHeader) + m_sound_data_size;
  120. // }
  121. //-----------------------------------------------------------------------------
  122. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  123. {
  124. size_t size = 0;
  125. size = compile_if_wav(fs, resource_path);
  126. if (size == 0)
  127. {
  128. size = 0; // compile_if_ogg(fs, resource_path);
  129. }
  130. out_file->write((char*)&m_sound_header, sizeof(SoundHeader));
  131. out_file->write((char*)m_sound_data, m_sound_data_size);
  132. if (m_sound_data)
  133. {
  134. default_allocator().deallocate(m_sound_data);
  135. m_sound_data_size = 0;
  136. m_sound_data = NULL;
  137. }
  138. }
  139. } // namespace sound_resource
  140. } // namespace crown