I receive a stream of audio data (one channel, 16000Hz, 170ms buffer) in my android app and I want to play this audio.
I discovered AudioTrack but when I am playing the sound I only get loud and awkward sound.
My code so far looks like this:
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 16000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, 128000, AudioTrack.MODE_STREAM);
audioTrack.play();
while (true) {
byte[] buf = new byte[32000];
inputStream.readFully(buf);
audioTrack.write(buf, 0, buf.length);
}
How to fix this?
refer to this example: buf size needs to be carefull calculated based on your data, not necessarily random 32000.
int bufsize = AudioTrack.getMinBufferSize(
8000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT
);
AudioTrack trackplayer = new AudioTrack(
AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_ STEREO,
AudioFormat.ENCODING_PCM_16BIT,
bufsize,
AudioTrack.MODE_STREAM
);
trackplayer.play();
trackplayer.write(bytes_pkg, 0, bytes_pkg.length);
trackplayer.stop();
trackplayer.release();
Related
I have a simple application that starts recording from the microphone and then immediately plays the sounds back to my headphones, however the problem here is: the sound quality is not good and there is a lasting echo after speaking something in the microphone which can last up to 5 seconds after speaking. I'm not sure whether the problem lies in the Java Sound API and I need to switch to another library.
I also want to mention that I use a headphone that is plugged into my computer. When I use low volume then the sound plays normal however when I turn it up to 90% of my maximum value the sound quality really becomes bad.
Here is my code:
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, format);
TargetDataLine mic = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
mic.open();
mic.start();
DataLine.Info dataLineInfo2 = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo2);
speakers.open();
speakers.start();
byte[] buffer = new byte[1024];
while(true) {
mic.read(buffer, 0, buffer.length);
speakers.write(buffer, 0, buffer.length);
}
I'm trying to capture the sound of the PC. I have managed to capture the sound that enters the microphone through TargetDataLine, but I cannot find the way to capture the sound that comes out of the speakers.
I've been watching the mixer but I have not managed to capture the sound. I would like to know if someone has done it and if you can give me some clue as to where to start.
Although, your question is not really according to the "rules", here is a code snippet:
private byte[] record() throws LineUnavailableException {
AudioFormat format = AudioUtil.getAudioFormat(audioConf);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// Checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
LOGGER.error("Line not supported");
System.exit(0);
}
microphone = (TargetDataLine) AudioSystem.getLine(info);
microphone.open(format);
microphone.start();
LOGGER.info("Listening, tap enter to stop ...");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[microphone.getBufferSize() / 5];
// Begin audio capture.
microphone.start();
// Here, stopped is a global boolean set by another thread.
while (!stopped) {
// Read the next chunk of data from the TargetDataLine.
numBytesRead = microphone.read(data, 0, data.length);
// Save this chunk of data.
byteArrayOutputStream.write(data, 0, numBytesRead);
}
return byteArrayOutputStream.toByteArray();
}
Get more info from here:
https://www.programcreek.com/java-api-examples/?class=javax.sound.sampled.TargetDataLine&method=read
This question already has answers here:
Can I get raw PCM data from MediaPlayer or SoundPool?
(3 answers)
Closed 5 years ago.
I want to process audio byte by byte in Android Studio while streaming the song in real time. Can't seem to get the data from the MediaPlayer class. I would appreciate any help. Thanks!
instead of MediaPlayer use AudioRecord
Audio Record - Android Developer
sample:
int sampleRate = 16000;
int bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord audioRecorder = new AudioRecord(
MediaRecorder.AudioSource.MIC, sampleRate,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
audioRecord.startRecording();
new Thread(new Runnable() {
#Override
public void run() {
while (mIsRecording) {
short[] buffer = new short[mBufferSize];
int readSize = mAudioRecorder.read(buffer, 0, buffer.length);
// do your stuff here
}
}
}).start();
How can i change audiotrack that has been recorded to ROBOTIC voice android.
audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
I'm using this instructions for get a signal audio of a microphone:
while(!stopCapture){
int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
if(cnt > 0){
byteArrayOutputStream.write(tempBuffer, 0, cnt);
}
}
byteArrayOutputStream.close();
byte audio[] = byteArrayOutputStream.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
AudioInputStream ais = new AudioInputStream(input, audioFormat, audio.length / audioFormat.getFrameSize());
But these instructions wait the entire signal before elaborate it.
I need elaborate it in real time...is possible?
How can i do it?
thanks