Skip to content

Commit

Permalink
Merge pull request #719 from OpenShot/audio-devices
Browse files Browse the repository at this point in the history
New audio device [type] switching + Audio sync improvements
  • Loading branch information
jonoomph committed Dec 7, 2021
2 parents 7b19fa2 + 91c47eb commit 3c8dc71
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 155 deletions.
22 changes: 14 additions & 8 deletions bindings/python/openshot.i
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@
%shared_ptr(juce::AudioBuffer<float>)
%shared_ptr(openshot::Frame)

/* Instantiate the required template specializations */
%template() std::map<std::string, int>;
%template() std::pair<int, int>;
%template() std::vector<int>;
%template() std::pair<double, double>;
%template() std::pair<float, float>;
%template() std::pair<std::string, std::string>;
%template() std::vector<std::pair<std::string, std::string>>;

%{
#include "OpenShotVersion.h"
#include "ReaderBase.h"
#include "WriterBase.h"
#include "AudioDevices.h"
#include "CacheBase.h"
#include "CacheDisk.h"
#include "CacheMemory.h"
Expand Down Expand Up @@ -79,7 +89,6 @@
#include "TimelineBase.h"
#include "Timeline.h"
#include "ZmqLogger.h"
#include "AudioDeviceInfo.h"

%}

Expand Down Expand Up @@ -116,12 +125,7 @@
}
}

/* Instantiate the required template specializations */
%template() std::map<std::string, int>;
%template() std::pair<int, int>;
%template() std::vector<int>;
%template() std::pair<double, double>;
%template() std::pair<float, float>;


/* Wrap std templates (list, vector, etc...) */
%template(ClipList) std::list<openshot::Clip *>;
Expand All @@ -131,6 +135,8 @@
%template(FieldVector) std::vector<openshot::Field>;
%template(MappedFrameVector) std::vector<openshot::MappedFrame>;
%template(MetadataMap) std::map<std::string, std::string>;

/* Deprecated */
%template(AudioDeviceInfoVector) std::vector<openshot::AudioDeviceInfo>;

/* Make openshot.Fraction more Pythonic */
Expand Down Expand Up @@ -256,6 +262,7 @@
%include "OpenShotVersion.h"
%include "ReaderBase.h"
%include "WriterBase.h"
%include "AudioDevices.h"
%include "CacheBase.h"
%include "CacheDisk.h"
%include "CacheMemory.h"
Expand Down Expand Up @@ -290,7 +297,6 @@
%include "TimelineBase.h"
%include "Timeline.h"
%include "ZmqLogger.h"
%include "AudioDeviceInfo.h"

#ifdef USE_OPENCV
%include "ClipProcessingJobs.h"
Expand Down
30 changes: 17 additions & 13 deletions bindings/ruby/openshot.i
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
%shared_ptr(juce::AudioBuffer<float>)
%shared_ptr(openshot::Frame)

/* Template specializations */
/* Instantiate the required template specializations */
%template() std::map<std::string, int>;
%template() std::pair<int, int>;
%template() std::vector<int>;
%template() std::pair<double, double>;
%template() std::pair<float, float>;
%template() std::pair<std::string, std::string>;
%template() std::vector<std::pair<std::string, std::string>>;

%{
/* Ruby and FFmpeg define competing RSHIFT macros,
Expand All @@ -60,6 +62,7 @@
#include "OpenShotVersion.h"
#include "ReaderBase.h"
#include "WriterBase.h"
#include "AudioDevices.h"
#include "CacheBase.h"
#include "CacheDisk.h"
#include "CacheMemory.h"
Expand Down Expand Up @@ -94,7 +97,6 @@
#include "TimelineBase.h"
#include "Timeline.h"
#include "ZmqLogger.h"
#include "AudioDeviceInfo.h"

/* Move FFmpeg's RSHIFT to FF_RSHIFT, if present */
#ifdef RSHIFT
Expand All @@ -115,9 +117,22 @@
%}
#endif

