I was wondering if anyone can help me figure this problem out? I'm a Product Designer and know little about this type of stuff and need a quick fix for an exhibition next week!
I'm using a infra-red proximity sensor (sharp GP2Y0A21YK), but the volume control itself can be a bit 'jumpy'. I've tried changing the song file to a WAV file and it does improve slightly. Any thoughts or advice is very much welcome! Here is the code below, thank you people!
Tom
/**
* Load File
* by Damien Di Fede.
*
* This sketch demonstrates how to use the <code>loadFile</code> method
* of <code>Minim</code>. The <code>loadFile</code> method allows you to
* specify the file you want to load with a <code>String</code> and optionally
* specify what you want the buffer size of the returned <code>AudioPlayer</code>
* to be. If you don't specify a buffer size, the returned player will have a
* buffer size of 1024. Minim is able to play wav files, au files, aif files,
* snd files, and mp3 files. When you call <code>loadFile</code>, if you just
* specify the filename it will try to load the file from the data folder of
* your sketch. However, you can also specify an absolute path
* (such as "C:\foo\bar\thing.wav") and the file will be loaded from that
* location (keep in mind that won't work from an applet). You can also specify
* a URL (such as "http://www.mysite.com/mp3/song.mp3") but keep in mind that
* if you run the sketch as an applet you may run in to security restrictions
* if the applet is not on the same domain as the file you want to load. You can
* get around the restriction by signing the applet. Before you exit your sketch
* make sure you call the <code>close</code> method of any <code>AudioPlayer</code>'s
* you have received from <code>loadFile</code>, followed by the <code>stop</code>
* method of <code>Minim</code>.
*/
import processing.serial.*;
import ddf.minim.*;
Serial myPort;
AudioPlayer player;
Minim minim;
int volume = 0;
int val; // Data received from the serial port
void setup()
{
size(512, 200, P2D);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
minim = new Minim(this);
// load a file, give the AudioPlayer buffers that are 1024 samples long
// player = minim.loadFile("groove.mp3");
// load a file, give the AudioPlayer buffers that are 2048 samples long
player = minim.loadFile("groove.wav", 2048);
// play the file
player.play();
player.loop();
println(player.hasControl(Controller.GAIN));
}
void draw()
{
background(0);
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
println(val);
}
val = constrain(val, 1 , 40);
float volume = map(val, 1, 40, -20, 20);
//println(volume);
//println("");
player.setGain(volume);
delay(100);
}
void stop()
{
// always close Minim audio classes when you are done with them