In this post, i am going to show the code for creating the AudioInputStream from an PCM - amplitude array. It basically converts the int [] array to byte array according to AudioFormat.
The code for the reverse operation (extract amplitude array from recorded wave file or AudioStream )is in my earlier post : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html
The code for converting PCM amplitude array to AudioStream is follows :
Showing posts with label Audio Processing. Show all posts
Showing posts with label Audio Processing. Show all posts
Java Sound : generate play sine wave - source code
Working source code example on how to generate and play sine wave in Java :
View my previous post for playing any PCM amplitude array.
Generate Sine wave of a particular frequency :
View my previous post for playing any PCM amplitude array.
Generate Sine wave of a particular frequency :
private static byte[] generateSineWavefreq(int frequencyOfSignal, int seconds) {
// total samples = (duration in second) * (samples per second)
byte[] sin = new byte[seconds * sampleRate];
double samplingInterval = (double) (sampleRate / frequencyOfSignal);
System.out.println("Sampling Frequency : "+sampleRate);
System.out.println("Frequency of Signal : "+frequencyOfSignal);
System.out.println("Sampling Interval : "+samplingInterval);
for (int i = 0; i < sin.length; i++) {
double angle = (2.0 * Math.PI * i) / samplingInterval;
sin[i] = (byte) (Math.sin(angle) * 127);
System.out.println("" + sin[i]);
}
Java Audio : Playing PCM amplitude Array
Play an Array of PCM Amplitude Values in Java – Quick Tutorial
Basic Steps:
Converting integer or float arrays to a byte array:
The line.write() method expects a byte[], so your raw PCM amplitude values must be packed accordingly. The conversion depends on the audio format you choose. For 8‑bit signed PCM (what we’ll use in the example), a normalised float between -1 and 1 maps to a single byte with
Basic Steps:
// Initialize a SourceDataLine for playback
SourceDataLine line = AudioSystem.getSourceDataLine(audioFormat);
line.open(audioFormat);
line.start();
// Write the entire byte array to the line
line.write(byteArray, 0, byteArray.length);
line.drain();
line.close();
Converting integer or float arrays to a byte array:
The line.write() method expects a byte[], so your raw PCM amplitude values must be packed accordingly. The conversion depends on the audio format you choose. For 8‑bit signed PCM (what we’ll use in the example), a normalised float between -1 and 1 maps to a single byte with
(byte)(amplitude * 127). For 16‑bit signed, you’d need to write two bytes per sample in the correct endian order. The snippet below shows the simple 8‑bit mapping.Java extract amplitude array from recorded wave
Extract amplitude array from recorded/saved wav : From File , AudioInputStream , ByteArray of File or ByteArrayInputStream - working java source code example
import java.io.ByteArrayInputStream;
import java.io.File;
Sound (audio file) player in java - working source code example
Sound (audio file) player in java - working source code example
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
Java Sound Capture from Microphone working code
Sound Capture: Record from Microphone and Save — Complete Java Example
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
/**
* Reads data from the input channel and writes to the output stream
*/
public class MicrophoneRecorder implements Runnable {
// record microphone && generate stream/byte array
private AudioInputStream audioInputStream;
Silence Removal and End Point Detection JAVA Code
For the purpose of silence removal of captured sound, we used the algorithm in our final year project. In this post, I am publishing the endpoint detection and silence removal code ( implementation of this algorithm in JAVA).
These links might be useful to you as well.
These links might be useful to you as well.
- For Capturing audio from microphone
- For Converting captured data into PCM integer or float array
- For Playing any PCM array of amplitudes
- array of original signal's amplitude data : float[] originalSignal
- sampling rate of original signal in Hz : int samplingRate
package org.ioe.tprsa.audio.preProcessings;
Silence Removal and End Point Detection MATLAB Code
Visit http://ganeshtiwaridotcomdotnp.blogspot.com/2011/06/final-report-text-prompted-remote.html for more detail about our project.
For the purpose of silence removal of captured sound, we used the algorithm specified in
"A New Silence Removal and Endpoint Detection Algorithm for Speech and Speaker Recognition Applications"
Our actual system was in JAVA but we verified the performance of this algorithm in MATLAB.
Inputs and Output
Here is the Matlab code :
It first records sound for 5 seconds and removes the silence and then plays back.
For the purpose of silence removal of captured sound, we used the algorithm specified in
"A New Silence Removal and Endpoint Detection Algorithm for Speech and Speaker Recognition Applications"
Our actual system was in JAVA but we verified the performance of this algorithm in MATLAB.
Inputs and Output
![]() |
| Before silence removal |
![]() |
| After automatic silence removal |
It first records sound for 5 seconds and removes the silence and then plays back.
Subscribe to:
Posts
(
Atom
)