/* Wrap std templates (list, vector, etc...) */
%template(ClipList) std::list<openshot::Clip *>;
%template(EffectBaseList) std::list<openshot::EffectBase *>;
%template(CoordinateVector) std::vector<openshot::Coordinate>;
%template(PointsVector) std::vector<openshot::Point>;
%template(FieldVector) std::vector<openshot::Field>;
%template(MappedFrameVector) std::vector<openshot::MappedFrame>;
%template(MetadataMap) std::map<std::string, std::string>;

/* Deprecated */
%template(AudioDeviceInfoVector) std::vector<openshot::AudioDeviceInfo>;

%include "OpenShotVersion.h"
%include "ReaderBase.h"
%include "WriterBase.h"
%include "AudioDevices.h"
%include "CacheBase.h"
%include "CacheDisk.h"
%include "CacheMemory.h"
Expand Down Expand Up @@ -173,15 +188,13 @@
%include "TimelineBase.h"
%include "Timeline.h"
%include "ZmqLogger.h"
%include "AudioDeviceInfo.h"

#ifdef USE_IMAGEMAGICK
%include "ImageReader.h"
%include "ImageWriter.h"
%include "TextReader.h"
#endif


/* Effects */
%include "effects/Bars.h"
%include "effects/Blur.h"
Expand All @@ -200,12 +213,3 @@
%include "effects/Wave.h"


/* Wrap std templates (list, vector, etc...) */
%template(ClipList) std::list<openshot::Clip *>;
%template(EffectBaseList) std::list<openshot::EffectBase *>;
%template(CoordinateVector) std::vector<openshot::Coordinate>;
%template(PointsVector) std::vector<openshot::Point>;
%template(FieldVector) std::vector<openshot::Field>;
%template(MappedFrameVector) std::vector<openshot::MappedFrame>;
%template(MetadataMap) std::map<std::string, std::string>;
%template(AudioDeviceInfoVector) std::vector<openshot::AudioDeviceInfo>;
29 changes: 0 additions & 29 deletions src/AudioDeviceInfo.h

This file was deleted.

41 changes: 41 additions & 0 deletions src/AudioDevices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @file
* @brief Utility methods for identifying audio devices
* @author Jonathan Thomas <jonathan@openshot.org>
* @author FeRD (Frank Dana) <ferdnyc@gmail.com>
*
* @ref License
*/

// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "AudioDevices.h"

#include <OpenShotAudio.h>

using namespace openshot;

using AudioDeviceList = std::vector<std::pair<std::string, std::string>>;

// Build a list of devices found, and return
AudioDeviceList AudioDevices::getNames() {
// A temporary device manager, used to scan device names.
// Its initialize() is never called, and devices are not opened.
std::unique_ptr<juce::AudioDeviceManager>
manager(new juce::AudioDeviceManager());

m_devices.clear();

auto &types = manager->getAvailableDeviceTypes();
for (auto* t : types) {
t->scanForDevices();
const auto names = t->getDeviceNames();
for (const auto& name : names) {
m_devices.emplace_back(
name.toStdString(), t->getTypeName().toStdString());
}
}
return m_devices;
}
47 changes: 47 additions & 0 deletions src/AudioDevices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @file
* @brief Header file for Audio Device Info struct
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/

// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef OPENSHOT_AUDIODEVICEINFO_H
#define OPENSHOT_AUDIODEVICEINFO_H

#include <string>
#include <vector>

namespace openshot {
/**
* @brief This struct hold information about Audio Devices
*
* The type and name of the audio device.
*/
struct
AudioDeviceInfo {
std::string name;
std::string type;
};

using AudioDeviceList = std::vector<std::pair<std::string, std::string>>;

/// A class which probes the available audio devices
class AudioDevices
{
public:
AudioDevices() = default;

/// Return a vector of std::pair<> objects holding the
/// device name and type for each audio device detected
AudioDeviceList getNames();
private:
AudioDeviceList m_devices;
};

}
#endif
29 changes: 12 additions & 17 deletions src/AudioReaderSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ using namespace std;
using namespace openshot;

