NXJ handling of .wav files

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

NXJ handling of .wav files

Postby avocado » Wed Jul 02, 2008 1:04 pm

I've had some weird problems playing .wav files in NXJ. I've downloaded random files that worked with the Sound.playSample(new File("filename")) method, but when I replicate the settings for those files, there's an error playing them on the brick.

I've read about some issues that crop up with endian-ness - Java being little endian and wav being big endian. What confuses me is that SOME wav files have worked, just none of mine.

The settings I tried to use were PCM 8-bit wav, 11.025kHz, mono, with various sample sizes.

Has anyone had a similar problem? I've scanned previous posts, and I didn't find anything, but if I missed it, please link me and I'll go check it out.
avocado
New User
 
Posts: 2
Joined: Tue Jul 01, 2008 7:19 pm
Location: Rochester, NY

Re: NXJ handling of .wav files

Postby takashic » Wed Jul 02, 2008 1:57 pm

avocado wrote:What confuses me is that SOME wav files have worked, just none of mine.

The settings I tried to use were PCM 8-bit wav, 11.025kHz, mono, with various sample sizes.

Has anyone had a similar problem? I've scanned previous posts, and I didn't find anything, but if I missed it, please link me and I'll go check it out.


Yes, I had the exact same issue when I testd NXJ sound.c for my nxtOSEK. (I am a very rare NXJ user).

If the source code still remains same as v0.5, NXJ sound for .wav file works for only
- Linear PCM with no data chunk.
As fas as I tested various 8bit monoral .wav files, there are other two types.
- Linear PCM with data chunk
- Non Linear PCM with no data chunk.
NXJ Java sound class for .wav file is implemented for only the first one, so you issue might be caused by the other two types of .wav files.
In my case, I wrote C API to handle these three types of .wav files.

I hope this might be a little help to solve your issue.

takashic
takashic
Novice
 
Posts: 32
Joined: Mon May 21, 2007 2:22 am

Postby gloomyandy » Wed Jul 02, 2008 3:30 pm

As always the source tells the story...
Code: Select all
 189     public static int playSample(File file, int vol)
  190     {
  191         // First check that we have a wave file. File must be at least 44 bytes
  192         // in size to contain a RIFF header.
  193         if (file.length() < RIFF_HDR_SIZE)
  194             return -1;
  195         // Now check for a RIFF header
  196         FileInputStream f = new FileInputStream(file);
  197         DataInputStream d = new DataInputStream(f);
  198         int sampleRate = 0;
  199         int dataLen = 0;
  200         try
  201         {
  202             if (d.readInt() != RIFF_RIFF_SIG)
  203                 return -1;
  204             // Skip chunk size
  205             d.readInt();
  206             // Check we have a wave file
  207             if (d.readInt() != RIFF_WAVE_SIG)
  208                 return -1;
  209             if (d.readInt() != RIFF_FMT_SIG)
  210                 return -1;
  211             // Now check that the format is PCM, Mono 8 bits. Note that these
  212             // values are stored little endian.
  213             d.readInt(); // Skip chunk size
  214             if (d.readShort() != RIFF_FMT_PCM)
  215                 return -1;
  216             if (d.readShort() != RIFF_FMT_1CHAN)
  217                 return -1;
  218             sampleRate = d.readByte() & 0xff;
  219             sampleRate |= (d.readByte() & 0xff) << 8;
  220             sampleRate |= (d.readByte() & 0xff) << 16;
  221             sampleRate |= (d.readByte() & 0xff) << 24;
  222             d.readInt();
  223             d.readShort();
  224             if (d.readShort() != RIFF_FMT_8BITS)
  225                 return -1;
  226             // Make sure we now have a data chunk
  227             if (d.readInt() != RIFF_DATA_SIG)
  228                 return -1;
  229             dataLen = d.readByte() & 0xff;
  230             dataLen |= (d.readByte() & 0xff) << 8;
  231             dataLen |= (d.readByte() & 0xff) << 16;
  232             dataLen |= (d.readByte() & 0xff) << 24;
  233             d.close();
  234         }
  235         catch (IOException e)
  236         {
  237             return -1;
  238         }
  239         if (vol >= 0)
  240             vol = (vol*masterVolume)/100;
  241         else
  242             vol = -vol;
  243         playSample(file.getPage(), RIFF_HDR_SIZE, dataLen, sampleRate, vol);
  244         return getTime();
  245     }
  246


So basically it must be a RIFF file, with WAV data, linear PCM, a single channel, 8 bits per sample and the first section must contain the wave data. There may be later sections containing none wave data, but the first must be the data.... When I put together the above code I found that most tools could generate this format. If you really want to use other formats it is probably better to convert to the above before sending the data to nxt. Or you could always write your own code that uses the native calls directly... If I get chance I'll add an access function to the low level PCM playing code at some point (I'll laso fix a bug I've just noticed that results in the master volume be applied twice if you just use playSample(file)!

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4042
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby avocado » Thu Jul 03, 2008 6:37 pm

