Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 ... 3 4 [5] 6
  ignore  |  Print  
  Need a really simple library for playing sounds and music? Try TinySound.  (Read 56413 times)
0 Members and 1 Guest are viewing this topic.
Offline StumpyStrust
« Reply #120 - Posted 2013-03-03 16:25:23 »

That is the issue. If you load the whole song into memory uncompressed it is huge. You need to stream it. Titysound streams sound files but not music if I remember correctly. It also supports ogg.

Then there is libgdx which does all of that and then some.

Offline jonjava
« Reply #121 - Posted 2013-03-03 16:38:42 »

Quote
Memory Usage
------------
The basic loading functions for Music and Sound objects produce implementations
that store all audio data in memory.  This is good for maintaining low latency,
but can also require a lot of heap space if you load many, or particularly long,
audio resources.  There are loading functions available that allow you to
request that the audio data be streamed from a file.  If this is requested, the
audio data will first be converted as usual and then written to a temporary file
from which it will be streamed.  This will dramatically reduce the overall
memory usage (after loading), but can potentially introduce occasional latency
when reading from disk.

source: https://github.com/finnkuusisto/TinySound



source: http://finnkuusisto.github.com/TinySound/doc/

Offline Sammidysam
« Reply #122 - Posted 2013-03-03 16:40:03 »

Even while streaming it still uses quite a bit of space, but definitely less than without streaming.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #123 - Posted 2013-03-03 16:49:37 »

I decided on posting this issue on this board as well;
Topics merged. Multi posting is dreadful.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Quarry
« Reply #124 - Posted 2013-03-03 18:44:12 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
public static void startSoundtrack() {
      Thread stThread = new Thread(new Runnable() {

         @Override
         public void run() {
            int currentSong = Main.rng.nextInt(MUSIC_AMOUNT);

            System.out.println("Started soundtrack, now at /music" + currentSong + ".ogg");

            while (!dead) {
               System.out.println("====================");
               
               Sound snd = TinySound.loadSound("music" + currentSong + ".ogg", true);
               if (snd != null)
                  snd.play(1);
               
               try {
                  Thread.sleep((int) (1000 * 7.2f * 60));
               } catch (InterruptedException e) {
               }

               snd.unload();
               
               currentSong = Main.rng.nextInt(MUSIC_AMOUNT);
               
               System.out.println("Switched song, now at /music" + currentSong + ".ogg");
            }
         }
      });
     
      stThread.start();
   }


This is the latest code, for music only. I switched streaming from file or not (true/false in snd declaration) and the difference was around 3 megabytes. Switching between Sound and Music also makes no difference. It really bothers me when I see VLC using 11 megabytes only while playing them

 Undecided
Offline kuusisto

Senior Member


Medals: 13



« Reply #125 - Posted 2013-03-03 20:04:39 »

Again, sorry I haven't been on in a while.  I've been crazy busy with my academic life.

What TinySound does when streaming is it first loads the whole thing into memory to convert to the correct format, writes it to a temporary file, and then streams that.  It can thus end up using a lot of memory at loading time, but that should be drastically reduced when streaming.  Now, depending on how you monitor memory usage, it can still appear to be using a lot more memory.  If you just look at how much memory the JVM has been allocated (e.g. the Task Manager on Windows, or top on Linux), it will be large.  This is because it needed that memory at some point.  The actual heap space in use when streaming, however, should be much lower.  You can tell how much memory is actually in use with something like:

1  
double mbInUse = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576.0);

A better solution would be to convert the audio data to the correct format as it's being read and written out to the temporary file, but I haven't done that.  I don't think it's trivial either.
Offline StumpyStrust
« Reply #126 - Posted 2013-03-03 20:11:04 »

It probably isn't. After you added the streaming I did what you said kuu and the memory usage is basically 40-50 mb which is fine even thought it says 200+. I still think this lib is great for how easy it is to add sound with ogg support to a game or app.

Offline Quarry
« Reply #127 - Posted 2013-03-03 20:28:42 »

Well, using mbInUse actually made me see the difference of 160 to 240 but I believe that it's still stupidly high
Offline kuusisto

Senior Member


Medals: 13



« Reply #128 - Posted 2013-03-03 20:34:03 »

Well, using mbInUse actually made me see the difference of 160 to 240 but I believe that it's still stupidly high

That is high.  Can you post all of the code you're testing with?

