Skip to content

Instantly share code, notes, and snippets.

@samsmo
Created March 5, 2012 03:56
Show Gist options
  • Save samsmo/1976469 to your computer and use it in GitHub Desktop.
Save samsmo/1976469 to your computer and use it in GitHub Desktop.
This class gets pixel data that fills arrays that a synth will use to play music for us!
#include "musicCreator.h"
musicCreator::musicCreator(string myPic){
//some bools
startMusic = false;
changePage = false;
//store pic name for the font var later
myPicName = myPic;
//load up the images (bg, button and our actual musician(image)!
myImg.loadImage(ofxiPhoneGetDocumentsDirectory()+myPic);
goBack = new Button(25,11,ofToDataPath("btn_back.png"));
myBG.loadImage(ofToDataPath("blank_bg.jpeg"));
//load our font for the image title
myTitle.loadFont(ofToDataPath("Carton-Slab.otf"), 16);
//resize to pit into the frame
myImg.resize(741,952);
pixels = myImg.getPixels();
//get the height and width so we can loop through columns / rows
h = myImg.height;
w = myImg.width;
sampling = 50;
//Loop through all the rows
for ( int x = 0 ; x < w ; x+=sampling )
{
//Loop through all the columns
for ( int y = 0 ; y < h ; y+=sampling )
{
//Get the R,G,B values from the pixels
//*3 because it's RGB not RGBA
int index = ( y * w + x ) * 3 ;
ofColor color ;
color.r = pixels[index] ; //red pixel
color.g = pixels[index+1] ; //blue pixel
color.b = pixels[index+2] ; //green pixel
pixelData.push_back(Pixels(ofPoint (x, y), color)) ;
}
}
//create an iterator to go through the saved pixels
std::vector<Pixels>::iterator p ;
//some vars that will help use generate some musics
//albeit a little atonal right now
//surely some music theory will help us?
//**see pentatonic scale for future implementation**
int ccount = 0;
double thisTotalAvg = 0;
int thisHalfAvg = 0;
int myColorAvg = 0;
int myPitchCount = 1;
double triggerHappy;
double maxVal=255;
//create the music by reading the pixels
for ( p = pixelData.begin() ; p != pixelData.end() ; p++ )
{
//set our color to an openFrameworks readable color object
ofColor color = p->color ;
//191 seems like a random number but
//this is how many pixels we actually are storing
//so the lead line trigger can only have a max of 191 values
//some simple math here, just trying to get some cooky sounds going
if(ccount < 191){
thisTotalAvg = (color.r+color.g+color.b)/3;
triggerHappy = ((thisTotalAvg) / (maxVal));
if(triggerHappy > 0.50){
leadLineTrigger[ccount] = 1;
}else{
leadLineTrigger[ccount] = 0;
}
}
//code for the dynamic musics
if(ccount < 16){
thisTotalAvg = (color.r+color.g+color.b)/3;
thisHalfAvg = (color.g+color.b)/2;
//dynamic chords
if(ccount < 15 && myPitchCount >= 1){
myColorAvg = ((thisTotalAvg) / ((255) / (60 - 35))) + 35;
leadLinePitch[ccount] = myColorAvg;
}
}
ccount++;
}
startMusic = true;
}
void musicCreator::draw(){
ofSetColor(255,255,255);
myBG.draw(0,0);
goBack->draw();
myImg.draw(13,57);
ofSetColor(255,222,184);
myTitle.drawString(myPicName, 295, 35);
}
bool musicCreator::playMusic(){
return startMusic;
}
//this is where we send our values out to our synth object..
int* musicCreator::returnTrigger(){
return leadLineTrigger;
}
int* musicCreator::returnPitch(){
return leadLinePitch;
}
//button code!
void musicCreator::touchDown(float x, float y){
if(x > goBack->xLoc && x < goBack->xLoc+goBack->bWidth && y> goBack->yLoc && y<goBack->yLoc+goBack->bHeight){
changePage = true;
cout << "touched back"<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment