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