Edit:
I just took your code that you most recently posted and added the minimum amount required to get it running.  I see what you mean about the memory usage, but it appears to be a result of the fact that you load and unload the Sound on each iteration.  When I added a
1  
System.gc();
right before the sleep, I was seeing less than 1MB of memory usage at the end of each iteration.  The moral of the story is that you shouldn't load and unload so much.  If you're going to use some music, load it and store it somewhere until you're done with it.  If you're using the streaming implementation, the Music object itself should take up very little memory, so there's no reason not to hang onto the reference.

Please let me know if that helps.
Offline Tireless

Innocent Bystander





« Reply #129 - Posted 2013-03-14 16:02:16 »

During the last years I have tried several solutions for audio in Java games, including the stock JavaSound classes, the JavaZoom libraries, and Paul Lamb's sound system. But I find the same problem in all of them: under Windows everything works fine, but under Linux (or at least for some distributions/java versions), simultaneous sounds don't work. You want to play an effect at the same time as music, crash. I have spent many evenings googling about this, but there are contradicting explanations on the internet and to be honest I don't know a word about pulseaudio, mixers, sound drivers, etc. I'd just like to focus on the gameplay and have the audio work like in Windows :/

The only solution I know to work is to use stuff like OpenAL but this requires distributing native code, and I wouldn't like having different distributions of games depending on the OS.

Is it known whether this library addresses that issue (in pure Java) or it also has that shortcoming? If it does address the issue, I think I'm going to switch to it in no time...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kuusisto

Senior Member


Medals: 13



« Reply #130 - Posted 2013-03-14 16:16:44 »

TinySound should work fine on Linux because it mixes all audio to a single line.  This was actually one of the problems that I was trying to address when I created TinySound.  I did once observe a latency issue with some drivers that I had installed, but others might be able to comment on their experiences.
Offline nsigma
« Reply #131 - Posted 2013-03-14 16:25:25 »

Any library that does software mixing (like this one) will work fine on Linux.  There are a variety of reasons multiple sounds don't work or don't work well on Linux when using JavaSound the way Paul Lamb's system and others do.  Even on Windows it's not a great way to work, constantly getting JavaSound to open and close multiple lines to the soundcard - that's not how to write any audio software, and just because you can get away with it on Windows don't assume that's the way it works cross-platform.

The upside ... pick a good library that does mixing in Java and you will get generally get better performance on Linux than Windows.  Wink

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline relminator
« Reply #132 - Posted 2013-03-15 02:04:17 »

Nice lib!  I'm gonna have a look-see later.
Offline tyeeeee1
« Reply #133 - Posted 2013-04-05 20:11:57 »

I've been trying to get sound working with various libraries and TinySound seems to be the best for me to use at the moment. It's been working great but when I profiled my program the memory being used went from it's usual 10mb and kept increasing over time. After coming to this thread and briefly scanning over a few pages I noticed people saying that the StreamSound/StreamMusic might lower the amount of memory used because the audio files won't be loaded into memory.

I, for some reason, was unsuccessful in attempting to get StreamMusic/StreamSound to work. Could someone look over the code below and tell me what I should chnage to get it to work.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
package Core;

import java.net.URL;
import kuusisto.tinysound.TinySound;
import kuusisto.tinysound.internal.Mixer;
import kuusisto.tinysound.internal.StreamMusic;

public class AudioHandler
{
   
    public AudioHandler()
    {
    }
   
    public void play()
    {
        //initialize TinySound
       TinySound.init();
        //load a sound and music
       //note: you can also load with Files, URLs and InputStreams
       URL url = this.getClass().getClassLoader().getResource("Resources/Sound/MainMenu.mid");
        StreamMusic song;
        Mixer mixer = new Mixer();
        try
        {
            song = new StreamMusic(url, 100000, mixer);
            song.play(true);
        }
        catch (Exception e)
        {
        }
    }
}


I based this off of the one piece of example code I found and then guessed the rest with http://finnkuusisto.github.io/TinySound/doc/ .
Offline cubus

Junior Member


Medals: 2



« Reply #134 - Posted 2013-04-05 21:31:18 »

1  
TinySound.loadMusic(URL url, boolean streamFromFile)


this first reads the _whole_ audio data from the stream into a temp buffer...
writes the temp buffer to a temp file...
streams from this file...

i'd rearrange the code to something like this
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class AudioHandler
{
    Music music;
    public AudioHandler()
    {
        TinySound.init();
        music = TinySound.loadMusic(...);
    }
   
    public void play()
    {
        music.play(...);
    }
}



