Hi Donal,
I have pulled together some of my code to form an example. I have no
idea if this code would compile, but it should be pretty close and
should serve as an example to get you going.
android.os.Process.setThreadPriority
(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
int buffersize = AudioRecord.getMinBufferSize(11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioRecord arec = new
AudioRecord(MediaRecorder.AudioSource.MIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
buffersize);
AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
ba.size(),
AudioTrack.MODE_STREAMING);
atrack.setPlaybackRate(11025);
byte[] buffer = new byte[buffersize];
arec.startRecording();
atrack.play();
while(isRecording) {
arec.read(buffer, 0, buffersize);
try {
atrack.write(buffer, 0, buffer.size());
} catch (IOException e) {
e.printStackTrace();
}
}
arec.stop();
atrack.stop();
This sets up an AudioRecord and an AudioTrack, tells the one to record
and the other to play. You then have the while loop which reads the
data out of the record and writes it to the play. This should
reproduce the microphone audio as output for you (the code above will
most likely put it out the speaker, play with the stream in the
AudioTrack constructor for earpiece).
You will need to run this code on a seperate thread and control the
lifetime of the thread using the isRecording boolean.
The first line elevates the thread to a higher priority, this may or
may not be correct, so you should play with it and see.
As for your delay, you may need to increase your buffer sizes to hold
a few seconds worth of data and then just write the data to the
AudioTrack later to create the delay.
Oh and watch for feedback if the sound does come out of the speaker as
it can be exponential if it's loud. You can always call:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
from your activity and just make sure you pass the same stream as the
one you pass to AudioTrack and your volume control will control the
volume of that stream, enabling you to turn it down ;-)
Hope that helps,
Mike
On Jan 21, 3:49 pm, Donal Rafferty <[email protected]> wrote:
> Really sorry to keep annoying you Micheal, head just doesn't seem to be
> functioning properly today.
>
> I currenlty have the AudiRecord buffer set up like this:
>
> static final int bufferSize = 80000;
> final short[] buffer = new short[bufferSize];
> short[] readBuffer = new short[bufferSize];
>
> File file = new File(Environment.getExternalStorageDirectory
> ().getAbsolutePath() + "/record.pcm");
>
> OutputStream os = new FileOutputStream(file);
> BufferedOutputStream bos = new BufferedOutputStream(os);
> DataOutputStream dos = new DataOutputStream(bos);
>
> So that sets up to buffer to the file record.pcm and the AudioRecord.read()
> methos is as follows:
>
> int bufferReadResult = audioRecord.read(readBuffer, 0,bufferSize);
>
> for (int i = 0; i < bufferSize; i++)
> buffer[i] = readBuffer[i];
> for (int i = 0; i < bufferReadResult; i++)
> dos.writeShort(buffer[i]);
>
> So it ends up reading to the file that way, so instead of the OutputStream
> os being set
> to a FileOutputStream do I set it to something else? and then add the
> OutputStream to
> a BufferedOutputStream and then that to DaraOutPutStream?
>
> Or does buffer[i] hold exactly what I need and I just need to get whats in
> buffer[i] to
> the AudioTrack?
>
> Thanks again and sorry to be so much hassle
>
> On Thu, Jan 21, 2010 at 3:19 PM, Michael <[email protected]> wrote:
> > Hi Donal,
>
> > Once you have constructed an AudioRecord instance you can call
> > startRecording and then you can use the various read methods to pull
> > the PCM data out.
> > Likewise once you have constructed an AudioTrack and called play you
> > can use one of the write methods to push the data you got from
> > AudioRecord into the play buffer.
>
> > Mike
>
> > On Jan 21, 3:12 pm, "[email protected]" <[email protected]> wrote:
> > > Hi Micheal,
>
> > > Thanks again,
>
> > > I now have it recording in PCM format from the mic and saving it to
> > > the sd card and then
> > > playing it back through the ear piece.
>
> > > So I just have to look into how to into piping the AudioRecord
> > > straight into the AuditTrack.
>
> > > So with the AudioRecord instead of creating a file to save to what
> > > should I look into doing?
>
> > > And with the AudioTrack, instead of looking for a file to read in what
> > > should I look into doing?
>
> > > Thanks,
> > > Kind Regards,
> > > Donal
>
> > > On Jan 21, 1:10 pm, Michael <[email protected]> wrote:
>
> > > > Hi Donal,
>
> > > > Yeah if you kick the record off and the play, then you can sit in a
> > > > loop reading the data out of Record and putting it into play (on a
> > > > thread other than the UI thread of course).
>
> > > > Not 100% sure about the earpiece, but you specify a stream when you
> > > > create the AudioTrack and I think a stream encpsulates audio routing
> > > > and volume amongst possibly other things.
>
> > > > If you set the stream to STREAM_VOICE_CALL it might come through the
> > > > earpiece as this is where a call would be routed, although I guess
> > > > that depends on headsets and stuff.
>
> > > > Can anyone clarify if I am right?
>
> > > > Mike
>
> > > > On Jan 21, 11:49 am, "[email protected]" <[email protected]> wrote:
>
> > > > > Hi Mike,
>
> > > > > Thanks for the prompt reply.
>
> > > > > I was looking at the AudioRecord class but had somehow missed the
> > > > > AudioTrack class,
>
> > > > > thanks for pointing it out.
>
> > > > > From looking at the classes I think what I have to do is read the
> > > > > recording data and put it straight into a streamed
> > > > > AudioTrack, would I be correct in saying that?
>
> > > > > And then is it possible to send it to the earpiece?
>
> > > > > Thanks again,
>
> > > > > Donal
>
> > > > > On Jan 21, 11:36 am, Michael <[email protected]> wrote:
>
> > > > > > AudioRecord and AudioTrack are the classes you need.
>
> > > > > > If you use them in streaming mode then you can pipe data out of
> > > > > > AudioRecord straight into AudioTrack and you should be able to
> > acheive
> > > > > > the desired effect.
>
> > > > > > I have done something similar, but without the delay.
>
> > > > > > Mike
>
> > > > > > On Jan 21, 11:28 am, "[email protected]" <[email protected]>
> > wrote:
>
> > > > > > > Hi all,
>
> > > > > > > I was wondering is it possible with Android to record from the
> > mic and
> > > > > > > play the sound back out the earpiece with just a 1 second delay?
>
> > > > > > > I have seen the tutorials and can currently record from the mike
> > and
> > > > > > > save a 3gp file to the sd card.
>
> > > > > > > However is it posssible to record and say stream the audio
> > straight to
> > > > > > > the ear piece with just a 1 - 2 second delay?
>
> > > > > > > If so how would I go about doing this?
>
> > > > > > > Would I need to record from the mic and save it to a file and
> > then
> > > > > > > repaly to the ear piece? and continually do this?
>
> > > > > > > Or would it be possible to record from the mic and stream it
> > directly
> > > > > > > to the ear piece with just a slight delay?
>
> > > > > > > Any help, info or directions to tutorials would be greatly
> > > > > > > appreciated,
>
> > > > > > > Thanks in advance
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]<android-developers%[email protected]>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en