Encoder.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Error.h>
  8. #include <AK/Span.h>
  9. #include <LibAudio/Forward.h>
  10. #include <LibAudio/Sample.h>
  11. namespace Audio {
  12. class Encoder {
  13. public:
  14. virtual ~Encoder() = default;
  15. // Encodes the given samples and writes them to the output stream.
  16. // Note that due to format restrictions, not all samples might be written immediately, this is only guaranteed after a call to finalize().
  17. virtual ErrorOr<void> write_samples(ReadonlySpan<Sample> samples) = 0;
  18. // Finalizes the stream, future calls to write_samples() will cause an error.
  19. // This method makes sure that all samples are encoded and written out.
  20. // This method is called in the destructor, but since this can error, you should call this function yourself before disposing of the decoder.
  21. virtual ErrorOr<void> finalize() = 0;
  22. // Sets the metadata for this audio file.
  23. // Not all encoders support this, and metadata may not be writeable after starting to write samples.
  24. virtual ErrorOr<void> set_metadata([[maybe_unused]] Metadata const& metadata) { return {}; }
  25. // Provides a hint about the total number of samples to the encoder, improving some encoder's performance in various aspects.
  26. // Note that the hint does not have to be fully correct; wrong hints never cause errors, not even indirectly.
  27. virtual void sample_count_hint([[maybe_unused]] size_t sample_count) { }
  28. };
  29. }