summaryrefslogtreecommitdiff
path: root/src/audio/include/audio_decoder.hpp
blob: 318e6fd48e827de94169e0492ac054f395d5b16f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <cstdint>
#include <memory>

#include "audio_converter.hpp"
#include "audio_events.hpp"
#include "audio_sink.hpp"
#include "audio_source.hpp"
#include "codec.hpp"
#include "track.hpp"
#include "types.hpp"

namespace audio {

/*
 * Sample-based timer for the current elapsed playback time.
 */
class Timer {
 public:
  Timer(std::shared_ptr<Track>, const codecs::ICodec::OutputFormat& format);

  auto AddSamples(std::size_t) -> void;

 private:
  std::shared_ptr<Track> track_;

  uint32_t current_seconds_;
  uint32_t current_sample_in_second_;
  uint32_t samples_per_second_;

  uint32_t total_duration_seconds_;
};

/*
 * Handle to a persistent task that takes bytes from the given source, decodes
 * them into sample::Sample (normalised to 16 bit signed PCM), and then
 * forwards the resulting stream to the given converter.
 */
class Decoder {
 public:
  static auto Start(std::shared_ptr<IAudioSource> source,
                    std::shared_ptr<SampleConverter> converter) -> Decoder*;

  auto Main() -> void;

  Decoder(const Decoder&) = delete;
  Decoder& operator=(const Decoder&) = delete;

 private:
  Decoder(std::shared_ptr<IAudioSource> source,
          std::shared_ptr<SampleConverter> converter);

  auto BeginDecoding(std::shared_ptr<TaggedStream>) -> bool;
  auto ContinueDecoding() -> bool;

  std::shared_ptr<IAudioSource> source_;
  std::shared_ptr<SampleConverter> converter_;

  std::shared_ptr<codecs::IStream> stream_;
  std::unique_ptr<codecs::ICodec> codec_;
  std::unique_ptr<Timer> timer_;

  std::optional<codecs::ICodec::OutputFormat> current_format_;
  std::optional<IAudioOutput::Format> current_sink_format_;

  cpp::span<sample::Sample> codec_buffer_;
};

}  // namespace audio