2013-09-11 17:32:40 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for Clip class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
|
|
|
|
* @section LICENSE
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2008-2013 OpenShot Studios, LLC
|
|
|
|
|
* (http://www.openshotstudios.com). This file is part of
|
|
|
|
|
* OpenShot Library (http://www.openshot.org), an open-source project
|
|
|
|
|
* dedicated to delivering high quality video editing and animation solutions
|
|
|
|
|
* to the world.
|
|
|
|
|
*
|
|
|
|
|
* OpenShot Library is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* OpenShot Library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-10-03 01:55:24 -05:00
|
|
|
#include "../include/Clip.h"
|
|
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2012-10-04 01:34:45 -05:00
|
|
|
// Init default settings for a clip
|
|
|
|
|
void Clip::init_settings()
|
2012-10-03 01:55:24 -05:00
|
|
|
{
|
|
|
|
|
// Init clip settings
|
2012-10-04 15:07:29 -05:00
|
|
|
Position(0.0);
|
|
|
|
|
Layer(0);
|
|
|
|
|
Start(0.0);
|
|
|
|
|
End(0.0);
|
2012-10-04 16:07:58 -05:00
|
|
|
gravity = GRAVITY_CENTER;
|
|
|
|
|
scale = SCALE_FIT;
|
|
|
|
|
anchor = ANCHOR_CANVAS;
|
2012-11-29 16:32:48 -06:00
|
|
|
waveform = false;
|
2012-10-03 01:55:24 -05:00
|
|
|
|
|
|
|
|
// Init scale curves
|
2012-11-08 18:02:20 -06:00
|
|
|
scale_x = Keyframe(1.0);
|
|
|
|
|
scale_y = Keyframe(1.0);
|
2012-10-03 01:55:24 -05:00
|
|
|
|
|
|
|
|
// Init location curves
|
|
|
|
|
location_x = Keyframe(0.0);
|
|
|
|
|
location_y = Keyframe(0.0);
|
|
|
|
|
|
|
|
|
|
// Init alpha & rotation
|
2012-11-08 04:35:21 -06:00
|
|
|
alpha = Keyframe(0.0);
|
2012-10-03 01:55:24 -05:00
|
|
|
rotation = Keyframe(0.0);
|
|
|
|
|
|
|
|
|
|
// Init time & volume
|
|
|
|
|
time = Keyframe(0.0);
|
2012-11-29 16:32:48 -06:00
|
|
|
volume = Keyframe(1.0);
|
2012-10-03 01:55:24 -05:00
|
|
|
|
2012-11-29 23:11:50 -06:00
|
|
|
// Init audio waveform color
|
|
|
|
|
wave_color = (Color){Keyframe(0), Keyframe(28672), Keyframe(65280)};
|
|
|
|
|
|
2012-10-04 01:34:45 -05:00
|
|
|
// Init crop settings
|
2012-10-04 16:07:58 -05:00
|
|
|
crop_gravity = GRAVITY_CENTER;
|
2012-10-04 01:34:45 -05:00
|
|
|
crop_width = Keyframe(-1.0);
|
|
|
|
|
crop_height = Keyframe(-1.0);
|
|
|
|
|
crop_x = Keyframe(0.0);
|
|
|
|
|
crop_y = Keyframe(0.0);
|
|
|
|
|
|
|
|
|
|
// Init shear and perspective curves
|
|
|
|
|
shear_x = Keyframe(0.0);
|
|
|
|
|
shear_y = Keyframe(0.0);
|
|
|
|
|
perspective_c1_x = Keyframe(-1.0);
|
|
|
|
|
perspective_c1_y = Keyframe(-1.0);
|
|
|
|
|
perspective_c2_x = Keyframe(-1.0);
|
|
|
|
|
perspective_c2_y = Keyframe(-1.0);
|
|
|
|
|
perspective_c3_x = Keyframe(-1.0);
|
|
|
|
|
perspective_c3_y = Keyframe(-1.0);
|
|
|
|
|
perspective_c4_x = Keyframe(-1.0);
|
|
|
|
|
perspective_c4_y = Keyframe(-1.0);
|
2012-10-04 16:07:58 -05:00
|
|
|
|
|
|
|
|
// Default pointers
|
|
|
|
|
file_reader = NULL;
|
2012-10-19 01:49:48 -05:00
|
|
|
resampler = NULL;
|
2013-09-28 22:00:52 -05:00
|
|
|
audio_cache = NULL;
|
2012-10-04 01:34:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default Constructor for a clip
|
|
|
|
|
Clip::Clip()
|
|
|
|
|
{
|
|
|
|
|
// Init all default settings
|
|
|
|
|
init_settings();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-09 01:45:34 -05:00
|
|
|
// Constructor with reader
|
2013-09-08 16:40:57 -05:00
|
|
|
Clip::Clip(ReaderBase* reader)
|
2012-10-09 01:45:34 -05:00
|
|
|
{
|
2012-10-21 05:29:29 -05:00
|
|
|
// Init all default settings
|
|
|
|
|
init_settings();
|
|
|
|
|
|
2012-10-09 01:45:34 -05:00
|
|
|
// set reader pointer
|
|
|
|
|
file_reader = reader;
|
2012-12-07 01:05:48 -06:00
|
|
|
|
|
|
|
|
// Open and Close the reader (to set the duration of the clip)
|
|
|
|
|
Open();
|
|
|
|
|
Close();
|
2012-10-09 01:45:34 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-04 01:34:45 -05:00
|
|
|
// Constructor with filepath
|
|
|
|
|
Clip::Clip(string path)
|
|
|
|
|
{
|
|
|
|
|
// Init all default settings
|
|
|
|
|
init_settings();
|
|
|
|
|
|
2012-10-04 18:02:46 -05:00
|
|
|
// Get file extension (and convert to lower case)
|
|
|
|
|
string ext = get_file_extension(path);
|
|
|
|
|
transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
|
2012-10-04 01:34:45 -05:00
|
|
|
|
2012-10-04 18:02:46 -05:00
|
|
|
// Determine if common video formats
|
2012-10-09 01:45:34 -05:00
|
|
|
if (ext=="avi" || ext=="mov" || ext=="mkv" || ext=="mpg" || ext=="mpeg" || ext=="mp3" || ext=="mp4" || ext=="mts" ||
|
|
|
|
|
ext=="ogg" || ext=="wav" || ext=="wmv" || ext=="webm" || ext=="vob")
|
2012-10-04 18:02:46 -05:00
|
|
|
{
|
2012-10-04 01:34:45 -05:00
|
|
|
try
|
|
|
|
|
{
|
2012-10-04 18:02:46 -05:00
|
|
|
// Open common video format
|
2012-10-04 01:34:45 -05:00
|
|
|
file_reader = new FFmpegReader(path);
|
|
|
|
|
cout << "READER FOUND: FFmpegReader" << endl;
|
2012-10-04 18:02:46 -05:00
|
|
|
} catch(...) { }
|
|
|
|
|
}
|
2012-10-04 01:34:45 -05:00
|
|
|
|
2012-10-04 18:02:46 -05:00
|
|
|
// If no video found, try each reader
|
|
|
|
|
if (!file_reader)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Try an image reader
|
|
|
|
|
file_reader = new ImageReader(path);
|
|
|
|
|
cout << "READER FOUND: ImageReader" << endl;
|
|
|
|
|
|
|
|
|
|
} catch(...) {
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Try a video reader
|
|
|
|
|
file_reader = new FFmpegReader(path);
|
|
|
|
|
cout << "READER FOUND: FFmpegReader" << endl;
|
|
|
|
|
|
|
|
|
|
} catch(BaseException ex) {
|
|
|
|
|
// No Reader Found, Throw an exception
|
|
|
|
|
cout << "READER NOT FOUND" << endl;
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
2012-10-04 01:34:45 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-09 01:45:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the current reader
|
2013-09-08 16:40:57 -05:00
|
|
|
void Clip::Reader(ReaderBase* reader)
|
2012-10-09 01:45:34 -05:00
|
|
|
{
|
|
|
|
|
// set reader pointer
|
|
|
|
|
file_reader = reader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the current reader
|
2013-09-28 22:00:52 -05:00
|
|
|
ReaderBase* Clip::Reader() throw(ReaderClosed)
|
2012-10-09 01:45:34 -05:00
|
|
|
{
|
2013-09-28 22:00:52 -05:00
|
|
|
if (file_reader)
|
|
|
|
|
return file_reader;
|
|
|
|
|
else
|
|
|
|
|
// Throw error if reader not initialized
|
|
|
|
|
throw ReaderClosed("No Reader has been initialized for this Clip. Call Reader(*reader) before calling this method.", "");
|
2012-10-04 16:07:58 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 16:22:18 -05:00
|
|
|
// Open the internal reader
|
2013-09-28 22:00:52 -05:00
|
|
|
void Clip::Open() throw(InvalidFile, ReaderClosed)
|
2012-10-08 16:22:18 -05:00
|
|
|
{
|
|
|
|
|
if (file_reader)
|
2012-10-09 01:45:34 -05:00
|
|
|
{
|
|
|
|
|
// Open the reader
|
2012-10-08 16:22:18 -05:00
|
|
|
file_reader->Open();
|
2012-10-09 01:45:34 -05:00
|
|
|
|
|
|
|
|
// Set some clip properties from the file reader
|
2012-12-07 01:05:48 -06:00
|
|
|
if (end == 0.0)
|
2012-11-08 04:35:21 -06:00
|
|
|
End(file_reader->info.duration);
|
2012-10-09 01:45:34 -05:00
|
|
|
}
|
2013-09-28 22:00:52 -05:00
|
|
|
else
|
|
|
|
|
// Throw error if reader not initialized
|
|
|
|
|
throw ReaderClosed("No Reader has been initialized for this Clip. Call Reader(*reader) before calling this method.", "");
|
2012-10-08 16:22:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the internal reader
|
2013-09-28 22:00:52 -05:00
|
|
|
void Clip::Close() throw(ReaderClosed)
|
2012-10-08 16:22:18 -05:00
|
|
|
{
|
|
|
|
|
if (file_reader)
|
|
|
|
|
file_reader->Close();
|
2013-09-28 22:00:52 -05:00
|
|
|
else
|
|
|
|
|
// Throw error if reader not initialized
|
|
|
|
|
throw ReaderClosed("No Reader has been initialized for this Clip. Call Reader(*reader) before calling this method.", "");
|
2012-10-08 16:22:18 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-14 02:36:05 -05:00
|
|
|
// Get end position of clip (trim end of video), which can be affected by the time curve.
|
2013-09-28 22:00:52 -05:00
|
|
|
float Clip::End() throw(ReaderClosed)
|
2012-10-14 02:36:05 -05:00
|
|
|
{
|
|
|
|
|
// if a time curve is present, use it's length
|
|
|
|
|
if (time.Points.size() > 1)
|
2012-11-08 04:35:21 -06:00
|
|
|
{
|
|
|
|
|
// Determine the FPS fo this clip
|
|
|
|
|
float fps = 24.0;
|
|
|
|
|
if (file_reader)
|
|
|
|
|
// file reader
|
|
|
|
|
fps = file_reader->info.fps.ToFloat();
|
2013-09-28 22:00:52 -05:00
|
|
|
else
|
|
|
|
|
// Throw error if reader not initialized
|
|
|
|
|
throw ReaderClosed("No Reader has been initialized for this Clip. Call Reader(*reader) before calling this method.", "");
|
2012-11-08 04:35:21 -06:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
return float(time.GetLength()) / fps;
|
2012-11-08 04:35:21 -06:00
|
|
|
}
|
2012-10-14 02:36:05 -05:00
|
|
|
else
|
|
|
|
|
// just use the duration (as detected by the reader)
|
|
|
|
|
return end;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 16:22:18 -05:00
|
|
|
// Get an openshot::Frame object for a specific frame number of this reader.
|
2012-10-14 03:43:52 -05:00
|
|
|
tr1::shared_ptr<Frame> Clip::GetFrame(int requested_frame) throw(ReaderClosed)
|
2012-10-08 16:22:18 -05:00
|
|
|
{
|
2013-09-28 22:00:52 -05:00
|
|
|
if (file_reader)
|
|
|
|
|
{
|
|
|
|
|
// Adjust out of bounds frame number
|
|
|
|
|
requested_frame = adjust_frame_number_minimum(requested_frame);
|
2012-10-08 16:22:18 -05:00
|
|
|
|
2013-09-28 22:00:52 -05:00
|
|
|
// Is a time map detected
|
|
|
|
|
int new_frame_number = requested_frame;
|
|
|
|
|
if (time.Values.size() > 1)
|
|
|
|
|
new_frame_number = time.GetInt(requested_frame);
|
2012-10-10 15:21:33 -05:00
|
|
|
|
2013-09-28 22:00:52 -05:00
|
|
|
// Now that we have re-mapped what frame number is needed, go and get the frame pointer
|
|
|
|
|
tr1::shared_ptr<Frame> frame = file_reader->GetFrame(new_frame_number);
|
2012-10-19 01:49:48 -05:00
|
|
|
|
2013-09-28 22:00:52 -05:00
|
|
|
// Get time mapped frame number (used to increase speed, change direction, etc...)
|
|
|
|
|
tr1::shared_ptr<Frame> new_frame = get_time_mapped_frame(frame, requested_frame);
|
2012-10-10 02:36:53 -05:00
|
|
|
|
2013-09-28 22:00:52 -05:00
|
|
|
// Return processed 'frame'
|
|
|
|
|
return new_frame;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// Throw error if reader not initialized
|
|
|
|
|
throw ReaderClosed("No Reader has been initialized for this Clip. Call Reader(*reader) before calling this method.", "");
|
2012-10-08 16:22:18 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-04 18:02:46 -05:00
|
|
|
// Get file extension
|
|
|
|
|
string Clip::get_file_extension(string path)
|
|
|
|
|
{
|
|
|
|
|
// return last part of path
|
|
|
|
|
return path.substr(path.find_last_of(".") + 1);
|
|
|
|
|
}
|
2012-10-10 02:36:53 -05:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
// Reverse an audio buffer
|
|
|
|
|
void Clip::reverse_buffer(juce::AudioSampleBuffer* buffer)
|
2012-10-10 02:36:53 -05:00
|
|
|
{
|
2012-10-21 05:29:29 -05:00
|
|
|
int number_of_samples = buffer->getNumSamples();
|
|
|
|
|
int channels = buffer->getNumChannels();
|
|
|
|
|
|
|
|
|
|
// Reverse array (create new buffer to hold the reversed version)
|
|
|
|
|
AudioSampleBuffer *reversed = new juce::AudioSampleBuffer(channels, number_of_samples);
|
|
|
|
|
reversed->clear();
|
|
|
|
|
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
{
|
|
|
|
|
int n=0;
|
|
|
|
|
for (int s = number_of_samples - 1; s >= 0; s--, n++)
|
|
|
|
|
reversed->getSampleData(channel)[n] = buffer->getSampleData(channel)[s];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy the samples back to the original array
|
|
|
|
|
buffer->clear();
|
|
|
|
|
// Loop through channels, and get audio samples
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
// Get the audio samples for this channel
|
|
|
|
|
buffer->addFrom(channel, 0, reversed->getSampleData(channel), number_of_samples, 1.0f);
|
|
|
|
|
|
|
|
|
|
delete reversed;
|
|
|
|
|
reversed = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adjust the audio and image of a time mapped frame
|
2013-09-28 22:00:52 -05:00
|
|
|
tr1::shared_ptr<Frame> Clip::get_time_mapped_frame(tr1::shared_ptr<Frame> frame, int frame_number) throw(ReaderClosed)
|
2012-10-21 05:29:29 -05:00
|
|
|
{
|
2013-09-28 22:00:52 -05:00
|
|
|
// Check for valid reader
|
|
|
|
|
if (!file_reader)
|
|
|
|
|
// Throw error if reader not initialized
|
|
|
|
|
throw ReaderClosed("No Reader has been initialized for this Clip. Call Reader(*reader) before calling this method.", "");
|
|
|
|
|
|
2012-10-21 17:51:37 -05:00
|
|
|
tr1::shared_ptr<Frame> new_frame;
|
2012-10-21 05:29:29 -05:00
|
|
|
|
2012-10-10 02:36:53 -05:00
|
|
|
// Check for a valid time map curve
|
|
|
|
|
if (time.Values.size() > 1)
|
|
|
|
|
{
|
2012-10-21 05:29:29 -05:00
|
|
|
// create buffer and resampler
|
|
|
|
|
juce::AudioSampleBuffer *samples = NULL;
|
2012-10-19 01:49:48 -05:00
|
|
|
if (!resampler)
|
|
|
|
|
resampler = new AudioResampler();
|
2012-10-10 02:36:53 -05:00
|
|
|
|
2012-10-19 01:49:48 -05:00
|
|
|
// Get new frame number
|
2012-10-19 22:11:22 -05:00
|
|
|
int new_frame_number = time.GetInt(frame_number);
|
2012-10-19 01:49:48 -05:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
// Create a new frame
|
|
|
|
|
int samples_in_frame = GetSamplesPerFrame(new_frame_number, file_reader->info.fps);
|
2012-10-21 17:51:37 -05:00
|
|
|
new_frame = tr1::shared_ptr<Frame>(new Frame(new_frame_number, 1, 1, "#000000", samples_in_frame, frame->GetAudioChannelsCount()));
|
2012-10-19 01:49:48 -05:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
// Copy the image from the new frame
|
2012-10-21 17:51:37 -05:00
|
|
|
new_frame->AddImage(file_reader->GetFrame(new_frame_number)->GetImage());
|
2012-10-19 01:49:48 -05:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
|
|
|
|
|
// Get delta (difference in previous Y value)
|
|
|
|
|
int delta = int(round(time.GetDelta(frame_number)));
|
|
|
|
|
|
|
|
|
|
// Init audio vars
|
|
|
|
|
int sample_rate = file_reader->GetFrame(new_frame_number)->GetAudioSamplesRate();
|
|
|
|
|
int channels = file_reader->info.channels;
|
|
|
|
|
int number_of_samples = file_reader->GetFrame(new_frame_number)->GetAudioSamplesCount();
|
|
|
|
|
|
|
|
|
|
// Determine if we are speeding up or slowing down
|
|
|
|
|
if (time.GetRepeatFraction(frame_number).den > 1)
|
2012-10-19 01:49:48 -05:00
|
|
|
{
|
2012-10-21 05:29:29 -05:00
|
|
|
// Resample data, and return new buffer pointer
|
2012-10-21 17:51:37 -05:00
|
|
|
AudioSampleBuffer *buffer = NULL;
|
|
|
|
|
int resampled_buffer_size = 0;
|
|
|
|
|
|
|
|
|
|
if (time.GetRepeatFraction(frame_number).num == 1)
|
|
|
|
|
{
|
|
|
|
|
// SLOW DOWN audio (split audio)
|
|
|
|
|
samples = new juce::AudioSampleBuffer(channels, number_of_samples);
|
|
|
|
|
samples->clear();
|
|
|
|
|
|
|
|
|
|
// Loop through channels, and get audio samples
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
// Get the audio samples for this channel
|
|
|
|
|
samples->addFrom(channel, 0, file_reader->GetFrame(new_frame_number)->GetAudioSamples(channel), number_of_samples, 1.0f);
|
|
|
|
|
|
|
|
|
|
// Reverse the samples (if needed)
|
|
|
|
|
if (!time.IsIncreasing(frame_number))
|
|
|
|
|
reverse_buffer(samples);
|
|
|
|
|
|
|
|
|
|
// Resample audio to be X times slower (where X is the denominator of the repeat fraction)
|
|
|
|
|
resampler->SetBuffer(samples, 1.0 / time.GetRepeatFraction(frame_number).den);
|
|
|
|
|
|
|
|
|
|
// Resample the data (since it's the 1st slice)
|
|
|
|
|
buffer = resampler->GetResampledBuffer();
|
|
|
|
|
|
|
|
|
|
// Save the resampled data in the cache
|
|
|
|
|
audio_cache = new juce::AudioSampleBuffer(channels, buffer->getNumSamples());
|
|
|
|
|
audio_cache->clear();
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
// Get the audio samples for this channel
|
|
|
|
|
audio_cache->addFrom(channel, 0, buffer->getSampleData(channel), buffer->getNumSamples(), 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the length of the resampled buffer
|
|
|
|
|
resampled_buffer_size = audio_cache->getNumSamples();
|
|
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
// Just take the samples we need for the requested frame
|
|
|
|
|
int start = (number_of_samples * (time.GetRepeatFraction(frame_number).num - 1));
|
2012-10-21 17:51:37 -05:00
|
|
|
if (start > 0)
|
|
|
|
|
start -= 1;
|
2012-10-21 05:29:29 -05:00
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
// Add new (slower) samples, to the frame object
|
2012-11-29 16:32:48 -06:00
|
|
|
new_frame->AddAudio(true, channel, 0, audio_cache->getSampleData(channel, start), number_of_samples, 1.0f);
|
2012-10-21 17:51:37 -05:00
|
|
|
|
|
|
|
|
// Clean up if the final section
|
|
|
|
|
if (time.GetRepeatFraction(frame_number).num == time.GetRepeatFraction(frame_number).den)
|
|
|
|
|
{
|
|
|
|
|
// Clear, since we don't want it maintain state yet
|
|
|
|
|
delete audio_cache;
|
|
|
|
|
audio_cache = NULL;
|
|
|
|
|
}
|
2012-10-21 05:29:29 -05:00
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
buffer = NULL;
|
2012-10-19 01:49:48 -05:00
|
|
|
|
2012-10-29 01:47:39 -05:00
|
|
|
|
|
|
|
|
// Determine next unique frame (after these repeating frames)
|
2012-12-06 17:58:51 -06:00
|
|
|
//int next_unique_frame = time.GetInt(frame_number + (time.GetRepeatFraction(frame_number).den - time.GetRepeatFraction(frame_number).num) + 1);
|
|
|
|
|
//if (next_unique_frame != new_frame_number)
|
|
|
|
|
// // Overlay the next frame on top of this frame (to create a smoother slow motion effect)
|
|
|
|
|
// new_frame->AddImage(file_reader->GetFrame(next_unique_frame)->GetImage(), float(time.GetRepeatFraction(frame_number).num) / float(time.GetRepeatFraction(frame_number).den));
|
2012-10-29 01:47:39 -05:00
|
|
|
|
2012-10-19 01:49:48 -05:00
|
|
|
}
|
2012-10-21 05:29:29 -05:00
|
|
|
else if (abs(delta) > 1 && abs(delta) < 100)
|
2012-10-19 01:49:48 -05:00
|
|
|
{
|
2012-10-21 05:29:29 -05:00
|
|
|
// SPEED UP (multiple frames of audio), as long as it's not more than X frames
|
|
|
|
|
samples = new juce::AudioSampleBuffer(channels, number_of_samples * abs(delta));
|
|
|
|
|
samples->clear();
|
|
|
|
|
int start = 0;
|
2012-10-19 01:49:48 -05:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
if (delta > 0)
|
|
|
|
|
{
|
|
|
|
|
// Loop through each frame in this delta
|
|
|
|
|
for (int delta_frame = new_frame_number - (delta - 1); delta_frame <= new_frame_number; delta_frame++)
|
|
|
|
|
{
|
|
|
|
|
// buffer to hold detal samples
|
|
|
|
|
int number_of_delta_samples = file_reader->GetFrame(delta_frame)->GetAudioSamplesCount();
|
|
|
|
|
AudioSampleBuffer* delta_samples = new juce::AudioSampleBuffer(channels, number_of_delta_samples);
|
|
|
|
|
delta_samples->clear();
|
2012-10-19 01:49:48 -05:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
delta_samples->addFrom(channel, 0, file_reader->GetFrame(delta_frame)->GetAudioSamples(channel), number_of_delta_samples, 1.0f);
|
|
|
|
|
|
|
|
|
|
// Reverse the samples (if needed)
|
|
|
|
|
if (!time.IsIncreasing(frame_number))
|
|
|
|
|
reverse_buffer(delta_samples);
|
|
|
|
|
|
|
|
|
|
// Copy the samples to
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
// Get the audio samples for this channel
|
|
|
|
|
samples->addFrom(channel, start, delta_samples->getSampleData(channel), number_of_delta_samples, 1.0f);
|
|
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
delete delta_samples;
|
|
|
|
|
delta_samples = NULL;
|
|
|
|
|
|
|
|
|
|
// Increment start position
|
|
|
|
|
start += number_of_delta_samples;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Loop through each frame in this delta
|
|
|
|
|
for (int delta_frame = new_frame_number - (delta + 1); delta_frame >= new_frame_number; delta_frame--)
|
|
|
|
|
{
|
|
|
|
|
// buffer to hold delta samples
|
|
|
|
|
int number_of_delta_samples = file_reader->GetFrame(delta_frame)->GetAudioSamplesCount();
|
|
|
|
|
AudioSampleBuffer* delta_samples = new juce::AudioSampleBuffer(channels, number_of_delta_samples);
|
|
|
|
|
delta_samples->clear();
|
|
|
|
|
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
delta_samples->addFrom(channel, 0, file_reader->GetFrame(delta_frame)->GetAudioSamples(channel), number_of_delta_samples, 1.0f);
|
|
|
|
|
|
|
|
|
|
// Reverse the samples (if needed)
|
|
|
|
|
if (!time.IsIncreasing(frame_number))
|
|
|
|
|
reverse_buffer(delta_samples);
|
|
|
|
|
|
|
|
|
|
// Copy the samples to
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
// Get the audio samples for this channel
|
|
|
|
|
samples->addFrom(channel, start, delta_samples->getSampleData(channel), number_of_delta_samples, 1.0f);
|
|
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
delete delta_samples;
|
|
|
|
|
delta_samples = NULL;
|
|
|
|
|
|
|
|
|
|
// Increment start position
|
|
|
|
|
start += number_of_delta_samples;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resample audio to be X times faster (where X is the delta of the repeat fraction)
|
2012-10-23 11:13:06 -05:00
|
|
|
resampler->SetBuffer(samples, float(start) / float(number_of_samples));
|
2012-10-21 05:29:29 -05:00
|
|
|
|
|
|
|
|
// Resample data, and return new buffer pointer
|
|
|
|
|
AudioSampleBuffer *buffer = resampler->GetResampledBuffer();
|
|
|
|
|
int resampled_buffer_size = buffer->getNumSamples();
|
|
|
|
|
|
|
|
|
|
// Add the newly resized audio samples to the current frame
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
// Add new (slower) samples, to the frame object
|
2012-11-29 16:32:48 -06:00
|
|
|
new_frame->AddAudio(true, channel, 0, buffer->getSampleData(channel), number_of_samples, 1.0f);
|
2012-10-21 05:29:29 -05:00
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
}
|
2012-10-21 17:51:37 -05:00
|
|
|
else
|
2012-10-21 05:29:29 -05:00
|
|
|
{
|
2012-10-21 17:51:37 -05:00
|
|
|
// Use the samples on this frame (but maybe reverse them if needed)
|
2012-10-21 05:29:29 -05:00
|
|
|
samples = new juce::AudioSampleBuffer(channels, number_of_samples);
|
|
|
|
|
samples->clear();
|
|
|
|
|
|
|
|
|
|
// Loop through channels, and get audio samples
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
|
|
|
|
// Get the audio samples for this channel
|
2012-10-21 17:51:37 -05:00
|
|
|
samples->addFrom(channel, 0, frame->GetAudioSamples(channel), number_of_samples, 1.0f);
|
2012-10-21 05:29:29 -05:00
|
|
|
|
|
|
|
|
// reverse the samples
|
2013-02-19 00:51:07 -06:00
|
|
|
if (!time.IsIncreasing(frame_number))
|
|
|
|
|
reverse_buffer(samples);
|
2012-10-21 05:29:29 -05:00
|
|
|
|
|
|
|
|
// Add reversed samples to the frame object
|
|
|
|
|
for (int channel = 0; channel < channels; channel++)
|
2012-11-29 16:32:48 -06:00
|
|
|
new_frame->AddAudio(true, channel, 0, samples->getSampleData(channel), number_of_samples, 1.0f);
|
2012-10-22 17:05:34 -05:00
|
|
|
|
|
|
|
|
|
2012-10-19 01:49:48 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
// clean up
|
|
|
|
|
//delete resampler;
|
|
|
|
|
//resampler = NULL;
|
2012-12-06 17:58:51 -06:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
delete samples;
|
|
|
|
|
samples = NULL;
|
2012-11-07 17:45:13 -06:00
|
|
|
|
2012-10-21 17:51:37 -05:00
|
|
|
} else
|
|
|
|
|
// Use original frame
|
2012-11-07 17:45:13 -06:00
|
|
|
return frame;
|
2012-10-21 05:29:29 -05:00
|
|
|
|
|
|
|
|
// Return new time mapped frame
|
|
|
|
|
return new_frame;
|
2012-10-10 02:36:53 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-10 15:21:33 -05:00
|
|
|
// Adjust frame number minimum value
|
|
|
|
|
int Clip::adjust_frame_number_minimum(int frame_number)
|
|
|
|
|
{
|
|
|
|
|
// Never return a frame number 0 or below
|
|
|
|
|
if (frame_number < 1)
|
|
|
|
|
return 1;
|
|
|
|
|
else
|
|
|
|
|
return frame_number;
|
|
|
|
|
|
|
|
|
|
}
|
2012-10-21 05:29:29 -05:00
|
|
|
|
|
|
|
|
// Calculate the # of samples per video frame (for a specific frame number)
|
2013-09-28 22:00:52 -05:00
|
|
|
int Clip::GetSamplesPerFrame(int frame_number, Fraction rate) throw(ReaderClosed)
|
2012-10-21 05:29:29 -05:00
|
|
|
{
|
2013-09-28 22:00:52 -05:00
|
|
|
// Check for valid reader
|
|
|
|
|
if (!file_reader)
|
|
|
|
|
// Throw error if reader not initialized
|
|
|
|
|
throw ReaderClosed("No Reader has been initialized for this Clip. Call Reader(*reader) before calling this method.", "");
|
|
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
// Get the total # of samples for the previous frame, and the current frame (rounded)
|
|
|
|
|
double fps = rate.Reciprocal().ToDouble();
|
|
|
|
|
double previous_samples = round((file_reader->info.sample_rate * fps) * (frame_number - 1));
|
|
|
|
|
double total_samples = round((file_reader->info.sample_rate * fps) * frame_number);
|
|
|
|
|
|
|
|
|
|
// Subtract the previous frame's total samples with this frame's total samples. Not all sample rates can
|
|
|
|
|
// be evenly divided into frames, so each frame can have have different # of samples.
|
|
|
|
|
double samples_per_frame = total_samples - previous_samples;
|
|
|
|
|
return samples_per_frame;
|
|
|
|
|
}
|
2013-12-03 00:13:25 -06:00
|
|
|
|
2013-12-06 00:40:26 -06:00
|
|
|
// Generate Json::JsonValue for this object
|
|
|
|
|
Json::Value Clip::JsonValue() {
|
|
|
|
|
|
2013-12-03 00:13:25 -06:00
|
|
|
// Create root json object
|
2013-12-06 00:40:26 -06:00
|
|
|
Json::Value root = ClipBase::JsonValue(); // get parent properties
|
2013-12-03 00:13:25 -06:00
|
|
|
root["gravity"] = gravity;
|
|
|
|
|
root["scale"] = scale;
|
|
|
|
|
root["anchor"] = anchor;
|
|
|
|
|
root["waveform"] = waveform;
|
|
|
|
|
|
2013-12-06 00:40:26 -06:00
|
|
|
// return JsonValue
|
|
|
|
|
return root;
|
2013-12-03 00:13:25 -06:00
|
|
|
}
|
|
|
|
|
|
2013-12-06 00:40:26 -06:00
|
|
|
// Load Json::JsonValue into this object
|
|
|
|
|
void Clip::Json(Json::Value root) {
|
|
|
|
|
|
|
|
|
|
// Set parent data
|
|
|
|
|
ClipBase::Json(root);
|
|
|
|
|
|
|
|
|
|
// Set data from Json (if key is found)
|
|
|
|
|
if (root["gravity"] != Json::nullValue)
|
|
|
|
|
gravity = (GravityType) root["gravity"].asInt();
|
|
|
|
|
if (root["scale"] != Json::nullValue)
|
|
|
|
|
scale = (ScaleType) root["scale"].asInt();
|
|
|
|
|
if (root["anchor"] != Json::nullValue)
|
|
|
|
|
anchor = (AnchorType) root["anchor"].asInt();
|
|
|
|
|
if (root["waveform"] != Json::nullValue)
|
|
|
|
|
waveform = root["waveform"].asBool();
|
|
|
|
|
}
|