Raw Audio Waveforms from AMR-NR Data? Java?

All your problems with Audio, Video and Images.

Distortion playing back recorded file

Postby tanmay » Tue Jan 19, 2010 4:55 am

Hey,
Thanks for providing the code and help with understanding AudioRecord. I tried the above code on a G1 running 1.6, it seems to be creating the file and also recording, but for some reason, when i download the file and try to test it with bplay ( a simple raw file player on linux) with the same Sampling rate and bits, I hear 90% noise and my recorded audio on top of that, which is hardly audible coz of the Loud noise.
It feels like something is overdriving the mic.

I even checked that the mic is not faulty by recording a file with Hertz wav recorder i got from the market, which works perfectly. Its just as soon as I try to deal with audioRecord and AudioTrack classes, the quality goes down.

Wondering if anyone had problems like this and would be able to tell me a fix . Its been days trying to fix this now, I have tried code obtained from diffrrent sources, but hasnt worked and has mainly given the same problem.

Would also appreciate if someone coud point me to a combined recorded/ player using AudioRecord and Track Libraries.

Thanks

Tanmay

jbs wrote:Okay. It works now. It turns out that the audio device stopped responding after the Music application, for lack of a more precise description, wonked-out; a reboot restored the audio device to a functional state.

After playing with the the code I have,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.io.BufferedOutputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import android.media.AudioFormat;
  9. import android.media.AudioRecord;
  10. import android.media.MediaRecorder;
  11.  
  12.  
  13. public class Recorder implements Runnable {
  14.         private int frequency;
  15.         private int channelConfiguration;
  16.         private volatile boolean isPaused;
  17.         private File fileName;
  18.         private volatile boolean isRecording;
  19.         private final Object mutex = new Object();
  20.  
  21.         // Changing the sample resolution changes sample type. byte vs. short.
  22.         private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
  23.  
  24.         /**
  25.          *
  26.          */
  27.         public Recorder() {
  28.                 super();
  29.                 this.setFrequency(11025);
  30.                 this.setChannelConfiguration(AudioFormat.CHANNEL_CONFIGURATION_MONO);
  31.                 this.setPaused(false);
  32.         }
  33.  
  34.         public void run() {
  35.                 // Wait until we're recording...
  36.                 synchronized (mutex) {
  37.                         while (!this.isRecording) {
  38.                                 try {
  39.                                         mutex.wait();
  40.                                 } catch (InterruptedException e) {
  41.                                         throw new IllegalStateException("Wait() interrupted!", e);
  42.                                 }
  43.                         }
  44.                 }
  45.  
  46.                 // Open output stream...
  47.                 if (this.fileName == null) {
  48.                         throw new IllegalStateException("fileName is null");
  49.                 }
  50.                 BufferedOutputStream bufferedStreamInstance = null;
  51.                 if (fileName.exists()) {
  52.                         fileName.delete();
  53.                 }
  54.                 try {
  55.                         fileName.createNewFile();
  56.                 } catch (IOException e) {
  57.                         throw new IllegalStateException("Cannot create file: " + fileName.toString());
  58.                 }
  59.                 try {
  60.                         bufferedStreamInstance = new BufferedOutputStream(
  61.                                         new FileOutputStream(this.fileName));
  62.                 } catch (FileNotFoundException e) {
  63.                         throw new IllegalStateException("Cannot Open File", e);
  64.                 }
  65.                 DataOutputStream dataOutputStreamInstance =
  66.                         new DataOutputStream(bufferedStreamInstance);
  67.                
  68.                 // We're important...
  69.                 android.os.Process
  70.                                 .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
  71.  
  72.                 // Allocate Recorder and Start Recording...
  73.                 int bufferRead = 0;
  74.                 int bufferSize = AudioRecord.getMinBufferSize(this.getFrequency(),
  75.                                 this.getChannelConfiguration(), this.getAudioEncoding());
  76.                 AudioRecord recordInstance = new AudioRecord(
  77.                                 MediaRecorder.AudioSource.MIC, this.getFrequency(), this
  78.                                                 .getChannelConfiguration(), this.getAudioEncoding(),
  79.                                 bufferSize);
  80.                 short[] tempBuffer = new short[bufferSize];
  81.                 recordInstance.startRecording();
  82.                 while (this.isRecording) {
  83.                         // Are we paused?
  84.                         synchronized (mutex) {
  85.                                 if (this.isPaused) {
  86.                                         try {
  87.                                                 mutex.wait(250);
  88.                                         } catch (InterruptedException e) {
  89.                                                 throw new IllegalStateException("Wait() interrupted!",
  90.                                                                 e);
  91.                                         }
  92.                                         continue;
  93.                                 }
  94.                         }
  95.                        
  96.                         bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
  97.                         if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
  98.                                 throw new IllegalStateException(
  99.                                                 "read() returned AudioRecord.ERROR_INVALID_OPERATION");
  100.                         } else if (bufferRead == AudioRecord.ERROR_BAD_VALUE) {
  101.                                 throw new IllegalStateException(
  102.                                                 "read() returned AudioRecord.ERROR_BAD_VALUE");
  103.                         } else if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
  104.                                 throw new IllegalStateException(
  105.                                                 "read() returned AudioRecord.ERROR_INVALID_OPERATION");
  106.                         }
  107.                         try {
  108.                                 for (int idxBuffer = 0; idxBuffer < bufferRead; ++idxBuffer) {
  109.                                         dataOutputStreamInstance.writeShort(tempBuffer[idxBuffer]);
  110.                                 }
  111.                         } catch (IOException e) {
  112.                                 throw new IllegalStateException(
  113.                                         "dataOutputStreamInstance.writeShort(curVal)");
  114.                         }
  115.                        
  116.                 }
  117.  
  118.                 // Close resources...
  119.                 recordInstance.stop();
  120.                 try {
  121.                         bufferedStreamInstance.close();
  122.                 } catch (IOException e) {
  123.                         throw new IllegalStateException("Cannot close buffered writer.");
  124.                 }
  125.         }
  126.  
  127.         public void setFileName(File fileName) {
  128.                 this.fileName = fileName;
  129.         }
  130.  
  131.         public File getFileName() {
  132.                 return fileName;
  133.         }
  134.  
  135.         /**
  136.          * @param isRecording
  137.          *            the isRecording to set
  138.          */
  139.         public void setRecording(boolean isRecording) {
  140.                 synchronized (mutex) {
  141.                         this.isRecording = isRecording;
  142.                         if (this.isRecording) {
  143.                                 mutex.notify();
  144.                         }
  145.                 }
  146.         }
  147.  
  148.         /**
  149.          * @return the isRecording
  150.          */
  151.         public boolean isRecording() {
  152.                 synchronized (mutex) {
  153.                         return isRecording;
  154.                 }
  155.         }
  156.  
  157.         /**
  158.          * @param frequency
  159.          *            the frequency to set
  160.          */
  161.         public void setFrequency(int frequency) {
  162.                 this.frequency = frequency;
  163.         }
  164.  
  165.         /**
  166.          * @return the frequency
  167.          */
  168.         public int getFrequency() {
  169.                 return frequency;
  170.         }
  171.  
  172.         /**
  173.          * @param channelConfiguration
  174.          *            the channelConfiguration to set
  175.          */
  176.         public void setChannelConfiguration(int channelConfiguration) {
  177.                 this.channelConfiguration = channelConfiguration;
  178.         }
  179.  
  180.         /**
  181.          * @return the channelConfiguration
  182.          */
  183.         public int getChannelConfiguration() {
  184.                 return channelConfiguration;
  185.         }
  186.  
  187.         /**
  188.          * @return the audioEncoding
  189.          */
  190.         public int getAudioEncoding() {
  191.                 return audioEncoding;
  192.         }
  193.  
  194.         /**
  195.          * @param isPaused
  196.          *            the isPaused to set
  197.          */
  198.         public void setPaused(boolean isPaused) {
  199.                 synchronized (mutex) {
  200.                         this.isPaused = isPaused;
  201.                 }
  202.         }
  203.  
  204.         /**
  205.          * @return the isPaused
  206.          */
  207.         public boolean isPaused() {
  208.                 synchronized (mutex) {
  209.                         return isPaused;
  210.                 }
  211.         }
  212.  
  213. }
  214.  
