i am developing an android app, which plays live speex audio stream. So i used jspeex library .
The audio stream is 11khz,16 bit.
At android side i have done as follows:
SpeexDecoder decoder = new SpeexDecoder();
decoder.init(1, 11025,1, true);
decoder.processData(subdata, 0, subdata.length);
byte[] decoded_data = new byte[decoder.getProcessedDataByteSize()];
int result= decoder.getProcessedData(decoded_data, 0);
When this decoded data is played by Audiotrack , some part of audio is clipped.
Also when decoder is set to nb-mode( first parameter set to 0) the sound quality is worse.
I wonder there is any parameter configuration mistake in my code.
Any help, advice appreciated.
Thanks in advance.
Sampling rate and buffer size should be set in an optimized way for the specific device. For example you can use AudioRecord.getMinBufferSize() to obtain the best size for your buffer:
int sampleRate = 11025; //try also different standard sampleRate
int bufferSize = AudioRecord.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
If your Audiotrack has a buffer which is too small or too large you will experience audio glitch. I suggest you to take a look here and play around with these values (sampleRate and bufferSize).
Related
I am trying to playback base64 encoded .wav audio data on Android, via AudioTrack, and all I'm getting is a very short burst of white noise.
I presume it's in .wav format anyway - it was recorded via JavaScript using my Mac's built-in microphone and converted to base64.
I've possibly set the variables wrong when creating the AudioTrack (bit of guesswork involved there) - I've guessed at the sample rate and encoding, and put the buffer size as that of the entire chunk of data. Very grateful for any assistance.
playChannelAudio("data:application/octet-stream;base64,GkXfo59ChoEBQveBAULygQRC84EIQoKEd2VibUKHgQRChYECGFOAZwH/////////FUmpZpkq17GDD0JATYCGQ2hyb21lV0GGQ2hyb21lFlSua7+uvdeBAXPFh+lZ/IvV7yqDgQKGhkFfT1BVU2Oik09wdXNIZWFkAQEAAIC7AAAAAADhjbWERzuAAJ+BAWJkgSAfQ7Z1Af/////////ngQCjQZSBAACA+4O1bX7U7Fau4djaow6/qEcFOx2D6vgdTWqanSWTXXa4MzJRsP2c4GEeQwl5/cRiYFl3QmBVeh8n5mGhkgidhLFrDoCIMEJBptK5vggcJl5zwHJSDWDOFwrI/RRvryg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SA+wObAtiCRYKkv35+bpr24xC7r99YsVj6s2j30vxq1D3kXAbvJr+sfWNWSpom4cF7x3905QEYsYj1oydJRs3Nd2wL8aI4NPeKYLrJNSqSXhtGG5XxmYWJwpDiXdQaX5fFIz65nxFAzxRxMg7KezqypulXxSiLdSulY2x1w5gTTLxVWztQviIYknGsoJhq47BN3V0XMSsksLJjipxT7TCqsNH0JazomASa4GZMbwA8cPN+fvlBb+1qmcbntdkqxsvHXi2lhKjRpl8XzyjQn+K5DiJS+p9P8IEz6QoqPo6WELX76WPY9IXyJNgZ9kO7pFBncr7LBrQr4o1DnUVQlnFp/Flm0GWLH7GYCFw2Bbm6uiQsCp7sY7cVloFkmmgtN+4jtv75bdgDafsTZI90drCFYAVTHBKKfRjxi01E7EM15Hy/irSvXFxeV4gbMQ/2u9kA2GI/29WIzqQW+TOLgaA7lENLrVVQtUxf5Vlmv08oCL86A3u/9E59JYe5dZ7sthg=");
private void playChannelAudio(String base64FormattedString) {
String strippedAudioData = base64FormattedString.substring(base64FormattedString.indexOf(',') + 1);
byte[] data = Base64.decode(strippedAudioData, Base64.DEFAULT);
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
data.length,
AudioTrack.MODE_STATIC);
audioTrack.write(data, 0, data.length);
audioTrack.play();
audioTrack.release();
}
Your audio data is encoded with Opus codec with a sample rate of 48000 Hz (detected by FFmpeg). You have given wrong sample rate as a parameter to the AudioTrack constructor (given 44100 but it's 48000) and also it's encoded with a codec, and AudioTrack expects raw/decoded PCM audio so it could be the reason for the white noise you are hearing. In order to play the audio correctly, first convert it to raw PCM audio data then encode it with Base64.
I have a server that encodes real-time voice into mono or stereo mp3 thanks to libmp3lame and sends it chunk by chunk through a WebSocket.
I'm trying to make an Android App that receives those mp3 chunks and play them with the most appropriate Audio player Android have. I went with AudioTrack since it seems pretty easy to add chunks to the player as well as "stream" oriented. (Since what I'm doing is sending to the track some byte array and not a full song that is locally stocked in the Android phone).
Since AudioTrack does not support compressed audio format (such as MP3), I have to decode those chunks into PCM to play them afterward. I'm using the famous JLayer to do this real-time decoding. Thanks to that, I can play each sample into my AudioTrack and hear what the server is sending.
My problem is that the received/player audio is badly hashed. (I can understand whatever the speaker is saying perfectly, but the quality is bad, like if the speaker had a "robotic voice").
Here is the code I'm using to receive/decode/play those byte[].
public void addSample(byte[] data) throws BitstreamException, DecoderException, IOException {
// JLayer decoder
Decoder decoder = new Decoder();
// Input Stream with the byte[] voice data
InputStream bis = new ByteArrayInputStream(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Bitstream bits = new Bitstream(bis);
// Decoding MP3 data into PCM in a PCM BUFFER
SampleBuffer pcmBuffer = (SampleBuffer) decoder.decodeFrame(bits.readFrame(), bits);
// Sending the PCMBuffer data into Audio Track to play it
mTrack.write(pcmBuffer.getBuffer(), 0, pcmBuffer.getBufferLength());
bits.closeFrame();
}
And here is my AudioTrack initialization
mTrack= new AudioTrack.Builder()
.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build())
.setAudioFormat(new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(48000)
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
.build())
.setBufferSizeInBytes(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT))
.build();
mTrack.play();
So to understand what was happening I tried to lag each data contained in the pcmBuffer. It seems like a huge part of those data where 0 at the very beginning of the buffer (I'd say 1/5 of the buffer is 0, all of them located at the beginning). So then I took an oscilloscope and tried to get the signal my Android phone was receiving. Here is the result:
As you can see, each frame is present, but as some "blank" or 0 data values. Those 0 in the beginning of each frame makes the signal hashed and pretty annoying to listen.
I have no idea whether this comes from the MP3 signal itself, the way I'm playing it, AudioTrack, JLayer, or the way I'm decoding it. So if anyone has an idea it would be really awesome.
EDIT :
Found out something interesting. By decoding each frame header I can have access to a lot of information such as the time in ms for each frame. I logged it :
System.out.println(bits.readFrame().ms_per_frame());
I found out that each of my frames are 24ms. When I look back at the oscilloscope, I can see that each frame actually take 24ms, but the beginning/end of each frame is filled with 0. So first of all, is it a decoding problem ? If it is not, how can I have a clear signal without small breakup in each frame ?
I've been printing all the data that each frame is sending me, each frame starts with a looot of zeros. How am I supposed to have a clear signal if each frame have some kind of audio void ?
If I print the MP3 data that I'm receiving each frame (96 bits), I have the first four bytes (probably the header?) that always have the same value :
"-1, -5, 20, -60"
Then I have a fifth bit that is always equal to 0, and sometimes a sixth bit that is also equal to 0. Should I be removing those ?
Say, I have an InputStream, providing audio/mpeg (or audio/aacp) - a Shoutcast radio.
Could somebody please show me a code example to playback such a stream?
I searched all over the internet, and it looks like the android.media.MediaPlayer can't playback a buffered stream. It can playback streams only via HTTP URL as DataSource.
Yes, there is a possibility to implement your own android.media.MediaDataSource and feed it to MediaPlayer.setDataSource(), but in case of audio/aacp the codec could not initialize.
Guess this could be done with OpenSL ES, but still I haven't found any example which would perform decoding from InputStream (not from URL), and then play back the output..
Come on guys! Any sample snippet in OpenSL ES decoding an input byte array and sending it to the audio output. Please!
Only one condition - input audio format should be obtained from the input stream.
The only one manual I found on OpenSL ES is Khronos OpenSL ES™ Registry, but that is not a reading for beginners at all. I'd rather die there searching for a proper "how-to" example... :(
Why is it so weird - it's very simple /though inconvenient/ (java), or it's very hard (NDK)? Why is there nothing in the middle?
your solution is something like this:
public void run() {
int buffersize = AudioRecord.getMinBufferSize(11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);
atrack.setPlaybackRate(11025);
byte[] buffer = new byte[buffersize];
atrack.play();
while (isPlaying) {
yourStream.read(buffer, 0, buffersize);
atrack.write(buffer, 0, buffer.length);
}
}
I have followed this example to convert raw audio data coming from AudioRecord to mp3, and it happened successfully, if I store this data in a file the mp3 file and play with music player then it is audible.
Now my question is instead of storing mp3 data to a file i need to play it with AudioTrack, the data is coming from the Red5 media server as live stream, but the problem is AudioTrack can only play PCM data, so i can only hear noise from my data.
Now i am using JLayer to my require task.
My code is as follows.
int readresult = recorder.read(audioData, 0, recorderBufSize);
int encResult = SimpleLame.encode(audioData,audioData, readresult, mp3buffer);
and this mp3buffer data is sent to other user by Red5 stream.
data received at other user is in form of stream, so for playing it the code is
Bitstream bitstream = new Bitstream(data.read());
Decoder decoder = new Decoder();
Header frameHeader = bitstream.readFrame();
SampleBuffer output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitstream);
short[] pcm = output.getBuffer();
player.write(pcm, 0, pcm.length);
But my code freezes at bitstream.readFrame after 2-3 seconds, also no sound is produced before that.
Any guess what will be the problem? Any suggestion is appreciated.
Note: I don't need to store the mp3 data, so i cant use MediaPlayer, as it requires a file or filedescriptor.
just a tip, but try to
output.close();
bitstream.closeFrame();
after yours write code. I'm processing MP3 same as you do, but I'm closing buffers after usage and I have no problem.
Second tip - do it in Thread or any other Background process. As you mentioned these deaf 2 seconds, media player may wait until you process whole stream because you are loading it in same thread.
Try both tips (and you should anyway). In first, problem could be in internal buffers; In second you probably fulfill Media's input buffer and you locked app (same thread, full buffer cannot receive your input and code to play it and release same buffer is not invoked because writing locks it...)
Also, if you don't doing it now, check for 'frameHeader == null' due to file end.
Good luck.
You need to loop through the frames like this:
While (frameHeader = bitstream.readFrame()){
SampleBuffer output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitstream);
short[] pcm = output.getBuffer();
player.write(pcm, 0, pcm.length);
bitstream.close();
}
And make sure you are not running them on main thread.(This is probably the reason of freezing.)
I capture sound in real time to buffer and then I process these data. But sometimes I get warning: buffer overflow what cause of problem in the processing.
I created AudioRecord:
bufferSize = ???;
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, **bufferSize**);
but there are not method for getMaximumBufferSize or semthing like that (only getMinBufferSize - but here is buffer owerflow). And I think that setting own buffer size is not good solution.
According to the API:
Upon creation, an AudioRecord object initializes its associated audio buffer that it will fill with the new audio data. The size of this buffer, specified during the construction, determines how long an AudioRecord can record before "over-running" data that has not been read yet. Data should be read from the audio hardware in chunks of sizes inferior to the total recording buffer size.
Your buffer should be large enough for the amount of buffered data you want to support, ie bitrate*time, and you need to make sure that you are reading from the AudioRecord consistently so that it does not fill the buffer, and that the size of the chunks that you read are smaller than the buffer size in the AudioRecorder. I like to read/write in 8k chunks but have seen other values too.