Showing posts with label Product: Android. Show all posts

Raw Audio Manipulation in Android

Tuesday, September 1, 2009 | 2:13 AM

Labels: ,

Version 1.5 of the Android SDK introduced a bunch of cool new features for developers. Though not as glamorous as some APIs, the new audio manipulation classes - AudioTrack and AudioRecord - offer powerful functionality to developers looking to manipulate raw audio.

These classes let you record audio directly from the audio input hardware of the device, and stream PCM audio buffers to the audio hardware for playback. Strong sauce indeed for those of you looking to have more control over audio input and playback.

Enough talk - on to the code. To test out these new APIs I put together a simple Android app that listens to 10 seconds of input from the microphone and then plays it back through the speaker in reverse. Perfect for decoding secret messages in old Beatles albums.

Start with the recording code. It's designed to record the incoming audio to a file on the SD Card that we'll read and playback later. As per the latest security patch, your application requires a uses-permission to record audio.

<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

The recording code here records a new set of 16bit mono audio at 11025Hz to reverseme.pcm on the SD card.

public void record() {
int frequency = 11025;
int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");

// Delete any previous recording.
if (file.exists())
file.delete();


// Create the new file.
try {
file.createNewFile();
} catch (IOException e) {
throw new IllegalStateException("Failed to create " + file.toString());
}

try {
// Create a DataOuputStream to write the audio data into the saved file.
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);

// Create a new AudioRecord object to record the audio.
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration,
audioEncoding, bufferSize);

short[] buffer = new short[bufferSize];
audioRecord.startRecording();


while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++)
dos.writeShort(buffer[i]);
}


audioRecord.stop();
dos.close();

} catch (Throwable t) {
Log.e("AudioRecord","Recording Failed");
}
}

Next we create a playback method that reads the file and plays back the contents in reverse. It's important to set the audio data encoding (here PCM 16 bits), channel, and frequency values to the same settings used in the AudioRecord object. Then again, playing with the playback frequency might be have its own appeal.


public void play() {
// Get the file we want to playback.
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");
// Get the length of the audio stored in the file (16 bit so 2 bytes per short)
// and create a short array to store the recorded audio.
int musicLength = (int)(file.length()/2);
short[] music = new short[musicLength];


try {
// Create a DataInputStream to read the audio data back from the saved file.
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);

// Read the file into the music array.
int i = 0;
while (dis.available() > 0) {
music[musicLength-1-i] = dis.readShort();
i++;
}


// Close the input streams.
dis.close();


// Create a new AudioTrack object using the same parameters as the AudioRecord
// object used to create the file.
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
musicLength,
AudioTrack.MODE_STREAM);
// Start playback
audioTrack.play();

// Write the music buffer to the AudioTrack object
audioTrack.write(music, 0, musicLength);


} catch (Throwable t) {
Log.e("AudioTrack","Playback Failed");
}
}

Finally, to drive this you need to update your application Activity to call the record and playback methods as appropriate. To keep this example as simple as possible I'm going to record for 10 seconds as soon as the application starts, and playback in reverse as soon as I've finished taking the sample.

To be more useful you'd almost certainly want to perform the playback operation in a Service and on a background thread.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Thread thread = new Thread(new Runnable() {
public void run() {
record();
}
});
thread.start();


try {
wait(10000);
} catch (InterruptedException e) {}

isRecording = false;

try {
thread.join();
} catch (InterruptedException e) {}

play();
finish();
}

The AudioTrack and AudioRecord classes offer a lot more functionality than I've demonstrated here. Using the AudioTrack streaming mode you can do processing of incoming audio and playback in near real time, letting you manipulate incoming or outgoing audio and perform signal processing on raw audio on the device.

Tell us how you've used these APIs in your Android apps in the comments!

Posted by: Reto Meier, EMEA Android Developer Advocate, Google UK

Calling EMEA Developers for ADC 2

Wednesday, July 22, 2009 | 8:16 AM

Labels: ,

It's been a couple of months since the announcement of Android Developer Challenge 2 during this year's Google I/O event. San Francisco is a long way from us here in EMEA, so we're taking this opportunity to highlight ADC 2 to our local developer community.

EMEA developers were particularly well represented in ADC 1, matching the Eurovision Song Contest with entries from 42 countries across Europe, the Middle East, and Africa. EMEA contestants submitted over a third of the total entries and did us proud by comprising over a third of the 50 finalists - outnumbering even the US developers!

Since then, in EMEA alone, Android phones have been launched in 15 countries on 3 different handsets from more than 25 operators. With more handsets, countries, and operators on the way, both here and around the world, the EMEA team want to see local developers doing even better in ADC 2. With €176,000 or £150,000 (based on the USD$250,000 prize) on offer for the overall winner, we hope you see this as an excellent opportunity to get involved in Android development.

This year ADC entries will be judged by regular phone-owners all over around the world, including people in Europe, the Middle East, and Africa where Android handsets are available. So take advantage of your local knowledge and localize your apps to make them as compelling as possible, and maximise your votes, from people all over EMEA and the rest of the world.

There are more details on the ADC 2 page along with the Terms and Conditions. Entries need to be submitted by August 31, that's less than 6 weeks away, so stop reading and get to it!

Good luck and happy coding!


Posted by: Reto Meier, EMEA Android Developer Advocate, Google UK