No sound with AudioTrack

All your problems with Audio, Video and Images.

No sound with AudioTrack

Postby vinntec » Wed Dec 14, 2011 11:32 am

Hi,

i'm programming for Android 2.1. Could you help me with the following problem?

I have three files, and the general purpose is to play a sound with audiotrack buffer by buffer. I'm getting pretty desperate here because I tried about everything, and there still is no sound coming out of my speakers (while android's integrated mediaplayer has no problem playing sounds via the emulator).

Source code:

An audioplayer class, which implements the audio track. It will receive a buffer, in which the sound is contained.

Syntax: [ Download ] [ Hide ]
  1. public AudioPlayer(int sampleRate, int channelConfiguration, int audioFormat) throws ProjectException { 
  2. minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfiguration, audioFormat); 
  3. audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfiguration, 
  4. audioFormat, minBufferSize, AudioTrack.MODE_STREAM);  
  5.  
  6. if(audioTrack == null) 
  7. throw new ProjectException("Erreur lors de l'instantiation de AudioTrack"); 
  8.  
  9. audioTrack.setStereoVolume((float)1.0, (float)1.0); 
  10.  
  11. @Override 
  12. public void addToQueue(short[] buffer) { 
  13. audioTrack.write(buffer, 0, buffer.length*Short.SIZE); 
  14. if(!isPlaying ) { 
  15. audioTrack.play(); 
  16. isPlaying = true; 

A model class, which I use to fill the buffer. Normally, it would load sound from a file, but here it just uses a simulator (440Hz), for debugging.

Buffer sizes are chosen very loosely; normally first buffer size should be 6615 and then 4410. That's, again, only for debug.

Syntax: [ Download ] [ Hide ]
  1. public void onTimeChange() { 
  2. if(begin) { 
  3.  
  4. //First fill about 300ms 
  5.  
  6. begin = false; 
  7. short[][] buffer = new short[channels][numFramesBegin]; 
  8. //numFramesBegin is for example 10000 
  9. //For debugging only buffer[0] is useful 
  10. fillSimulatedBuffer(buffer, framesRead); 
  11. framesRead += numFramesBegin; 
  12. audioPlayer.addToQueue(buffer[0]); 
  13.  
  14.  
  15. else { 
  16. try { 
  17. short[][] buffer = new short[channels][numFrames]; 
  18.  
  19. //Afterwards fill like 200ms 
  20.  
  21. fillSimulatedBuffer(buffer, framesRead); 
  22. framesRead += numFrames; 
  23. audioPlayer.addToQueue(buffer[0]); 
  24. } catch (Exception e) { 
  25. e.printStackTrace(); 
  26.  
  27.  
  28. }  
  29.  
  30. private short simulator(int time, short amplitude) { 
  31. //a pure A (frequency=440) 
  32. //this is probably wrong due to sampling rate, but 44 and 4400 won't work either 
  33. return (short)(amplitude*((short)(Math.sin((double)(simulatorFrequency*time))))); 
  34.  
  35. private void fillSimulatedBuffer(short[][] buffer, int offset) { 
  36. for(int i = 0; i < buffer[0].length; i++) 
  37. buffer[0][i] = simulator(offset + i, amplitude); 
  38. }  

A timeTask class that calls model.ontimechange() every 200 ms.

Syntax: [ Download ] [ Hide ]
  1. public class ReadMusic extends TimerTask { 
  2. private final Model model; 
  3.  
  4. public ReadMusic(Model model) { 
  5. this.model = model; 
  6.  
  7.  
  8. @Override 
  9. public void run() { 
  10. System.out.println("Task run"); 
  11. model.onTimeChange(); 
  12.  
  13. }  

What debugging showed me:

-timeTask works fine, it does its job;
-Buffer values seem coherent, and buffer size is bigger than minBufSize;
-Audiotrack's playing state is "playing"
-no exceptions are caught in model functions.

Any ideas would be greatly appreciated!
vinntec
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 14, 2011 11:27 am

Top

Re: No sound with AudioTrack

Postby vinntec » Fri Dec 16, 2011 9:30 pm

OK I found the problem.

There is an error in the current AudioTrack documentation regarding AudioTrack and short buffer input: the specified buffer size should be the size of the buffer itself (buffer.length) and *not* the size in bytes.
vinntec
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 14, 2011 11:27 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 16 guests