Thanks for the help! I obviously need to learn more about wav files, and you've given me a good place to start.

Any recommendations for software that gives that many options for recording/converting wav? I'm sure I can find something, regardless.

Thanks again. :)
avocado
New User
 
Posts: 2
Joined: Tue Jul 01, 2008 7:19 pm
Location: Rochester, NY

Postby 99jonathan » Sun Jul 06, 2008 10:24 pm

The native Sound Recorder that comes with Windows has a vast range of WAV formats. Just change the Format after pressing Save As.
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby jmernin » Tue Aug 19, 2008 4:55 pm

I too am having problems getting a WAV file to play on my NXT.

I've converted it using Windows Sound Recorder. From the "Save As" menu, I pressed the "Change" button and from there I chose "PCM" as the format and "8kHz, 8bit, Mono, 7Kb/s" from the Attributes drop down menu.

I then transferred the resultant .wav file onto the NXT using "nxjupload.bat" but when I try to play the file from the NXT menu system, the NXT just gives a single "click" sound and returns to the menu system.

I've tried a few other formats too as well as some different conversion tools but to no avail.

Any ideas?
jmernin
New User
 
Posts: 23
Joined: Thu Jan 17, 2008 11:52 pm

Postby gloomyandy » Tue Aug 19, 2008 8:33 pm

What version of leJOS are you running?

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4042
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby takashic » Wed Aug 20, 2008 2:00 pm

Hi Andy,

I also encountered the same problem before, then I found out the cause
is in sound.c (I just checked the latest sound.c in the trank, but it is still there).

In sound.c file, at line 385, there is the following code and it causes an
overflow if the lengh of sound sample is long.
int ms = (sample.count*1000*sample.len*32)/(OSC/(2*sample.clock_div));
So I modified the above code to:
int ms = (int)(((long long int)sample.count*1000*sample.len*32)/(OSC/(2*sample.clock_div)));

