Slick Forums

Discuss the Slick 2D Library
It is currently Sat Feb 23, 2019 12:14 am

All times are UTC




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Mon Dec 13, 2010 6:06 pm 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
In order to create Sounds and Musics from other sources than classpath or filesystem local to the project, it would be great to have a constructor using an InputStream or similar.

One problem here is to know which type of resource is it: ogg, wav, etc. Dunno exactly the best solution, but I suppose it should be similar to Image constructor using the ref parameter for that.

An example:

Code:
InputStream in = ....
Sound sound = new Sound(in, "mySound.ogg");

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 1:42 am 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
Here is the patch for the Sound class, take a look at it if you want.

Code:
Index: src/org/newdawn/slick/Sound.java
===================================================================
--- src/org/newdawn/slick/Sound.java   (revision 1529)
+++ src/org/newdawn/slick/Sound.java   (working copy)
@@ -1,10 +1,12 @@
 package org.newdawn.slick;

+import java.io.InputStream;
 import java.net.URL;

 import org.newdawn.slick.openal.Audio;
 import org.newdawn.slick.openal.SoundStore;
 import org.newdawn.slick.util.Log;
+import org.newdawn.slick.util.ResourceLoader;

 /**
  * A single sound effect loaded from either OGG or XM/MOD file. Sounds are allocated to
@@ -23,25 +25,7 @@
     * @throws SlickException Indicates a failure to load the sound effect
     */
    public Sound(URL url) throws SlickException {
-      SoundStore.get().init();
-      String ref = url.getFile();
-      
-      try {
-         if (ref.toLowerCase().endsWith(".ogg")) {
-            sound = SoundStore.get().getOgg(url.openStream());
-         } else if (ref.toLowerCase().endsWith(".wav")) {
-            sound = SoundStore.get().getWAV(url.openStream());
-         } else if (ref.toLowerCase().endsWith(".aif")) {
-            sound = SoundStore.get().getAIF(url.openStream());
-         } else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {
-            sound = SoundStore.get().getMOD(url.openStream());
-         } else {
-            throw new SlickException("Only .xm, .mod, .aif, .wav and .ogg are currently supported.");
-         }
-      } catch (Exception e) {
-         Log.error(e);
-         throw new SlickException("Failed to load sound: "+ref);
-      }
+      this(url.getFile());
    }

    /**
@@ -51,17 +35,27 @@
     * @throws SlickException Indicates a failure to load the sound effect
     */
    public Sound(String ref) throws SlickException {
-      SoundStore.get().init();
-      
+      this(ResourceLoader.getResourceAsStream(ref), ref);
+   }
+   
+   /**
+    * Create a new Sound from an input stream.
+    * @param inputStream the stream of the sound
+    * @param ref The location of the OGG or MOD/XM to load
+    * @throws SlickException Indicates a failure to load the sound effect
+    */
+   public Sound(InputStream inputStream, String ref) throws SlickException {
+      SoundStore soundStore = SoundStore.get();
+      soundStore.init();
       try {
          if (ref.toLowerCase().endsWith(".ogg")) {
-            sound = SoundStore.get().getOgg(ref);
+            sound = soundStore.getOgg(ref, inputStream);
          } else if (ref.toLowerCase().endsWith(".wav")) {
-            sound = SoundStore.get().getWAV(ref);
+            sound = soundStore.getWAV(ref, inputStream);
          } else if (ref.toLowerCase().endsWith(".aif")) {
-            sound = SoundStore.get().getAIF(ref);
+            sound = soundStore.getAIF(ref, inputStream);
          } else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {
-            sound = SoundStore.get().getMOD(ref);
+            sound = soundStore.getMOD(ref, inputStream);
          } else {
             throw new SlickException("Only .xm, .mod, .aif, .wav and .ogg are currently supported.");
          }



UPDATE: fixed patch format.

_________________
Image


Last edited by arielsan on Thu Jan 20, 2011 2:02 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 1:53 am 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
In the case of the Music, I dunno how to deal with one special case, when using streamingHint.

This is the current patch, but that case is not handled right, it keeps the previous logic working:

EDITED: invalid patch removed

_________________
Image


Last edited by arielsan on Tue Dec 14, 2010 1:57 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 1:56 am 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
sorry, made one mistake, here is the patch again, remember that streamingHint case is not handled correctly:

Code:
Index: src/org/newdawn/slick/Music.java
===================================================================
--- src/org/newdawn/slick/Music.java   (revision 1529)
+++ src/org/newdawn/slick/Music.java   (working copy)
@@ -1,5 +1,6 @@
 package org.newdawn.slick;

+import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
@@ -7,6 +8,7 @@
 import org.newdawn.slick.openal.AudioImpl;
 import org.newdawn.slick.openal.SoundStore;
 import org.newdawn.slick.util.Log;
+import org.newdawn.slick.util.ResourceLoader;
 /**
  * A piece of music loaded and playable within the game. Only one piece of music can
@@ -77,44 +79,21 @@
    /**
     * Create and load a piece of music (either OGG or MOD/XM)
     *
-    * @param ref The location of the music
+    * @param url The location of the music
     * @throws SlickException
     */
-   public Music(URL ref) throws SlickException {
-      this(ref, false);
+   public Music(URL url) throws SlickException {
+      this(url.getFile(), false);
    }

    /**
     * Create and load a piece of music (either OGG or MOD/XM)
     *
     * @param url The location of the music
-    * @param streamingHint A hint to indicate whether streaming should be used if possible
     * @throws SlickException
     */
    public Music(URL url, boolean streamingHint) throws SlickException {
-      SoundStore.get().init();
-      String ref = url.getFile();
-      
-      try {
-         if (ref.toLowerCase().endsWith(".ogg")) {
-            if (streamingHint) {
-               sound = SoundStore.get().getOggStream(url);
-            } else {
-               sound = SoundStore.get().getOgg(url.openStream());
-            }
-         } else if (ref.toLowerCase().endsWith(".wav")) {
-            sound = SoundStore.get().getWAV(url.openStream());
-         } else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {
-            sound = SoundStore.get().getMOD(url.openStream());
-         } else if (ref.toLowerCase().endsWith(".aif") || ref.toLowerCase().endsWith(".aiff")) {
-            sound = SoundStore.get().getAIF(url.openStream());
-         } else {
-            throw new SlickException("Only .xm, .mod, .ogg, and .aif/f are currently supported.");
-         }
-      } catch (Exception e) {
-         Log.error(e);
-         throw new SlickException("Failed to load sound: "+url);
-      }
+      this(url.getFile(), streamingHint);
    }

    /**
@@ -125,27 +104,40 @@
     * @throws SlickException
     */
    public Music(String ref, boolean streamingHint) throws SlickException {
-      SoundStore.get().init();
+      this(ResourceLoader.getResourceAsStream(ref), ref, streamingHint);
+   }
+   
+   /**
+    * Create and load a piece of music (either OGG or MOD/XM)
+    * @param inputStream the stream of the music.
+    * @param ref The location of the music
+    * @param streamingHint A hint to indicate whether streaming should be used if possible
+    * @throws SlickException
+    */
+   public Music(InputStream inputStream, String ref, boolean streamingHint) throws SlickException {
+      SoundStore soundStore = SoundStore.get();
+      soundStore.init();

       try {
          if (ref.toLowerCase().endsWith(".ogg")) {
             if (streamingHint) {
-               sound = SoundStore.get().getOggStream(ref);
+               // don't know how to deal with this case.
+               sound = soundStore.getOggStream(ref);
             } else {
-               sound = SoundStore.get().getOgg(ref);
+               sound = soundStore.getOgg(ref, inputStream);
             }
          } else if (ref.toLowerCase().endsWith(".wav")) {
-            sound = SoundStore.get().getWAV(ref);
+            sound = soundStore.getWAV(ref, inputStream);
          } else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {
-            sound = SoundStore.get().getMOD(ref);
+            sound = soundStore.getMOD(ref, inputStream);
          } else if (ref.toLowerCase().endsWith(".aif") || ref.toLowerCase().endsWith(".aiff")) {
-            sound = SoundStore.get().getAIF(ref);
+            sound = soundStore.getAIF(ref, inputStream);
          } else {
             throw new SlickException("Only .xm, .mod, .ogg, and .aif/f are currently supported.");
          }
       } catch (Exception e) {
          Log.error(e);
-         throw new SlickException("Failed to load sound: "+ref);
+         throw new SlickException("Failed to load music: "+ref);
       }
    }



UPDATE: fixed patch format.

_________________
Image


Last edited by arielsan on Thu Jan 20, 2011 1:59 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 3:35 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1242
When opening a URL it is important to use openStream() - otherwise the URLStreamHandler is not invoked. This class (URLStreamHandler) allows create virtual file systems transparently for the user code.

This means that url.getFile() should not be used to open a resource.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 10:23 am 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
@arielsan
great work for RFE
it seems you copy / pasted difference between versions (with + and - corrections) so could you edit the post and paste the final version?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 10:48 am 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
MatthiasM wrote:
When opening a URL it is important to use openStream() - otherwise the URLStreamHandler is not invoked. This class (URLStreamHandler) allows create virtual file systems transparently for the user code.

This means that url.getFile() should not be used to open a resource.


The way url.getFile() is slick original code, not introduced in the patch, so I would not modify the way slick is doing that, I believe that must be a kev decision.

But, of course, thanks for the comment, I didn't knew that.

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 11:07 am 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
Kova wrote:
@arielsan
great work for RFE
it seems you copy / pasted difference between versions (with + and - corrections) so could you edit the post and paste the final version?


The first patch is for Sound class and it is on the final version, the second one is Music class and it is not final because I don't know how to manage a thing.

Or you mean you want to see how the classes looks at the end, after applying the patches?

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 11:58 am 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
arielsan wrote:
Or you mean you want to see how the classes looks at the end, after applying the patches?


yes I mean that it is more useful to post after patch code as they work and you can get the diff from the trunk to your working version anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 12:52 pm 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
Then, this is how Sound class looks by now (only modified part):

Code:
....
   /**
    * Create a new Sound
    *
    * @param url The location of the OGG or MOD/XM to load
    * @throws SlickException Indicates a failure to load the sound effect
    */
   public Sound(URL url) throws SlickException {
      this(url.getFile());
   }
   
   /**
    * Create a new Sound
    *
    * @param ref The location of the OGG or MOD/XM to load
    * @throws SlickException Indicates a failure to load the sound effect
    */
   public Sound(String ref) throws SlickException {
      this(ResourceLoader.getResourceAsStream(ref), ref);
   }
   
   /**
    * Create a new Sound from an input stream.
    * @param inputStream the stream of the sound
    * @param ref The location of the OGG or MOD/XM to load
    * @throws SlickException Indicates a failure to load the sound effect
    */
   public Sound(InputStream inputStream, String ref) throws SlickException {
      SoundStore soundStore = SoundStore.get();
      soundStore.init();
      try {
         if (ref.toLowerCase().endsWith(".ogg")) {
            sound = soundStore.getOgg(ref, inputStream);
         } else if (ref.toLowerCase().endsWith(".wav")) {
            sound = soundStore.getWAV(ref, inputStream);
         } else if (ref.toLowerCase().endsWith(".aif")) {
            sound = soundStore.getAIF(ref, inputStream);
         } else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {
            sound = soundStore.getMOD(ref, inputStream);
         } else {
            throw new SlickException("Only .xm, .mod, .aif, .wav and .ogg are currently supported.");
         }
      } catch (Exception e) {
         Log.error(e);
         throw new SlickException("Failed to load sound: "+ref);
      }
   }
...

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2010 1:41 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1242
This is again not handling the URL right. You have to call openStream() on an URL to get an InputStream:
Code:
....
   /**
    * Create a new Sound
    *
    * @param url The location of the OGG or MOD/XM to load
    * @throws SlickException Indicates a failure to load the sound effect
    */
   public Sound(URL url) throws SlickException {
      this(url.openStream(), url.getPath());
   }
....


Also it's not a good idea to use getFile() as this also returns the query part - which makes examining the file type based on extension impossible when a query is used. While getPath() also has issues with things like: "http://www.bla.com/images.php?nr=42" but it's still a bit better.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2010 4:21 pm 
Offline
Regular
User avatar

Joined: Tue Apr 07, 2009 12:58 pm
Posts: 232
Location: Uruguay
Hi ppl, any advance in this request?

I started to use UnicodeFont, and I saw there is no stream constructors either, I am thinking that it would be nice to have constructors using streams for all slick resources, so you could derive the responsibility to the user to know where the resource data is if he wants.

Thanks.

ps: I could provide patches for those too, to help, but first I want to know if there is any interest at all.

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2010 6:20 pm 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 598
arielsan wrote:
Hi ppl, any advance in this request?

I started to use UnicodeFont, and I saw there is no stream constructors either, I am thinking that it would be nice to have constructors using streams for all slick resources, so you could derive the responsibility to the user to know where the resource data is if he wants.

Thanks.

ps: I could provide patches for those too, to help, but first I want to know if there is any interest at all.


I'm interested. Good work btw. Maybe kevin is open to these patches?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2010 9:13 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
I've no problem with the patches, just haven't done a bug run recently.. currently enjoying the xmas break :)

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2010 9:18 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Test.

Kev


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group