Parsed in 0.052 seconds, using GeSHi 1.0.8.4


And, I drive that code with:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class AudioRecorderPCM extends Activity {
  2.         /** Called when the activity is first created. */
  3.         @Override
  4.         public void onCreate(Bundle savedInstanceState) {
  5.                 super.onCreate(savedInstanceState);
  6.                 setContentView(R.layout.main);
  7.                
  8.                 // Record 20 seconds of audio.
  9.                 Recorder recorderInstance = new Recorder();
  10.                 Thread th = new Thread(recorderInstance);
  11.                 recorderInstance.setFileName(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.raw"));
  12.                 th.start();
  13.                 recorderInstance.setRecording(true);
  14.                 synchronized (this) {
  15.                 try {
  16.                         this.wait(20000);
  17.                 } catch (InterruptedException e) {
  18.                         e.printStackTrace();
  19.                 }
  20.                
  21.                 }
  22.                 recorderInstance.setRecording(false);
  23.                 try {
  24.                         th.join();
  25.                 } catch (InterruptedException e) {
  26.                         e.printStackTrace();
  27.                 }
  28.                
  29.                
  30.         }
  31. }
  32.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


This writes a file 'test.raw' to the SDCard. Each sample is a signed, big endian, 16-bit integer. I was able to play it in matlab with wavplay.

Hope this helps anyone else who is trying to get this part of the API to work. This code works on my G1, but is seems as if there is no way to tell what the sampling rate and sample resolution on a given device are. It doesn't seem to throw any errors if you pick a bad combination of parameters; it just fails.
tanmay
Freshman
Freshman
 
Posts: 3
Joined: Tue Jan 19, 2010 4:40 am

Top

Postby maxsap » Wed Feb 10, 2010 2:05 am

I Have tried to playback the file using matlab's wavplay but with no success i get
??? Error using ==> load
Number of columns on line 1 of ASCII file
C:\Users\maxsap\Documents\MATLAB\reverseme.pcm
must be the same as previous lines.
everytime i try to load the file.
also I am trying to playback the file using

[syntax="java"]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);
ObjectInputStream ob = new ObjectInputStream(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_VOICE_CALL,
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");
}
}
}

but no luck there too, I am hearing a strange sound as if it is playing the sound in reverse.
I am using a htc magic with android 1.5.
anyhelp would be wellcome,regards maxsap
maxsap
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 10, 2010 1:53 am

Postby maxsap » Thu Feb 11, 2010 9:09 pm

Ok fixed my bad, here is the code so others can slve it too :

private 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[i] = dis.readShort();//the fault was here.
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");
}
}
}

there are some buffer overflows but those can easily be fixed using bigger buffe size.
maxsap
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 10, 2010 1:53 am

Postby xdebugx » Fri Apr 02, 2010 4:16 am

I'm trying to analyze the audio recorded in realtime, but all im getting is garbage when i look at it. I believe the problem is when i try to convert the shorts that are read from the buffer to ints for displaying the frequency. I'm not sure how to do this or wether its an endianess problem or what. Please help, how to utilize the short data recived from the audioRecord.read(). Thanks
xdebugx
Junior Developer
Junior Developer
 
Posts: 24
Joined: Fri Apr 02, 2010 4:06 am

Postby xdebugx » Mon Apr 05, 2010 2:59 am

I found out what the problem was for getting the frequency from the data recieved by the AudioRecord.read() method. The data is PCM encoded. To decode there are various methods such as counting zero crossings, FFT, and correlation matching. For the non-precise frequency data I needed, I used zero crossing. My sample code is below, doesn't give an exact match of the tone that I play in the microphone so something may not be quite right in the code, or it could be ambient noise. Please let me know if you see an obvious mistake. You can research other PCM decoding methods if you need high quality frequency decoding.

