WavLoader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "WavLoader.h"
  8. #include "LoaderError.h"
  9. #include <AK/Debug.h>
  10. #include <AK/Endian.h>
  11. #include <AK/FixedArray.h>
  12. #include <AK/NumericLimits.h>
  13. #include <AK/Try.h>
  14. #include <LibCore/MemoryStream.h>
  15. namespace Audio {
  16. static constexpr size_t const maximum_wav_size = 1 * GiB; // FIXME: is there a more appropriate size limit?
  17. WavLoaderPlugin::WavLoaderPlugin(StringView path)
  18. : m_file(Core::File::construct(path))
  19. {
  20. }
  21. MaybeLoaderError WavLoaderPlugin::initialize()
  22. {
  23. if (m_backing_memory.has_value())
  24. m_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(m_backing_memory.value()));
  25. else
  26. m_stream = LOADER_TRY(Core::Stream::File::open(m_file->filename(), Core::Stream::OpenMode::Read));
  27. TRY(parse_header());
  28. return {};
  29. }
  30. WavLoaderPlugin::WavLoaderPlugin(Bytes const& buffer)
  31. : m_backing_memory(buffer)
  32. {
  33. }
  34. template<typename SampleReader>
  35. MaybeLoaderError WavLoaderPlugin::read_samples_from_stream(Core::Stream::Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const
  36. {
  37. switch (m_num_channels) {
  38. case 1:
  39. for (auto& sample : samples)
  40. sample = Sample(LOADER_TRY(read_sample(stream)));
  41. break;
  42. case 2:
  43. for (auto& sample : samples) {
  44. auto left_channel_sample = LOADER_TRY(read_sample(stream));
  45. auto right_channel_sample = LOADER_TRY(read_sample(stream));
  46. sample = Sample(left_channel_sample, right_channel_sample);
  47. }
  48. break;
  49. default:
  50. VERIFY_NOT_REACHED();
  51. }
  52. return {};
  53. }
  54. // There's no i24 type + we need to do the endianness conversion manually anyways.
  55. static ErrorOr<double> read_sample_int24(Core::Stream::Stream& stream)
  56. {
  57. u8 byte = 0;
  58. TRY(stream.read(Bytes { &byte, 1 }));
  59. i32 sample1 = byte;
  60. TRY(stream.read(Bytes { &byte, 1 }));
  61. i32 sample2 = byte;
  62. TRY(stream.read(Bytes { &byte, 1 }));
  63. i32 sample3 = byte;
  64. i32 value = 0;
  65. value = sample1 << 8;
  66. value |= sample2 << 16;
  67. value |= sample3 << 24;
  68. return static_cast<double>(value) / static_cast<double>((1 << 24) - 1);
  69. }
  70. template<typename T>
  71. static ErrorOr<double> read_sample(Core::Stream::Stream& stream)
  72. {
  73. T sample { 0 };
  74. TRY(stream.read(Bytes { &sample, sizeof(T) }));
  75. if constexpr (IsIntegral<T>) {
  76. return static_cast<double>(AK::convert_between_host_and_little_endian(sample)) / static_cast<double>(NumericLimits<T>::max());
  77. } else {
  78. return static_cast<double>(AK::convert_between_host_and_little_endian(sample));
  79. }
  80. }
  81. LoaderSamples WavLoaderPlugin::samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const
  82. {
  83. FixedArray<Sample> samples = LOADER_TRY(FixedArray<Sample>::try_create(samples_to_read));
  84. auto stream = LOADER_TRY(Core::Stream::MemoryStream::construct(move(data)));
  85. switch (m_sample_format) {
  86. case PcmSampleFormat::Uint8:
  87. TRY(read_samples_from_stream(*stream, read_sample<u8>, samples));
  88. break;
  89. case PcmSampleFormat::Int16:
  90. TRY(read_samples_from_stream(*stream, read_sample<i16>, samples));
  91. break;
  92. case PcmSampleFormat::Int24:
  93. TRY(read_samples_from_stream(*stream, read_sample_int24, samples));
  94. break;
  95. case PcmSampleFormat::Float32:
  96. TRY(read_samples_from_stream(*stream, read_sample<float>, samples));
  97. break;
  98. case PcmSampleFormat::Float64:
  99. TRY(read_samples_from_stream(*stream, read_sample<double>, samples));
  100. break;
  101. default:
  102. VERIFY_NOT_REACHED();
  103. }
  104. return samples;
  105. }
  106. LoaderSamples WavLoaderPlugin::get_more_samples(size_t max_samples_to_read_from_input)
  107. {
  108. if (!m_stream)
  109. return LoaderError { LoaderError::Category::Internal, static_cast<size_t>(m_loaded_samples), "No stream; initialization failed" };
  110. auto remaining_samples = m_total_samples - m_loaded_samples;
  111. if (remaining_samples <= 0)
  112. return FixedArray<Sample> {};
  113. // One "sample" contains data from all channels.
  114. // In the Wave spec, this is also called a block.
  115. size_t bytes_per_sample
  116. = m_num_channels * pcm_bits_per_sample(m_sample_format) / 8;
  117. // Might truncate if not evenly divisible by the sample size
  118. auto max_samples_to_read = max_samples_to_read_from_input / bytes_per_sample;
  119. auto samples_to_read = min(max_samples_to_read, remaining_samples);
  120. auto bytes_to_read = samples_to_read * bytes_per_sample;
  121. dbgln_if(AWAVLOADER_DEBUG, "Read {} bytes WAV with num_channels {} sample rate {}, "
  122. "bits per sample {}, sample format {}",
  123. bytes_to_read, m_num_channels, m_sample_rate,
  124. pcm_bits_per_sample(m_sample_format), sample_format_name(m_sample_format));
  125. auto sample_data = LOADER_TRY(ByteBuffer::create_zeroed(bytes_to_read));
  126. LOADER_TRY(m_stream->read(sample_data.bytes()));
  127. // m_loaded_samples should contain the amount of actually loaded samples
  128. m_loaded_samples += samples_to_read;
  129. return samples_from_pcm_data(sample_data.bytes(), samples_to_read);
  130. }
  131. MaybeLoaderError WavLoaderPlugin::seek(int sample_index)
  132. {
  133. dbgln_if(AWAVLOADER_DEBUG, "seek sample_index {}", sample_index);
  134. if (sample_index < 0 || sample_index >= static_cast<int>(m_total_samples))
  135. return LoaderError { LoaderError::Category::Internal, m_loaded_samples, "Seek outside the sample range" };
  136. size_t sample_offset = m_byte_offset_of_data_samples + static_cast<size_t>(sample_index * m_num_channels * (pcm_bits_per_sample(m_sample_format) / 8));
  137. LOADER_TRY(m_stream->seek(sample_offset, Core::Stream::SeekMode::SetPosition));
  138. m_loaded_samples = sample_index;
  139. return {};
  140. }
  141. // Specification reference: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
  142. MaybeLoaderError WavLoaderPlugin::parse_header()
  143. {
  144. if (!m_stream)
  145. return LoaderError { LoaderError::Category::Internal, 0, "No stream" };
  146. bool ok = true;
  147. size_t bytes_read = 0;
  148. auto read_u8 = [&]() -> ErrorOr<u8, LoaderError> {
  149. u8 value;
  150. LOADER_TRY(m_stream->read(Bytes { &value, 1 }));
  151. bytes_read += 1;
  152. return value;
  153. };
  154. auto read_u16 = [&]() -> ErrorOr<u16, LoaderError> {
  155. u16 value;
  156. LOADER_TRY(m_stream->read(Bytes { &value, 2 }));
  157. bytes_read += 2;
  158. return value;
  159. };
  160. auto read_u32 = [&]() -> ErrorOr<u32, LoaderError> {
  161. u32 value;
  162. LOADER_TRY(m_stream->read(Bytes { &value, 4 }));
  163. bytes_read += 4;
  164. return value;
  165. };
  166. #define CHECK_OK(category, msg) \
  167. do { \
  168. if (!ok) \
  169. return LoaderError { category, String::formatted("Parsing failed: {}", msg) }; \
  170. } while (0)
  171. u32 riff = TRY(read_u32());
  172. ok = ok && riff == 0x46464952; // "RIFF"
  173. CHECK_OK(LoaderError::Category::Format, "RIFF header");
  174. u32 sz = TRY(read_u32());
  175. ok = ok && sz < maximum_wav_size;
  176. CHECK_OK(LoaderError::Category::Format, "File size");
  177. u32 wave = TRY(read_u32());
  178. ok = ok && wave == 0x45564157; // "WAVE"
  179. CHECK_OK(LoaderError::Category::Format, "WAVE header");
  180. u32 fmt_id = TRY(read_u32());
  181. ok = ok && fmt_id == 0x20746D66; // "fmt "
  182. CHECK_OK(LoaderError::Category::Format, "FMT header");
  183. u32 fmt_size = TRY(read_u32());
  184. ok = ok && (fmt_size == 16 || fmt_size == 18 || fmt_size == 40);
  185. CHECK_OK(LoaderError::Category::Format, "FMT size");
  186. u16 audio_format = TRY(read_u16());
  187. CHECK_OK(LoaderError::Category::Format, "Audio format"); // incomplete read check
  188. ok = ok && (audio_format == WAVE_FORMAT_PCM || audio_format == WAVE_FORMAT_IEEE_FLOAT || audio_format == WAVE_FORMAT_EXTENSIBLE);
  189. CHECK_OK(LoaderError::Category::Unimplemented, "Audio format PCM/Float"); // value check
  190. m_num_channels = TRY(read_u16());
  191. ok = ok && (m_num_channels == 1 || m_num_channels == 2);
  192. CHECK_OK(LoaderError::Category::Unimplemented, "Channel count");
  193. m_sample_rate = TRY(read_u32());
  194. CHECK_OK(LoaderError::Category::IO, "Sample rate");
  195. TRY(read_u32());
  196. CHECK_OK(LoaderError::Category::IO, "Data rate");
  197. u16 block_size_bytes = TRY(read_u16());
  198. CHECK_OK(LoaderError::Category::IO, "Block size");
  199. u16 bits_per_sample = TRY(read_u16());
  200. CHECK_OK(LoaderError::Category::IO, "Bits per sample");
  201. if (audio_format == WAVE_FORMAT_EXTENSIBLE) {
  202. ok = ok && (fmt_size == 40);
  203. CHECK_OK(LoaderError::Category::Format, "Extensible fmt size"); // value check
  204. // Discard everything until the GUID.
  205. // We've already read 16 bytes from the stream. The GUID starts in another 8 bytes.
  206. TRY(read_u32());
  207. TRY(read_u32());
  208. CHECK_OK(LoaderError::Category::IO, "Discard until GUID");
  209. // Get the underlying audio format from the first two bytes of GUID
  210. u16 guid_subformat = TRY(read_u16());
  211. ok = ok && (guid_subformat == WAVE_FORMAT_PCM || guid_subformat == WAVE_FORMAT_IEEE_FLOAT);
  212. CHECK_OK(LoaderError::Category::Unimplemented, "GUID SubFormat");
  213. audio_format = guid_subformat;
  214. }
  215. if (audio_format == WAVE_FORMAT_PCM) {
  216. ok = ok && (bits_per_sample == 8 || bits_per_sample == 16 || bits_per_sample == 24);
  217. CHECK_OK(LoaderError::Category::Unimplemented, "Bits per sample (PCM)"); // value check
  218. // We only support 8-24 bit audio right now because other formats are uncommon
  219. if (bits_per_sample == 8) {
  220. m_sample_format = PcmSampleFormat::Uint8;
  221. } else if (bits_per_sample == 16) {
  222. m_sample_format = PcmSampleFormat::Int16;
  223. } else if (bits_per_sample == 24) {
  224. m_sample_format = PcmSampleFormat::Int24;
  225. }
  226. } else if (audio_format == WAVE_FORMAT_IEEE_FLOAT) {
  227. ok = ok && (bits_per_sample == 32 || bits_per_sample == 64);
  228. CHECK_OK(LoaderError::Category::Unimplemented, "Bits per sample (Float)"); // value check
  229. // Again, only the common 32 and 64 bit
  230. if (bits_per_sample == 32) {
  231. m_sample_format = PcmSampleFormat::Float32;
  232. } else if (bits_per_sample == 64) {
  233. m_sample_format = PcmSampleFormat::Float64;
  234. }
  235. }
  236. ok = ok && (block_size_bytes == (m_num_channels * (bits_per_sample / 8)));
  237. CHECK_OK(LoaderError::Category::Format, "Block size sanity check");
  238. dbgln_if(AWAVLOADER_DEBUG, "WAV format {} at {} bit, {} channels, rate {}Hz ",
  239. sample_format_name(m_sample_format), pcm_bits_per_sample(m_sample_format), m_num_channels, m_sample_rate);
  240. // Read chunks until we find DATA
  241. bool found_data = false;
  242. u32 data_size = 0;
  243. u8 search_byte = 0;
  244. while (true) {
  245. search_byte = TRY(read_u8());
  246. CHECK_OK(LoaderError::Category::IO, "Reading byte searching for data");
  247. if (search_byte != 0x64) // D
  248. continue;
  249. search_byte = TRY(read_u8());
  250. CHECK_OK(LoaderError::Category::IO, "Reading next byte searching for data");
  251. if (search_byte != 0x61) // A
  252. continue;
  253. u16 search_remaining = TRY(read_u16());
  254. CHECK_OK(LoaderError::Category::IO, "Reading remaining bytes searching for data");
  255. if (search_remaining != 0x6174) // TA
  256. continue;
  257. data_size = TRY(read_u32());
  258. found_data = true;
  259. break;
  260. }
  261. ok = ok && found_data;
  262. CHECK_OK(LoaderError::Category::Format, "Found no data chunk");
  263. ok = ok && data_size < maximum_wav_size;
  264. CHECK_OK(LoaderError::Category::Format, "Data was too large");
  265. m_total_samples = data_size / block_size_bytes;
  266. dbgln_if(AWAVLOADER_DEBUG, "WAV data size {}, bytes per sample {}, total samples {}",
  267. data_size,
  268. block_size_bytes,
  269. m_total_samples);
  270. m_byte_offset_of_data_samples = bytes_read;
  271. return {};
  272. }
  273. }