FFmpeg-based audio file decoder for audio-reactive shaders. More...
#include "file_audio.hpp"#include "audio.hpp"#include <RtAudio.h>#include <algorithm>#include <atomic>#include <cctype>#include <cmath>#include <cstring>#include <filesystem>#include <fstream>#include <iostream>#include <memory>#include <string>#include <vector>#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#include <libavutil/opt.h>#include <libswresample/swresample.h>Go to the source code of this file.
Classes | |
| class | anonymous_namespace{file_audio.cpp}::FileAudioOutput |
Namespaces | |
| namespace | anonymous_namespace{file_audio.cpp} |
Functions | |
| static void | closeDecoderResources () |
| static bool | decodeAllSamples () |
| Decode every audio packet from the open format context. | |
| static bool | decodeAudioFile (const std::string &filepath) |
| void | file_audio_close () |
| Stop file-audio playback and release decoder/sample resources. | |
| std::string | file_audio_current_source_path () |
| Return the source track at the current playback position. | |
| bool | file_audio_enable_output (int output_device) |
| Configure real-time playback of the decoded file through an output device. | |
| bool | file_audio_has_output_clock () |
| Check whether real-time output playback can provide the master clock. | |
| bool | file_audio_is_active () |
| Return true while decoded file-audio samples remain. | |
| bool | file_audio_open (const std::string &filepath) |
| Open and fully decode an audio file or M3U playlist to mono float PCM at 44.1 kHz. | |
| double | file_audio_playback_time () |
| Return the current output-device playback timestamp in seconds. | |
| void | file_audio_process_frame (double video_fps, acmx2::audio::AudioAnalyzer &analyzer) |
| Advance one video-frame worth of samples and update audio analysis. | |
| void | file_audio_set_repeat (bool enabled) |
| Enable or disable looping of the currently decoded audio file. | |
| std::vector< std::string > | file_audio_source_paths () |
| Return the successfully decoded source tracks in playback order. | |
| static bool | isM3uPath (const std::string &filepath) |
| static std::vector< std::string > | readM3uPlaylist (const std::string &filepath) |
| static std::string | trimPlaylistLine (std::string line) |
Variables | |
| static int | audioStreamIndex = -1 |
| static AVCodecContext * | codecCtx = nullptr |
| static std::vector< float > | decodedSamples |
| constexpr unsigned int | anonymous_namespace{file_audio.cpp}::FILE_AUDIO_SAMPLE_RATE = 44100 |
| static std::atomic< bool > | fileAudioActive {false} |
| std::unique_ptr< FileAudioOutput > | anonymous_namespace{file_audio.cpp}::fileAudioOutput |
| static std::atomic< bool > | fileAudioRepeat {false} |
| static std::vector< std::size_t > | fileAudioSourceEndPositions |
| static std::vector< std::string > | fileAudioSourcePaths |
| static AVFormatContext * | fmtCtx = nullptr |
| static double | framePlaybackPos = 0.0 |
| static size_t | playbackPos = 0 |
| static SwrContext * | swrCtx = nullptr |
FFmpeg-based audio file decoder for audio-reactive shaders.
Decodes an entire audio file upfront into a mono float buffer at 44 100 Hz using FFmpeg's libavformat / libavcodec / libswresample. Each video frame, file_audio_process_frame() advances through the buffer and feeds the shared AudioAnalyzer used by the shader uniform pipeline.
Definition in file ACMX2/file_audio.cpp.
|
static |
Definition at line 252 of file ACMX2/file_audio.cpp.
References audioStreamIndex, closeDecoderResources(), codecCtx, fmtCtx, and swrCtx.
Referenced by closeDecoderResources(), decodeAudioFile(), and file_audio_close().
|
static |
Decode every audio packet from the open format context.
Reads packets from fmtCtx, sends them through the codec, and resamples the output to mono float 44 100 Hz via swrCtx. Decoded samples are appended to the module-level decodedSamples vector. The resampler is flushed at the end to capture trailing samples.
true if decoding completed without fatal errors. Definition at line 329 of file ACMX2/file_audio.cpp.
References audioStreamIndex, codecCtx, decodeAllSamples(), decodedSamples, fmtCtx, and swrCtx.
Referenced by decodeAllSamples(), and decodeAudioFile().
|
static |
Definition at line 384 of file ACMX2/file_audio.cpp.
References audioStreamIndex, closeDecoderResources(), codecCtx, decodeAllSamples(), decodeAudioFile(), decodedSamples, fmtCtx, and swrCtx.
Referenced by decodeAudioFile(), and file_audio_open().
| void file_audio_close | ( | ) |
Stop file-audio playback and release decoder/sample resources.
Close the file audio decoder and release all resources.
Stop file-audio playback and release decoder/sample resources.
Stops file playback, frees any remaining FFmpeg contexts (safe to call even if already closed), and releases the decoded sample buffer.
Definition at line 607 of file ACMX2/file_audio.cpp.
References closeDecoderResources(), decodedSamples, file_audio_close(), fileAudioActive, anonymous_namespace{file_audio.cpp}::fileAudioOutput, fileAudioRepeat, fileAudioSourceEndPositions, fileAudioSourcePaths, framePlaybackPos, and playbackPos.
Referenced by file_audio_close(), file_audio_open(), and ACView::~ACView().
| std::string file_audio_current_source_path | ( | ) |
Return the source track at the current playback position.
Definition at line 505 of file ACMX2/file_audio.cpp.
References file_audio_current_source_path(), fileAudioActive, fileAudioSourceEndPositions, fileAudioSourcePaths, and playbackPos.
Referenced by file_audio_current_source_path().
| bool file_audio_enable_output | ( | int | output_device | ) |
Configure real-time playback of the decoded file through an output device.
The stream is opened immediately and starts when file_audio_process_frame() first advances the file, keeping audible playback aligned with visual analysis.
| output_device | RtAudio device ID, or -1 for the default output. |
true when the output stream was configured successfully. Definition at line 519 of file ACMX2/file_audio.cpp.
References decodedSamples, file_audio_enable_output(), and anonymous_namespace{file_audio.cpp}::fileAudioOutput.
Referenced by file_audio_enable_output().
| bool file_audio_has_output_clock | ( | ) |
Check whether real-time output playback can provide the master clock.
This remains true from output configuration until the decoded audio reaches its end. Before the stream starts, its timestamp is zero.
Definition at line 536 of file ACMX2/file_audio.cpp.
References file_audio_has_output_clock(), fileAudioActive, and anonymous_namespace{file_audio.cpp}::fileAudioOutput.
Referenced by ACView::draw(), file_audio_has_output_clock(), and file_audio_playback_time().
| bool file_audio_is_active | ( | ) |
Return true while decoded file-audio samples remain.
Check whether file audio playback is still active.
Return true while decoded file-audio samples remain.
true if a file is open and unplayed samples remain. Definition at line 602 of file ACMX2/file_audio.cpp.
References file_audio_is_active(), and fileAudioActive.
Referenced by file_audio_is_active().
| bool file_audio_open | ( | const std::string & | filepath | ) |
Open and fully decode an audio file or M3U playlist to mono float PCM at 44.1 kHz.
Open and fully decode an audio file to mono float PCM at 44 100 Hz.
Open and fully decode an audio file or M3U playlist to mono float PCM at 44.1 kHz.
Uses FFmpeg to open filepath, locate the first audio stream, decode it, and resample all samples into an internal buffer. After this call the FFmpeg decoder contexts are freed; only the sample buffer remains in memory.
| filepath | Path to an audio or video file containing an audio track. |
true on success, false on any decoder or I/O error. Definition at line 457 of file ACMX2/file_audio.cpp.
References decodeAudioFile(), decodedSamples, file_audio_close(), file_audio_open(), fileAudioActive, fileAudioSourceEndPositions, fileAudioSourcePaths, framePlaybackPos, isM3uPath(), playbackPos, and readM3uPlaylist().
Referenced by file_audio_open().
| double file_audio_playback_time | ( | ) |
Return the current output-device playback timestamp in seconds.
Definition at line 541 of file ACMX2/file_audio.cpp.
References file_audio_has_output_clock(), file_audio_playback_time(), anonymous_namespace{file_audio.cpp}::FILE_AUDIO_SAMPLE_RATE, and anonymous_namespace{file_audio.cpp}::fileAudioOutput.
Referenced by ACView::draw(), and file_audio_playback_time().
| void file_audio_process_frame | ( | double | video_fps, |
| acmx2::audio::AudioAnalyzer & | analyzer ) |
Advance one video-frame worth of samples and update audio analysis.
Advance playback by one video frame and update the audio analyzer.
Advance one video-frame worth of samples and update audio analysis.
Without output playback, advances by exactly one video-frame duration using a fractional sample accumulator. With output playback, analyzes the sample window at the output device's current timestamp.
| video_fps | Video frame rate — determines how many audio samples are consumed per call. |
| analyzer | Shared analyzer that also services live audio input. |
Definition at line 549 of file ACMX2/file_audio.cpp.
References decodedSamples, file_audio_process_frame(), anonymous_namespace{file_audio.cpp}::FILE_AUDIO_SAMPLE_RATE, fileAudioActive, anonymous_namespace{file_audio.cpp}::fileAudioOutput, fileAudioRepeat, framePlaybackPos, playbackPos, and acmx2::audio::AudioAnalyzer::process_samples().
Referenced by file_audio_process_frame().
| void file_audio_set_repeat | ( | bool | enabled | ) |
Enable or disable looping of the currently decoded audio file.
| enabled | When true, playback restarts at the first sample at EOF. |
Definition at line 532 of file ACMX2/file_audio.cpp.
References file_audio_set_repeat(), and fileAudioRepeat.
Referenced by file_audio_set_repeat().
| std::vector< std::string > file_audio_source_paths | ( | ) |
Return the successfully decoded source tracks in playback order.
A single audio file returns one path. An M3U source returns each usable playlist entry with relative paths resolved against the playlist directory.
Definition at line 501 of file ACMX2/file_audio.cpp.
References file_audio_source_paths(), and fileAudioSourcePaths.
Referenced by file_audio_source_paths(), and ACView::runFileAudioMuxSync().
|
static |
Definition at line 271 of file ACMX2/file_audio.cpp.
References isM3uPath().
Referenced by file_audio_open(), and isM3uPath().
|
static |
Definition at line 280 of file ACMX2/file_audio.cpp.
References readM3uPlaylist(), and trimPlaylistLine().
Referenced by file_audio_open(), and readM3uPlaylist().
|
static |
Definition at line 262 of file ACMX2/file_audio.cpp.
References trimPlaylistLine().
Referenced by readM3uPlaylist(), and trimPlaylistLine().
|
static |
Definition at line 38 of file ACMX2/file_audio.cpp.
Referenced by closeDecoderResources(), decodeAllSamples(), and decodeAudioFile().
|
static |
Definition at line 36 of file ACMX2/file_audio.cpp.
Referenced by closeDecoderResources(), decodeAllSamples(), and decodeAudioFile().
|
static |
Definition at line 39 of file ACMX2/file_audio.cpp.
Referenced by decodeAllSamples(), decodeAudioFile(), file_audio_close(), file_audio_enable_output(), file_audio_open(), and file_audio_process_frame().
|
static |
Definition at line 42 of file ACMX2/file_audio.cpp.
Referenced by file_audio_close(), file_audio_current_source_path(), file_audio_has_output_clock(), file_audio_is_active(), file_audio_open(), and file_audio_process_frame().
|
static |
Definition at line 43 of file ACMX2/file_audio.cpp.
Referenced by file_audio_close(), file_audio_process_frame(), file_audio_set_repeat(), and anonymous_namespace{file_audio.cpp}::FileAudioOutput::write_samples().
|
static |
Definition at line 45 of file ACMX2/file_audio.cpp.
Referenced by file_audio_close(), file_audio_current_source_path(), and file_audio_open().
|
static |
Definition at line 44 of file ACMX2/file_audio.cpp.
Referenced by file_audio_close(), file_audio_current_source_path(), file_audio_open(), and file_audio_source_paths().
|
static |
Definition at line 35 of file ACMX2/file_audio.cpp.
Referenced by closeDecoderResources(), decodeAllSamples(), and decodeAudioFile().
|
static |
Definition at line 41 of file ACMX2/file_audio.cpp.
Referenced by file_audio_close(), file_audio_open(), and file_audio_process_frame().
|
static |
Definition at line 40 of file ACMX2/file_audio.cpp.
Referenced by file_audio_close(), file_audio_current_source_path(), file_audio_open(), and file_audio_process_frame().
|
static |
Definition at line 37 of file ACMX2/file_audio.cpp.
Referenced by closeDecoderResources(), decodeAllSamples(), and decodeAudioFile().