Offline cubus

Junior Member


Medals: 2



« Reply #135 - Posted 2013-04-05 22:31:38 »

when you call play(false) twice, does it play the sound twice?

Offline tyeeeee1
« Reply #136 - Posted 2013-04-05 22:36:29 »

As far as I can hear, it doesn't.
Offline philfrei
« Reply #137 - Posted 2013-04-05 22:42:07 »

@tyeeeeee1
I don't understand. Did you or didn't you separate the loading from the play as suggested by cubus?

The code that you displayed still has the two together in a single call. Each time you load the file you will burn more memory as you store yet another copy in RAM. That is why the play() method should NOT be in the same method with the loadMusic() method.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline tyeeeee1
« Reply #138 - Posted 2013-04-05 22:47:04 »

@tyeeeeee1
I don't understand. Did you or didn't you separate the loading from the play as suggested by cubus?

The code that you displayed still has the two together in a single call. Each time you load the file you will burn more memory as you store yet another copy in RAM. That is why the play() method should NOT be in the same method with the loadMusic() method.

Derp... Guess I had the half-updated code in my CnP and didn't notice. Yes, I separated both of them, here is what the code actually looks like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
private Music music;
    public AudioHandler()
    {
        //initialize TinySound
       TinySound.init();
        //load a sound and music
       //note: you can also load with Files, URLs and InputStreams
       URL url = this.getClass().getClassLoader().getResource("Resources/Sound/MainMenu.mid");
        music = TinySound.loadMusic(url, true);
    }
   
    public void play()
    {
        music.play(false);
    }


It's still using the same amount of RAM as before the change though. The '.mid' file that I'm using is only 2kb.


Edit: I accidentally left the program open for a few minutes and noticed that it eventually drops back down from 50mb to 25mb so I'll assume I shouldn't care too much about the RAM anymore then.
Offline cubus

Junior Member


Medals: 2



« Reply #139 - Posted 2013-04-05 22:53:05 »

does the memory usage grow, or is it just as before?
that's what i was pointing out in the first post: TinySound loads the _whole_ audio data and stores it in a temp file, from which it then streams.

i tried the code now. you must call music.rewind() before music.play() to replay it.

