how to change audiotrack to ROBOTIC voice android? - java

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);

Related

Echo when recording and playing sound

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);
}

Detect Specific Frequency From Microphone Java

I'm trying to capture audio that is coming from microphone and i wanted to check the frequency of sound. If I get a frequency greater then let's say : 1316.8 then I will start recording for 1 minute.
I am struggling with converting byte Data to Frequency.
I have used Javax.sound to capture audio that is coming from microphone and I have done the recording part as well.
AudioFormat format = new AudioFormat(44100, 16, 2, true, true);
DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format);
try {
TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
targetLine.open(format);
targetLine.start();
SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(sourceInfo);
sourceLine.open(format);
sourceLine.start();
int numBytesRead;
byte[] targetData = new byte[targetLine.getBufferSize() / 5];
I expect the output to be like Frequency of every sound that is coming from microphone.

byte by byte dynamic audio processing in android studio [duplicate]

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();

AudioRecord record audio quality is very distorted when played back using AudioTrack

Hey I am attempting the following: Record audio on one mobile device (e.g Android Smartphone) send the record audio to N mobile devices using multicast and playback the recorded audio. I can record, transmit, and playback the audio. However the playback audio is very distorted and I cannot figure out if it is the AudioRecord or the AudioTrack which is configured wrong.
My recorder is configured like this:
public static int RECORDER_SAMPLERATE = 44100;
...
mRecorder = new AudioRecord.Builder()
.setAudioSource(MediaRecorder.AudioSource.MIC)
.setAudioFormat(
new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(RECORDER_SAMPLERATE)
.setChannelIndexMask(AudioFormat.CHANNEL_IN_DEFAULT)
.build())
.setBufferSizeInBytes(AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT) * 4)
.build();
And my track is configured like this:
mTrack = new AudioTrack.Builder().setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build())
.setAudioFormat(
new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(44100)
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO).build())
.setBufferSizeInBytes(AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT))
.build();
I have tried different buffer sizes, different Usage and Content types, but still get the same distorted result.
I hope someone can help with this

How to play back raw audio stream?

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();

Categories