Skip to content

Instantly share code, notes, and snippets.

@antimodular
Created June 22, 2019 05:05
Show Gist options
  • Save antimodular/26c59230e8f756ddd30f557c933a1123 to your computer and use it in GitHub Desktop.
Save antimodular/26c59230e8f756ddd30f557c933a1123 to your computer and use it in GitHub Desktop.
//
// threaded_fileHandler.h
// matrixDesk
//
// Created by Stephan Schulz on 2019-06-12.
//
#ifndef threaded_fileHandler_h
#define threaded_fileHandler_h
class threaded_fileHandler: public ofThread{
private:
public:
struct oneFile {
float trimAtEndAmt;
float fadeIn;
float fadeOut;
string src, dst;
string fileName;
string filePath;
};
// vector<oneFile> trimFiles;
// vector<oneFile> deleteFiles;
bool bDebug = false;
ofThreadChannel<oneFile> toTrimFile;
ofThreadChannel<bool> trimmedFile;
bool bTrimmed;
bool newFrame;
threaded_fileHandler():newFrame(true){
startThread();
}
~threaded_fileHandler(){
toTrimFile.close();
trimmedFile.close();
waitForThread(true);
}
void update(){
newFrame = false;
while(trimmedFile.tryReceive(bTrimmed)){
newFrame = true;
}
if(newFrame){
// if(!texture.isAllocated()){
// texture.allocate(pixels);
// }
// texture.loadData(pixels);
}
}
bool isFrameNew(){
return newFrame;
}
void addToTrim(string _src, string _dst, float _trimAtEndAmt,float _fadeIn, float _fadeOut){
std::unique_lock<std::mutex> lock(mutex);
oneFile tp;
tp.src = _src;
tp.dst = _dst;
tp.trimAtEndAmt = _trimAtEndAmt;
tp.fadeIn = _fadeIn;
tp.fadeOut = _fadeOut;
// trimFiles.send(tp);
// trimFiles.push_back(tp);
toTrimFile.send(tp);
// condition.notify_all();
}
void threadedFunction(){
oneFile tempFile;
// while(tempFile.receive(tempFile)){
// trimAudioFile();
// }
oneFile temp_oneFile;
while(toTrimFile.receive(temp_oneFile)){
trimAudioFile(temp_oneFile.src,temp_oneFile.dst,temp_oneFile.trimAtEndAmt, temp_oneFile.fadeIn, temp_oneFile.fadeOut);
#if __cplusplus>=201103
trimmedFile.send(std::move(true));
#else
trimmedFile.send(true);
#endif
}//end
}
void trimAudioFile(string _src, string _dst, float _trimAtEndAmt,float _fadeIn, float _fadeOut){
if(bDebug) ofLog()<<"trim _src "<<_src<<" to "<<_dst;
float minClipLength = 2;
float minClipSilence = 0.5;
float maxClipSilence = 0.5;
//we get duration of file
//we want to eliminate noise generated by the intercom button release. so trim the begining
//we normalize
//we want to to fade in
//we want to to fade out the last few milliseconds
//we want to short clips to have silence added at the end, so they sound better looped
//in older code i first trimmed file to remove button noise
//here i just move the fadeOut to an earlier point, so that by the time button noise would come fadeOut is already at zero
//new brew does not install ffmpeg with all flags
//so get it from here https://ffmpeg.zeranoe.com/builds/
//i got "static and shared builds for macOS 64-bit"
//probe to get durtion of file recording
// string probeScript = "/usr/local/bin/ffprobe -i "+_src+" -show_format -v quiet | grep duration";
string pathToFFMPEG = ofToDataPath("../../../../../ffmpeg/bin/");
string probeScript = pathToFFMPEG+"ffprobe -i "+_src+" -show_format -v quiet | grep duration";
string durationStr = ofSystem(probeScript);
vector<string> splitString = ofSplitString(durationStr, "=");
float duration = ofToFloat(splitString[1]);
if(bDebug) ofLog()<<"duration float = "<<duration;
if(bDebug) ofLog()<<endl;
//add some silence at end of file
//this helped so that files when looped do not sound to mechanical
float addedSilence = 0.3;
if(duration < minClipLength){
if(minClipSilence == -1 || maxClipSilence == -1){
addedSilence = (minClipLength - duration) + 0.3;
}else{
addedSilence = ofRandom(minClipSilence,maxClipSilence);
}
}
//the fadeOut starts
float fadeOutStartAt = duration-_fadeOut-_trimAtEndAmt;
if(duration <= fadeOutStartAt){
//file too short to have allow for fade out. it's more important to remove button noise
fadeOutStartAt = duration-_trimAtEndAmt;
}
if(duration <= fadeOutStartAt){
//file still too short to have a fadeOut
fadeOutStartAt = duration;
}
//https://ffmpeg.org/ffmpeg-all.html#afade-1
//https://trac.ffmpeg.org/wiki/AfadeCurves
//:curve=cub
//:curve=exp
//here we do not really trim
//but rather start the fade a bit earlier so that we can be sure to have faded to 0 before the noise at end can be heard
// ofLog()<<"fadein, out, trim + add silence if too short";
// string trimScript = "/usr/local/bin/ffmpeg -i ";
string trimScript = pathToFFMPEG+ "ffmpeg -i "; //space before and after i is important
trimScript += _src;
trimScript += " -filter_complex ";
trimScript += "\"";//start info in quotes
// trimScript += "[0:a] loudnorm [0a]; "; //ffmpeg normalize
// trimScript += "[0a] atrim=start=0:end=1[1a];"; //+ofToString(new_duration)+",asetpts=PTS-STARTPTS;";
trimScript += "[0a] afade=type=in:duration="+ofToString(_fadeIn)+":curve=cub, afade=type=out:start_time="+ofToString(fadeOutStartAt) +":duration="+ofToString(_fadeOut)+":curve=exp [1a];"; //fade in and out
trimScript += " aevalsrc=0:duration="+ofToString(addedSilence)+"[silence];"; //create a silence buffer
trimScript += " [1a][silence]concat=n=2:v=0:a=1[out]";
trimScript += "\""; //end info in quotes
trimScript += " -map [out]"; //combine all
trimScript += " -v quiet"; //do not print to console
trimScript += " "+_dst;
if(bDebug) ofLog()<<"trimScript "<<trimScript;
if(bDebug) ofLog()<<ofSystem(trimScript);
else ofSystem(trimScript);
//"[0:v]trim=1.40.1:1.59.3,setpts=PTS-STARTPTS[v]"
if( remove(_src.c_str()) != 0 ){
ofLog()<<"Error deleting temporary rec file";
}
//--------------------------------------------------------------
}
};
#endif /* threaded_fileHandler_h */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment