Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Sound in Java  (Read 4415 times)
0 Members and 1 Guest are viewing this topic.
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Posted 2011-04-28 02:51:38 »

Hi all,

I have looked around for information on this subject and it seems to be...

a.Outdated.
b.Confusing and ill explained.

If someone could please give me a loose explanation on this subject orjust point me in the right direction of reading materials I would be grateful.


Christopher.

Offline CyanPrime
« Reply #1 - Posted 2011-04-28 03:45:24 »

Look into slick2d, or slick-util. Both do sound very easily.
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #2 - Posted 2011-04-28 07:36:42 »

Look into slick2d, or slick-util. Both do sound very easily.

These are awesome. They also in turn led me to Tiled...
http://www.mapeditor.org/

There goes another 10,000 hours of my life.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #3 - Posted 2011-04-28 08:50:21 »

Paul Lamb Sound Library is just fine Smiley

Offline philfrei
« Reply #4 - Posted 2011-04-28 09:45:08 »

Yeah, I am on my third pass through the Java Tutorials section on Sound, just the ".sampled" section. I think I'm starting to make some headway. http://download.oracle.com/javase/tutorial/sound/index.html

If you do decide to try writing raw Java audio, I recommend making sure you give the section "Using Files and Format Converters" a look. For no apparent reason, this section has the best example code. Right where you expect it! Several sections AFTER where it should be! Cranky

These guys have some useful information posted:
http://jsresources.org/
There's a pretty great FAQ section.

StackOverFlow has some interesting java sound questions come up now and then. So does Java-Gaming.org!http://www.java-gaming.org/boards/java-sound-openal/16/view.html

Java Sound is quite powerful, actually. But with power comes responsibility...you need to be pretty solid with threads & file i/o, for starters.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #5 - Posted 2011-04-28 09:54:56 »

Yeah, I am on my third pass through the Java Tutorials section on Sound, just the ".sampled" section. I think I'm starting to make some headway. http://download.oracle.com/javase/tutorial/sound/index.html

If you do decide to try writing raw Java audio, I recommend making sure you give the section "Using Files and Format Converters" a look. For no apparent reason, this section has the best example code. Right where you expect it! Several sections AFTER where it should be! Cranky

These guys have some useful information posted:
http://jsresources.org/
There's a pretty great FAQ section.

StackOverFlow has some interesting java sound questions come up now and then. So does Java-Gaming.org!http://www.java-gaming.org/boards/java-sound-openal/16/view.html

Java Sound is quite powerful, actually. But with power comes responsibility...you need to be pretty solid with threads & file i/o, for starters.

I knew you would be one of the few keen to talk audio!

You have had success playing .ogg files yes?

I just want to start there...
Create a class, choose a file, play it... speakers start moving.

Then I can hopefully start working through the more complex stuff.

Offline philfrei
« Reply #6 - Posted 2011-04-28 20:49:22 »

Dragging my feet. No excuses.
Best ogg-related tutorial I found:
http://www.jcraft.com/jorbis/tutorial/Tutorial.html
If you search, you will find examples of playing back a .wav in java.

I don't know that there is a "just do it" way with Java Sound. It is powerful, designed more for tool-making than "simple" playback for application-level developers. Also, it is free so there is less support than there would be if you forked over $500 for a solution.

You will have to either dive in or go for one of the other solutions offerred. Do look at the code in the section on format conversions. It seems illogical that there would be the most concrete code examples, under a heading that sounds complex, but they are truly helpful.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-04-29 01:48:44 »

You could go two ways here.
1. Easy way, just play the sound straight through or loop. No control over where to start, pause, or volume:
1  
2  
3  
4  
java.applet.AudioClip clip = java.applet.Applet.newAudioClip(new URL("url/to/file.wav"));
clip.play();
clip.loop();
clip.stop();


2. The complicated but fast to learn way, javax.sound.sampled package:
1  
2  
3  
4  
5  
6  
7  
8  
9  
AudioInputStream audio = AudioSystem.getAudioInputStream(new URL("url/to/file.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
clip.loop(5);
clip.stop();
clip.setMicrosecondPosition(123456789L);
clip.start();
//etc...

Of course there are many different overloaded methods in AudioSystem and other classes that allow complete control over the output.

Hope that helps Smiley

Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #8 - Posted 2011-04-30 03:45:10 »

Ok,

I finally got some audio playback from java!
It was a tearful moment to say the least.

My problem was I was trying to play back a 24bit file. Once i tried playback with a 16bit It returned some audio.
The audio I am hearing however sounds like it is being bit crushed down to 8bit audio... Is there a a way to set bitrate playback?

This is also only with AIF and WAV files. I have still had no luck getting OGG playback, the program still crashes as I was experiencing before.

Thanks in advance.
Christopher.


Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #9 - Posted 2011-04-30 03:57:27 »

Hmmm actually...

It doesnt sound bitcrushed it sounds plain shit. Perhaps a streaming problem... I am missing something...

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Scarzzurs
« Reply #10 - Posted 2011-04-30 10:01:03 »

I gave up on Java Audio after reliably getting segmentation faults when following the API (and apparently the bug I experienced, was fixed many version ago).

I've been using LWJGL for audio since, with little to no problems. It might take some time to learn, but should be worth it.
I use VorbisSPI for ogg-vorbis support, which adds around 25 lines of extra code (compared to using just wave).

- Scarzzurs

My games and Projects:
BlastingPixels.com,
Old website
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #11 - Posted 2011-04-30 23:00:54 »

I gave up on Java Audio after reliably getting segmentation faults when following the API (and apparently the bug I experienced, was fixed many version ago).

I've been using LWJGL for audio since, with little to no problems. It might take some time to learn, but should be worth it.
I use VorbisSPI for ogg-vorbis support, which adds around 25 lines of extra code (compared to using just wave).

- Scarzzurs

I have been using Slick2d which apparently makes the code as simple as...

Sound fx = null;
fx = new Sound("testdata/testloop.ogg");
fx.loop();

this does achieve playback, albeit, the horribly distorted one...
Does lwjgl have a wiki with decent tutorials like slick... at this point I just want to get normal audio coming out my speakers as I am concerned I am missing something...

Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #12 - Posted 2011-05-01 00:19:17 »

FIXED

If you would like to have streaming audio with only a few commands I recommend reading the following code/guide...
http://lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_2_-_Loading_Sounds_for_LWJGL

In order for this code to run you will need the following in your build path,

lwjgl.jar
slick.jar
jogg(current version number).jar
jorbis(current version number).jar

These can be downloaded here for lwjgl,
http://lwjgl.org/download.php

and here for Slick and the other Jars,
http://slick.cokeandcode.com/

For information on how to add these to your build path see here...
http://www.cs.bsu.edu/homepages/pvg/misc/slick_eclipse_tutorial.php

Just do the same for the jogg, jorbis jars, which will be located in the slick2d libs folder.

Ogg files are recommended because of the their minimal file size yet quality compression, a good converter for mac can be found here...
http://download.cnet.com/Switch-Audio-Converter/3000-2140_4-10703967.html

Hopefully this helps someone avoid the pain of java sound.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-05-01 03:29:31 »

Java Sound isn't that painful. It's powerful and useful, no need for external libraries.

Offline philfrei
« Reply #14 - Posted 2011-05-02 04:54:33 »

Hi Christopher - I appreciate the references you listed! Will check them out at some point, hopefully sooner than later.

My experience with .wav's is limited. I can export my sounds to .wav's via Cakewalk's "Sonar" app. But they only play back, on my computer, via Windows built-in media player, if the sample size is 16 bits. So it doesn't surprise me if you had 24-bit encoding and were unable to do playback.

Stuff that I have exported to 16-bit wavs plays back with great fidelity. I'm curious if you ever looked at the section on converting formats as I suggested. There is info there about how to set files from one type to another. Also, to query the "AudioSystem" to see if you have an output line that supports the type of sound file you are hoping to play back. Did you try this approach at all? Just curious.

I just wrote a routine that converts wav data, stereo 16-bit, 44100, little endian byte arrays to float arrays (-1.0 to 0.9999). Next step, to convert them back for playback...

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #15 - Posted 2011-05-02 11:53:06 »

Java Sound isn't that painful. It's powerful and useful, no need for external libraries.

Don't worry I'm just being dramatic. Like all noobies i want less code more action. I was so frustrated the other day that if the Java Sound API were a person i would have kicked them in the teeth.

Turns out I wasn't doing a simple check to see if the music was playing before making the request. This sends numerous continuous requests to stream the sound file and as a result creates the illusion of poor quality audio.

1  
2  
3  
if(!music.playing()){
           music.play();
      }


Noobie mistake.

Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #16 - Posted 2011-05-02 12:00:17 »

Hi Christopher - I appreciate the references you listed! Will check them out at some point, hopefully sooner than later.

My experience with .wav's is limited. I can export my sounds to .wav's via Cakewalk's "Sonar" app. But they only play back, on my computer, via Windows built-in media player, if the sample size is 16 bits. So it doesn't surprise me if you had 24-bit encoding and were unable to do playback.

Stuff that I have exported to 16-bit wavs plays back with great fidelity. I'm curious if you ever looked at the section on converting formats as I suggested. There is info there about how to set files from one type to another. Also, to query the "AudioSystem" to see if you have an output line that supports the type of sound file you are hoping to play back. Did you try this approach at all? Just curious.

I just wrote a routine that converts wav data, stereo 16-bit, 44100, little endian byte arrays to float arrays (-1.0 to 0.9999). Next step, to convert them back for playback...

I went over this again just now and It made a lot more sense this time around. My silence filled speakers were a bit a distraction when i passed over it before. Strange how this section is several sections down when it is the most relevant information. I already had a basic understanding that this is how java handled audio based on working with DAW,s. I definitely want to go down the path you have taken too with converting to code.

Apparently this is even more compact and quicker to load?
Also you will be able to protect your game music this way yes? otherwise the user will have a nice fat data directory full of your handcrafted compositions...?

 

Offline philfrei
« Reply #17 - Posted 2011-05-02 21:11:22 »

Thanks for the reply! Agreed, I've had to take multiple passes on the Java Audio documentation and I really don't understand why the best example code is so far back.

The point of using a floats array is that this is a good data representation for DSP. I think it is pretty much standard, like using unit vectors. I don't know that anything is saved in terms of memory or RAM or speed. A single float requires several bytes, and you are still using 44100 frames per second, presumably. A friend has a "stretching" algorithm that I want to try, but it assumes a float array.

(My task today, if I get time this evening, is to take my float array and convert it back to a wav for playback. Have to make a .wav FileFormat, attach it to a SourceDataLine that supports wav playback, yada yada. We'll get to the stretching part later.)

About protecting composition, I'm not sure what to say. If you have a good ear, you can pretty much pick apart (transcribe) anything. And of course, you can direct a sound stream into a DAW. With a book, every word is there. With a painting or film, all the visuals are right there in the open. Software is also possible to decompile, as witnessed by several threads on this site, including a rather humorous one in which one of my own bright attempts at a Captcha Applet was decompiled. (Within hours of posting, Markus Persson & Dzzd had pretty much shredded it to pieces.)
http://www.java-gaming.org/topics/use-of-an-applet-for-captcha/23389/view.html

If you embed your mp3's or ogg's or wav's or whatever as a resource, I'm sure it can be decompiled. But what else is there? It's too slow/unreliable to stream (except for maybe some very special cases where it could work...), so audio content should be delivered with the game, I think. I suppose one could make some sort of scrambler and include that as a layer. But the code for the descrambling would also be easy to decompile and redo. Costs/benefits.

Another thought: I'll put off worry about theft until it becomes apparent someone likes something enough to actually steal it.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #18 - Posted 2011-05-03 00:19:10 »

Thanks for the reply! Agreed, I've had to take multiple passes on the Java Audio documentation and I really don't understand why the best example code is so far back.

The point of using a floats array is that this is a good data representation for DSP. I think it is pretty much standard, like using unit vectors. I don't know that anything is saved in terms of memory or RAM or speed. A single float requires several bytes, and you are still using 44100 frames per second, presumably. A friend has a "stretching" algorithm that I want to try, but it assumes a float array.

(My task today, if I get time this evening, is to take my float array and convert it back to a wav for playback. Have to make a .wav FileFormat, attach it to a SourceDataLine that supports wav playback, yada yada. We'll get to the stretching part later.)

About protecting composition, I'm not sure what to say. If you have a good ear, you can pretty much pick apart (transcribe) anything. And of course, you can direct a sound stream into a DAW. With a book, every word is there. With a painting or film, all the visuals are right there in the open. Software is also possible to decompile, as witnessed by several threads on this site, including a rather humorous one in which one of my own bright attempts at a Captcha Applet was decompiled. (Within hours of posting, Markus Persson & Dzzd had pretty much shredded it to pieces.)
http://www.java-gaming.org/topics/use-of-an-applet-for-captcha/23389/view.html

If you embed your mp3's or ogg's or wav's or whatever as a resource, I'm sure it can be decompiled. But what else is there? It's too slow/unreliable to stream (except for maybe some very special cases where it could work...), so audio content should be delivered with the game, I think. I suppose one could make some sort of scrambler and include that as a layer. But the code for the descrambling would also be easy to decompile and redo. Costs/benefits.

Another thought: I'll put off worry about theft until it becomes apparent someone likes something enough to actually steal it.

Wow! have to let me know if you get playback happening and whether the stretching algorithm works. The one in ableton live blows my mind. It auto warps loops to your current tempo and then you can just drag and stretch transients as if they were physically tangible. This in combination with stretching methods such as beat preservation, pitch preservation etc.

The example you posted with the captcha gave me a good laugh, thanks for sharing.

If someone can build it, someone can un-build it. Its not that I want to keep the entire world out of the files and music I write, just the general population. Even if you were a paying customer, if a game you downloaded came with a folder labeled "soundtrack" it would be hard to justify buying a soundtrack for that game if it were offered as a product. I mean not that much is to garnered from online music sales these days.

All this assumes however, that users would like my music so much that they would want to steal/hack/download it...

"Don't worry about people stealing an idea. If it's original, you'll have to jam it down their throats." -- Howard Aiken   

Offline philfrei
« Reply #19 - Posted 2011-05-03 05:29:49 »

Ableton sounds like a fantastic tool.

Meanwhile, back at the cave site for recreating stone wheels, we have achieved playback of a wav previously loaded into an array of floats. Nothing more, as of yet.

Edit: added a "play backwards" button. It can now load wave files and play them backwards. Fidelity is pretty fine, as far as I can tell. I've got OK studio monitors.

Spent too much time tracking down a stoopid bug in a for loop (I'm not used to decrementing). Tongue

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #20 - Posted 2011-05-03 06:51:29 »

Ableton sounds like a fantastic tool.

Meanwhile, back at the cave site for recreating stone wheels, we have achieved playback of a wav previously loaded into an array of floats. Nothing more, as of yet.

Edit: added a "play backwards" button. It can now load wave files and play them backwards. Fidelity is pretty fine, as far as I can tell. I've got OK studio monitors.

Spent too much time tracking down a stoopid bug in a for loop (I'm not used to decrementing). Tongue

Congrats!

Putting in my pre-order now for the philfrei "play wavs backwards" vst now  Wink

Offline philfrei
« Reply #21 - Posted 2011-05-08 04:52:10 »

Got some crude stretching effects to work. It sounds pretty good. The GUI is crude. Linear interpolation works! Grin

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #22 - Posted 2011-05-08 07:15:43 »

Got some crude stretching effects to work. It sounds pretty good. The GUI is crude. Linear interpolation works! Grin

Nice work!

Send us a link when you have something substantial.

Offline philfrei
« Reply #23 - Posted 2011-05-08 22:29:17 »

http://www.hexara.com/ClipScratcher.jar
You can download the above jar. It's only 24kb, and should have the source code embedded. It is misnamed, as it is hard to manipulate it to get a true "scratch" effect that one can do with a turntable. But I have to admit, I'm not interested in pursuing that aspect. There are already quite a few excellent tools out there that can do that sort of thing. (I want to get on with implementing JOrbis, etc.)

To run, click on the jar file. This should give you the following GUI:
Load a wav via the dropdown "File" menu on the top left. Once a wav is loaded, when you mouse over the rectangle, the green label will display your mouse's X-position, in terms of seconds. The rectangle's x-length is set to correspond to the duration of the wav.
"Play" button will play the entire wav normally. "Yalp" button will play the entire wav in reverse. Multiple clicks of either will launch overlapping sounds. There is no way to stop a sound.

Click inside the rectangle and an orange cursor will appear. It's location in seconds is displayed in the blue box. When you hold the mousedown while there is a cursor, the cursor will move towards the x-location of the mouse. When you release the mousedown, the cursor disappears and the sound stops. The next click will place a new cursor.

I have no claims that the code is optimal or totally clean (for example, it is possible to run a sound past the beginning or end and get an exception), am just pleased that it works, doing (a) translation of wav data to float & back to wav for playback, (b) linear interpolation, allowing bidirectional speedup/slowdown. But I think it helps demonstrate that javax.audio.sampled is capable of a lot more! Grin



"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #24 - Posted 2011-05-08 23:59:59 »

All clips I played were very scratchy. Plus I was confused on what to do Tongue

Offline philfrei
« Reply #25 - Posted 2011-05-09 01:34:46 »

All clips I played were very scratchy. Plus I was confused on what to do Tongue
Dang. Well that is disappointing. I mean about the scratchiness. I know the GUI is bogus.

I'm not sure about how to figure this out. Maybe you can try loading this sound:
http://www.hexara.com/Audio/e4.wav
I KNOW that it plays cleanly on my system. It could be that your source sounds are of a different flavor of .wav than the specific one that I have encoded.

Also, if you are willing, you could post an example of a .wav that was scratchy, I could try it out at my end.
For the GUI: first downmouse in the rectangle places a cursor, first release does nothing, second downmouse starts playback, the rate being determined by the x-distance between the mouse and the cursor (farther away makes it faster, closer makes it slower) and the second release ends playback and removes the cursor.

Thanks for giving it a try!

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #26 - Posted 2011-05-09 03:00:02 »

This .wav file played fine for me. Maybe the one I used wasn't quite "clean" Tongue

Offline philfrei
« Reply #27 - Posted 2011-05-09 03:49:07 »

That is a relief!

There are lots of possibilities. It could be a 24-bit encoding rather than 16-bit. That is the most common thing that happens. 24-bit is of a higher quality, but 16-bit is more common, and more commonly supported.

I think one of the reasons that sound programs get so large is that there are so many different possible encodings and codecs to support.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #28 - Posted 2011-05-09 04:28:10 »

It doesn't say what encoding it is but I bet it's 24-bit.

Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #29 - Posted 2011-05-09 08:06:17 »

Well done,

This worked well for me. I tried it with a few different loops before it tried it with one of my ambient compositions hitting play and yalp a bunch of times at varying intervals to create a swirling morphing sound, you have not set a boolean on is music playing(intentional or not yet implemented) but it was perfect for ambient weirdness.  Tongue

Pages: [1] 2
  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.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

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

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

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

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

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

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

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

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

Norakomi (42 views)
2014-08-06 19:49:38
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!