Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
mmap_reader.hpp
Go to the documentation of this file.
1#pragma once
2
3// MemoryMappedAudioReader — zero-copy audio file access via memory mapping.
4// Combines MemoryMappedFile with codec decoding for efficient large-file access.
5
6#include <limits>
7#include <memory>
8#include <optional>
11#include <string>
12#include <string_view>
13
14namespace pulp::audio {
15
20 public:
21 // All special members are out-of-line (defined in the .cpp where RangedState
22 // is complete) — the pimpl unique_ptr requires a complete type for
23 // construction-cleanup, destruction, and move.
28
30 bool open(std::string_view path);
31 bool open(std::string_view path, std::size_t maximum_mapped_bytes);
32
34 void close();
35
37 bool is_open() const;
38
40 const AudioFileInfo& info() const {
41 return info_;
42 }
43
53 bool read_frames(float** dest_channels, uint32_t num_channels, uint64_t start_frame,
54 uint64_t num_frames);
55
59 bool read_frames_ranged_only(float** dest_channels, uint32_t num_channels, uint64_t start_frame,
60 uint64_t num_frames);
61
67
69 std::optional<AudioFileData> read_all();
70
72 const uint8_t* data() const;
73 size_t size() const;
74
77 bool copy_access_policy_to(std::string_view destination) const noexcept;
79 prepare_access_policy_target(std::string_view destination) const noexcept;
80 bool path_refers_to_open_file(std::string_view path) const noexcept;
82
83 private:
84 // Ranged (seek-based) decoder state over the mapped bytes; nullptr when the
85 // active format can't be range-read (read_frames then decode-once-caches).
86 // BackingState retains the original source handle for identity/access-policy
87 // operations and owns an immutable temporary snapshot used for all reads.
88 struct BackingState;
89 struct RangedState;
90
91 std::unique_ptr<BackingState> backing_;
92 AudioFileInfo info_;
93 std::string path_;
94 std::unique_ptr<RangedState> ranged_;
95
96 bool read_frames_impl(float** dest_channels, uint32_t num_channels, uint64_t start_frame,
97 uint64_t num_frames, bool allow_whole_file_fallback);
98};
99
100} // namespace pulp::audio
void close()
Close the mapped file.
bool supports_ranged_read() const
True when a ranged (seek-based, no whole-file decode) reader is active for this file.
bool path_refers_to_open_file(std::string_view path) const noexcept
runtime::AccessPolicyTarget prepare_access_policy_target(std::string_view destination) const noexcept
bool read_frames_ranged_only(float **dest_channels, uint32_t num_channels, uint64_t start_frame, uint64_t num_frames)
Strict counterpart to read_frames().
bool open(std::string_view path)
Open and memory-map an audio file. Returns false on failure.
MemoryMappedAudioReader(MemoryMappedAudioReader &&) noexcept
bool copy_access_policy_to(std::string_view destination) const noexcept
Copy the access policy captured by the retained source handle to an existing artifact.
bool is_open() const
Whether an immutable snapshot of the source is currently mapped.
const uint8_t * data() const
Raw bytes from the immutable source snapshot (for custom decoding).
std::optional< AudioFileData > read_all()
Read the entire immutable source snapshot.
bool read_frames(float **dest_channels, uint32_t num_channels, uint64_t start_frame, uint64_t num_frames)
Read a range of frames into deinterleaved float buffers.
const AudioFileInfo & info() const
File info (available after open).
Definition mmap_reader.hpp:40
runtime::FileIdentity opened_file_identity() const noexcept
Definition memory_mapped_file.hpp:26
Definition analyzer_provider.hpp:12
STL namespace.
Definition audio_file.hpp:13
Definition memory_mapped_file.hpp:16