NXJ Playing Sound Files!!

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

NXJ Playing Sound Files!!

Postby 99jonathan » Mon Jun 25, 2007 11:04 pm

At least thats the goal... :lol:

anyways, I'm currently working on a class to decode WAVs (MIDIs will probably be next) and play them in Lejos in the least amount of computing power and memory. I've found a number of sites with great info on them and am making great progress. I'll hook you all up with a test file (i don't have a nxt lol) once I've got something to test!
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby 99jonathan » Tue Jun 26, 2007 3:55 am

Ok so I ran into one "small" problem... My ReadInt() methods were returning wildly strange numbers. After some detective work I discovered that .WAVs are big-endian. Java reads in little-endian. Is there already a class in Lejos that deals with this, or should I write my own?
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby 99jonathan » Tue Jun 26, 2007 4:04 am

Ok so I wrote a few simple methods for dealing with the problem. All is well!!
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby 99jonathan » Sun Jul 01, 2007 5:08 am

I am VERY close to posting something to test!!

However, does anyone know how to access a file using a InputStream?
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby imaqine » Thu Jul 12, 2007 2:43 pm

Can't help you with your question but how's the progress going?
imaqine
Moderator
 
Posts: 79
Joined: Sun Apr 08, 2007 6:20 am

Postby 99jonathan » Thu Jul 12, 2007 3:38 pm

I am very close to release... Right now it can load the sound file, read it, etc. I'm currently figuring out the most efficient way of reading/playing. Since we only have one channel to play on, and a measly centisecond minimum playtime, either we skip 99% (literally) of the sound file, or we play it reaaaaaaaaaaaaaaaaally slow. I'm thinking about writing both options and then making it a boolean input.

Is there any way to perhaps improve on the current Sound class? (most preferably with quicker tones)
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby 99jonathan » Thu Jul 12, 2007 5:24 pm

Ok............... FIRST RELEASE!!!

I polished up the code, commented it all, etc.

Here it is:

Code: Select all
   
public static void main(String[] args){
        //"bob3.wav" is the file name, 1f is how fast to play it.
        //thus 2f would be double speed, .5f would be half as fast.
        //-1f means to play the whole file (reeeeeally slow)
        //0 will cause it to exit, so don't do that
   SoundDecode.Play("bob3.wav",1f);
}


I also don't know the proper way to tell the user that there was a problem, so it just puts it on the LCD.


Get this: The total memory use by variables declared in the program is 14 bytes!!!

So anyways, enjoy (if you can)!!

http://putstuff.putfile.com/94608/5738971
Last edited by 99jonathan on Thu Jul 12, 2007 6:40 pm, edited 2 times in total.
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby bbagnall » Thu Jul 12, 2007 5:52 pm

Hi Jonathan,

To read a file you need to replace the existing line with this:

DataInputStream dis = new DataInputStream(new FileInputStream(new File(file)));

You're also going to need some try-catch blocks around some of the IO code for it to compile properly.

That's a great job you've done there! I'm glad you decided to take up this project as it adds so much to leJOS NXJ. We'll probably integrate it into the Sound class, perhaps as Sound.playFile(String file) and .playFile(String file, int speed), etc...

We generally like to stick to mimicking the official Java API where possible but it looks hella complex and over-engineered for our purposes:
http://java.sun.com/j2se/1.5.0/docs/api ... mmary.html
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby 99jonathan » Thu Jul 12, 2007 6:39 pm

Ok, fixed the problems (i just did one all inclusive try statement that catches IOException). This should work!!

http://putstuff.putfile.com/94608/5738971

Yea i actually looked at the source of the official java method. It was helluva complex... it took me days to find the method where it reads files!! By that time i had already managed to read the files up to the actual data. I was able to make my code a little more efficient by using some of the tricks they used though.
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby imaqine » Thu Jul 12, 2007 6:41 pm

That's great to hear! I won't be able to test it out until later tonight but it looks promising. What do you have planned for the next release?
imaqine
Moderator
 
Posts: 79
Joined: Sun Apr 08, 2007 6:20 am

Postby 99jonathan » Thu Jul 12, 2007 6:44 pm

I'll probably get .midis to play too. If you have any other suggestions, just tell me!
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby imaqine » Thu Jul 12, 2007 6:52 pm

I know the original priority list mentioned:
Playing sound files, and volume control for sound.
for sound files so perhaps once midi's are working then volume control would be the next logical step.
imaqine
Moderator
 
Posts: 79
Joined: Sun Apr 08, 2007 6:20 am

Postby bbagnall » Tue Jul 24, 2007 3:20 am

Do you have a good wav file to test this out with? I tried playing a couple of Windows wav files and it just shuts off the NXJ brick when I run it. The code is as follows:

public class SoundTest {
public static void main(String[] args) {
SoundDecode.Play("Menu.wav");
SoundDecode.Play("Insert.wav");
}
}
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby 99jonathan » Tue Jul 24, 2007 5:13 am

hmmm....

Try using Chord.wav from C:/WINDOWS/Media/. It works fine on my comp using the same code. (slightly modded to play on the computer)

Are you sure that it can find the files? I have zippo idea on whether my io is correct.
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby bbagnall » Tue Jul 24, 2007 3:06 pm

That file is a little too big at 95 kb. I tried ringin.wav which is 10 kb and down-sampled at 88 kbps, same result.

BTW It sounds like you haven't tested this on the NXT yet. Why not?
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Next

Return to NXJ Projects

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests

more stuff