Thanks to GCC supports C99 long long int, it works fine. (instead of
single "click", wav sound could be generated in nxtOSEK.

Takashic
takashic
Novice
 
Posts: 32
Joined: Mon May 21, 2007 2:22 am

Postby gloomyandy » Wed Aug 20, 2008 2:32 pm

Hi,
Although the above code could be causing this problem, in this case I don't think it is... The above code is used to return an estimate of how much longer there is to play for the current sound/sample. However in the case of playing a sample from the menu system this function is not used (or at least the value it returns is not...). The menu simply starts the sample playing and continues, it does not wait for it to finish. I'll take a look at re-working that code....

Meanwhile if someone can point me at a file that they are unable to play I'll try and work out what is going on... I've just uploaded the standard MS ringin.wav and it plays fine....

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4042
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby jmernin » Wed Aug 20, 2008 4:20 pm

Andy,

I am running a version of leJOS taken from trunk/snapshot on 8th August 2008. You may recall I moved to this version in an effort to resolve some data aborts I was seeing using Bluetooth data streams.

According to the dates in the SVN log, I am on SVN revision 1709.

Also, I will try out the ringing.wav file from Windows later also and see if that works. Maybe this will help to narrow down the problem.
jmernin
New User
 
Posts: 23
Joined: Thu Jan 17, 2008 11:52 pm

Postby gloomyandy » Wed Aug 20, 2008 9:17 pm

Hi,
OK I've been taking another look at this and I think the problem may be that there are extra "chunk" sections in many wav/riff files. You can see some of the gory details here...
http://sharkysoft.com/archive/wave/docs/javadocs/lava/riff/wave/doc-files/riffwave-frameset.htm
I've updated the code (now in svn and available in the latest snapshot including a re-built menu program that uses the new code), to skip any of these sections and also made it more tolerant of other differences. If you are happy using the snapshots maybe you could give it a try...

I've been using the following audio editor...
http://www.free-audio-editor.com/
When I save the wav file I used a format of 8bits Mono and a bit rate of 11025Hz (I think other bit rates should work). Seems to work for me... but HiFi it isn't!

All the best

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4042
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby jmernin » Thu Aug 28, 2008 8:41 pm

Andy,

I finally got a chance to test this for you:

1. Downloaded the updated snapshot (SVN 1735)
2. Re-flashed my NXT using "nxjflash" command. Interestingly, I still had to put the NXT into manual flash mode (using paper clip). I seem to recall you saying that this was perhaps no longer necessary.
3. Uploaded the ringin.wav file from my Windows XP system
4. Tried playing this file from the NXT menu system but alas, all I get is a single click from the NXT.

I also tried playing it like this::

public class PlaySound
{
public static void main (String[] aArg) throws Exception
{
File f = new File("ringin.wav");
Sound.playSample(f, 100);
}
}

both to no avail.

P.S. The Version screen on the NXT reports Firmware version 0.6 (rev. 1732) and Menu version 0.6 (rev. 1666)

Is this correct?
jmernin
New User
 
Posts: 23
Joined: Thu Jan 17, 2008 11:52 pm

Postby gloomyandy » Thu Aug 28, 2008 10:07 pm

Hi,
Hmm I really don't understand what is going on here. Lets take things one step at a time....
1. The new flash program is run via nxjflash.bat (not the nxjflash.exe), you may want to delete the old nxjflash.exe program.

2. Have you replaced all of your lejos_nxj directory with the snapshot. You will need all of the files in the snapshot not just the new firmware. The easy way to do this is to simply rename the the exisiting lejos_nxj directory and name the snapshot dir as lejos_nxj.

3. When you flash the nxt you should see a display like...
Code: Select all
VM file: h:\Lego\lejos\trunk\snapshot\bin\lejos_nxt_rom.bin
Menu file: h:\Lego\lejos\trunk\snapshot\bin\StartUpText.bin
VM size: 36000 bytes.
Menu size: 32118 bytes.
Total image size 68214/81920 bytes.
No devices in firmware update mode were found.
Searching for other NXT devices...
Found nxt name nxt2 address 001653015066
The following NXT devices have been found:
  1:  nxt2  001653015066
Select the device to update, or enter 0 to exit.
Device number to update: 1
Waiting for device to re-boot...
Found nxt name %%NXT-SAMBA%% address 1
Connected to SAM-BA v1.4
NXT now open in firmware update mode.
Unlocking pages.
Writing memory image...
Starting new image.

Please check that the paths point to the new snapshot download and that the files sizes match the above.

4. The ringin.wav file should be 10026 bytes in length when you look at it using nxjbrowse. Note that on XP there are two versions of this file both in the Windows\media directory. One is called simply regin.wav and is 10026 bytes in length. The other is called Windows XP ringin.wav and is 38930 bytes in size. You want the smaller version.

5. When you compile your test program make sure you use the new classes.jar from the snapshot, all of the changes ar in that file. You may want to use the command line tools rather than an IDE like Eclipse.

6. Can you print out the return value from playSample. I added some new return codes to work out what step is failing.

All of the files I've tried play fine on my nxt using the menu and firmware from the snapshot. The version numbers for the firmware and menu match. Note though that the code changes are in classes.jar used to build the menu system and the version of that does not show up in the menu version number. Please check the sizes as reported by nxjflash.

If none of the above work can you please send me a copy of the ringin.wav that you have downloaded to the nxt.

All the best

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4042
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby jmernin » Thu Aug 28, 2008 11:05 pm

There is definitely something funny going on here.

In an effort to start over, I reinstalled the previous firmware and went through your instructions again, one step at a time.

Here is the output I got when I ran the nxjflash.bat from the latest snapshot:

Code: Select all
VM file: C:\Work\apps\lejos_nxj\bin\lejos_nxt_rom.bin
Menu file: C:\Work\apps\lejos_nxj\bin\StartUpText.bin
VM size: 35792 bytes.
Menu size: 32074 bytes.
Total image size 67914/81920 bytes.
No devices in firmware update mode were found.
Searching for other NXT devices...
Found nxt name Rover address 00165303D0A1
The following NXT devices have been found:
  1:  Rover  00165303D0A1
Select the device to update, or enter 0 to exit.
Device number to update: 1
Waiting for device to re-boot...
Found nxt name %%NXT-SAMBA%% address 1
Connected to SAM-BA v1.4
NXT now open in firmware update mode.
Unlocking pages.
Writing memory image...
Starting new image.


As you will agree, this looks to be correct (perhaps I did inadvertently use nxjflash.exe the first time around). In any case, it would appear that I now have the correct firmware installed (Version screen now says "firmware 1706, menu 1666").

I had also already done most of the other steps you recommended (i.e. renamed the new snapshot directory to lejos_nxj, recompiled my code using the correct classes.jar etc.) and I can confirm that I only have one ringin.wav file in C:\WINDOWS\Media and it is indeed 10,026 bytes in size.

However, the funny thing is, after I upgraded to the latest firmware, I can no longer access the NXT using USB, only via Bluetooth. Prior to this, my PC used to beep each time I connected and disconnected the NXT and the NXT always showed up in the Windows Device Manager as a MINDSTORMS NXT device. However, it's no longer doing this, which is a bit strange. I tried repowering and reseating the batteries but to no avail.

Anyway, the return code from the playSample is showing 1207. This suggests that the duration of the sample is 1.207 seconds but in the Windows Sound Recorder where I'm playing the ringin.wav file (to test that's ok), it shows a 0.90 seconds long.
jmernin
New User
 
Posts: 23
Joined: Thu Jan 17, 2008 11:52 pm

Postby jmernin » Thu Aug 28, 2008 11:10 pm

I will email you some sample sound files right after this but had one or two other details I meant to include in the previous post.

1. I've tried using both versions of Sound.playSample method but neither works
2. I have all 3 motors connected but only the Ultrasonic sensor (to Sensor Port 2)
3. I uploaded ringout.wav from Windows too an it also does not play.

P.S. What is your email address so that I can email you those files?
jmernin
New User
 
Posts: 23
Joined: Thu Jan 17, 2008 11:52 pm

Next

Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests

more stuff