OpenShot Library | libopenshot  0.3.3
AudioResampler.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include "AudioResampler.h"
14 
15 using namespace std;
16 using namespace openshot;
17 
18 // Default constructor, max frames to cache is 20 // resample_source(NULL), buffer_source(NULL), num_of_samples(0), new_num_of_samples(0), dest_ratio(0), source_ratio(0), resampled_buffer(NULL), isPrepared(false)
19 AudioResampler::AudioResampler(int numChannels)
20 {
21  buffer = NULL;
22  resample_source = NULL;
23  buffer_source = NULL;
24  num_of_samples = 0;
25  new_num_of_samples = 0;
26  dest_ratio = 0;
27  source_ratio = 0;
28  resampled_buffer = NULL;
29  isPrepared = false;
30 
31  // Init buffer source
32  buffer_source = new AudioBufferSource(buffer);
33 
34  // Init resampling source
35  resample_source = new juce::ResamplingAudioSource(buffer_source, false, numChannels);
36 
37  // Init resampled buffer
38  resampled_buffer = new juce::AudioBuffer<float>(2, 1);
39  resampled_buffer->clear();
40 
41  // Init callback buffer
42  resample_callback_buffer.buffer = resampled_buffer;
43  resample_callback_buffer.numSamples = 1;
44  resample_callback_buffer.startSample = 0;
45 }
46 
47 // Descructor
48 AudioResampler::~AudioResampler()
49 {
50  // Clean up
51  if (buffer_source)
52  delete buffer_source;
53  if (resample_source)
54  delete resample_source;
55  if (resampled_buffer)
56  delete resampled_buffer;
57 }
58 
59 // Sets the audio buffer and updates the key settings
60 void AudioResampler::SetBuffer(juce::AudioBuffer<float> *new_buffer, double sample_rate, double new_sample_rate)
61 {
62  if (sample_rate <= 0)
63  sample_rate = 44100;
64  if (new_sample_rate <= 0)
65  new_sample_rate = 44100;
66 
67  // Set the sample ratio (the ratio of sample rate change)
68  source_ratio = sample_rate / new_sample_rate;
69 
70  // Call SetBuffer with ratio
71  SetBuffer(new_buffer, source_ratio);
72 }
73 
74 // Sets the audio buffer and key settings
75 void AudioResampler::SetBuffer(juce::AudioBuffer<float> *new_buffer, double ratio)
76 {
77  // Update buffer & buffer source
78  buffer = new_buffer;
79  buffer_source->setBuffer(buffer);
80 
81  // Set the sample ratio (the ratio of sample rate change)
82  source_ratio = ratio;
83  dest_ratio = 1.0 / ratio;
84  num_of_samples = buffer->getNumSamples();
85  new_num_of_samples = round(num_of_samples * dest_ratio);
86 
87  // Set resample ratio
88  resample_source->setResamplingRatio(source_ratio);
89 
90  // Prepare to play resample source
91  if (!isPrepared)
92  {
93  // Prepare to play the audio sources (and set the # of samples per chunk to a little more than expected)
94  resample_source->prepareToPlay(num_of_samples + 10, 0);
95  isPrepared = true;
96  }
97 
98  // Resize buffer for the newly resampled data
99  resampled_buffer->setSize(buffer->getNumChannels(), new_num_of_samples, true, true, true);
100  resample_callback_buffer.numSamples = new_num_of_samples;
101  resample_callback_buffer.startSample = 0;
102  resample_callback_buffer.clearActiveBufferRegion();
103 }
104 
105 // Get the resampled audio buffer
106 juce::AudioBuffer<float>* AudioResampler::GetResampledBuffer()
107 {
108  // Resample the current frame's audio buffer (into the temp callback buffer)
109  resample_source->getNextAudioBlock(resample_callback_buffer);
110 
111  // Return buffer pointer to this newly resampled buffer
112  return resampled_buffer;
113 }
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
juce::AudioBuffer< float >
AudioResampler.h
Header file for AudioResampler class.
openshot::AudioBufferSource
This class is used to expose an AudioBuffer<float> as an AudioSource in JUCE.
Definition: AudioBufferSource.h:27