sound_resource.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "json_parser.h"
  34. namespace crown
  35. {
  36. namespace sound_resource
  37. {
  38. struct WAVHeader
  39. {
  40. char riff[4]; // Should contain 'RIFF'
  41. int32_t chunk_size; // Not Needed
  42. char wave[4]; // Should contain 'WAVE'
  43. char fmt[4]; // Should contain '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; // Samples 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 contain 'data'
  52. int32_t data_size; // Data dimension
  53. };
  54. // size_t compile_if_ogg(Filesystem& fs, const char* resource_path)
  55. // {
  56. // // Retrieves resource absolute path
  57. // DynamicString s(default_allocator());
  58. // fs.get_absolute_path(resource_path, s);
  59. // const char* abs_path = s.c_str();
  60. // OggVorbis_File ogg_stream;
  61. // bool result = ov_fopen(os::normalize_path(abs_path), &ogg_stream) == 0;
  62. // if (result == false)
  63. // {
  64. // return 0;
  65. // }
  66. // vorbis_info* info = ov_info(&ogg_stream, -1);
  67. // int64_t size = ov_raw_total(&ogg_stream, -1);
  68. // int32_t rate = info->rate;
  69. // int32_t channels = info->channels;
  70. // ov_clear(&ogg_stream);
  71. // File* in_file = fs.open(resource_path, FOM_READ);
  72. // m_sound_header.version = SOUND_VERSION;
  73. // m_sound_header.size = size;
  74. // m_sound_header.sample_rate = rate;
  75. // m_sound_header.block_size = (channels * 16) / 8;
  76. // m_sound_header.avg_bytes_ps = rate * ((channels * 16) / 8);
  77. // m_sound_header.channels = channels;
  78. // m_sound_header.bits_ps = 16;
  79. // m_sound_header.sound_type = SoundType::OGG;
  80. // m_sound_data_size = size;
  81. // m_sound_data = (uint8_t*)default_allocator().allocate(m_sound_data_size);
  82. // in_file->read((char*)m_sound_data, m_sound_data_size);
  83. // fs.close(in_file);
  84. // return sizeof(SoundHeader) + m_sound_data_size;
  85. // }
  86. void compile(const char* path, CompileOptions& opts)
  87. {
  88. const uint32_t VERSION = 1;
  89. Buffer buf = opts.read(path);
  90. JSONParser json(array::begin(buf));
  91. JSONElement root = json.root();
  92. DynamicString name;
  93. root.key("source").to_string(name);
  94. Buffer sound = opts.read(name.c_str());
  95. const WAVHeader* wav = (const WAVHeader*)array::begin(sound);
  96. const char* wavdata = (const char*) (wav + 1);
  97. // Write
  98. SoundResource sr;
  99. sr.version = VERSION;
  100. sr.size = wav->data_size;
  101. sr.sample_rate = wav->fmt_sample_rate;
  102. sr.avg_bytes_ps = wav->fmt_avarage;
  103. sr.channels = wav->fmt_channels;
  104. sr.block_size = wav->fmt_block_align;
  105. sr.bits_ps = wav->fmt_bits_ps;
  106. sr.sound_type = SoundType::WAV;
  107. opts.write(sr.version);
  108. opts.write(sr.size);
  109. opts.write(sr.sample_rate);
  110. opts.write(sr.avg_bytes_ps);
  111. opts.write(sr.channels);
  112. opts.write(sr.block_size);
  113. opts.write(sr.bits_ps);
  114. opts.write(sr.sound_type);
  115. opts.write(sr._pad[0]);
  116. opts.write(sr._pad[1]);
  117. opts.write(sr._pad[2]);
  118. opts.write(wavdata, wav->data_size);
  119. }
  120. void* load(File& file, Allocator& a)
  121. {
  122. const size_t file_size = file.size();
  123. void* res = a.allocate(file_size);
  124. file.read(res, file_size);
  125. return res;
  126. }
  127. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  128. {
  129. }
  130. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  131. {
  132. }
  133. void unload(Allocator& allocator, void* resource)
  134. {
  135. allocator.deallocate(resource);
  136. }
  137. uint32_t size(const SoundResource* sr)
  138. {
  139. return sr->size;
  140. }
  141. uint32_t sample_rate(const SoundResource* sr)
  142. {
  143. return sr->sample_rate;
  144. }
  145. uint32_t avg_bytes_ps(const SoundResource* sr)
  146. {
  147. return sr->avg_bytes_ps;
  148. }
  149. uint32_t channels(const SoundResource* sr)
  150. {
  151. return sr->channels;
  152. }
  153. uint16_t block_size(const SoundResource* sr)
  154. {
  155. return sr->block_size;
  156. }
  157. uint16_t bits_ps(const SoundResource* sr)
  158. {
  159. return sr->bits_ps;
  160. }
  161. uint8_t sound_type(const SoundResource* sr)
  162. {
  163. return sr->sound_type;
  164. }
  165. const char* data(const SoundResource* sr)
  166. {
  167. return (char*)sr + sizeof(SoundResource);
  168. }
  169. } // namespace sound_resource
  170. } // namespace crown