sound_resource.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "sound_resource.h"
  6. #include "dynamic_string.h"
  7. #include "filesystem.h"
  8. #include "json_parser.h"
  9. #include "compile_options.h"
  10. namespace crown
  11. {
  12. namespace sound_resource
  13. {
  14. struct WAVHeader
  15. {
  16. char riff[4]; // Should contain 'RIFF'
  17. int32_t chunk_size; // Not Needed
  18. char wave[4]; // Should contain 'WAVE'
  19. char fmt[4]; // Should contain 'fmt '
  20. int32_t fmt_size; // Size of format chunk
  21. int16_t fmt_tag; // Identifies way data is stored, 1 means no compression
  22. int16_t fmt_channels; // Channel, 1 means mono, 2 means stereo
  23. int32_t fmt_sample_rate; // Samples per second
  24. int32_t fmt_avarage; // Avarage bytes per sample
  25. int16_t fmt_block_align; // Block alignment
  26. int16_t fmt_bits_ps; // Number of bits per sample
  27. char data[4]; // Should contain 'data'
  28. int32_t data_size; // Data dimension
  29. };
  30. void compile(const char* path, CompileOptions& opts)
  31. {
  32. const uint32_t VERSION = 1;
  33. Buffer buf = opts.read(path);
  34. JSONParser json(array::begin(buf));
  35. JSONElement root = json.root();
  36. DynamicString name;
  37. root.key("source").to_string(name);
  38. Buffer sound = opts.read(name.c_str());
  39. const WAVHeader* wav = (const WAVHeader*)array::begin(sound);
  40. const char* wavdata = (const char*) (wav + 1);
  41. // Write
  42. SoundResource sr;
  43. sr.version = VERSION;
  44. sr.size = wav->data_size;
  45. sr.sample_rate = wav->fmt_sample_rate;
  46. sr.avg_bytes_ps = wav->fmt_avarage;
  47. sr.channels = wav->fmt_channels;
  48. sr.block_size = wav->fmt_block_align;
  49. sr.bits_ps = wav->fmt_bits_ps;
  50. sr.sound_type = SoundType::WAV;
  51. opts.write(sr.version);
  52. opts.write(sr.size);
  53. opts.write(sr.sample_rate);
  54. opts.write(sr.avg_bytes_ps);
  55. opts.write(sr.channels);
  56. opts.write(sr.block_size);
  57. opts.write(sr.bits_ps);
  58. opts.write(sr.sound_type);
  59. opts.write(sr._pad[0]);
  60. opts.write(sr._pad[1]);
  61. opts.write(sr._pad[2]);
  62. opts.write(wavdata, wav->data_size);
  63. }
  64. void* load(File& file, Allocator& a)
  65. {
  66. const size_t file_size = file.size();
  67. void* res = a.allocate(file_size);
  68. file.read(res, file_size);
  69. return res;
  70. }
  71. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  72. {
  73. }
  74. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  75. {
  76. }
  77. void unload(Allocator& allocator, void* resource)
  78. {
  79. allocator.deallocate(resource);
  80. }
  81. uint32_t size(const SoundResource* sr)
  82. {
  83. return sr->size;
  84. }
  85. uint32_t sample_rate(const SoundResource* sr)
  86. {
  87. return sr->sample_rate;
  88. }
  89. uint32_t avg_bytes_ps(const SoundResource* sr)
  90. {
  91. return sr->avg_bytes_ps;
  92. }
  93. uint32_t channels(const SoundResource* sr)
  94. {
  95. return sr->channels;
  96. }
  97. uint16_t block_size(const SoundResource* sr)
  98. {
  99. return sr->block_size;
  100. }
  101. uint16_t bits_ps(const SoundResource* sr)
  102. {
  103. return sr->bits_ps;
  104. }
  105. uint8_t sound_type(const SoundResource* sr)
  106. {
  107. return sr->sound_type;
  108. }
  109. const char* data(const SoundResource* sr)
  110. {
  111. return (char*)sr + sizeof(SoundResource);
  112. }
  113. } // namespace sound_resource
  114. } // namespace crown