recorder.read(audioData,0,tempBuffSize);
int last=audioData[0];
int numCrossing=0;
for (int p=1;p<tempBuffSize;p++) {
if (last>0) {
if (audioData[p]<0) {
numCrossing++;
last=audioData[p];
}
} else
if (audioData[p]>0) {
numCrossing++;
last=audioData[p];
}
}
frequency=numCrossing*(sampleRate/tempBuffSize);
xdebugx
Junior Developer
Junior Developer
 
Posts: 24
Joined: Fri Apr 02, 2010 4:06 am

Streams from Socket insread of file

Postby kchaits123 » Sat May 01, 2010 9:07 am

Hi,

Is it possible to use the dataOutputStream of a socket instead of that of a file, to write the audio-data to the socket directly?

I am trying this, but this is miserably failing.

Could you please let me know what could be going wrong?
kchaits123
Freshman
Freshman
 
Posts: 2
Joined: Sat May 01, 2010 9:03 am

Top

Re: Raw Audio Waveforms from AMR-NR Data? Java?

Postby Roardog » Fri Jun 17, 2011 2:25 pm

Sorry to dredge up an old thread...

What if I want to record for an unknown amount of time? Specifically what if, like any any recorder app, I don't want the recording to stop until the user presses the stop button?

In your sample code you put the main thread to sleep for 20 seconds while you record, I assume this it to give clock cycles to recorderInstance so it can execute, but what would I do if I want to keep checking for a user interaction?
Roardog
Freshman
Freshman
 
Posts: 5
Joined: Fri Jun 17, 2011 1:21 am

Re: Raw Audio Waveforms from AMR-NR Data? Java?

Postby thenewday007 » Wed Oct 05, 2011 9:10 am

It's my first time to come here.Haha,Nice to meet everybody.I learn from my friends that this forum is very interesting.

buy aion kinah | buy wow gold eu | wow gold for sale
thenewday007
Freshman
Freshman
 
Posts: 5
Joined: Wed Oct 05, 2011 8:42 am

Re:

Postby 8200 » Fri Nov 04, 2011 10:55 am

Hi,

thank you for you code example, jbs. I have tested the code on my Android 2.2 mobile and it really works :-).
But the resulting raw-audio-file is very noisy. I openend it with Matlab.

Do I have to filter the audio-data for getting a useful audio-quality?


With android sound recorder I get good audio-quality records so it should also be possible with the AudioRecord.


Thank you for your help!

Regards,
Arthur

jbs wrote:Okay. It works now. It turns out that the audio device stopped responding after the Music application, for lack of a more precise description, wonked-out; a reboot restored the audio device to a functional state.

