Today I was working with the Xuggler library and I tried capturing my screen which worked flawless. But I also wanted to add audio from my microphone to the video file I captured. This was not as easy as I had expected, and now I'm stuck with this strange NullPointerException.
This is my code (abbreviated):
AudioFormat format = new AudioFormat(8000.0F, 16, 1, true, false);
writer.addAudioStream(1, 0, 1, (int) format.getSampleRate());
TargetDataLine line = getTargetDataLineForRecord(format);
final int frameSizeInBytes = format.getFrameSize();
final int bufferLengthInFrames = line.getBufferSize() / 8;
final int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
final byte[] buf = new byte[bufferLengthInBytes];
final long startTime = System.nanoTime();
...
while (recording) {
int numBytesRead = 0;
numBytesRead = line.read(buf, 0, bufferLengthInBytes);
int numSamplesRead = numBytesRead / 2;
short[] audioSamples = new short[numSamplesRead];
if (format.isBigEndian()) {
for (int i = 0; i < numSamplesRead; i++) {
audioSamples[i] = (short) ((buf[2 * i] << 8) | buf[2 * i + 1]);
}
} else {
for (int i = 0; i < numSamplesRead; i++) {
audioSamples[i] = (short) ((buf[2 * i + 1] << 8) | buf[2 * i]);
}
}
writer.encodeAudio(1, audioSamples, System.nanoTime() - startTime, TimeUnit.NANOSECONDS); // CaptureScreen.java:118
}
writer.close();
And here is the NullPointerException:
java.lang.NullPointerException
at com.xuggle.mediatool.MediaWriter.encodeAudio(MediaWriter.java:923)
at exe.media.CaptureScreen.captureScreen(CaptureScreen.java:118)
at exe.media.CaptureScreen.main(CaptureScreen.java:43)
The problem I'm having is at this line (118):
writer.encodeAudio(1, audioSamples, System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
For some reason when I try to encode the audio samples, xuggle throws a NullPointerException, I'm not sure if this is a bug or just me doing something stupid but I am unable to solve it anyway.
For better understanding I have posted all the code on pastebin and this includes code for capturing my screen and also this code where I try to record the audio.
These are the jars I have included:
commons-cli-1.2.jar
logback-classic-1.1.2.jar
logback-core-1.1.2.jar
xuggle-xuggler-arch-x86_x64-w64-mingw32.jar*
xuggle-xuggler-noarch-5.4.jar*
(The '*' means I didn't download the jar from it's primary location.
Thanks in advance and remember ANY helpful answer will be rewarded the 50 rep bounty!
I assume that you have line.open(format); and line.start(); somewhere?
You may need to make sure that you have audiosamples: if (audioSamples.length > 0) writer.encodeAudio ...
Also, you may want to add slf4j-api-1.6.4.jar and slf4j-simple-1.6.4.jar to get more details on the errors from Xuggler.
Related
I am trying to extract data from .wav file to draw a wave graph, but I am stuck as I get the only a stream of 0s with my code:
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("audio/sine_-06_05_02000.wav"));
AudioFormat format = ais.getFormat();
format = new AudioFormat(format.getFrameRate(), format.getSampleSizeInBits(), format.getChannels(), true, true);
ais = AudioSystem.getAudioInputStream(format, ais);
int sample_size = format.getSampleSizeInBits() / 8;
ArrayList<Long> data = new ArrayList<Long>();
int size = 400;
while (data.size() < size)
{
byte[] buffer = new byte[8];
ais.read(buffer, 8-sample_size, sample_size);
if (buffer[8-sample_size] < 0)
{
for (int i = 0; i < 8 - sample_size; i++)
{
buffer[i] = -1;
}
}
data.add(ByteBuffer.wrap(buffer).getLong());
}
for(long value:data)
{
System.out.println(value);
}
Please tell me why I cannot get the data and where my code is wrong if you could find out. Thank you!
Edit: I figured it out that my audio resource was digital, but the code was for analog audio.
I am developing an application that uses mp3 encoding/decoding. While in principle it behaves correctly for most of the files, there are some exceptions. I detected that these files have a missing header. I get an array out of bound exception when attempting to decode. I used two approaches but both failed.
The first:
DecodedMpegAudioInputStream dais = (DecodedMpegAudioInputStream) AudioSystem.getAudioInputStream(daisf, ais);
byte[] audioData = IOUtils.toByteArray(dais);//exception here
And the second:
ByteOutputStream bos = new ByteOutputStream();
// Get the decoded stream.
byte[] byteData = new byte[1];
int nBytesRead = 0;
int offset = 0;
int cnt=1;
while (nBytesRead != -1) {
System.out.println("cnt="+cnt);
nBytesRead = dais.read(byteData, offset, byteData.length);//exception here at first loop
if (nBytesRead != -1) {
int numShorts = nBytesRead >> 1;
for (int j = 0; j < numShorts; j++) {
bos.write(byteData[j]);
}
}
cnt+=1;
}
byte[] audioData = bos.getBytes();
It seems that there is an issue with the headers or the structure of it, because the dais stream has content/bytes. However it can be opened with audacity and ffplay so I believe there should be a workaround. Any ideas how to counter it?
You could use code redundancy to improve reliability. Look into alternative libraries, such as Xuggler or JLayer.
Recently, I've been experimenting with mixing AudioInputStreams together. After reading this post, or more importantly Jason Olson's answer, I came up with this code:
private static AudioInputStream mixAudio(ArrayList audio) throws IOException{
ArrayList<byte[]> byteArrays = new ArrayList();
long size = 0;
int pos = 0;
for(int i = 0; i < audio.size(); i++){
AudioInputStream temp = (AudioInputStream) audio.get(i);
byteArrays.add(convertStream(temp));
if(size < temp.getFrameLength()){
size = temp.getFrameLength();
pos = i;
}
}
byte[] compiledStream = new byte[byteArrays.get(pos).length];
for(int i = 0; i < compiledStream.length; i++){
int byteSum = 0;
for(int j = 0; j < byteArrays.size(); j++){
try{
byteSum += byteArrays.get(j)[i];
}catch(Exception e){
byteArrays.remove(j);
}
}
compiledStream[i] = (byte) (byteSum / byteArrays.size());
}
return new AudioInputStream(new ByteArrayInputStream(compiledStream), ((AudioInputStream)audio.get(pos)).getFormat(), ((AudioInputStream)audio.get(pos)).getFrameLength());
}
private static byte[] convertStream(AudioInputStream stream) throws IOException{
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int numRead;
while((numRead = stream.read(buffer)) != -1){
byteStream.write(buffer, 0, numRead);
}
return byteStream.toByteArray();
}
This code works very well for mixing audio files. However, it seems the more audio files being mixed, the more white noise that appears in the returned AudioInputStream. All of the files being combined are identical when it comes to formatting. If anyone has any suggestions\advice, thanks in advance.
I could be wrong, but I think your problem has to do with the fact that you are messing with the bytes instead of what the bytes mean. For instance, if you are working with a 16 bit sampling rate, 2 bytes form the number that corresponds to the amplitude rather than just 1 byte. So, you end up getting something close but not quite right.
I am currently trying to implement some code using Android to detect when a number of specific audio frequency ranges are played through the phone's microphone. I have set up the class using the AudioRecord class:
int channel_config = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int format = AudioFormat.ENCODING_PCM_16BIT;
int sampleSize = 8000;
int bufferSize = AudioRecord.getMinBufferSize(sampleSize, channel_config, format);
AudioRecord audioInput = new AudioRecord(AudioSource.MIC, sampleSize, channel_config, format, bufferSize);
The audio is then read in:
short[] audioBuffer = new short[bufferSize];
audioInput.startRecording();
audioInput.read(audioBuffer, 0, bufferSize);
Performing an FFT is where I become stuck, as I have very little experience in this area. I have been trying to use this class:
FFT in Java and Complex class to go with it
I am then sending the following values:
Complex[] fftTempArray = new Complex[bufferSize];
for (int i=0; i<bufferSize; i++)
{
fftTempArray[i] = new Complex(audio[i], 0);
}
Complex[] fftArray = fft(fftTempArray);
This could easily be me misunderstanding how this class is meant to work, but the values returned jump all over the place and aren't representative of a consistent frequency even in silence. Is anyone aware of a way to perform this task, or am I overcomplicating matters to try and grab only a small number of frequency ranges rather than to draw it as a graphical representation?
First you need to ensure that the result you are getting is correctly converted to a float/double. I'm not sure how the short[] version works, but the byte[] version only returns the raw byte version. This byte array then needs to be properly converted to a floating point number. The code for the conversion should look something like this:
double[] micBufferData = new double[<insert-proper-size>];
final int bytesPerSample = 2; // As it is 16bit PCM
final double amplification = 100.0; // choose a number as you like
for (int index = 0, floatIndex = 0; index < bytesRecorded - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
double sample = 0;
for (int b = 0; b < bytesPerSample; b++) {
int v = bufferData[index + b];
if (b < bytesPerSample - 1 || bytesPerSample == 1) {
v &= 0xFF;
}
sample += v << (b * 8);
}
double sample32 = amplification * (sample / 32768.0);
micBufferData[floatIndex] = sample32;
}
Then you use micBufferData[] to create your input complex array.
Once you get the results, use the magnitudes of the complex numbers in the results. Most of the magnitudes should be close to zero except the frequencies that have actual values.
You need the sampling frequency to convert the array indices to such magnitudes to frequencies:
private double ComputeFrequency(int arrayIndex) {
return ((1.0 * sampleRate) / (1.0 * fftOutWindowSize)) * arrayIndex;
}
Is it possible to create a temporary file that contains a "loop" of a wav file?
Or is it possible to manipulate the stream sent to a stream reader/writer?
Basically I want to play some wav file for a period of time and if that time is greater than the length of time that the wav file provides I want to loop.
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.loop((int)(Math.ceil(timeRequested / audioIn.getFrameLength())));
I'm not sure I understand the exact limitations you have for implementing a solution, but it seems that the cleanest way to do this would be to store the audio data in a buffer the first time you play through the file. Then, if there are more iterations (full or partial) wanted by the user, just rewrite the data you have cached back out to the SourceDataLine the desired number of times.
Here is a link to a sample sound file player (PCM) that has code that should be very easy to modify (or just learn from). I also hacked up some (note: untested) code that just shows the logic I described above: (You probably also want to modify what I have below to conform to the rules concerning only writing chunks of data that have a size that is a multiple of the frame size.)
public void playSoundFile(SourceDataLine line, InputStream inputStream, AudioFormat format, long length, float times)
{
int index = 0;
int size = length * format.getFrameSize();
int currentSize = 0;
byte[] buffer = new byte[size];
AudioInputStream audioInputStream = new AudioInputStream(inputStream, format, length);
while (index < size)
{
currentSize = audioInputStream.read(buffer, index, size - index);
line.write(buffer, index, currentSize);
index += currentSize;
}
float currentTimes = 1.0;
while (currentTimes < times)
{
float timesLeft = times - currentTimes;
int writeBlockSize = line.available();
index = 0;
if (timesLeft >= 1.0)
{
while (index < size)
{
currentSize = ((size - index) < writeBlockSize) ? size - index : writeBlockSize;
line.write(buffer, index, currentSize);
index += currentSize;
}
currentTimes += 1.0;
}
else
{
int partialSize = (int)(timesLeft * ((float) size) + 0.5f);
while (index < partialSize)
{
currentSize = ((partialSize - index) < writeBlockSize) ? partialSize - index : writeBlockSize;
line.write(buffer, index, currentSize);
index += currentSize;
}
currentTimes += timesLeft;
}
}
}
Hope that helps!
I think I'll use the frames per second and framesize information in the audio input stream to figure this out. Other useful information is in getMicrosecondPosition() method of the SourceDataLine object to determine the time/duration played so far.
this along with mark and reset methods in the audio input stream probably take care of it all.