I was looking into the Java sound API and noticed that it allows us to play audio files. I have two questions. Given an audio file, how can we use javax.sound to play the audio file at any random location. Moreover, does javax.sound convert audio files to text files containing their lyrics?
"to play the audio file at any random location":
When you are creating an AudioInputStream object you can just give it the bytestream starting at the position at which you want to start like so:
audioInputStream = new AudioInputStream( byteArrayInputStream, audioFormat,
audioData.length/audioFormat.getFrameSize());
This is from the complete example-code at:
http://www.developer.com/java/other/article.php/1565671/Java-Sound-An-Introduction.htm
To your second question: There exist several speech-recognition packages but as far as i know they do a poor job at parsing music because there is too much "noise".
Given an audio file, how can we use javax.sound to play the audio file ..
If you'd read the JavaSound info page you'd have seen source that can play a sound.
..at any random location?
Clip also provides setMicrosecondPosition(long) & setFramePosition(int). Feed a random number to either, and you're set to go.
Related
I want to read wav files in Java and I am going to classify them with K-means.
How can I read wav files in Java and assign them into an array or something like that(you can suggest ideas for it) to classify them?
EDIT: I want to use APIs for reading wav files and for K-means.
The official Java Sound Programmer Guide walks through reading and writing audio files.
This article by A Greensted: Reading and Writing Wav Files in java should be helpful. The WavFile class is very useful and it can be tweaked to return the entire data array instead of buffered fragments.
Equivalent to matlab's wavread function:
http://web.archive.org/web/20120531113946/http://www.builogic.com/java/javasound-read-write.html
You could read the sound files using javax sound library and FileInputStream
(found a nice example here)
and treat the wave files as a vector of bits (0,1) or bytes.. using multiple sequence alignment (Wiki) create a distance matrix between every stream of bits/bytes, and from there, the clustering should be straight forward.
The Problem is, that this method is very sensitive to noise, etc, but it is worth a shot...
Not sure if this will help someone. Java JDK already provides AudioSystem class.
I used this as part of my tests to check generated WAV properties,
AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(new File(response.get()));
assertEquals(1, audioFileFormat.getFormat().getChannels());
assertEquals(8000.0, audioFileFormat.getFormat().getSampleRate(), 0.0f);
assertEquals(8, audioFileFormat.getFormat().getSampleSizeInBits());
Suppose I have a sound file that I wish to play in this manner:
Start from the 0:00 mark.
When the sound file ends, replay from the 0:30.00 mark.
Essentially, I wish for the song to play from 0:00 to 0:30, then loop from 0:30 to the end.
How do you do this?
Usually, when playing Audio Files, I use AudioClip.
AudioClip audio = Applet.newAudioClip( this.getClass().getResource( "..." ) );
audio.loop();
It's because of bad experiences with loading the audio file when the project is packed into a .jar file. If the method you recommend uses another class, I would still be more than happy to listen. However, if you've encountered the same problem as I have and know how to fix it, please do tell me.
You might use a Clip for this, or if the clip is too large, feed the bytes directly to a SourceDataLine.
So, I went over the Java's sound tutorial and I did not find it all so helpful.
Anyways, what I understood from the tutorial for recording sound from a mic is this:
Although they do show how to get a target data line and so on, they do not tell how you can actually record sound [or maybe I didn't get it all well].
My understanding so far has been this:
Mixer can be your sound card or sound software drivers that can be used to process the sound, whether input or output
TargetDataLine is used when you want to output your sound into the computer. Like save it to the disk
Port is where your external devices like mic, etc are connected
Problems that remain
How do I select the proper mixer? Java's tut says that you get all the available mixers and query each one to see if it has what you want. That's quite vague for a beginner
How do I get the port on which my integrated mic is? Specifically, how do I get input from it into the mixer?
How do I output this to the disk?
Using the AudioSystem.getTargetDataLine(AudioFormat format) method you will get
... a target data line that can be used for recording audio data in the format specified by the AudioFormat object. The returned line will be provided by the default system mixer, or, if not possible, by any other mixer installed in the system that supports a matching TargetDataLine object.
See the accepted answer for Java Sound API - capturing microphone for an example of this.
If you want more control of which data line to use you can enumerate all the mixers and the data lines they support and pick the one you want. Here is some more information regarding how you would go about doing that: Java - recording from mixer
Once you've obtained the TargetDataLine you should open() it, and then call read() repeatedly to obtain data from that data line. The byte[] that you fill up with data with each call to read() can be written to disk e.g. through a FileOutputStream.
I need to extract the audio stream from a movie and eventually convert it to a certain format, let's say MP3 at 192 kbps, for later processing, more exactly to detect the voices.
Are there any libraries for extracting the audio stream from a movie?
you could use mplayer to extract audio to a seperate file:
mplayer yourmovie.mov -vo null -vc null -ao pcm:fast
http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.html
Assuming you have a DVD it can be as simple as stripping the audio track, which you can do with various bits of DVD ripping software.
It it has a standard Dolby Digital audio track (up to 48kHz 16bit) or possibly TrueHD in the case of Blu-Ray (up to 96kHz, 24bit) you should be able to decode either using either mplayer or ffmpeg.
If it's Java you're interested in check out FMJ:
http://fmj-sf.net/index.php
This provides a Java wrapper to ffmpeg.
I recorded some audio files that must be played from java. I did it about half-year ago. Now, when I add files, they sound as if being sped-up with higher pitch. Old files sound normally, new ones don't. I suppose there is something that has to be changed in audio parameters. What could it be?
That's the code I'm using to play .wav files:
AudioInputStream result1 = AudioSystem.getAudioInputStream(new File("/home/nikkka/Desktop/alphabet/result.wav"));
DataLine.Info info = new DataLine.Info(Clip.class, result1.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(result1);
clip.start();
At the risk of stating the obvious ...
If the old file still play fine and the new ones don't, then it must be something different about the way that you recorded the new files.
It doesn't sound like the real problem is anything to do with programming, let alone Java.
of course. but the thing is, i recorded the old files with settings i don't actually remember.
My suggestion is to fiddle with the settings until you can once again record files that play properly.