Buffer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "Buffer.h"
  8. #include <AK/Atomic.h>
  9. #include <AK/Debug.h>
  10. #include <AK/StdLibExtras.h>
  11. #include <AK/String.h>
  12. namespace Audio {
  13. i32 LegacyBuffer::allocate_id()
  14. {
  15. static Atomic<i32> next_id;
  16. return next_id++;
  17. }
  18. template<typename SampleReader>
  19. static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector<Sample>& samples, int num_channels)
  20. {
  21. double left_channel_sample = 0;
  22. double right_channel_sample = 0;
  23. switch (num_channels) {
  24. case 1:
  25. for (;;) {
  26. left_channel_sample = read_sample(stream);
  27. samples.append(Sample(left_channel_sample));
  28. if (stream.handle_any_error()) {
  29. break;
  30. }
  31. }
  32. break;
  33. case 2:
  34. for (;;) {
  35. left_channel_sample = read_sample(stream);
  36. right_channel_sample = read_sample(stream);
  37. samples.append(Sample(left_channel_sample, right_channel_sample));
  38. if (stream.handle_any_error()) {
  39. break;
  40. }
  41. }
  42. break;
  43. default:
  44. VERIFY_NOT_REACHED();
  45. }
  46. }
  47. static double read_float_sample_64(InputMemoryStream& stream)
  48. {
  49. LittleEndian<double> sample;
  50. stream >> sample;
  51. return double(sample);
  52. }
  53. static double read_float_sample_32(InputMemoryStream& stream)
  54. {
  55. LittleEndian<float> sample;
  56. stream >> sample;
  57. return double(sample);
  58. }
  59. static double read_norm_sample_24(InputMemoryStream& stream)
  60. {
  61. u8 byte = 0;
  62. stream >> byte;
  63. u32 sample1 = byte;
  64. stream >> byte;
  65. u32 sample2 = byte;
  66. stream >> byte;
  67. u32 sample3 = byte;
  68. i32 value = 0;
  69. value = sample1 << 8;
  70. value |= (sample2 << 16);
  71. value |= (sample3 << 24);
  72. return double(value) / NumericLimits<i32>::max();
  73. }
  74. static double read_norm_sample_16(InputMemoryStream& stream)
  75. {
  76. LittleEndian<i16> sample;
  77. stream >> sample;
  78. return double(sample) / NumericLimits<i16>::max();
  79. }
  80. static double read_norm_sample_8(InputMemoryStream& stream)
  81. {
  82. u8 sample = 0;
  83. stream >> sample;
  84. return double(sample) / NumericLimits<u8>::max();
  85. }
  86. ErrorOr<NonnullRefPtr<LegacyBuffer>> LegacyBuffer::from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format)
  87. {
  88. InputMemoryStream stream { data };
  89. return from_pcm_stream(stream, num_channels, sample_format, data.size() / (pcm_bits_per_sample(sample_format) / 8));
  90. }
  91. ErrorOr<NonnullRefPtr<LegacyBuffer>> LegacyBuffer::from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples)
  92. {
  93. Vector<Sample> fdata;
  94. fdata.ensure_capacity(num_samples);
  95. switch (sample_format) {
  96. case PcmSampleFormat::Uint8:
  97. read_samples_from_stream(stream, read_norm_sample_8, fdata, num_channels);
  98. break;
  99. case PcmSampleFormat::Int16:
  100. read_samples_from_stream(stream, read_norm_sample_16, fdata, num_channels);
  101. break;
  102. case PcmSampleFormat::Int24:
  103. read_samples_from_stream(stream, read_norm_sample_24, fdata, num_channels);
  104. break;
  105. case PcmSampleFormat::Float32:
  106. read_samples_from_stream(stream, read_float_sample_32, fdata, num_channels);
  107. break;
  108. case PcmSampleFormat::Float64:
  109. read_samples_from_stream(stream, read_float_sample_64, fdata, num_channels);
  110. break;
  111. default:
  112. VERIFY_NOT_REACHED();
  113. }
  114. // We should handle this in a better way above, but for now --
  115. // just make sure we're good. Worst case we just write some 0s where they
  116. // don't belong.
  117. VERIFY(!stream.handle_any_error());
  118. return LegacyBuffer::create_with_samples(move(fdata));
  119. }
  120. }