We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello!
I wrote this code to make sounds according to the pixel colour value. I also want the pixels to be drawn simultaneously in sync with the sound.
Please help me as I'm pretty new to Processing
Link to the picture used http://theblackliberalboomer.com/wp-content/uploads/2013/01/President-Obama-Official-Pic-2013.jpg
PImage obama;
import ddf.minim.*;
import ddf.minim.ugens.*;
Minim minim;
AudioOutput out;
Oscil wave;
void setup(){
size(612,612);
obama = loadImage("obama.jpg");
minim = new Minim(this);
out = minim.getLineOut();
wave = new Oscil( 440, 0.5f, Waves.SINE );
wave.patch( out );
noLoop();
}
void draw(){
obama.loadPixels();
//image(obama,0,0);
for(int x = 0; x<612; x++){
for(int y=0; y<612; y++){
int index = x+y*612;
color c = obama.pixels[index];
//stroke(c);
float amp = map( c, 0, 255, 1, 0 );
wave.setAmplitude( amp );
float freq = map( c, 0, 255, 110, 880 );
wave.setFrequency( freq );
stroke(c);
point(x,y);
//delay(500);
}
}
}
Answers
Cool stuff, I think your problem has this simple solution.
Move the looping variables outside of draw,
loop through a portion of them every frame (speed),
and remove noLoop.
Hope this helps
Hi prince, Thanks a lot! This works perfect. Even though I couldn't get the whole idea/logic completely in the start of the draw(). Sorry to bother. I'm new to processing and have been trusting on this one 'for' loop for all my codes :)
Abey
Also, if add strokeWeight(3); towards the end it doesn't work. or instead a small rectangle instead of the point(). Any idea?
Two suggestions.
I would keep the amplitude fix in order to observe solely the effect of the frequency based on color. Otherwise, lower frequencies will not be played loud ever.
@prince_polka Related to mapping color to amplitude (or frequency), one needs to remember color ranges from 0x0 to 0xfffff in RGB mode (there is also the alpha channel). The mapping in the above example is limited to the range of 0 to 255 (aka 0x0 to 0xff). You could change it by setting the frequency based on a single primary color (either red,blue,green as in
red(obama.pixels[index])
or you do the mapping across the full range or you do the mapping using hue() function and the HSB mode (check colorMode in the reference).Kf
@ krajfer, color is a bit tricky,
Transparent colors goes from 0x0 to 0xffffff,
If it's opaque then black is #ff000000 aka 0xFF<<24 aka -16777216
And white is #ffffffff
Only 6digits is allowed with the hashtag, but you get my point
Unsure whether it has any significance in this color to sound case, but it's something to keep in mind.
https://processing.org/reference/colorMode_.html
[https://processing.org/reference/map_.html(https://processing.org/reference/map_.html)
This hurts my brain...
Sounds like if the sound was generated by a quantum computer. Neat! @xxMrPHDxx
Kf
Thanks a lot guys! Since I'm new to processing, I'm gonna take some days to understand the whole code. But never mind, I love these!
Yeah. It depends on the image you are using :D @kfrajer
No problem @abeythomas