Buffer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <AK/Atomic.h>
  27. #include <LibAudio/Buffer.h>
  28. namespace Audio {
  29. i32 Buffer::allocate_id()
  30. {
  31. static Atomic<i32> next_id;
  32. return next_id++;
  33. }
  34. template<typename SampleReader>
  35. static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector<Frame>& samples, ResampleHelper& resampler, int num_channels)
  36. {
  37. double norm_l = 0;
  38. double norm_r = 0;
  39. switch (num_channels) {
  40. case 1:
  41. for (;;) {
  42. while (resampler.read_sample(norm_l, norm_r)) {
  43. samples.append(Frame(norm_l));
  44. }
  45. norm_l = read_sample(stream);
  46. if (stream.handle_any_error()) {
  47. break;
  48. }
  49. resampler.process_sample(norm_l, norm_r);
  50. }
  51. break;
  52. case 2:
  53. for (;;) {
  54. while (resampler.read_sample(norm_l, norm_r)) {
  55. samples.append(Frame(norm_l, norm_r));
  56. }
  57. norm_l = read_sample(stream);
  58. norm_r = read_sample(stream);
  59. if (stream.handle_any_error()) {
  60. break;
  61. }
  62. resampler.process_sample(norm_l, norm_r);
  63. }
  64. break;
  65. default:
  66. VERIFY_NOT_REACHED();
  67. }
  68. }
  69. static double read_norm_sample_24(InputMemoryStream& stream)
  70. {
  71. u8 byte = 0;
  72. stream >> byte;
  73. u32 sample1 = byte;
  74. stream >> byte;
  75. u32 sample2 = byte;
  76. stream >> byte;
  77. u32 sample3 = byte;
  78. i32 value = 0;
  79. value = sample1 << 8;
  80. value |= (sample2 << 16);
  81. value |= (sample3 << 24);
  82. return double(value) / NumericLimits<i32>::max();
  83. }
  84. static double read_norm_sample_16(InputMemoryStream& stream)
  85. {
  86. LittleEndian<i16> sample;
  87. stream >> sample;
  88. return double(sample) / NumericLimits<i16>::max();
  89. }
  90. static double read_norm_sample_8(InputMemoryStream& stream)
  91. {
  92. u8 sample = 0;
  93. stream >> sample;
  94. return double(sample) / NumericLimits<u8>::max();
  95. }
  96. RefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, int bits_per_sample)
  97. {
  98. InputMemoryStream stream { data };
  99. return from_pcm_stream(stream, resampler, num_channels, bits_per_sample, data.size() / (bits_per_sample / 8));
  100. }
  101. RefPtr<Buffer> Buffer::from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, int bits_per_sample, int num_samples)
  102. {
  103. Vector<Frame> fdata;
  104. fdata.ensure_capacity(num_samples);
  105. switch (bits_per_sample) {
  106. case 8:
  107. read_samples_from_stream(stream, read_norm_sample_8, fdata, resampler, num_channels);
  108. break;
  109. case 16:
  110. read_samples_from_stream(stream, read_norm_sample_16, fdata, resampler, num_channels);
  111. break;
  112. case 24:
  113. read_samples_from_stream(stream, read_norm_sample_24, fdata, resampler, num_channels);
  114. break;
  115. default:
  116. VERIFY_NOT_REACHED();
  117. }
  118. // We should handle this in a better way above, but for now --
  119. // just make sure we're good. Worst case we just write some 0s where they
  120. // don't belong.
  121. VERIFY(!stream.handle_any_error());
  122. return Buffer::create_with_samples(move(fdata));
  123. }
  124. }