Getting the current master volume in Java - java

I have Clip plaiong on my app and I'd like to get either the master volume or the volume of the Clip that is playing... I am fairly new to this.
Here's the code where i play the audio:
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("src/music/music.wav"));
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
clip.loop(-1);

Float volume = clip.getControl(FloatControl.Type.VOLUME).getValue();
You should probably look at Java's API entry for FloatControl.

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.

How to capture sound in Java?

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

issue when opening wav file using (Java) AudioInputStream

I am using JDK7 and trying to run a wav file - I tried the following test but got the error copied below:
Error:
line with format ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame, not supported.
Sample Code:
import javax.sound.sampled.*;
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
new File("C://Users//xyz//Desktop//centerClosed.wav"));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
Any ideas on how I go about handling this case? Thanks in advance
Your wav file seems to be in ULAW format, sampled at 8kHz, a format the clip apparently does not understand.
Try converting the audio to 44.1kHz PCM like this:
import javax.sound.sampled.*;
try {
Clip clip = AudioSystem.getClip();
AudioInputStream ulawIn = AudioSystem.getAudioInputStream(
new File("C://Users//xyz//Desktop//centerClosed.wav"));
// define a target AudioFormat that is likely to be supported by your audio hardware,
// i.e. 44.1kHz sampling rate and 16 bit samples.
AudioInputStream pcmIn = AudioSystem.getAudioInputStream(
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100f, 16, 1, 2, 44100f, true)
ulawIn);
clip.open(pcmIn);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}

Why FrameLength is -1?

I am trying to get the amplitude of one of the mp3 files. Following is the code:
AudioInputStream ain = AudioSystem.getAudioInputStream(file);
AudioFormat baseFormat = ain.getFormat();
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels()*2,
baseFormat.getSampleRate(),
false);
//play(file,din,decodedFormat,ain);
ain = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));
DecodedMpegAudioInputStream decodedStream = new DecodedMpegAudioInputStream(decodedFormat, ain);
System.out.println(ain.getFrameLength());
System.out.println(decodedStream.getFrameLength());
The problem is the last printlns are returning -1 in other words there is no information of frame length in the mp3 file or the audiostream is unable to read the frame length (is that possible?). I am trying to learn about mp3 audio file format. Am I doing this correct? OR is there any other way to get the frame length?
Verify if AudioSystem.getAudioFileFormat(file) is an MPEG format and run through its properties
AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file);
if (audioFileFormat instanceof TAudioFileFormat) {
Map<String, Object> properties = ((TAudioFileFormat) audioFileFormat).properties();
// ...
}
There's properties like mp3.length.bytes and may help

Categories