Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / Java Sound & OpenAL / Re: Reading sound from a stream on: 2006-01-05 00:35:46
Never mind. I figured it out. If I know my sample rate, sample size in bits,channels, whether it is signed, and whether it is bigEndian, I can use this constructor:

AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian) 

to create an AudioFormat object. For example:

 
 AudioFormat af = new AudioFormat(11025, 8, 1, false, false); // an 8-bit, mono PCM (like a WAV) at 11025khz

With that object, I can create a DataLine.Info object:

  DataLine.Info info = new DataLine.Info(Clip.class, af, 1024); // not sure what the buffer is for yet, but 1024 seems fine for now

then create a Line as a Clip, open it, and start it playing:

  Clip c = (Clip) AudioSystem.getLine(info);
  c.open(af, b, 0, b.length);
  c.start();

Lastly, I remove the header from the byte array so the click goes away.

int headerSize = 64; // not actual size. Brute force, actually. :-)
for (int i=0; i<headerSize; i++) {
b = -126; // signed byte of 1; 1 seems to work better than 0 for some reason?
}


So, if I know exactly what format my sounds are in, I can play them without the click. (Interestingly, loop() still has a click, so I guess I would write a sound player with a loop() command that finds the sound's duration and just does start() again.)

I hope this is useful for somebody else who wants to play streamed sounds in an applet without a click.

Now to search for (or implement!) the equivalent of AudioFileStream.getFormat(), so the sound file's format doesn't have to be known beforehand... if anybody has a suggestion, I'd love to hear it.

2  Java Game APIs & Engines / Java Sound & OpenAL / Reading sound from a stream on: 2006-01-04 20:16:08
I have a problem... I would like to play sounds that are in a different JAR or ZIP from an applet. At the moment, I load them using the "jar:[url]!/[sound]" technique. It works fine.

However, I would like to have progress bars that keep track of these sounds. I currently have a working progress bar that uses a URLConnection object and a ZipInputStream so I can compare the incoming bytes to the expected bytes. It can return a file's contents as a byte[]. However, when I try to construct a sound with this code:

// 'input' is the object that reads a file from a stream
byte[] b = input.getBytes("mysound.wav"); /* returns byte[] of the expected length, so I'm pretty sure that there's no extra or missing data */
play(b);

public void play(byte[] b) {
   AudioData ad = new AudioData(b);
   AudioDataStream = new AudioDataStream(ad);
   AudioPlayer.player.start(ads);
}


the AudioPlayer plays the sound with a slight 'click' at the beginning, like it's playing the sound's header, and if I use a ContinuousDataStream, it plays before each loop of the sound.  If I strip the header from the byte[] before calling play(), I hear a corrupted version of the sound (seems reasonable, since AudioData can't figure out what the sound's structure if I strip the header...).

Is there a better way to convert a byte[] into a sound?

It seems (I haven't tried this yet; I get confused by the documentation) that I could use the AudioInputStream and Line object to do this, but I get lost in all of the options and sound engineering jargon.
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (14 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!