SDL_wave.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // RIFF WAVE files are little-endian
  20. /*******************************************/
  21. // Define values for Microsoft WAVE format
  22. /*******************************************/
  23. // FOURCC
  24. #define RIFF 0x46464952 // "RIFF"
  25. #define WAVE 0x45564157 // "WAVE"
  26. #define FACT 0x74636166 // "fact"
  27. #define LIST 0x5453494c // "LIST"
  28. #define BEXT 0x74786562 // "bext"
  29. #define JUNK 0x4B4E554A // "JUNK"
  30. #define FMT 0x20746D66 // "fmt "
  31. #define DATA 0x61746164 // "data"
  32. // Format tags
  33. #define UNKNOWN_CODE 0x0000
  34. #define PCM_CODE 0x0001
  35. #define MS_ADPCM_CODE 0x0002
  36. #define IEEE_FLOAT_CODE 0x0003
  37. #define ALAW_CODE 0x0006
  38. #define MULAW_CODE 0x0007
  39. #define IMA_ADPCM_CODE 0x0011
  40. #define MPEG_CODE 0x0050
  41. #define MPEGLAYER3_CODE 0x0055
  42. #define EXTENSIBLE_CODE 0xFFFE
  43. // Stores the WAVE format information.
  44. typedef struct WaveFormat
  45. {
  46. Uint16 formattag; // Raw value of the first field in the fmt chunk data.
  47. Uint16 encoding; // Actual encoding, possibly from the extensible header.
  48. Uint16 channels; // Number of channels.
  49. Uint32 frequency; // Sampling rate in Hz.
  50. Uint32 byterate; // Average bytes per second.
  51. Uint16 blockalign; // Bytes per block.
  52. Uint16 bitspersample; // Currently supported are 8, 16, 24, 32, and 4 for ADPCM.
  53. /* Extra information size. Number of extra bytes starting at byte 18 in the
  54. * fmt chunk data. This is at least 22 for the extensible header.
  55. */
  56. Uint16 extsize;
  57. // Extensible WAVE header fields
  58. Uint16 validsamplebits;
  59. Uint32 samplesperblock; // For compressed formats. Can be zero. Actually 16 bits in the header.
  60. Uint32 channelmask;
  61. Uint8 subformat[16]; // A format GUID.
  62. } WaveFormat;
  63. // Stores information on the fact chunk.
  64. typedef struct WaveFact
  65. {
  66. /* Represents the state of the fact chunk in the WAVE file.
  67. * Set to -1 if the fact chunk is invalid.
  68. * Set to 0 if the fact chunk is not present
  69. * Set to 1 if the fact chunk is present and valid.
  70. * Set to 2 if samplelength is going to be used as the number of sample frames.
  71. */
  72. Sint32 status;
  73. /* Version 1 of the RIFF specification calls the field in the fact chunk
  74. * dwFileSize. The Standards Update then calls it dwSampleLength and specifies
  75. * that it is 'the length of the data in samples'. WAVE files from Windows
  76. * with this chunk have it set to the samples per channel (sample frames).
  77. * This is useful to truncate compressed audio to a specific sample count
  78. * because a compressed block is usually decoded to a fixed number of
  79. * sample frames.
  80. */
  81. Uint32 samplelength; // Raw sample length value from the fact chunk.
  82. } WaveFact;
  83. // Generic struct for the chunks in the WAVE file.
  84. typedef struct WaveChunk
  85. {
  86. Uint32 fourcc; // FOURCC of the chunk.
  87. Uint32 length; // Size of the chunk data.
  88. Sint64 position; // Position of the data in the stream.
  89. Uint8 *data; // When allocated, this points to the chunk data. length is used for the memory allocation size.
  90. size_t size; // Number of bytes in data that could be read from the stream. Can be smaller than length.
  91. } WaveChunk;
  92. // Controls how the size of the RIFF chunk affects the loading of a WAVE file.
  93. typedef enum WaveRiffSizeHint
  94. {
  95. RiffSizeNoHint,
  96. RiffSizeForce,
  97. RiffSizeIgnoreZero,
  98. RiffSizeIgnore,
  99. RiffSizeMaximum
  100. } WaveRiffSizeHint;
  101. // Controls how a truncated WAVE file is handled.
  102. typedef enum WaveTruncationHint
  103. {
  104. TruncNoHint,
  105. TruncVeryStrict,
  106. TruncStrict,
  107. TruncDropFrame,
  108. TruncDropBlock
  109. } WaveTruncationHint;
  110. // Controls how the fact chunk affects the loading of a WAVE file.
  111. typedef enum WaveFactChunkHint
  112. {
  113. FactNoHint,
  114. FactTruncate,
  115. FactStrict,
  116. FactIgnoreZero,
  117. FactIgnore
  118. } WaveFactChunkHint;
  119. typedef struct WaveFile
  120. {
  121. WaveChunk chunk;
  122. WaveFormat format;
  123. WaveFact fact;
  124. /* Number of sample frames that will be decoded. Calculated either with the
  125. * size of the data chunk or, if the appropriate hint is enabled, with the
  126. * sample length value from the fact chunk.
  127. */
  128. Sint64 sampleframes;
  129. void *decoderdata; // Some decoders require extra data for a state.
  130. WaveRiffSizeHint riffhint;
  131. WaveTruncationHint trunchint;
  132. WaveFactChunkHint facthint;
  133. } WaveFile;