After playing with the the code I have,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.io.BufferedOutputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import android.media.AudioFormat;
  9. import android.media.AudioRecord;
  10. import android.media.MediaRecorder;
  11.  
  12.  
  13. public class Recorder implements Runnable {
  14.         private int frequency;
  15.         private int channelConfiguration;
  16.         private volatile boolean isPaused;
  17.         private File fileName;
  18.         private volatile boolean isRecording;
  19.         private final Object mutex = new Object();
  20.  
  21.         // Changing the sample resolution changes sample type. byte vs. short.
  22.         private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
  23.  
  24.         /**
  25.          *
  26.          */
  27.         public Recorder() {
  28.                 super();
  29.                 this.setFrequency(11025);
  30.                 this.setChannelConfiguration(AudioFormat.CHANNEL_CONFIGURATION_MONO);
  31.                 this.setPaused(false);
  32.         }
  33.  
  34.         public void run() {
  35.                 // Wait until we're recording...
  36.                 synchronized (mutex) {
  37.                         while (!this.isRecording) {
  38.                                 try {
  39.                                         mutex.wait();
  40.                                 } catch (InterruptedException e) {
  41.                                         throw new IllegalStateException("Wait() interrupted!", e);
  42.                                 }
  43.                         }
  44.                 }
  45.  
  46.                 // Open output stream...
  47.                 if (this.fileName == null) {
  48.                         throw new IllegalStateException("fileName is null");
  49.                 }
  50.                 BufferedOutputStream bufferedStreamInstance = null;
  51.                 if (fileName.exists()) {
  52.                         fileName.delete();
  53.                 }
  54.                 try {
  55.                         fileName.createNewFile();
  56.                 } catch (IOException e) {
  57.                         throw new IllegalStateException("Cannot create file: " + fileName.toString());
  58.                 }
  59.                 try {
  60.                         bufferedStreamInstance = new BufferedOutputStream(
  61.                                         new FileOutputStream(this.fileName));
  62.                 } catch (FileNotFoundException e) {
  63.                         throw new IllegalStateException("Cannot Open File", e);
  64.                 }
  65.                 DataOutputStream dataOutputStreamInstance =
  66.                         new DataOutputStream(bufferedStreamInstance);
  67.                
  68.                 // We're important...
  69.                 android.os.Process
  70.                                 .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
  71.  
  72.                 // Allocate Recorder and Start Recording...
  73.                 int bufferRead = 0;
  74.                 int bufferSize = AudioRecord.getMinBufferSize(this.getFrequency(),
  75.                                 this.getChannelConfiguration(), this.getAudioEncoding());
  76.                 AudioRecord recordInstance = new AudioRecord(
  77.                                 MediaRecorder.AudioSource.MIC, this.getFrequency(), this
  78.                                                 .getChannelConfiguration(), this.getAudioEncoding(),
  79.                                 bufferSize);
  80.                 short[] tempBuffer = new short[bufferSize];
  81.                 recordInstance.startRecording();
  82.                 while (this.isRecording) {
  83.                         // Are we paused?
  84.                         synchronized (mutex) {
  85.                                 if (this.isPaused) {
  86.                                         try {
  87.                                                 mutex.wait(250);
  88.                                         } catch (InterruptedException e) {
  89.                                                 throw new IllegalStateException("Wait() interrupted!",
  90.                                                                 e);
  91.                                         }
  92.                                         continue;
  93.                                 }
  94.                         }
  95.                        
  96.                         bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
  97.                         if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
  98.                                 throw new IllegalStateException(
  99.                                                 "read() returned AudioRecord.ERROR_INVALID_OPERATION");
  100.                         } else if (bufferRead == AudioRecord.ERROR_BAD_VALUE) {
  101.                                 throw new IllegalStateException(
  102.                                                 "read() returned AudioRecord.ERROR_BAD_VALUE");
  103.                         } else if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
  104.                                 throw new IllegalStateException(
  105.                                                 "read() returned AudioRecord.ERROR_INVALID_OPERATION");
  106.                         }
  107.                         try {
  108.                                 for (int idxBuffer = 0; idxBuffer < bufferRead; ++idxBuffer) {
  109.                                         dataOutputStreamInstance.writeShort(tempBuffer[idxBuffer]);
  110.                                 }
  111.                         } catch (IOException e) {
  112.                                 throw new IllegalStateException(
  113.                                         "dataOutputStreamInstance.writeShort(curVal)");
  114.                         }
  115.                        
  116.                 }
  117.  
  118.                 // Close resources...
  119.                 recordInstance.stop();
  120.                 try {
  121.                         bufferedStreamInstance.close();
  122.                 } catch (IOException e) {
  123.                         throw new IllegalStateException("Cannot close buffered writer.");
  124.                 }
  125.         }
  126.  
  127.         public void setFileName(File fileName) {
  128.                 this.fileName = fileName;
  129.         }
  130.  
  131.         public File getFileName() {
  132.                 return fileName;
  133.         }
  134.  
  135.         /**
  136.          * @param isRecording
  137.          *            the isRecording to set
  138.          */
  139.         public void setRecording(boolean isRecording) {
  140.                 synchronized (mutex) {
  141.                         this.isRecording = isRecording;
  142.                         if (this.isRecording) {
  143.                                 mutex.notify();
  144.                         }
  145.                 }
  146.         }
  147.  
  148.         /**
  149.          * @return the isRecording
  150.          */
  151.         public boolean isRecording() {
  152.                 synchronized (mutex) {
  153.                         return isRecording;
  154.                 }
  155.         }
  156.  
  157.         /**
  158.          * @param frequency
  159.          *            the frequency to set
  160.          */
  161.         public void setFrequency(int frequency) {
  162.                 this.frequency = frequency;
  163.         }
  164.  
  165.         /**
  166.          * @return the frequency
  167.          */
  168.         public int getFrequency() {
  169.                 return frequency;
  170.         }
  171.  
  172.         /**
  173.          * @param channelConfiguration
  174.          *            the channelConfiguration to set
  175.          */
  176.         public void setChannelConfiguration(int channelConfiguration) {
  177.                 this.channelConfiguration = channelConfiguration;
  178.         }
  179.  
  180.         /**
  181.          * @return the channelConfiguration
  182.          */
  183.         public int getChannelConfiguration() {
  184.                 return channelConfiguration;
  185.         }
  186.  
  187.         /**
  188.          * @return the audioEncoding
  189.          */
  190.         public int getAudioEncoding() {
  191.                 return audioEncoding;
  192.         }
  193.  
  194.         /**
  195.          * @param isPaused
  196.          *            the isPaused to set
  197.          */
  198.         public void setPaused(boolean isPaused) {
  199.                 synchronized (mutex) {
  200.                         this.isPaused = isPaused;
  201.                 }
  202.         }
  203.  
  204.         /**
  205.          * @return the isPaused
  206.          */
  207.         public boolean isPaused() {
  208.                 synchronized (mutex) {
  209.                         return isPaused;
  210.                 }
  211.         }
  212.  
  213. }
  214.  
