MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open()

bool MayaFlux::IO::SoundFileReader::open ( const std::string &  filepath,
FileReadOptions  options = FileReadOptions::ALL 
)
overridevirtual

Open an audio file for reading.

Parameters
filepathPath to the file.
optionsFile read options.
Returns
True if the file was opened successfully.

Implements MayaFlux::IO::FileReader.

Definition at line 130 of file SoundFileReader.cpp.

131{
132 std::unique_lock<std::shared_mutex> lock(m_context_mutex);
133
134 if (m_context) {
135 m_context.reset();
137 m_cached_metadata.reset();
138 m_cached_regions.clear();
139 }
140
141 m_filepath = filepath;
142 m_options = options;
143 clear_error();
144
145 auto ctx = std::make_shared<FFmpegContext>();
146 if (avformat_open_input(&ctx->format_context, filepath.c_str(), nullptr, nullptr) < 0) {
147 set_error("Failed to open file: " + filepath);
148 return false;
149 }
150
151 if (avformat_find_stream_info(ctx->format_context, nullptr) < 0) {
152 set_error("Failed to find stream info");
153 return false;
154 }
155
156 const AVCodec* codec = nullptr;
157 ctx->audio_stream_index = av_find_best_stream(
158 ctx->format_context, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
159
160 if (ctx->audio_stream_index < 0 || !codec) {
161 set_error("No audio stream found");
162 return false;
163 }
164
165 ctx->codec_context = avcodec_alloc_context3(codec);
166 if (!ctx->codec_context) {
167 set_error("Failed to allocate codec context");
168 return false;
169 }
170
171 AVStream* stream = ctx->format_context->streams[ctx->audio_stream_index];
172 if (avcodec_parameters_to_context(ctx->codec_context, stream->codecpar) < 0) {
173 set_error("Failed to copy codec parameters");
174 return false;
175 }
176
177 if (avcodec_open2(ctx->codec_context, codec, nullptr) < 0) {
178 set_error("Failed to open codec");
179 return false;
180 }
181
182 if (stream->duration != AV_NOPTS_VALUE && stream->time_base.num && stream->time_base.den) {
183 double duration_seconds = stream->duration * av_q2d(stream->time_base);
184 ctx->total_frames = static_cast<uint64_t>(duration_seconds * ctx->codec_context->sample_rate);
185 } else if (ctx->format_context->duration != AV_NOPTS_VALUE) {
186 double duration_seconds = ctx->format_context->duration / static_cast<double>(AV_TIME_BASE);
187 ctx->total_frames = static_cast<uint64_t>(duration_seconds * ctx->codec_context->sample_rate);
188 } else {
189 ctx->total_frames = 0;
190 }
191
192 ctx->sample_rate = ctx->codec_context->sample_rate;
193 ctx->channels = ctx->codec_context->ch_layout.nb_channels;
194 if (!setup_resampler(ctx)) {
195 set_error("Failed to setup resampler");
196 return false;
197 }
198
199 if (!ctx->is_valid()) {
200 set_error("Invalid context after initialization");
201 return false;
202 }
203
205 extract_metadata(ctx);
206 }
207
209 extract_regions(ctx);
210 }
211
212 m_context = std::move(ctx);
214 return true;
215}
void extract_regions(const std::shared_ptr< FFmpegContext > &ctx)
Extract region information from the file.
void set_error(const std::string &error) const
Set the last error message.
std::atomic< uint64_t > m_current_frame_position
Current frame position for reading.
void extract_metadata(const std::shared_ptr< FFmpegContext > &ctx)
Extract metadata from the file.
bool setup_resampler(const std::shared_ptr< FFmpegContext > &ctx)
Set up the FFmpeg resampler if needed.
std::shared_ptr< FFmpegContext > m_context
std::optional< FileMetadata > m_cached_metadata
Cached file metadata.
std::vector< FileRegion > m_cached_regions
Cached file regions (markers, loops, etc.).
void clear_error() const
Clear the last error message.
std::string m_filepath
Path to the currently open file.
FileReadOptions m_options
File read options used for this session.
@ EXTRACT_METADATA
Extract file metadata.
@ EXTRACT_REGIONS
Extract semantic regions (format-specific)
@ NONE
No special options.

References clear_error(), MayaFlux::IO::EXTRACT_METADATA, extract_metadata(), MayaFlux::IO::EXTRACT_REGIONS, extract_regions(), m_cached_metadata, m_cached_regions, m_context, m_context_mutex, m_current_frame_position, m_filepath, m_options, MayaFlux::IO::NONE, set_error(), and setup_resampler().

+ Here is the call graph for this function: