ACMX 2.136.0
Dual-Backend Real-Time GPU Video Synthesis
Loading...
Searching...
No Matches
file_audio.cpp File Reference

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< FileAudioOutputanonymous_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

Detailed Description

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.

Function Documentation

◆ closeDecoderResources()

void closeDecoderResources ( )
static

◆ decodeAllSamples()

bool decodeAllSamples ( )
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.

Returns
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().

◆ decodeAudioFile()

bool decodeAudioFile ( const std::string & filepath)
static

◆ file_audio_close()

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().

◆ file_audio_current_source_path()

std::string file_audio_current_source_path ( )

Return the source track at the current playback position.

Returns
Current file/playlist entry path, or an empty string when inactive.

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().

◆ file_audio_enable_output()

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.

Parameters
output_deviceRtAudio device ID, or -1 for the default output.
Returns
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().

◆ file_audio_has_output_clock()

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().

◆ file_audio_is_active()

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.

Returns
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().

◆ file_audio_open()

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.

Parameters
filepathPath to an audio or video file containing an audio track.
Returns
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().

◆ file_audio_playback_time()

double file_audio_playback_time ( )

Return the current output-device playback timestamp in seconds.

Returns
Playback position measured by the RtAudio callback, or 0 when no output clock is available.

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().

◆ file_audio_process_frame()

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.

Parameters
video_fpsVideo frame rate — determines how many audio samples are consumed per call.
analyzerShared 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().

◆ file_audio_set_repeat()

void file_audio_set_repeat ( bool enabled)

Enable or disable looping of the currently decoded audio file.

Parameters
enabledWhen 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().

◆ file_audio_source_paths()

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().

◆ isM3uPath()

bool isM3uPath ( const std::string & filepath)
static

Definition at line 271 of file ACMX2/file_audio.cpp.

References isM3uPath().

Referenced by file_audio_open(), and isM3uPath().

◆ readM3uPlaylist()

std::vector< std::string > readM3uPlaylist ( const std::string & filepath)
static

Definition at line 280 of file ACMX2/file_audio.cpp.

References readM3uPlaylist(), and trimPlaylistLine().

Referenced by file_audio_open(), and readM3uPlaylist().

◆ trimPlaylistLine()

std::string trimPlaylistLine ( std::string line)
static

Definition at line 262 of file ACMX2/file_audio.cpp.

References trimPlaylistLine().

Referenced by readM3uPlaylist(), and trimPlaylistLine().

Variable Documentation

◆ audioStreamIndex

int audioStreamIndex = -1
static

Definition at line 38 of file ACMX2/file_audio.cpp.

Referenced by closeDecoderResources(), decodeAllSamples(), and decodeAudioFile().

◆ codecCtx

AVCodecContext* codecCtx = nullptr
static

Definition at line 36 of file ACMX2/file_audio.cpp.

Referenced by closeDecoderResources(), decodeAllSamples(), and decodeAudioFile().

◆ decodedSamples

std::vector<float> decodedSamples
static

◆ fileAudioActive

◆ fileAudioRepeat

std::atomic<bool> fileAudioRepeat {false}
static

◆ fileAudioSourceEndPositions

std::vector<std::size_t> fileAudioSourceEndPositions
static

◆ fileAudioSourcePaths

std::vector<std::string> fileAudioSourcePaths
static

◆ fmtCtx

AVFormatContext* fmtCtx = nullptr
static

Definition at line 35 of file ACMX2/file_audio.cpp.

Referenced by closeDecoderResources(), decodeAllSamples(), and decodeAudioFile().

◆ framePlaybackPos

double framePlaybackPos = 0.0
static

◆ playbackPos

size_t playbackPos = 0
static

◆ swrCtx

SwrContext* swrCtx = nullptr
static

Definition at line 37 of file ACMX2/file_audio.cpp.

Referenced by closeDecoderResources(), decodeAllSamples(), and decodeAudioFile().