📄 aflibaudioedit.cc
字号:
/* * Copyright: (C) 1999-2001 Bruce W. Forsberg * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. * * This 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Bruce Forsberg forsberg@tns.net * */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <iostream>using std::cerr;using std::cout;using std::endl;#include "aflibAudioEdit.h"#include "aflibData.h"/*! \brief Constructor - requires an aflibAudio object. User must create this object with at least one input. The ID for this input can be assumed to be 1.*/aflibAudioEdit::aflibAudioEdit(aflibAudio& audio) : aflibAudio(audio){}/*! \brief Destructor.*/aflibAudioEdit::~aflibAudioEdit(){ // Erase all clips if (_clip_array.size() != 0) { _clip_array.erase( _clip_array.begin(), _clip_array.end()); }}voidaflibAudioEdit::addInput(int input){ // This function will add another input into this edit class. This will // allow the user to create an output from multiple inputs. // If this is the first input added then this object will be enabled // with a call to enable. _input_array.push_back(input); if (_input_array.size() == 1) { enable(TRUE); }}voidaflibAudioEdit::removeInput(int input){ // This function will remove an input from this object. If this is the last input // then this object will be disabled with a call to enable. First thing that will // be done is that all clips with this input will be removed. // remove all segments for this input removeSegmentsFromInput(input); // remove this input from list _input_array.remove(input); if (_input_array.size() == 0) { enable(FALSE); }}/*! \brief Add an audio clip using samples. This function will add a new segment at the position specified. If will push out all data after this insertion point in the existing audio clip array. This function allows one to specify the positions as samples.*/voidaflibAudioEdit::addSegment( int input, long long input_start_position, long long input_stop_position, long long output_insert_position, double factor){ long long delta; set<aflibEditClip, less < aflibEditClip > >::iterator it; set<aflibEditClip, less < aflibEditClip > >::iterator remove_first = _clip_array.end(); set<aflibEditClip, less < aflibEditClip > >::iterator remove_last = _clip_array.end(); set<aflibEditClip, less < aflibEditClip > > insert_array; long long insertion_point; delta = input_stop_position - input_start_position; // Lets create an insertion point. We do this by breaking up the segment into // two at the insertion point removeSegment(output_insert_position, output_insert_position); insertion_point = 0; // Go through the list of clips and find the insertion point. All clips after // the insertion point are pushed out to make room for this clip for (it = _clip_array.begin(); it != _clip_array.end(); it++) { if (output_insert_position == (*it).getStopSamplesOutput()) { insertion_point = output_insert_position; } else if ((*it).getStartSamplesOutput() >= output_insert_position) { aflibEditClip seg((*it).getInput(), (long long)((*it).getStartSamplesOutput() + delta * factor), (long long)((*it).getStopSamplesOutput() + delta * factor), (*it).getStartSamplesInput(), (*it).getStopSamplesInput(), (*it).getSampleRateFactor()); // Change current clip by marking it for removal and adding modified clip to // insert array. Then add new segment if (remove_first == _clip_array.end()) { remove_first = it; } remove_last = it; insert_array.insert(seg); } } // Create the new clip to be inserted aflibEditClip seg1(input, insertion_point, (long long)(insertion_point + delta * factor), input_start_position, input_stop_position, factor); insert_array.insert(seg1); // Remove the elements now if ((_clip_array.size() != 0) && (remove_first != _clip_array.end())) { // IF we did not find the last element then use the last element if (remove_last == _clip_array.end()) _clip_array.erase( remove_first, remove_last); else _clip_array.erase( remove_first, ++remove_last); } // Now add the modified and new elements _clip_array.insert(insert_array.begin(), insert_array.end()); recomputeConfig(); printClips();}/*! \brief Add an audio clip using seconds. This function will add a new segment at the position specified. If will push out all data after this insertion point in the existing audio clip array. This function allows one to specify the positions in seconds.*/voidaflibAudioEdit::addSegment( int input, double input_start_seconds, double input_stop_seconds, double output_insert_seconds, double factor){ const aflibConfig& cfg = getInputConfig(); long long start_samples; long long stop_samples; long long insert_samples; // Convert seconds to samples and call samples function start_samples = (long long)(input_start_seconds * cfg.getSamplesPerSecond()); stop_samples = (long long)(input_stop_seconds * cfg.getSamplesPerSecond()); insert_samples = (long long)(output_insert_seconds * cfg.getSamplesPerSecond()); addSegment(input, start_samples, stop_samples, insert_samples, factor);}/*! \brief Remove an audio clip segment by segment number. This function allows one to remove a segment from the audio clip list by its segment number. One should verify the segment first with a call to getSegment. Segment numbers can change when ever there is a change made. */voidaflibAudioEdit::removeSegment(int seg_num){ set<aflibEditClip, less < aflibEditClip > >::iterator it; int j; long long start_position = 0; long long stop_position = 0; // Find the segment to be removed start and stop positions if (seg_num <= (int)_clip_array.size()) { for (it = _clip_array.begin(), j = 1; it != _clip_array.end(); it++, j++) { if (j == seg_num) { start_position = (*it).getStartSamplesOutput(); stop_position = (*it).getStopSamplesOutput(); break; } } // IF we found the segment then remove if ((start_position !=0) || (stop_position != 0)) { removeSegment(start_position, stop_position); } }}/*! \brief Remove an audio clip segment by samples. This function allows one to remove an audio segment by specifing a start and stop samples position. This segment can span one or more audio segments. These samples are referenced to the current output.*/voidaflibAudioEdit::removeSegment( long long output_start_position, long long output_stop_position){ set<aflibEditClip, less < aflibEditClip > >::iterator it; set<aflibEditClip, less < aflibEditClip > >::iterator remove_first = _clip_array.end(); set<aflibEditClip, less < aflibEditClip > >::iterator remove_last = _clip_array.end(); set<aflibEditClip, less < aflibEditClip > > insert_array; long long output_lost = output_stop_position - output_start_position; bool remove_segment = FALSE; for (it = _clip_array.begin(); it != _clip_array.end(); it++) { aflibEditClip cur_seg = (*it); // If we are removing part of this segment if ((output_start_position > cur_seg.getStartSamplesOutput()) && (output_start_position < cur_seg.getStopSamplesOutput())) { // IF completly contained within this segment then break segment in two if (output_stop_position < cur_seg.getStopSamplesOutput()) { long long lost_samples = cur_seg.getStopSamplesOutput() - output_start_position; cur_seg.setStopSamplesOutput(output_start_position); cur_seg.setStopSamplesInput( cur_seg.getStopSamplesInput() - lost_samples ); long long delta = (*it).getStopSamplesOutput() - output_stop_position; aflibEditClip seg(cur_seg.getInput(), output_start_position, (*it).getStopSamplesOutput() - (output_stop_position - output_start_position), (*it).getStopSamplesInput() - delta, (*it).getStopSamplesInput(), (*it).getSampleRateFactor()); // Change current clip by marking it for removal and adding modified clip to // insert array. Then add new segment if (remove_first == _clip_array.end()) { remove_first = it; } remove_last = it; remove_segment = TRUE; insert_array.insert(cur_seg); insert_array.insert(seg); } // ELSE IF segment just ends at this segment then just readjust end point else if (output_stop_position == cur_seg.getStopSamplesOutput()) { long long lost_samples = output_stop_position - output_start_position; cur_seg.setStopSamplesOutput( cur_seg.getStopSamplesOutput() - lost_samples ); cur_seg.setStopSamplesInput( cur_seg.getStopSamplesInput() - lost_samples ); // Change current clip by marking it for removal and adding modified clip to // insert array. if (remove_first == _clip_array.end()) { remove_first = it; } remove_last = it; remove_segment = TRUE; // If segment to add has 0 length then entire segment was deleted so don't add if (cur_seg.getStopSamplesOutput() - cur_seg.getStartSamplesOutput() != 0) { insert_array.insert(cur_seg); } } // ELSE this segment spans more than this segment else { long long lost_samples = cur_seg.getStopSamplesOutput() - output_start_position; cur_seg.setStopSamplesOutput( output_start_position ); cur_seg.setStopSamplesInput( cur_seg.getStopSamplesInput() - lost_samples ); // Change current clip by marking it for removal and adding modified clip to // insert array. if (remove_first == _clip_array.end()) { remove_first = it; } remove_last = it; remove_segment = TRUE; insert_array.insert(cur_seg); } } // ELSE IF segment we are removing starts with this segment or before else if ((output_start_position <= cur_seg.getStartSamplesOutput()) && (output_stop_position > cur_seg.getStartSamplesOutput())) { // IF cut ends before this segment if (output_stop_position < cur_seg.getStopSamplesOutput()) { long long lost_samples = output_stop_position - cur_seg.getStartSamplesOutput(); long long lost_before_clip = cur_seg.getStartSamplesOutput() - output_start_position; cur_seg.setStartSamplesOutput( (*it).getStartSamplesOutput() - lost_before_clip); cur_seg.setStopSamplesOutput( (*it).getStopSamplesOutput() - lost_before_clip - lost_samples); cur_seg.setStartSamplesInput( (*it).getStartSamplesInput() + lost_samples); cur_seg.setStopSamplesInput( (*it).getStopSamplesInput() ); // Change current clip by marking it for removal and adding modified clip to // insert array. if (remove_first == _clip_array.end()) { remove_first = it; } remove_last = it; remove_segment = TRUE; insert_array.insert(cur_seg); } // ELSE IF entire segment else if (output_stop_position == cur_seg.getStopSamplesOutput()) { // erase current clip by marking it for removal if (remove_first == _clip_array.end()) { remove_first = it; } remove_last = it; remove_segment = TRUE; } // ELSE cut ends after this segment then remove clip and continue else { // erase current clip by marking it for removal if (remove_first == _clip_array.end()) { remove_first = it; } remove_last = it; remove_segment = TRUE; } } // ELSE IF all segments after the cut need to have output readjusted else if (output_stop_position <= cur_seg.getStartSamplesOutput()) { cur_seg.setStartSamplesOutput((*it).getStartSamplesOutput() - output_lost); cur_seg.setStopSamplesOutput((*it).getStopSamplesOutput() - output_lost); // Change current clip by marking it for removal and adding modified clip to // insert array. Then add new segment if (remove_first == _clip_array.end()) { remove_first = it; } remove_last = it; remove_segment = TRUE; insert_array.insert(cur_seg); } } // Remove the elements now // There must be data in clip array to erase _clip_array if ((_clip_array.size() != 0) && (remove_segment == TRUE)) { _clip_array.erase( remove_first, ++remove_last); } // Now add the modified and new elements if (insert_array.size() != 0) _clip_array.insert(insert_array.begin(), insert_array.end()); recomputeConfig(); printClips();}/*! \brief Remove an audio clip segment by seconds. This function allows one to remove an audio segment by specifing a start and stop seconds position. This segment can span one or more audio segments. These seconds are referenced to the current output.*/voidaflibAudioEdit::removeSegment( double output_start_seconds, double output_stop_seconds){ const aflibConfig& cfg = getInputConfig(); long long start_samples; long long stop_samples; // Convert seconds to samples and call samples remove function start_samples = (long long)(output_start_seconds * cfg.getSamplesPerSecond()); stop_samples = (long long)(output_stop_seconds * cfg.getSamplesPerSecond()); removeSegment(start_samples, stop_samples);}/*! \brief Remove all audio segments for a specific input This function allows one to remove all audio segments for a specific input.*/ voidaflibAudioEdit::removeSegmentsFromInput(int input){ // This function will remove all audio clips from a specific input. int num_segs; int inp, i; long long start_i, stop_i, start_o, stop_o; double factor; num_segs = getNumberOfSegments(); // Count backwards so that num_segs changing does not affect us // Remove all clips from this input for (i = num_segs; i != 0; i--) { getSegment(i, inp, start_i, stop_i, start_o, stop_o, factor);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -