Loading...
Searching...
No Matches
AudioWaveformer.h
Go to the documentation of this file.
// Copyright (c) 2008-2022 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef OPENSHOT_WAVEFORMER_H
#define OPENSHOT_WAVEFORMER_H
#include "ReaderBase.h"
#include "Frame.h"
#include "KeyFrame.h"
#include "Fraction.h"
#include <memory>
#include <vector>
#include <string>
namespace openshot {
struct AudioWaveformData
{
std::vector<float> max_samples;
std::vector<float> rms_samples;
void resize(int total_samples) {
max_samples.resize(total_samples);
rms_samples.resize(total_samples);
}
void zero(int total_samples) {
std::fill(max_samples.begin(), max_samples.end(), 0);
std::fill(rms_samples.begin(), rms_samples.end(), 0);
}
void scale(int total_samples, float factor) {
for (auto s = 0; s < total_samples; s++) {
max_samples[s] *= factor;
rms_samples[s] *= factor;
}
}
void clear() {
max_samples.clear();
max_samples.shrink_to_fit();
rms_samples.clear();
rms_samples.shrink_to_fit();
}
std::vector<std::vector<float>> vectors() {
std::vector<std::vector<float>> output;
output.push_back(max_samples);
output.push_back(rms_samples);
return output;
}
};
class AudioWaveformer {
private:
ReaderBase* reader;
std::unique_ptr<ReaderBase> detached_reader;
ReaderBase* resolved_reader = nullptr;
bool source_initialized = false;
public:
AudioWaveformer(ReaderBase* reader);
AudioWaveformData ExtractSamples(int channel, int num_per_second, bool normalize);
AudioWaveformData ExtractSamples(const std::string& path, int channel, int num_per_second, bool normalize);
AudioWaveformData ApplyKeyframes(const AudioWaveformData& base,
const openshot::Keyframe* time_keyframe,
const openshot::Keyframe* volume_keyframe,
const openshot::Fraction& project_fps,
const openshot::Fraction& source_fps,
int source_channels,
int num_per_second,
int channel,
bool normalize);
AudioWaveformData ExtractSamples(const std::string& path,
const openshot::Keyframe* time_keyframe,
const openshot::Keyframe* volume_keyframe,
const openshot::Fraction& project_fps,
int channel,
int num_per_second,
bool normalize);
~AudioWaveformer();
private:
AudioWaveformData ExtractSamplesFromReader(openshot::ReaderBase* source_reader, int channel, int num_per_second, bool normalize);
openshot::ReaderBase* ResolveSourceReader(openshot::ReaderBase* source_reader);
openshot::Fraction ResolveSourceFPS(openshot::ReaderBase* source_reader);
openshot::ReaderBase* ResolveWaveformReader();
};
}
#endif
path
path
Definition FFmpegWriter.cpp:1468
Fraction.h
Header file for Fraction class.
Frame.h
Header file for Frame class.
KeyFrame.h
Header file for the Keyframe class.
ReaderBase.h
Header file for ReaderBase class.
openshot::AudioWaveformer::AudioWaveformer
AudioWaveformer(ReaderBase *reader)
Default constructor.
Definition AudioWaveformer.cpp:35
openshot::AudioWaveformer::~AudioWaveformer
~AudioWaveformer()
Destructor.
Definition AudioWaveformer.cpp:45
openshot::AudioWaveformer::ApplyKeyframes
AudioWaveformData ApplyKeyframes(const AudioWaveformData &base, const openshot::Keyframe *time_keyframe, const openshot::Keyframe *volume_keyframe, const openshot::Fraction &project_fps, const openshot::Fraction &source_fps, int source_channels, int num_per_second, int channel, bool normalize)
Apply time and volume keyframes to an existing waveform data set.
Definition AudioWaveformer.cpp:114
openshot::AudioWaveformer::ExtractSamples
AudioWaveformData ExtractSamples(int channel, int num_per_second, bool normalize)
Extract audio samples from any ReaderBase class (legacy overload, now delegates to audio-only path).
Definition AudioWaveformer.cpp:51
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition KeyFrame.h:53
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::AudioWaveformData
This struct holds the extracted waveform data (both the RMS root-mean-squared average,...
Definition AudioWaveformer.h:34
openshot::AudioWaveformData::resize
void resize(int total_samples)
Resize both datasets.
Definition AudioWaveformer.h:39
openshot::AudioWaveformData::rms_samples
std::vector< float > rms_samples
Definition AudioWaveformer.h:36
openshot::AudioWaveformData::max_samples
std::vector< float > max_samples
Definition AudioWaveformer.h:35
openshot::AudioWaveformData::zero
void zero(int total_samples)
Zero out # of values in both datasets.
Definition AudioWaveformer.h:45
openshot::AudioWaveformData::vectors
std::vector< std::vector< float > > vectors()
Return a vector of vectors (containing both datasets).
Definition AudioWaveformer.h:67
openshot::AudioWaveformData::scale
void scale(int total_samples, float factor)
Scale # of values by some factor.
Definition AudioWaveformer.h:51
openshot::AudioWaveformData::clear
void clear()
Clear and free memory of both datasets.
Definition AudioWaveformer.h:59