Skip to content

Instantly share code, notes, and snippets.

@joselynNeon
Created October 29, 2014 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joselynNeon/972a215d9ac5c7e66d36 to your computer and use it in GitHub Desktop.
Save joselynNeon/972a215d9ac5c7e66d36 to your computer and use it in GitHub Desktop.
sound_mesh
#include "testApp.h"
//--------------------------------------------------------------
ofMesh testApp::pixelManipulate(ofImage imageA, ofMesh meshA, float intensityThreshold, float sketchDepth){
imageA.resize(200, 200);
//create mesh with points as the primitive
//meshA.setMode(OF_PRIMITIVE_POINTS);
//create a mesh with lines
// meshA.setMode(OF_PRIMITIVE_LINE_LOOP);
//meshA.setMode(OF_PRIMITIVE_TRIANGLE_FAN);
meshA.setMode(OF_PRIMITIVE_LINE_STRIP);
int w = imageA.getWidth();
int h = imageA.getHeight();
//loop through each pixel in the image using image width & height
for (int x=0; x<w; x++){
for(int y=0; y<h; y++){
//create the color object at that pixel
ofColor c = imageA.getColor(x, y);
//check the intensity of the pixel's color
float intensity = c.getLightness();
//if the intensity exceeds the threshold, create a vertex at the location of the pixel
//& color it with the pixel's color
if (intensity >= intensityThreshold){
//pushes brighter colors in the positive z direction
//pushes whiter colors in the negative z direction
float saturation = c.getSaturation();
float z = ofMap(saturation, 0, 255, -sketchDepth, sketchDepth);
//the image is now 1/4 the size of the OF window, so multiply
//the pixel location by 4 so that the mesh covers the OF window
ofVec3f pos(4*x, 4*y, z);
meshA.addVertex(pos);
meshA.addColor(c);
}
}
}
return meshA;
}
//--------------------------------------------------------------
void testApp::setup(){
ofBackground(0,0,0);
//______Serial
ofSetVerticalSync(true);
bSendSerialMessage = false;
ofBackground(255);
ofSetLogLevel(OF_LOG_VERBOSE);
serial.listDevices();
vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
// this should be set to whatever com port your serial device is connected to.
// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)
// arduino users check in arduino app....
int baud = 9600;
serial.setup(0, baud); //open the first device
//serial.setup("COM4", baud); // windows example
serial.setup("/dev/tty.usbmodem1411", baud); // mac osx example
//serial.setup("/dev/ttyUSB0", baud); //linux example
nTimesRead = 0;
nBytesRead = 0;
readTime = 0;
memset(bytesReadString, 0, 4);
///_____end serial_____
//----video-------
// cuba.loadMovie("flower.mp4");
// cuba.play();
//-----------------sound-----------------------
soundStream.listDevices();
int bufferSize = 256;
left.assign(bufferSize, 0.0);
right.assign(bufferSize, 0.0);
volHistory.assign(400, 0.0);
bufferCounter= 0;
drawCounter= 0;
smoothedVol = 0.0;
scaledVol= 0.0;
soundStream.setup(this, 0, 2, 44100, bufferSize, 4);
//-----------------sound-----------------------
//-------------------mesh----------------------------
imageOne.loadImage("flower.png");
//-------------------mesh----------------------------
}
//--------------------------------------------------------------
void testApp::update(){
// Serial _____________________
if (bSendSerialMessage){
// (1) write the letter "a" to serial:
serial.writeByte('a');
// (2) read
// now we try to read 3 bytes
// since we might not get them all the time 3 - but sometimes 0, 6, or something else,
// we will try to read three bytes, as much as we can
// otherwise, we may have a "lag" if we don't read fast enough
// or just read three every time. now, we will be sure to
// read as much as we can in groups of three...
nTimesRead = 0;
nBytesRead = 0;
int nRead = 0; // a temp variable to keep count per read
unsigned char bytesReturned[3];
memset(bytesReadString, 0, 4);
memset(bytesReturned, 0, 3);
while( (nRead = serial.readBytes( bytesReturned, 3)) > 0){
nTimesRead++;
nBytesRead = nRead;
};
memcpy(bytesReadString, bytesReturned, 3);
bSendSerialMessage = false;
readTime = ofGetElapsedTimef();
}
//____video____
cuba.update();
//-----------------sound-----------------------
//lets scale the vol up to a 0-1 range
scaledVol = ofMap(smoothedVol, 0.0, 0.17, 0.0, 700.0, true);
//lets record the volume into an array
volHistory.push_back( scaledVol );
//if we are bigger the the size we want to record - lets drop the oldest value
if( volHistory.size() >= 400 ){
volHistory.erase(volHistory.begin(), volHistory.begin()+1);
}
//-----------------sound-----------------------
}
//--------------------------------------------------------------
void testApp::draw(){
//video____
//cuba.draw(0, 0, ofGetWidth(), ofGetHeight());
//-----------------sound-----------------------
// ofCircle(200, 200, scaledVol * 330.0f);
cout<< scaledVol << endl;
//-----------------sound-----------------------
//-------------------mesh----------------------------
//ofColor centerColor = ofColor(85, 78, 68);
//ofColor edgeColor(0, 0, 0);
//ofBackgroundGradient(centerColor, edgeColor, OF_GRADIENT_CIRCULAR);
//3D view
//left click to drag to rotate
//right click to drag to zoom
easyCam.begin();
ofPushMatrix();
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2);
meshOne = pixelManipulate(imageOne, meshPassOne, 10, scaledVol);
meshOne.draw();
ofPopMatrix();
easyCam.end();
//-------------------mesh----------------------------
}
//--------------------------------------------------------------
void testApp::audioIn(float * input, int bufferSize, int nChannels){
float curVol = 0.0;
// samples are "interleaved"
int numCounted = 0;
//lets go through each sample and calculate the root mean square which is a rough way to calculate volume
for (int i = 0; i < bufferSize; i++){
left[i]= input[i*2]*0.5;
right[i]= input[i*2+1]*0.5;
curVol += left[i] * left[i];
curVol += right[i] * right[i];
numCounted+=2;
}
//this is how we get the mean of rms :)
curVol /= (float)numCounted;
// this is how we get the root of rms :)
curVol = sqrt( curVol );
smoothedVol *= 0.93;
smoothedVol += 0.07 * curVol;
bufferCounter++;
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
if( key == 's' ){
soundStream.start();
}
if( key == 'e' ){
soundStream.stop();
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
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 dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
//-----------------sound-----------------------
void audioIn(float * input, int bufferSize, int nChannels);
vector <float> left;
vector <float> right;
vector <float> volHistory;
int bufferCounter;
int drawCounter;
float smoothedVol;
float scaledVol;
ofSoundStream soundStream;
//-----------------sound-----------------------
//-------------------mesh----------------------------
//create image objects
ofImage imageOne;
//create a mesh to pass to object
ofMesh meshPassOne;
ofMesh meshOne;
//add a camera
ofEasyCam easyCam;
//variables to allow to toggle oribal on & off
ofMesh meshCopy;
bool orbiting;
float startOrbitTime;
//variables to store the polar coordinates of each vertex
vector<float> distances;
vector<float> angles;
ofVec3f meshCentroid;
ofMesh pixelManipulate(ofImage imageA, ofMesh meshA, float intensityThreshold, float sketchDepth);
ofPoint pointOnLine(ofPoint p1, ofPoint p2, float t);
//-------------------mesh----------------------------
//__________video____
ofVideoPlayer cuba;
//__________adding Serial________
bool bSendSerialMessage; // a flag for sending serial
char bytesRead[3]; // data from serial, we will be trying to read 3
char bytesReadString[4]; // a string needs a null terminator, so we need 3 + 1 bytes
int nBytesRead; // how much did we read?
int nTimesRead; // how many times did we read?
float readTime; // when did we last read?
ofSerial serial;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment