Buffer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibAudio/Buffer.h>
  27. namespace Audio {
  28. template<typename SampleReader>
  29. static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector<Sample>& samples, ResampleHelper& resampler, int num_channels)
  30. {
  31. double norm_l = 0;
  32. double norm_r = 0;
  33. switch (num_channels) {
  34. case 1:
  35. for (;;) {
  36. while (resampler.read_sample(norm_l, norm_r)) {
  37. samples.append(Sample(norm_l));
  38. }
  39. norm_l = read_sample(stream);
  40. if (stream.handle_any_error()) {
  41. break;
  42. }
  43. resampler.process_sample(norm_l, norm_r);
  44. }
  45. break;
  46. case 2:
  47. for (;;) {
  48. while (resampler.read_sample(norm_l, norm_r)) {
  49. samples.append(Sample(norm_l, norm_r));
  50. }
  51. norm_l = read_sample(stream);
  52. norm_r = read_sample(stream);
  53. if (stream.handle_any_error()) {
  54. break;
  55. }
  56. resampler.process_sample(norm_l, norm_r);
  57. }
  58. break;
  59. default:
  60. ASSERT_NOT_REACHED();
  61. }
  62. }
  63. static double read_norm_sample_24(InputMemoryStream& stream)
  64. {
  65. u8 byte = 0;
  66. stream >> byte;
  67. u32 sample1 = byte;
  68. stream >> byte;
  69. u32 sample2 = byte;
  70. stream >> byte;
  71. u32 sample3 = byte;
  72. i32 value = 0;
  73. value = sample1 << 8;
  74. value |= (sample2 << 16);
  75. value |= (sample3 << 24);
  76. return double(value) / NumericLimits<i32>::max();
  77. }
  78. static double read_norm_sample_16(InputMemoryStream& stream)
  79. {
  80. LittleEndian<i16> sample;
  81. stream >> sample;
  82. return double(sample) / NumericLimits<i16>::max();
  83. }
  84. static double read_norm_sample_8(InputMemoryStream& stream)
  85. {
  86. u8 sample = 0;
  87. stream >> sample;
  88. return double(sample) / NumericLimits<u8>::max();
  89. }
  90. RefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, int bits_per_sample)
  91. {
  92. InputMemoryStream stream { data };
  93. return from_pcm_stream(stream, resampler, num_channels, bits_per_sample, data.size() / (bits_per_sample / 8));
  94. }
  95. RefPtr<Buffer> Buffer::from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, int bits_per_sample, int num_samples)
  96. {
  97. Vector<Sample> fdata;
  98. fdata.ensure_capacity(num_samples);
  99. switch (bits_per_sample) {
  100. case 8:
  101. read_samples_from_stream(stream, read_norm_sample_8, fdata, resampler, num_channels);
  102. break;
  103. case 16:
  104. read_samples_from_stream(stream, read_norm_sample_16, fdata, resampler, num_channels);
  105. break;
  106. case 24:
  107. read_samples_from_stream(stream, read_norm_sample_24, fdata, resampler, num_channels);
  108. break;
  109. default:
  110. ASSERT_NOT_REACHED();
  111. }
  112. // We should handle this in a better way above, but for now --
  113. // just make sure we're good. Worst case we just write some 0s where they
  114. // don't belong.
  115. ASSERT(!stream.handle_any_error());
  116. return Buffer::create_with_samples(move(fdata));
  117. }
  118. }