Parsed in 0.061 seconds, using GeSHi 1.0.8.4


And, I drive that code with:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class AudioRecorderPCM extends Activity {
  2.         /** Called when the activity is first created. */
  3.         @Override
  4.         public void onCreate(Bundle savedInstanceState) {
  5.                 super.onCreate(savedInstanceState);
  6.                 setContentView(R.layout.main);
  7.                
  8.                 // Record 20 seconds of audio.
  9.                 Recorder recorderInstance = new Recorder();
  10.                 Thread th = new Thread(recorderInstance);
  11.                 recorderInstance.setFileName(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.raw"));
  12.                 th.start();
  13.                 recorderInstance.setRecording(true);
  14.                 synchronized (this) {
  15.                 try {
  16.                         this.wait(20000);
  17.                 } catch (InterruptedException e) {
  18.                         e.printStackTrace();
  19.                 }
  20.                
  21.                 }
  22.                 recorderInstance.setRecording(false);
  23.                 try {
  24.                         th.join();
  25.                 } catch (InterruptedException e) {
  26.                         e.printStackTrace();
  27.                 }
  28.                
  29.                
  30.         }
  31. }
  32.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


This writes a file 'test.raw' to the SDCard. Each sample is a signed, big endian, 16-bit integer. I was able to play it in matlab with wavplay.

Hope this helps anyone else who is trying to get this part of the API to work. This code works on my G1, but is seems as if there is no way to tell what the sampling rate and sample resolution on a given device are. It doesn't seem to throw any errors if you pick a bad combination of parameters; it just fails.
8200
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 04, 2011 10:34 am

Top
Previous

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 19 guests