the only memory it allocates when replaying is in setPosition() which gets called from rewind(), coz this closes the stream and reopens it. (i don't know why it is implemented this way, since it creates the temp file, which i thought gets created to have a resetable input stream...Huh)
Offline tyeeeee1
« Reply #140 - Posted 2013-04-05 22:57:27 »

does the memory usage grow, or is it just as before?
that's what i was pointing out in the first post: TinySound loads the _whole_ audio data and stores it in a temp file, from which it then streams.

i tried the code now. you must call music.rewind() before music.play() to replay it.

the only memory it allocates when replaying is in setPosition() which gets called from rewind(), coz this closes the stream and reopens it. (i don't know why it is implemented this way, since it creates the temp file, which i thought gets created to have a resetable input stream...Huh)

When I first start the program it's at the usual 10mb and then within seconds it grows to 40mb and by the end of the mid file, which is a few seconds long, it stops around 50mb then after a while longer it drops from 60+mb to 20mb and starts growing up to around 70mb and repeats the cycle. I've noticed that if I leave the file on loop it does the same thing.
I'm starting to think that there is something in the rest of my program that is causing the memory to grow or something.
Offline philfrei
« Reply #141 - Posted 2013-04-05 22:59:14 »

Cubus, I'm wondering if the info you are giving is correct. Usually it is a "Clip" type structure that has the position reset feature, not a streaming structure (e.g. SourceDataLine in java audio terms).

I would like to suggest changing .loadMusic() in your updated example to .streamMusic().

I *think* that loadMusic() functions like a Clip, loading the entire file to memory, and allowing looping and position setting. And streamMusic() functions like a SourceDataLine, allowing streaming without loading any more than a single buffer's worth of sound data from the file at a time.

I could be wrong but it is worth a test, I think.


"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline cubus

Junior Member


Medals: 2



« Reply #142 - Posted 2013-04-05 23:01:52 »

I'm starting to think that there is something in the rest of my program that is causing the memory to grow or something.

i think so. i tried it with a 500 KB wav file and after the music is loaded it does not grow more than ~1 KB per 20 seconds, and these are from my debug strings...
Offline tyeeeee1
« Reply #143 - Posted 2013-04-05 23:04:50 »

According to NetBeans ".streamMusic()" doesn't exist. I was originally trying to set up streaming but I couldn't get it to work due to a lack of examples of how to use TinySound.
I just tried removing the two lines that use the AudioHandler class and used memory, when profiling, never grows beyond 20MB. The two lines that I use are:

1  
2  
3  
4  
5  
6  
public ScreenMainMenu(GameCanvas canvas)
    {
        this.canvas = canvas;
        temp = new AudioHandler();
        temp.play();
    }


This method is only called once and that's at the start of the program, when they're removed the program never uses more than 20mb of memory.
Offline philfrei
« Reply #144 - Posted 2013-04-05 23:06:44 »

My apologies, esp to Cubus! I was extrapolating from what I know about Java's two playback mechanisms, not from direct usage of TinySound.

EDIT: I was just taking a refresher look at the api and code, and it seems to me that if you want to stream instead of loading data into memory, use this form:

loadMusic(file-or-url, true);
loadSound(file-or-url, true);

where true is for the boolean he calls "streamFromFile".

And...it appears you are already doing so.  Smiley

OK, I will go away now. Sorry for adding to any confusion.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline cubus

Junior Member


Medals: 2



« Reply #145 - Posted 2013-04-05 23:27:19 »

np philfrei Smiley

FYI https://github.com/finnkuusisto/TinySound
Offline tyeeeee1
« Reply #146 - Posted 2013-04-05 23:36:19 »

I just did a quick build of my program to check if the audio is actually streaming. From what I read on the link that cubus just linked:

Quote
Memory Usage
------------
The basic loading functions for Music and Sound objects produce implementations
that store all audio data in memory.  This is good for maintaining low latency,
but can also require a lot of heap space if you load many, or particularly long,
audio resources.  There are loading functions available that allow you to
request that the audio data be streamed from a file.  If this is requested, the
audio data will first be converted as usual and then written to a temporary file
from which it will be streamed.  This will dramatically reduce the overall
memory usage (after loading), but can potentially introduce occasional latency
when reading from disk.

I should, assuming that the file being created is created in the same folder as my .jar is, see the file that the data is being streamed from. After doing the test and letting it run for a while I never noticed nor saw any file being created on the startup of my .jar so I'll assume that both ".loadMusic(url, true)" and ".loadSound(url, true)" aren't telling it to stream the music.
Is there another way to tell TinySound to stream the music other than this way and the other way that I first tried which didn't work?
Offline cubus

Junior Member


Medals: 2



« Reply #147 - Posted 2013-04-05 23:43:29 »

the methods with the streamFromFile flag DO stream, you can see in in the source code Wink
the temp file (which is used for streaming) should be in your temp folder, it gets created with

1  
temp = File.createTempFile("tiny", "sound")

Offline tyeeeee1
« Reply #148 - Posted 2013-04-05 23:47:10 »

Found it, looks like the streaming is working properly then. I wonder what could be causing 40mb of ram to be used then since my program runs on a max of 20mb without TinySound being used at all. Is 40mb just for TinySound normal or...?

Edit: Also, are the temp TinySound files supposed to be deleted? I have around 15 5mb files in my temp folder just for it.
Offline Sammidysam
« Reply #149 - Posted 2013-04-06 02:18:58 »

I have 1.82 GB of undeleted TinySound garbage.  I feel like maybe deleting the files wasn't originally accounted for in the source?  If so, you (kuusisto) should implement a basic scanner to scan for old undeleted files and delete them, like in this code bit:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
private void scanAndDeleteOldFiles(String name, String suffix){
   // if the download is aborted, a temporary file will be left behind. this method deletes all temporary files left behind in the past
  DirectoryStream<Path> ds = null;
   try {
      ds = Files.newDirectoryStream(Paths.get(System.getProperty("java.io.tmpdir")), name + '*' + suffix);
      for(Path file : ds){
         if(file.toFile().delete())
            System.out.println("Old file " + file.toFile().getAbsolutePath() + " deleted successfully.");
         else
            System.out.println("Old file " + file.toFile().getAbsolutePath() + " denied being deleted. That evil file!");
      }
   } catch (IOException e) {
      ErrorLogger.logError(e);
      return;
   } finally {
      try {
         ds.close();
      } catch (IOException e) {
         ErrorLogger.logError(e);
         return;
      }
   }
}
Pages: 1 ... 3 4 [5] 6
  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.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!