GenericTypes.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GenericTypes.h"
  7. #include <AK/BinarySearch.h>
  8. #include <AK/Error.h>
  9. #include <AK/Optional.h>
  10. namespace Audio {
  11. size_t SeekTable::size() const
  12. {
  13. return m_seek_points.size();
  14. }
  15. ReadonlySpan<SeekPoint> SeekTable::seek_points() const
  16. {
  17. return m_seek_points.span();
  18. }
  19. Optional<SeekPoint const&> SeekTable::seek_point_before(u64 sample_index) const
  20. {
  21. if (m_seek_points.is_empty())
  22. return {};
  23. size_t nearby_seek_point_index = 0;
  24. AK::binary_search(m_seek_points, sample_index, &nearby_seek_point_index, [](auto const& sample_index, auto const& seekpoint_candidate) {
  25. return static_cast<i64>(sample_index) - static_cast<i64>(seekpoint_candidate.sample_index);
  26. });
  27. // Binary search will always give us a close index, but it may be too large or too small.
  28. // By doing the index adjustment in this order, we will always find a seek point before the given sample.
  29. while (nearby_seek_point_index < m_seek_points.size() - 1 && m_seek_points[nearby_seek_point_index].sample_index < sample_index)
  30. ++nearby_seek_point_index;
  31. while (nearby_seek_point_index > 0 && m_seek_points[nearby_seek_point_index].sample_index > sample_index)
  32. --nearby_seek_point_index;
  33. return m_seek_points[nearby_seek_point_index];
  34. }
  35. Optional<u64> SeekTable::seek_point_sample_distance_around(u64 sample_index) const
  36. {
  37. if (m_seek_points.is_empty())
  38. return {};
  39. size_t nearby_seek_point_index = 0;
  40. AK::binary_search(m_seek_points, sample_index, &nearby_seek_point_index, [](auto const& sample_index, auto const& seekpoint_candidate) {
  41. return static_cast<i64>(sample_index) - static_cast<i64>(seekpoint_candidate.sample_index);
  42. });
  43. while (nearby_seek_point_index < m_seek_points.size() && m_seek_points[nearby_seek_point_index].sample_index <= sample_index)
  44. ++nearby_seek_point_index;
  45. // There is no seek point beyond the sample index.
  46. if (nearby_seek_point_index >= m_seek_points.size())
  47. return {};
  48. auto upper_seek_point_index = nearby_seek_point_index;
  49. while (nearby_seek_point_index > 0 && m_seek_points[nearby_seek_point_index].sample_index > sample_index)
  50. --nearby_seek_point_index;
  51. auto lower_seek_point_index = nearby_seek_point_index;
  52. VERIFY(upper_seek_point_index >= lower_seek_point_index);
  53. return m_seek_points[upper_seek_point_index].sample_index - m_seek_points[lower_seek_point_index].sample_index;
  54. }
  55. ErrorOr<void> SeekTable::insert_seek_point(SeekPoint seek_point)
  56. {
  57. if (auto previous_seek_point = seek_point_before(seek_point.sample_index); previous_seek_point.has_value() && previous_seek_point->sample_index == seek_point.sample_index) {
  58. // Do not insert a duplicate seek point.
  59. return {};
  60. }
  61. // FIXME: This could be even faster if we used binary search while finding the insertion point.
  62. return m_seek_points.try_insert_before_matching(seek_point, [&](auto const& other_seek_point) {
  63. return seek_point.sample_index < other_seek_point.sample_index;
  64. });
  65. }
  66. }