• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a way to produce sound with Java?  RSS feed

 
Carey Brown
Saloon Keeper
Posts: 2644
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "rsltn" ?
 
Kevin Simonson
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What is an example of command line arguments that you're using?
Edit: sorry I missed your comment at the end.

java Synth 96 6 0 127 6500
 
Kevin Simonson
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What is "rsltn" ?

Resolution. I guess I should have spelled it out.
 
Kevin Simonson
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Simonson wrote:
Carey Brown wrote:What is an example of command line arguments that you're using?
Edit: sorry I missed your comment at the end.

java Synth 96 6 0 127 6500

Although I think what I'm going to go with for my game is probably:

java Synth 96 7 0 127 5500
 
Carey Brown
Saloon Keeper
Posts: 2644
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might consider dropping the last command line argument and changing your sleep() call to

 
Stephan van Hulst
Saloon Keeper
Posts: 7385
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you shouldn't be using Thread.sleep() at all. If you want to block the current thread until the track has finished playing, you should use locks and conditions and add a meta event listener to the sequencer.
 
Kevin Simonson
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Actually, you shouldn't be using Thread.sleep() at all. If you want to block the current thread until the track has finished playing, you should use locks and conditions and add a meta event listener to the sequencer.

Stephan, can you tell me where I can go to find out how to "use locks and conditions" and adding "a meta event listener to the sequencer"? Are those things new to Java 8 or Java 9? I'm mostly familiar with Java 6 and 7, and I don't remember anything about locks, conditions, or meta event listeners.
 
Campbell Ritchie
Sheriff
Posts: 54536
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads and locks go back to Java1.0.0. Try the Java™ Tutorials. Don't know about meta‑listeners.
 
Stephan van Hulst
Saloon Keeper
Posts: 7385
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The high-level concurrency library was added in Java 5. The classes I'm referring to specifically are java.util.concurrent.locks.ReentrantLock and java.util.concurrent.locks.Condition. You can create a class that is responsible for playing a sequence and blocking until it's done:
 
Stephan van Hulst
Saloon Keeper
Posts: 7385
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, let's say that you expanded your application and you want to start playing your melody in the background when something happens and you want to continue doing things in the mean time, but when the application has finished, you want it to wait until the melody has finished playing. You would then use an ExecutorService to play your melody. Add this method to the SequencePlayer:

As long as the threads in the executor service are not daemons, any melodies that started playing will continue playing when the executor service is shut down. That will give you very gracious termination of your application.

Note that I haven't tested this code or that in the previous post. There may be some mistakes I haven't thought of.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!