Java sound: which is more efficient - WAV or MIDI - java

I have a Java application that needs to play a few distinct 'sounds/riffs' to indicate status. I would like to know whether it is better to record these as audio files (wav or whatever format) and play them back using the Java audio classes, or whether it would be better to store MIDI data and play them using the Java MIDI classes.
In my case, storage space is not a problem (within reason). I have about 5-8 status that I would like to play different melodies for. Each melody would be 1-3 seconds, consisting of 2-8 notes.

PCM (found within your WAV file) and MIDI are tools for entirely different jobs.
PCM is a way to encode audio, the sound itself. MIDI is a way to encode messages for controlling synthesizes... note on, note off, etc.
If you're playing back music and you don't particularly need high control over what it sounds like (as each system's MIDI synth can sound different), MIDI is an efficient way to encode it. If you need good quality instruments, vocals, etc., you need an actual sound format like PCM in WAV, MP3, AAC, etc.

Related

Using JLayer (AKA JavaLayer) to analyze mp3 files in java?

I would like to perform a FFT on frames of an MP3 file using Java (think spectrum analyzer). I found JLayer which seems to fit the requirement of MP3 Decoding, but I'm not sure how to use it (Most examples are simply players that use the higher level helper, but that's not what I am looking for). FFT seems easy compared to decoding MP3 files ;)
My question is basically this: How would I take an MP3 file in java, and decode it to raw audio data for analysis in Java using JLayer
I am on the same Boat - trying to decode and analyze MP3 files using Java. You may want to check out MP3 SPI from the same author. There is a good example of getting the raw decoded PCM data from an MP3 file in his page:
http://www.javazoom.net/mp3spi/documents.html
Good luck,
Uri

Video converting into mp3 on Java, Android

I need to convert mp4/flv files info mp3 in my Android application, but I don't know C/C++ and Android NDK. Do you know libraries/methods for easy converting on Java? Thank you for anyway.
Your question is how to extract audio from MP4/ FLV files and save as mp3 file. Right ?
Then, very sorry, Android SDK does not provide any API for transformating or track extraction.
Also using available media framework to achieve the same is also not trivial (and even if you do, you will lose portability).
What I would suggest is to use your MP4 & FLV Parser to extract audio track, do transcoding (if audio track is non-mp3), and save the transcoded (if audio track extracted is mp3, then extracted data) data.
Or you can port FFMPEG code base and use the same. This again may be overkill for your small task.
Suppose you just want to extract mp3 track from MP4, then you understand the native mp4 parser and use the APIs for extraction. You may have to replicate some code from stagefright / opencore.
Shash
it's probably irrelevant for you anymore but if some one still need a mp4 to mp3 parser here's an api that can do the job

WAV to Speex conversion taking a lot of time

I am using the JSpeex API to convert a .wav file into .spx file. Everything goes perfect when tested on desktop; it took only 2 seconds.
Android developer used the same code but it took around 3 minutes to encode the same file on their simulator & phone. Is there any way to reduce this time for encoding? Code used to convert is as follows:
new JSpeexEnc().encode(new File("source.wav"), new File("dest.spx"));
Compression takes time. The better the compression, the longer it takes, and Speex is pretty good compression.
2 seconds of desktop computer time is absolutely ages.
JSpeex is a java implementation. Use a native implementation, ideally use the platform codecs, instead.
On phones, speech is best compressed using AMR - not necessarily the best quality/compression, but most likely hardware accelerated since its the format used by GSM. You can usually get AMR straight from the microphone.
How do you get large WAV files onto an Android device in the first place? If its actually the output of the microphone, consider using AMR as outlined above.
If you need Speex and you have a wav file, then consider sending it to a server for compression.

Display sound (.wav/.mp3) as graph in Java

I need to display a graph of a sound file - i.e. a wave form as displayed by audio editors such as Audacity.
How should I go about this? Is there anything in the Java Sound API that can help me?
In Java Sound API poke around AudioSystem.getAudioInputStream
which will return AudioInputStream which shows you how to open audio files form within Java. It is good enough to read .wav files, but not quite so for mp3s.
It lets you read bytes one by one directly. Also getAudioFormat will return object that has sample rate, bit/sample, etc. After reading your waveform you can draw it in your favorite way on the screen (Swing, AWT, png file on the web, etc.).
In the end I found code to do exactly what I wanted in Chapter 10 of the book Swing Hacks.

Playing small sounds in Java game

For the computer game I'm making, I obviously want to play sound. So far, I've been using AudioClip to play WAV files. While this approach works fine, the WAV files tend to be gigantic. A few seconds of sound end up being hundreds of kB. I'm faced with having a game download that's 95% audio!
The obvious option here would be to use MP3 or Ogg Vorbis. But I've had limited success with this - I can play MP3 using JLayer (but it plays in the same thread). As for Ogg, I've had no luck at all. Worse, JLayer's legal status is a bit on the dubious side.
So my question is to both Java developers and generally people who actually know something about sound: What do I do? Can I somehow "trim the fat" off my WAVs? Is there some way of playing Ogg in Java? Is there some other sound format I should use instead?
You could use JOrbis library to play back OGG music. For working sample usage you can look at these files here.
I also experimented with some lossless audio compression, for example I had a 16 bit mono sound. I separated the upper and lower bytes of each sample and put them after each other. Then I applied a differential replacement where each byte is replaced by its difference from the last byte. And finally used GZIP to compress the data. I was able to reduce the data size to 50-60% of the original size. Unfortunately this was not enough so I turned to the OGG format.
One thing I noticed with 8 bit music is that if I change the audio volume, the playback becomes very noisy. I solved this problem by upsampling the audio data to 16 bit right before the SourceDataLine.write().
These may be outdated, but they are officially recognized by the Xiph.org team (who maintain Ogg and Vorbis, among others).
http://www.vorbis.com/software/#java
The problem you describe is addressed by the Service Provider Interface (SPI) for sound in Java. The result is that simply adding JAR files to your classpath will add functionality to the default Java Sound API. Thus enabling the handling of more sound formats without changing code.
Last time I tried this the Javazoom people offered a working MP3 SPI JAR. Which was based on the JLayer you mentioned.
For Vorbis OGG there now also seems to be an SPI library. Check out the docs on the Vorbis SPI on Javazoom.
If you decide to stay with wav format...
It is probably not very important to have high quality sound files. You can use your favorite wav editor to lower the bit rate, switch to mono, or both. That will save tons of space and you won't notice the difference in quality while playing the game.
Hope this helps.

Categories

Resources