Android: play mono sound through a single channel - android

I am trying to play a mono sound through a single channel, the only thing I can think of is interweaving a second channel into the byte array, changing AudioFormat to stereo then calling
audioTrack.setStereoVolume(leftVolume, rightVolume); to set one channel's volume to zero. Is there a simpler way?
Playback:
AudioTrack audioTrack = null; // Get audio track
try {
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, (int)numSamples*2,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length); // Load the track
audioTrack.play(); // Play the track
}
catch (Exception e){ }

Related

Android Audio recording sample sounds weird

I wrote code to record audio from my phone in PCM16bit Raw format but when I play it back the output sounds weird. What could be wrong.
My code segments are as below
short[] audioBuffer = new short[bufferSize / 2];
AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC,
SAMPLE_RATE,
AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
Sample rate is at 44100 .
int numberOfShort1 = record.read(audioData,0,audioData.length);
shortsRead += numberOfShort;
readtimes++;
try {
os.write(audioData, 0, audioData.length);
} catch (IOException e) {
Log.e(LOG_TAG, "Error saving recording ", e);
//runonce=2;
return;
}
Could someone tell me whats wrong with my code?I am able to play other raw audios well.
This is how it sounds Link to audio
My voice sounds like some weird alien growl.

How can i use AudioTrack to play mp3 file and also seek to position

Iam trying to develop an application where i can set the speed of music file(mp3) to be set like 1x,1.5x,2x,2.5x like this.but MediaPlayer does not support this feauture unless it is 23 api.How can i use AudioTrack to play this mp3 file and also seek to position.the below code gives me "zzzzzzz" sound.
public void playAudio(){
int minBufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
int bufferSize = 512;
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
int i = 0;
byte[] s = new byte[bufferSize];
try {
final String path= Environment.getExternalStorageDirectory().getAbsolutePath() + "/folioreader/audio"+".mp3";
FileInputStream fin = new FileInputStream(path);
DataInputStream dis = new DataInputStream(fin);
audioTrack.play();
while((i = dis.read(s, 0, bufferSize)) > -1){
audioTrack.write(s, 0, i);
}
audioTrack.stop();
audioTrack.release();
dis.close();
fin.close();
} catch (FileNotFoundException e) {
// TODO
e.printStackTrace();
} catch (IOException e) {
// TODO
e.printStackTrace();
}
}
AudioTrack can play uncompressed audio (WAV) not any compressed ones (mp3 or AAC etc.,) You need to call MediaCodec to decode and then use Audio Track to play audio. Refer these links, https://developer.android.com/reference/android/media/AudioTrack.html and https://developer.android.com/reference/android/media/MediaCodec.html.
For faster playback, give sample rate proportional to the speed that you require. For ex. to play 8kHz audio in 2X rate, give 16kHz in AudioTrack and so on. This is crude way.

Converting .pcm to aac while recording from mic

I have a program that records sound from microphone and saves the sound in a .pcm file.
I have a separate function in the program that converts .pcm to aac using MediaCodec and MediaMuxer.
Is it possible to modify my program so that directly converts .pcm to aac using a MediaCodec?How can I do that? Is there a guide or is there a code sample to do that?
Here is a code snippet from my program that records sound from the
microphone:
recorder = new AudioRecord(
MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE,
RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING,
bufferSize);
recorder.startRecording();
isRecording = true;
FileOutputStream os = null;
try {
os = new FileOutputStream(filePathAudioRec);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (isRecording) {
// gets the voice output from microphone to byte format
recorder.read(sData, 0, BufferElements2Rec);
try {
// writes the data to file from buffer
// stores the voice buffer
byte bData[] = short2byte(sData);
os.write(bData, 0, BufferElements2Rec * BytesPerElement); //do I need to write it to a file ??
} catch (IOException e) {
e.printStackTrace();
}
// inside while loop: add code to convert to .mp4 while the data is collected from the MIC
}

how to decode sound from mp3 to pcm by mpg123 library

int minSize =AudioTrack.getMinBufferSize( 45100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT );
track = new AudioTrack( AudioManager.STREAM_MUSIC,45100 ,
AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
minSize, AudioTrack.MODE_STREAM);
track.setStereoVolume(1f,1f);
while(true)
{
Log.e(""," sizewe are using for track buffer is "+minSize);
int i = 0; // Read the file into the "music" array
music=new short[4188];
while (dis.available() > 0)
{
music[i] = dis.readShort();
i++;
if(i==4188)
break;
}
Log.e("","music lengh= "+music.length+" and dis.available="+dis.available());
track.play();
track.write(music,0, music.length);
track.flush();
track.stop();
}
could some one tell me how to decode sound when we playing a large mp3 file in audiotrack using bytebuffer ,i want to decode it in real time when playing the song .. here i am getting song from .aiff file which is a pcm format
first: i want to play a mp3 file by audiotrack class
second: .wav is also a pcm file but it is not playing.
thanks in advance

Play Audio (mp3, ogg) from sd card with AudioTrack

I want to play audio file from SD card with AudioTrack. I've tried with this code:
int minBufferSize = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
int bufferSize = 512;
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
AudioTrack.MODE_STREAM);
int i = 0;
byte[] s = new byte[bufferSize];
FileInputStream fin = new FileInputStream(path);
DataInputStream dis = new DataInputStream(fin);
at.play();
while ((i = dis.read(s, 0, bufferSize)) > -1)
{
at.write(s, 0, i);
}
at.stop();
at.release();
dis.close();
fin.close();
But it doesn't play audio file properly. Instead of original audio it plays some kind noise sound.
From the following link i have found that AudioTrack only plays PCM audio,
http://developer.android.com/guide/topics/media/index.html
And Please have a look on this code.
http://mindtherobot.com/blog/624/android-audio-play-an-mp3-file-on-an-audiotrack/
this is provided the information about how to Play an MP3 file on an AudioTrack.
Hope this helps u .
The AudioTrack class only supports PCM audio data, that's why you're hearing noise.
Use MediaPlayer or SoundPool if you want to play compressed audio files like .mp3 and .ogg.
Alternatively you could decode the compressed audio in your app and write the resulting PCM data to an AudioTrack, but that's a much bigger task.

Categories

Resources