// Constructor that reads samples from a reader
AudioReaderSource::AudioReaderSource(
ReaderBase *audio_reader, int64_t starting_frame_number, int buffer_size
) :
position(0),
size(buffer_size),
buffer(new juce::AudioBuffer<float>(audio_reader->info.channels, buffer_size)),
speed(1),
reader(audio_reader),
frame_number(starting_frame_number),
frame_position(0),
estimated_frame(0)
{
// Zero the buffer contents
AudioReaderSource::AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number, int buffer_size)
: reader(audio_reader), frame_number(starting_frame_number),
size(buffer_size), position(0), frame_position(0), estimated_frame(0), speed(1) {

// Initialize an audio buffer (based on reader)
buffer = new juce::AudioBuffer<float>(reader->info.channels, size);

// initialize the audio samples to zero (silence)
buffer->clear();
}

Expand Down Expand Up @@ -61,7 +56,7 @@ void AudioReaderSource::GetMoreSamplesFromReader()
estimated_frame = frame_number;

// Init new buffer
juce::AudioBuffer<float> *new_buffer = new juce::AudioSampleBuffer(reader->info.channels, size);
auto *new_buffer = new juce::AudioBuffer<float>(reader->info.channels, size);
new_buffer->clear();

// Move the remaining samples into new buffer (if any)
Expand Down Expand Up @@ -130,7 +125,7 @@ void AudioReaderSource::GetMoreSamplesFromReader()
}

// Reverse an audio buffer
juce::AudioBuffer<float>* AudioReaderSource::reverse_buffer(juce::AudioSampleBuffer* buffer)
juce::AudioBuffer<float>* AudioReaderSource::reverse_buffer(juce::AudioBuffer<float>* buffer)
{
int number_of_samples = buffer->getNumSamples();
int channels = buffer->getNumChannels();
Expand All @@ -139,7 +134,7 @@ juce::AudioBuffer<float>* AudioReaderSource::reverse_buffer(juce::AudioSampleBuf
ZmqLogger::Instance()->AppendDebugMethod("AudioReaderSource::reverse_buffer", "number_of_samples", number_of_samples, "channels", channels);

// Reverse array (create new buffer to hold the reversed version)
juce::AudioBuffer<float> *reversed = new juce::AudioSampleBuffer(channels, number_of_samples);
auto *reversed = new juce::AudioBuffer<float>(channels, number_of_samples);
reversed->clear();

for (int channel = 0; channel < channels; channel++)
Expand Down Expand Up @@ -229,7 +224,7 @@ void AudioReaderSource::getNextAudioBlock(const juce::AudioSourceChannelInfo& in
}

// Prepare to play this audio source
void AudioReaderSource::prepareToPlay(int, double) { }
void AudioReaderSource::prepareToPlay(int, double) {}

// Release all resources
void AudioReaderSource::releaseResources() { }
Expand Down
2 changes: 1 addition & 1 deletion src/AudioReaderSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace openshot
void GetMoreSamplesFromReader();

/// Reverse an audio buffer (for backwards audio)
juce::AudioBuffer<float>* reverse_buffer(juce::AudioSampleBuffer* buffer);
juce::AudioBuffer<float>* reverse_buffer(juce::AudioBuffer<float>* buffer);

public:

Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ add_feature_info("IWYU (include-what-you-use)" ENABLE_IWYU "Scan all source file
# Main library sources
set(OPENSHOT_SOURCES
AudioBufferSource.cpp
AudioDevices.cpp
AudioReaderSource.cpp
AudioResampler.cpp
CacheBase.cpp
Expand Down

0 comments on commit 3c8dc71

Please sign in to comment.