Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  Sound conversion problem using javax.sound......  (Read 10666 times)
0 Members and 1 Guest are viewing this topic.
Offline vinodpatel2006

Junior Devvie





« Posted 2008-05-13 10:10:24 »

   
Hello,

I have a situation, i have data and source sound format. For example, if i wand to process .MP3 file then format of the data like bitRate, sample rate, channels etc. have in the one object (You can consider that object as like simple object containing getter/setter for all properties). And, all related data in another object/file (Like in byte array). I have both format and data separate not in same file like normal .MP3 file.

Know i want to convert that data in to PCM format so that i can play using the javax.sound..... package classes....

If any one have idea then its most welcome.
Any help would be appreciated.....

Thanks In Advance
Offline vinodpatel2006

Junior Devvie





« Reply #1 - Posted 2008-05-17 04:59:04 »

Hey, There is no solution for this problem...?
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #2 - Posted 2008-05-17 11:11:51 »

JLayer is an MP3 decoder in java so maybe you can use that.
I'm not completely understanding why or how you have your data separated from the format data, but if your separated files are still based on mp3 you might have to adapt JLayer to your needs (it's open source). Or roll your own...

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline vinodpatel2006

Junior Devvie





« Reply #3 - Posted 2008-05-19 05:09:24 »

Hello,

      I have one application that originally written in to the C++, that application access the video data from our hand written server. That server work like RTP server. In abstract, server first sends the audio data format (It may be in any format like PCM OR compressed using any of the technique), then sends the audio data in given format. The client application convert that audio data in to PCM if it is not in that format and plays it.

     Know we are converting the client application in to the Java, server application is same, not changed. Hence, we have to achieve same functionality in Java.

 By default windows has in built  API/Functions to convert a different sound format in to PCM format.

 But, how i can do that in java.....?


Thanks and Regards
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #4 - Posted 2008-05-19 12:32:16 »

You might want to read about Java Sound SPI.
Per default, JavaSound doesn't support many formats, so you need extensions in the form of an 'SPI' (Service Provider Interface) to get support for additional formats like MP3.

http://java.sun.com/docs/books/tutorial/sound/
http://www.javaworld.com/javaworld/jw-11-2000/jw-1103-mp3.html
http://www.javazoom.net/mp3spi/mp3spi.html

Offline vinodpatel2006

Junior Devvie





« Reply #5 - Posted 2008-05-20 05:25:04 »

Hello erikd,

       Thanks for suggestion....
       
      Following is a audio format class which i am getting from the server. That represents the format of the data.

                       
                       WaveFormatEX waveFormat = new WaveFormatEX();
         waveFormat.wBitsPerSample = 16;
         waveFormat.nBlockAlign = 1;
         waveFormat.nAvgBytesPerSec = 2000;
         waveFormat.nSamplesPerSec = 11025;
         waveFormat.wFormatTag = 85;    /// What is this ?
         waveFormat.nChannels = 1;      
         waveFormat.cbSize = 12;    // What is this ?
         waveFormat.exData = new byte[12];


Above is one of the data format i am getting.  The sound is some how compressed.
The original file is .wav but when i opened its property it shows Audio Format - MPEG LAYER - 3

Now from above information i want to create javax.sound.sampled.AudioFormat object.

Do you have any suggesting for that...?

Thanks and Regards
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #6 - Posted 2008-05-20 08:52:56 »

Ok, I'm beginning to understand your problem now.

WAVEFORMATEX is a Windows specific struct which describes an audio stream.
wFormatTag is an enumeration of the audio format, which can be things like PCM, MP3, whatever. The value 85 probably means MP3 in your case.
cbSize is the size of exData, which stores more properties about wFormatTag.

Read more about it here:
http://msdn.microsoft.com/en-us/library/ms789667.aspx
http://dn.codegear.com/fi/print/21300

The problem is that this is Windows specific data, so there's no standard API in java to handle it.
If I were you, I'd first assume MP3 and just try to use JLayer to play the stream associated with this WAVEFORMATEX struct. Not sure if that's going to work immediately, you might need to use some of the struct's data for example to be able to parse the stream, but I also wouldn't be at all surprised if it does Just Work in the case of MP3...

Offline vinodpatel2006

Junior Devvie





« Reply #7 - Posted 2008-05-20 09:23:05 »

Hello,

      Thanks for help...

      I have another problem, how i can mix sound balance and volume?. In Java, there are some controls like float control using that i can change volume and balance of the SourceDataLine (javax.sound.sampled). But here i have different situation. I have more than one SourceDataLine opened and all are playing individual audio file (Streams).  I have to changed volume and balance (Sterio sound) of only one stream. I mean balancing effect should come on only one stream, other stream should not get effect of balance and volume.

    If i use Java float control then it would affect all playing streams/file.

   Do you have any idea.....?


Thanks and Regards
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #8 - Posted 2008-05-20 10:45:44 »

It seems like you're using the Controls from the mixer, not the individual Lines?

Offline vinodpatel2006

Junior Devvie





« Reply #9 - Posted 2008-05-20 10:49:21 »

Hello,

         I have individual lines to play all audio files. Does control from all line behave independently...?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #10 - Posted 2008-05-20 11:49:51 »

My understanding is that you query a Line for available Controls, but a Mixer is also a Line.
So I'd guess that if you get a Control from a Mixer, you'll get a global Control for all it's lines whereas if you'd get a Control from an individual Line, it should work independently.

I'd have to check to be sure (haven't tried it in a long time), but IIRC this is how it works.

Offline vinodpatel2006

Junior Devvie





« Reply #11 - Posted 2008-05-20 12:32:26 »

