Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Best way to play audio in java  (Read 924 times)
0 Members and 1 Guest are viewing this topic.
Offline brollysan

Junior Member


Medals: 1



« Posted 2013-04-09 14:20:35 »

What is the best way of playing audio (multiple, realtime) in a java game only using the native libraries? Mp3 is not supported so compressed files are out the window and mostly .wav used, this leads to huge jars and files I don't want to load into memory so Clip is also not an option.

I am currently using a dataline to read into a buffer and play the audio but reading from a buffer requires a loop, this loop means I can only play one audiofile in the same thread as else the audios won't be "realtime". By this I mean that if I force the loop to go through multiple buffers, in the case that 100 sounds must be played, the 100th sound won't play untill all the others have played, this will be very noticeable in such a case.

The solutions?

  • JavaFX- http://stackoverflow.com/questions/6045384/playing-mp3-and-wav-in-java
    but this requires the JavaFX library and I want to use as much native code as possible (for learning) and I THINK javaFX spawns a new thread on media.play
  • Use clip, this means that if I use .wav I might have to load hundreds of megs into memory, BAD choice, this also assumes that Clip.play() spawns a new thread
  • Use an executorservice, set a max limit of concurrent audioplaying (like 20) and every time a play is requested, send the bufferedaudiostream into a new thread to the executor and play

I may have to go with the third option but I am afraid of threadoverheads and the cost of recreating the same dataline over and over again but if I limit the number of datalines I can't for instance implement the effect of ambient sound where you can hear multiple crickets. Here is the source for reading from a buffer, might be of some interest to some.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public void playSound() {
      SourceDataLine line;
      int audioBufferByteSize = 1024;
      long streamLengthBytes;
      try {

         // URL url = getClass().getResource("forest.wav");
        // AudioInputStream inputStream =
        // AudioSystem.getAudioInputStream(url);
        InputStream iStream = getClass().getResourceAsStream("forest.wav");
         InputStream buffered = new BufferedInputStream(iStream, 1);
         AudioInputStream aStream = AudioSystem
               .getAudioInputStream(buffered);
         AudioFormat audioFormat = aStream.getFormat();
         DataLine.Info info = new DataLine.Info(SourceDataLine.class,
               audioFormat, audioBufferByteSize);
         line = (SourceDataLine) AudioSystem.getLine(info);
         line.open(audioFormat);
         streamLengthBytes = audioFormat.getFrameSize()
               * aStream.getFrameLength();
         aStream.mark((int) streamLengthBytes);


         while (true) {
            aStream.reset();
            line.start();
            int nBytesRead = 0;
            byte[] sampledData = new byte[audioBufferByteSize];
            while (nBytesRead != -1) {
               nBytesRead = aStream.read(sampledData, 0,
                     sampledData.length);
               if (nBytesRead >= 0) {
                  line.write(sampledData, 0, nBytesRead);
               }
            }
            Thread.sleep(10);

         }
Offline cubus

Junior Member


Medals: 2



« Reply #1 - Posted 2013-04-09 14:35:14 »

for compressed audio http://code.google.com/p/jdogg/

create one thread for audio-playback and mix all audio streams into the SourceDataLine.
Offline nsigma
« Reply #2 - Posted 2013-04-09 16:17:48 »

http://www.java-gaming.org/topics/need-a-really-simple-library-for-playing-sounds-and-music-try-tinysound/25974/view.html

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-04-09 16:34:04 »


TinySound seems to use a bit of RAM whether you stream or not, I personally wouldn't recommend it. If you're using .mid files anywhere just use the default libraries for midi files as they work quite well.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-04-09 19:07:24 »


TinySound seems to use a bit of RAM whether you stream or not, I personally wouldn't recommend it. If you're using .mid files anywhere just use the default libraries for midi files as they work quite well.
I'll take more RAM usage over a worse library anyday.
Offline brollysan

Junior Member


Medals: 1



« Reply #5 - Posted 2013-04-09 19:36:06 »

I want to use avoid the usage of libraries entirely, except for maybe if we decide to use mp3.
Offline nsigma
« Reply #6 - Posted 2013-04-09 19:48:53 »

I want to use avoid the usage of libraries entirely, except for maybe if we decide to use mp3.

Then you could just use it as example code to answer your question. Low level audio code is difficult to get right. Primary point is to never use more than one output line, keep it open and mix all your sounds in a single thread to the output line. Ignore Clip as it uses a line per sound.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline davedes
« Reply #7 - Posted 2013-04-10 07:16:52 »

I want to use avoid the usage of libraries entirely, except for maybe if we decide to use mp3.
Why? JavaSound is a "library" itself, after all.

Quote
TinySound seems to use a bit of RAM whether you stream or not, I personally wouldn't recommend it.
A "bit of RAM" is pretty much negligible in the 21st century. If you're on a low-end device like Android or iOS, you won't be using anything built on JavaSound in the first place.

Another good option for sound is LWJGL or JOAL, or some library built on top of it.

Offline nsigma
« Reply #8 - Posted 2013-04-10 11:52:59 »

A "bit of RAM" is pretty much negligible in the 21st century.

Yes, my laptop (which is getting old  Emo ) has 4GB of RAM, which seems to be the low-end these days.  That's enough RAM (excluding swap) to store over 6 hours of stereo audio!  Storing a few minutes of audio at a time in RAM is hardly excessive.   Smiley

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!