diff options
| author | Tim Redfern <tim@getdrop.com> | 2022-12-18 23:30:14 +0000 |
|---|---|---|
| committer | Tim Redfern <tim@getdrop.com> | 2022-12-18 23:30:14 +0000 |
| commit | 493d6151ee284b5cfa136b88793498d7d9a59051 (patch) | |
| tree | 840160bc94ec89297597617adc352b10d065b9cc /glitcher2/src | |
| parent | 36a3cf9cf6206a4be7021f980da51c89229dc8c7 (diff) | |
standalone of10 versionmain
Diffstat (limited to 'glitcher2/src')
| -rw-r--r-- | glitcher2/src/audioGlitcher.h | 182 | ||||
| -rw-r--r-- | glitcher2/src/main.cpp | 6 | ||||
| -rw-r--r-- | glitcher2/src/ofApp.cpp | 88 | ||||
| -rw-r--r-- | glitcher2/src/ofApp.h | 31 |
4 files changed, 307 insertions, 0 deletions
diff --git a/glitcher2/src/audioGlitcher.h b/glitcher2/src/audioGlitcher.h new file mode 100644 index 0000000..a33b3fb --- /dev/null +++ b/glitcher2/src/audioGlitcher.h @@ -0,0 +1,182 @@ +#pragma once + +#include "ofMain.h" +#include "ofxOpenCv.h" + +class audioGlitcher { + public: + + ofxCvColorImage buffer; + ofFbo renderFBO; + IplImage tmp; + vector<float> *samples; + int interp_x,interp_y; + float trans_x,trans_y; + float scale; + float rotation; + float origin_x,origin_y; + + void setup(int w,int h,vector<float> *s){ + buffer.allocate(w,h); + renderFBO.allocate(w,h,GL_RGB); + samples=s; + origin_x=origin_y=0.5f; + trans_x=trans_y=0.0f; + scale=1.0f; + rotation=0.0f; + interp_x=16; + interp_y=12; + } + void set_interp(int ix,int iy){ + interp_x=ix; + interp_y=iy; + } + void set_trans(float x,float y){ + trans_x=x; + trans_y=y; + } + void set_scale(float s){ + scale=s; + } + void set_rot(float r){ + rotation=r; + } + void set_origin(float x,float y){ + origin_x=x; + origin_y=y; + } + void draw(int x,int y){ + renderFBO.draw(x,y); + } + void update(){ + + renderFBO.readToPixels(buffer.getPixels()); + buffer.flagImageChanged(); + + //create low res remap target + + cv::Mat dstX(interp_x,interp_y,CV_32FC1); + cv::Mat dstY(interp_x,interp_y,CV_32FC1); + cv::Mat srcX(interp_x,interp_y,CV_32FC1); + cv::Mat srcY(interp_x,interp_y,CV_32FC1); + + float xFactor=renderFBO.getWidth()/srcX.cols; + float yFactor=renderFBO.getHeight()/srcX.rows; + for (int i=0;i<srcX.cols;i++){ + for (int j=0;j<srcX.rows;j++){ + srcX.at<float>(j,i)=i*xFactor; + srcY.at<float>(j,i)=j*yFactor; + } + } + + //transform the low res matrix + float tX=trans_x-.05; //fraction of image + float tY=trans_y-.08; //fraction of image + float oX=origin_x; //fraction of image + float oY=origin_y; //fraction of image + float s=scale; + float r=rotation; + + cv::Point2f srcTri[3], dstTri[3]; + + // Compute matrix by creating triangle and transforming + srcTri[0].x=0; + srcTri[0].y=0; + srcTri[1].x=dstX.rows-1; + srcTri[1].y=0; + srcTri[2].x=0; + srcTri[2].y=dstX.cols-1; + + for (int i=0;i<3;i++){ + + dstTri[i].x=srcTri[i].x+(tX*dstX.cols); + dstTri[i].y=srcTri[i].y+(tY*dstX.cols); //use cols for equiv coords + //rotate and scale around centre + //transform to centre + dstTri[i].x-=(oX*dstX.cols); + dstTri[i].y-=(oY*dstX.cols); + + dstTri[i].x*=s; + dstTri[i].y*=s; + + double dx=(dstTri[i].x*cos(r))-(dstTri[i].y*sin(r)); + double dy=(dstTri[i].x*sin(r))+(dstTri[i].y*cos(r)); + + dstTri[i].x=dx; + dstTri[i].y=dy; + + //transform back + dstTri[i].x+=(oX*dstX.cols); + dstTri[i].y+=(oY*dstX.cols); + } + + cv::Mat trans_mat=getAffineTransform(srcTri,dstTri); + warpAffine(srcX,dstX,trans_mat, srcX.size(), cv::INTER_LINEAR, cv::BORDER_WRAP); + warpAffine(srcY,dstY,trans_mat, srcY.size(), cv::INTER_LINEAR, cv::BORDER_WRAP); + + cv::Mat scaledstX; + cv::Mat scaledstY; + + cv::resize(dstX,scaledstX, cv::Size(renderFBO.getWidth(),renderFBO.getHeight()), 0, 0, cv::INTER_LINEAR); + cv::resize(dstY,scaledstY, cv::Size(renderFBO.getWidth(),renderFBO.getHeight()), 0, 0, cv::INTER_LINEAR); + + cv::Mat buf = cv::cvarrToMat(buffer.getCvImage(),false); //copyData= + cv::Mat dstbuf; + + cv::remap(buf,dstbuf,scaledstX,scaledstY, cv::INTER_NEAREST, cv::BORDER_WRAP); //, cv::Scalar(0,0, 0) ); + tmp = IplImage(dstbuf); + + //buffer=tmp; + //buffer.remap(scaledstX,scaledstY); + buffer=&tmp; + + renderFBO.begin(); + + // + ofSetColor(255,255,255); + + buffer-=1; + buffer.draw(0,0); + // + + //ofEnableAlphaBlending(); + //ofSetColor(0,0,0,128); + //ofRect(0,0,ofGetWidth(),ofGetHeight()); + //ofDisableAlphaBlending(); + + if (false){ + ofNoFill(); + ofPushMatrix(); + ofTranslate(renderFBO.getWidth()/2,renderFBO.getHeight()/2); + ofRect(-20,-20,40,40); + ofPopMatrix(); + } + + if (true) { + ofPushMatrix(); + ofTranslate(0,renderFBO.getHeight()/2); + + + ofSetColor(0,0,0); + ofFill(); + ofBeginShape(); + for (int i=0;i<samples->size();i++){ + ofVertex(i,(*samples)[i]*renderFBO.getHeight()); + } + ofEndShape(); + + ofSetColor(255,255,255); + ofNoFill(); + ofBeginShape(); + for (int i=0;i<samples->size();i++){ + ofVertex(i,(*samples)[i]*renderFBO.getHeight()); + } + ofEndShape(); + + ofPopMatrix(); + } + + renderFBO.end(); + //renderFBO.flagImageChanged(); + } +};
\ No newline at end of file diff --git a/glitcher2/src/main.cpp b/glitcher2/src/main.cpp new file mode 100644 index 0000000..d77aea4 --- /dev/null +++ b/glitcher2/src/main.cpp @@ -0,0 +1,6 @@ +#include "ofApp.h" + +int main() { + ofSetupOpenGL(512,384, OF_WINDOW); + ofRunApp(new ofApp()); +} diff --git a/glitcher2/src/ofApp.cpp b/glitcher2/src/ofApp.cpp new file mode 100644 index 0000000..9615a75 --- /dev/null +++ b/glitcher2/src/ofApp.cpp @@ -0,0 +1,88 @@ +#include "ofApp.h" + + +//-------------------------------------------------------------- +void ofApp::setup() { + ofSetLogLevel(OF_LOG_WARNING); + + ofSetFrameRate(60); + + int bufferSize= ofGetWidth(); //should be based on the size of glitch buffer + + soundStream.listDevices(); + //nb all you have to do to make audio work is to turn off pulseaudio in configuration + soundStream.setup(this, 0, 1, 44100, bufferSize, 1); + samples.resize(bufferSize); + + glitch.setup(ofGetWidth(),ofGetHeight(),&samples); + //glitch.set_interp(ofRandom(30)+2,ofRandom(22)+2); + + + frame=0; +} + +//-------------------------------------------------------------- +void ofApp::update() { + ofSetWindowTitle(ofToString(ofGetFrameRate())); + + glitch.update(); +} + +//-------------------------------------------------------------- +void ofApp::draw() { + frame++; + if (frame==1000){ + //glitch.set_interp(ofRandom(30)+2,ofRandom(22)+2); + frame=0; + } + + + glitch.draw(0,0); //eventually textured into a viewport + + +} + +void ofApp::audioIn(float * input, int bufferSize, int nChannels){ + for (int i=0;i<bufferSize/nChannels;i+=nChannels){ + samples[i]=input[i*nChannels]; + } +} + + + +//-------------------------------------------------------------- +void ofApp::exit() { + +} + +//-------------------------------------------------------------- +void ofApp::keyPressed (int key) { + switch (key) { + case ' ': + glitch.set_interp(ofRandom(30)+2,ofRandom(22)+2); + glitch.set_rot(ofRandom(1)>0.5?ofRandom(0.1):0); + break; + + case OF_KEY_UP: + break; + + case OF_KEY_DOWN: + break; + } +} + +//-------------------------------------------------------------- +void ofApp::mouseDragged(int x, int y, int button) +{} + +//-------------------------------------------------------------- +void ofApp::mousePressed(int x, int y, int button) +{} + +//-------------------------------------------------------------- +void ofApp::mouseReleased(int x, int y, int button) +{} + +//-------------------------------------------------------------- +void ofApp::windowResized(int w, int h) +{} diff --git a/glitcher2/src/ofApp.h b/glitcher2/src/ofApp.h new file mode 100644 index 0000000..beaceaf --- /dev/null +++ b/glitcher2/src/ofApp.h @@ -0,0 +1,31 @@ +#pragma once + +#include "ofMain.h" +#include "ofxOpenCv.h" +#include "audioGlitcher.h" + + +class ofApp : public ofBaseApp { +public: + + void setup(); + void update(); + void draw(); + void exit(); + + void keyPressed(int key); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void windowResized(int w, int h); + + void audioIn(float * input, int bufferSize, int nChannels); + + audioGlitcher glitch; + + ofSoundStream soundStream; + vector<float> samples; + + int frame; + +}; |