Hello,

        you are correct, lines from the mixer returns individual controls and does not affects other lines.

       But the problem is that it does not set the sound and balance uniformaly, it sets step by step like jumping style.....

Regards
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #12 - Posted 2008-05-20 13:14:14 »

How are you updating the Controls? If you're updating controls using a GUI, that might be the problem, which you could solve with interpolation.

EDIT: This topic is starting to get offtopic... So were you able to play your MP3 stream in the meantime?  Smiley

Offline vinodpatel2006

Junior Devvie





« Reply #13 - Posted 2008-05-21 03:49:51 »

Hello,

 Smiley

I now able to play the MP3 stream.
Here the process what i have done to play that.

I have downloaded the MP3 SPI suggested by you and registered it.  Now, i am generating one WAV file using audio format and audio data sent by the server. I am creating a file that is in same format as windows WAV file format (http://ccrma.stanford.edu/courses/422/projects/WaveFormat/). We can play that generated file using the javax.sound classes.

Another process, if you have a audio data in the file and MP3 SPI registered, you can directly get AudioInputStream using AudioSystem.getAudioInputStream(InputStream stream) method. It automatically analyse the data and determine the format of that data.

Thanks and Regards
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #14 - Posted 2008-05-21 12:16:06 »

Good to hear!  Smiley

Offline vinodpatel2006

Junior Devvie





« Reply #15 - Posted 2008-05-21 12:34:44 »

Hello,

     Can i change a sound balance by changing a row sound data....? I means bytes of the audio input stream.

Thanks and Regards
Offline vinodpatel2006

Junior Devvie





« Reply #16 - Posted 2008-05-22 12:06:29 »

Hello,

        i got some success in changing the sound volume and balance by shifting the row bits. Here is the code snap shots

      for(int Icount=0;Icount<size;Icount+=2){
         int b =  ((buffer[Icount+1] << 8 ) | (buffer[Icount] & 0xFF)) & 0xFFFF;
         if(Jcount%2==0){
            b = (b * 0)>> 15;
            Jcount = 1;
         }
         else{
            b = (b * 32768)>> 15;
            Jcount = 0;
         }
         if(b<0){
            out.println(b);
         }
         buffer[Icount] = (byte) (b & 0xff);
         buffer[Icount+1] = (byte) ((b & 0xff00)>>8 );         
      }

This code is in loop where Icount start from 0....buffer.length...

The problem is that it generates some noise.

Can you help me for that?

Thanks and Regards
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #17 - Posted 2008-05-23 07:38:43 »

I'm not sure what this code is supposed to do, but I notice the following:

1  
b = (b * 0)>> 15;

is basically the same as
1  
b = 0;


and
1  
b = (b * 32768)>> 15;

is the same as
1  
b = b;

so it's a redundant line that's doing nothing.

What is Jcount used for?

Offline vinodpatel2006

Junior Devvie





« Reply #18 - Posted 2008-05-23 10:59:41 »


b = (b * 0)>> 15;  is for left channel, it masks all bits so that left chennel will not have sound.

b = (b * 32768)>> 15; is for right channel. If we increase multiplyer (32768) sound will high on corresponding channel.

Jcount is used just to swith between left and right channel. See.  if(Jcount%2==0){

Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #19 - Posted 2008-05-23 11:35:46 »

b = (b * 0)>> 15;  is for left channel, it masks all bits so that left chennel will not have sound.

b = (b * 32768)>> 15; is for right channel. If we increase multiplyer (32768) sound will high on corresponding channel.

Jcount is used just to swith between left and right channel. See.  if(Jcount%2==0){



What erikd is getting at, is the above loop is (unintentionally?) ~equivalent to the much simpler loop :-

1  
2  
3  
4  
5  
6  
assert Jcount==0 || Jcount==1; // because the above code example doesn't give the initial value of Jcount
for(int Icount=0;Icount<size;Icount++){
if( (lcount/2)%2==Jcount) {
   buffer[lcount] = 0;
}
}

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline vinodpatel2006

Junior Devvie





« Reply #20 - Posted 2008-05-23 12:18:31 »

This is a trial example and not the final code. Hence no improvmemt is done in the code. You can assume Jcount initially zero.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #21 - Posted 2008-05-23 13:05:18 »

1  
int b =  ((buffer[Icount+1] << 8 ) | (buffer[Icount] & 0xFF)) & 0xFFFF;


this code will probably not give the result you expect as a byte is signed in java, and is probably the cause of the noise you're hearing.

I'd advise to use Abuse's code instead, or if you really want to hang on to the code you posted, update the above line to
1  
int b =  (((buffer[Icount+1] & 0xff) << 8 ) | (buffer[Icount] & 0xff)) & 0xffff;


EDIT: Oops, forget what I said. The & 0xffff does the trick, sorry about that  Embarrassed. Still, I'd use Abuse's code...

Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #22 - Posted 2008-05-23 13:37:35 »

That code wasn't intended as a replacement, I was just trying to highlight that the loop may not be doing what you intended it to do.

Do you realy want to be zero'ing every other pair of bytes in the buffer?

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline vinodpatel2006

Junior Devvie





« Reply #23 - Posted 2008-05-26 06:22:12 »

Hello,

      Do you know when i can make sound silence? I have PCM_SIGNED data. What i can use 0 OR -1 to make sound silence?
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #24 - Posted 2008-05-27 08:07:28 »

Just use 0.

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.

toopeicgaming1999 (66 views)
2014-11-26 15:22:04

toopeicgaming1999 (58 views)
2014-11-26 15:20:36

toopeicgaming1999 (12 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (28 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (53 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!