| 211 | } |
| 212 | |
| 213 | AudioWaveformData AudioWaveformer::ExtractSamplesFromReader(ReaderBase* source_reader, int channel, int num_per_second, bool normalize) { |
| 214 | AudioWaveformData data; |
| 215 | |
| 216 | if (!source_reader || num_per_second <= 0) { |
| 217 | return data; |
| 218 | } |
| 219 | |
| 220 | // Open reader (if needed) |
| 221 | if (!source_reader->IsOpen()) { |
| 222 | source_reader->Open(); |
| 223 | } |
| 224 | |
| 225 | const auto retry_delay = std::chrono::milliseconds(100); |
| 226 | const auto max_wait_for_open = std::chrono::milliseconds(3000); |
| 227 | |
| 228 | auto get_frame_with_retry = [&](int64_t frame_number) -> std::shared_ptr<openshot::Frame> { |
| 229 | std::chrono::steady_clock::time_point wait_start; |
| 230 | bool waiting_for_open = false; |
| 231 | while (true) { |
| 232 | try { |
| 233 | return source_reader->GetFrame(frame_number); |
| 234 | } catch (const openshot::ReaderClosed&) { |
| 235 | auto now = std::chrono::steady_clock::now(); |
| 236 | if (!waiting_for_open) { |
| 237 | waiting_for_open = true; |
| 238 | wait_start = now; |
| 239 | } else if (now - wait_start >= max_wait_for_open) { |
| 240 | throw; |
| 241 | } |
| 242 | |
| 243 | std::this_thread::sleep_for(retry_delay); |
| 244 | } |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | int sample_rate = source_reader->info.sample_rate; |
| 249 | if (sample_rate <= 0) { |
| 250 | sample_rate = num_per_second; |
| 251 | } |
| 252 | int sample_divisor = sample_rate / num_per_second; |
| 253 | if (sample_divisor <= 0) { |
| 254 | sample_divisor = 1; |
| 255 | } |
| 256 | |
| 257 | // Determine length of video frames (for waveform) |
| 258 | int64_t reader_video_length = source_reader->info.video_length; |
| 259 | if (reader_video_length < 0) { |
| 260 | reader_video_length = 0; |
| 261 | } |
| 262 | float reader_duration = source_reader->info.duration; |
| 263 | double fps_value = source_reader->info.fps.ToDouble(); |
| 264 | float frames_duration = 0.0f; |
| 265 | if (reader_video_length > 0 && fps_value > 0.0) { |
| 266 | frames_duration = static_cast<float>(reader_video_length / fps_value); |
| 267 | } |
| 268 | if (reader_duration <= 0.0f) { |
| 269 | reader_duration = frames_duration; |
| 270 | } |
nothing calls this directly
no test coverage detected