OpenShot Library | libopenshot  0.5.0
AudioWaveformer.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2022 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_WAVEFORMER_H
14 #define OPENSHOT_WAVEFORMER_H
15 
16 #include "ReaderBase.h"
17 #include "Frame.h"
18 #include "KeyFrame.h"
19 #include "Fraction.h"
20 #include <memory>
21 #include <vector>
22 #include <string>
23 
24 
25 namespace openshot {
26 
34  {
35  std::vector<float> max_samples;
36  std::vector<float> rms_samples;
37 
39  void resize(int total_samples) {
40  max_samples.resize(total_samples);
41  rms_samples.resize(total_samples);
42  }
43 
45  void zero(int total_samples) {
46  std::fill(max_samples.begin(), max_samples.end(), 0);
47  std::fill(rms_samples.begin(), rms_samples.end(), 0);
48  }
49 
51  void scale(int total_samples, float factor) {
52  for (auto s = 0; s < total_samples; s++) {
53  max_samples[s] *= factor;
54  rms_samples[s] *= factor;
55  }
56  }
57 
59  void clear() {
60  max_samples.clear();
61  max_samples.shrink_to_fit();
62  rms_samples.clear();
63  rms_samples.shrink_to_fit();
64  }
65 
67  std::vector<std::vector<float>> vectors() {
68  std::vector<std::vector<float>> output;
69  output.push_back(max_samples);
70  output.push_back(rms_samples);
71  return output;
72  }
73  };
74 
84  private:
85  ReaderBase* reader;
86  std::unique_ptr<ReaderBase> detached_reader;
87  ReaderBase* resolved_reader = nullptr;
88  bool source_initialized = false;
89 
90  public:
92  AudioWaveformer(ReaderBase* reader);
93 
98  AudioWaveformData ExtractSamples(int channel, int num_per_second, bool normalize);
99 
101  AudioWaveformData ExtractSamples(const std::string& path, int channel, int num_per_second, bool normalize);
102 
105  const openshot::Keyframe* time_keyframe,
106  const openshot::Keyframe* volume_keyframe,
107  const openshot::Fraction& project_fps,
108  const openshot::Fraction& source_fps,
109  int source_channels,
110  int num_per_second,
111  int channel,
112  bool normalize);
113 
115  AudioWaveformData ExtractSamples(const std::string& path,
116  const openshot::Keyframe* time_keyframe,
117  const openshot::Keyframe* volume_keyframe,
118  const openshot::Fraction& project_fps,
119  int channel,
120  int num_per_second,
121  bool normalize);
122 
125 
126  private:
127  AudioWaveformData ExtractSamplesFromReader(openshot::ReaderBase* source_reader, int channel, int num_per_second, bool normalize);
128  openshot::ReaderBase* ResolveSourceReader(openshot::ReaderBase* source_reader);
129  openshot::Fraction ResolveSourceFPS(openshot::ReaderBase* source_reader);
130  openshot::ReaderBase* ResolveWaveformReader();
131  };
132 
133 }
134 
135 #endif
Header file for Fraction class.
Header file for Frame class.
Header file for the Keyframe class.
Header file for ReaderBase class.
This class is used to extra audio data used for generating waveforms.
AudioWaveformer(ReaderBase *reader)
Default constructor.
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.
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)
This class represents a fraction.
Definition: Fraction.h:30
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:76
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
This struct holds the extracted waveform data (both the RMS root-mean-squared average,...
void resize(int total_samples)
Resize both datasets.
std::vector< float > rms_samples
std::vector< float > max_samples
void zero(int total_samples)
Zero out # of values in both datasets.
void scale(int total_samples, float factor)
Scale # of values by some factor.
void clear()
Clear and free memory of both datasets.
std::vector< std::vector< float > > vectors()
Return a vector of vectors (containing both datasets)