We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSound,  Music Libraries › Playing mic input through speakers
Page Index Toggle Pages: 1
Playing mic input through speakers (Read 5895 times)
Playing mic input through speakers
Jan 20th, 2008, 8:33pm
 
I was wondering if anyone knew of a way through Processing to play audio though the speakers as it comes in through the mic.  Basically, whatever comes in the mic is played back realtime.
Re: Playing mic input through speakers
Reply #1 - Jan 21st, 2008, 4:38pm
 
Check out Minim!

Here's some code that does what you want:

Code:

class playBack implements AudioSignal, AudioListener{ //Just a simple "re-route" audio class.
float[] left, right;
//Getting.
public void samples(float[] arg0) {
left = arg0;
}

public void samples(float[] arg0, float[] arg1) {
left = arg0;
right = arg1;
}
//Sending back.
public void generate(float[] arg0) {
System.arraycopy(left, 0, arg0, 0, arg0.length);
}

public void generate(float[] arg0, float[] arg1) {
System.out.println(arg0[0]);
if (left!=null && right!=null){
System.arraycopy(left, 0, arg0, 0, arg0.length);
System.arraycopy(right, 0, arg1, 0, arg1.length);
}
}
}

//Here's the runner code:

Minim.start(this);
AudioOutput ap = Minim.getLineOut();
AudioInput ai = Minim.getLineIn();
playBack pb = new playBack();
ai.addListener(pb); //Samples from mic go to pb
ap.addSignal(pb); //ap will playback pb constantly.
Re: Playing mic input through speakers
Reply #2 - Jan 21st, 2008, 5:32pm
 
Thanks!
Re: Playing mic input through speakers
Reply #3 - Jan 27th, 2008, 12:25pm
 
It sounds funny, because i have a short delay and some kind of  "Computer Voice" how to fix that?
Re: Playing mic input through speakers
Reply #4 - Jan 27th, 2008, 6:01pm
 
Well, check for a few things:

When sound comes through a mic, most OS default play it to speakers: check your volume levels, and look for a "Microphone" sound level, adjusting this will let you do what the program above does ... without having to Tongue

Or, check that your selected "Input source" is actually the mike: if you've selected stereo out, than you'll be writing out to a stream that you're reading: which results in lots and lots of feedback. (but that doesn't sound like what you described)

hmmm can you give us more symptoms?
Re: Playing mic input through speakers
Reply #5 - May 29th, 2009, 10:28am
 
hi, i recently used your code in my program but i get some disturbance i guess from going from array to array. I get some peak noises like the sound you hear when going from normal sound to zero and then to normal again in consecutive samples.

I know this post is from a year and a half ago but i decided to take a shot  Smiley
Re: Playing mic input through speakers
Reply #6 - Oct 14th, 2009, 10:26am
 
I had some trouble running the above code, so I looked at the Waverender code on the minim site
http://code.compartmental.net/minim/examples/Recordable/addListener/waveform.pde

and pieced that code together with the code in the post.

My main goal was to get my line-in on my Mac (I plugged in a microphone) to play through my Mac speakers using minim.  Thus, this code connectes AudioInput to AudioOutput in Minim.

Code:

import ddf.minim.*;

Minim minim;
AudioInput in;
AudioOutput out;
WaveformRenderer waveform;

void setup()
{
 size(512, 200, P3D);

 minim = new Minim(this);
 minim.debugOn();

 // get a line in from Minim, default bit depth is 16
 in = minim.getLineIn(Minim.STEREO, 512);
 out = minim.getLineOut(Minim.STEREO, 512);

 waveform = new WaveformRenderer();
 in.addListener(waveform);


 // adds the signal to the output
 out.addSignal(waveform);

}

void draw()
{
 background(0);
 stroke(255);
 // draw the waveforms
 for(int i = 0; i < in.bufferSize() - 1; i++)
 {
   line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
   line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
 }
}


void stop()
{
 // always close Minim audio classes when you are done with them
 in.close();
 out.close();

 minim.stop();

 super.stop();
}


class WaveformRenderer implements AudioListener, AudioSignal
{

 private float[] left;
 private float[] right;

 WaveformRenderer()
 {
   //    left = null;
   //    right = null;
 }

 synchronized void samples(float[] samp)
 {
   left = samp;
 }

 synchronized void samples(float[] sampL, float[] sampR)
 {
   left = sampL;
   right = sampR;
 }


 void generate(float[] samp)
 {
   System.arraycopy(right, 0, samp, 0, samp.length);

 }

 // this is a stricly mono signal
 void generate(float[] sampL, float[] sampR)
 {
   if (left!=null && right!=null){
     System.arraycopy(left, 0, sampL, 0, sampL.length);
     System.arraycopy(right, 0, sampR, 0, sampR.length);
   }
 }


}
Re: Playing mic input through speakers
Reply #7 - Nov 17th, 2009, 9:44am
 
is this really the only way to route audio through minim (for filtering, beat detecting, in my case)? i think the delay is too big, if i change buffer size the sound starts to crackle  Sad ...
Page Index Toggle Pages: 1