How to loop a Midi sequence Java - java

I am fairly new to Java programming, and would like to know how to properly loop a MIDI sequence whilst a game is playing. I have some code and I know that I should use setLoopCount() in order to do it but am unsure how to implement it.
Here is the code I have so far
Sequencer myseq;
myseq = MidiSystem.getSequencer();
myseq.open();
File myMidiFile = new File("sounds/music.midi");
Sequence supersequence = MidiSystem.getSequence(myMidiFile);
myseq.setSequence(supersequence);
myseq.start();
Any help is appreciated.

I think this could help you:
myseq.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);

You don't need an external method to loop a Midi Sequence. Based on your code above you should use the following:
import java.io.*;
import java.util.*;
import javax.sound.midi.*;
public class Midiplayer{
public static void main(String[] args) throws InvalidMidiDataException, IOException, MidiUnavailableException {
//Create scanner object
Scanner in= new Scanner(System.in);
//Request Loop Count
System.out.println("How Many Loops?");
int loops= in.nextInt();
//Retrieve the MIDI File
System.out.println("Please type in the exact location of your midi:");
String fileAndLocation= in.next();
Sequence myseq = MidiSystem.getSequence(new File(fileAndLocation));
// Create a sequencer for the sequence
final Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(myseq);
sequencer.setLoopCount(loops);
//Exit message
System.out.println("Press 'ctrl' and 'c' on keyboard simultaneously to end sequence and program.");
// Start playback, repeats automatically
sequencer.start();
//Don't forget to close reader
in.close();
}
}
Do note though that the above does not create an infinite loop. My guess is that you need to set "loops" to 1 and increment it within a while statement that tests like the following:
while(loops>0){
//Start/restart Midi Sequence
sequencer.start();
//increment loops
loops++;
//Re-instantiate .setLoopCount()
sequencer.setLoopCount(loops);
}
If the loop determination is user based, then have the user input 0 in in the place of infinity so that:
if(loops==0){
//Declare and instantiate a boolean to test
boolean infinite=true;
//Make loops equal to 1 so that the later while statement initiates and repeates
loops++;
//Re-instantiate .setLoopCount()
sequencer.setLoopCount(loops);
}
if(infinite==true){
while(loops>0){
//Start/restart Midi Sequence
sequencer.start();
//increment loops
loops++;
//Re-instantiate .setLoopCount(), thus always adding a loop to setLoopCount(), making it infinite because loops will always be greater than zero
sequencer.setLoopCount(loops);
}
}
else{
//Starts sequence and repeats according to defined loop while loops>0 (so long as you have an error cather for loops<0)
sequencer.start();
}
To end the user interface for an infinite loop, at some point remind the user that in COMMAND PROMPT, 'ctrl'+'c' quits the program.
I hope this helps a lot of people (I based it off my own midi program, which works). However, do note that anything about an infinite loop I have not yet tested. It is only theory based on heavy analysis of the situation and the available classes and variables.
Do note that .setLoopCounter() is present in Java 1.4.1 and on.
Thank you and I am glad to help.

There is a good (but old) book about java game development: http://www.brackeen.com/javagamebook/ The source code is available at the website. Check out the chapter 4... In the SoundManagerTest.java file, you will find an example about looping midi sounds. I hope it is not too outdated.
Btw: There seems to be an issue with MIDI looping and Java 5. Have a look at the end of the page:
Sound Issues in Java 5 and Java 6.
Sun updated the sound engine in Java 5 which led to a few problems. Here are the fixes:
MIDI music doesn't loop. Add this line (in bold) in MidiPlayer.java:
public void meta(MetaMessage event) {
if (event.getType() == END_OF_TRACK_MESSAGE) {
if (sequencer != null && sequencer.isOpen() && loop) {
sequencer.setTickPosition(0);
sequencer.start();
}
}
}

Add a listener for the end of the song like this:
myseq.addMetaEventListener(new MetaEventListener() {
public void meta(MetaMessage msg) {
if (msg.getType() == 0x2F) { // End of track
// Restart the song
sequencer.setTickPosition(0);
sequencer.start();
}
}
});
This is also useful if you want to create a playlist and have it continue to the next song.

Related

using a loop to invoke multiple methods

I am newish to Java and trying to building a small rocket program.
I have 3 distinct methods that change the size and colour of the rockets exhaust jet on the graphical display when invoked which work great individually.
public void pulse1()
{
jet.setDiameter(6);
jet.setColour(OUColour.RED);
jet.setXPos(58);
}
public void pulse2()
{
jet.setDiameter(12);
jet.setColour(OUColour.ORANGE);
jet.setXPos(55);
}
public void pulse3()
{
jet.setDiameter(24);
jet.setColour(OUColour.RED);
jet.setXPos(48);
}
However, what I am trying to do is code another method ignition() that uses some sort of loop to invoke each of the three pulse methods in that chronological order a maximum of 5 times with a 500 millisecond delay between each call. (the idea being to simulate on the graphical display the firing up of the rockets engines)
Thus far I have tried the following without success.
public void ignition()
{
pulse1();
delay(500); // uses the inbuilt delay method
pulse2();
delay(500);
pulse3();
}
In Java, a loop will execute the contents of a code block. A code block is anything between two curly braces.
{
statement1;
statement2;
} // statement2 and statement2 are both inside the code block
So, when you declare a loop (perhaps with for or while), the loop will act on the very next code block. You can simply call the delay function once each time within the loop block, and it will wait once per loop.
A way to achieve what you are talking about using a for loop might be like so
public void ignition() {
for(int i = 0; i < 5; i++) {
pulse1();
delay(500); // uses the inbuilt delay method
pulse2();
delay(500);
pulse3();
delay(500);
}
EDIT: Misinterpreted what OP wanted to loop through
As you are not definite on how many number of times you should traverse in a loop but have a maximum limit of 5, use a random no. generator.
int i = rand.nextInt(5) + 1; //1 is minimum and 5 is maximum
int a=0;
while(a<i){
pulse1();
delay(500); // uses the inbuilt delay method
pulse2();
delay(500);
pulse3();
a++;
}
You could also use Thread.sleep(500) if your delay method is giving you issues.

Java: Running potentially blocking code

I am developing a small game, (Java, LibGdx) where the player fills cloze-style functions with predefined lines of code. The game would then compile the code and run a small test suite to verify that the function does the stuff it is supposed to.
Compiling and running the code already works, but I am faced with the problem of detecting infinite loops. Consider the following function:
// should compute the sum of [1 .. n]
public int foo(int n) {
int i = 0;
while (n > 0) {
i += n;
// this is the place where the player inserts one of many predefined lines of code
// the right one would be: n--;
// but the player could also insert something silly like: i++;
}
return i;
}
Please note that the functions actually used may be more complex and in general it is not possible to make sure that there cannot be any infinite loops.
Currently I am running the small test suite (provided for every function) in a Thread using an ExecutorService, setting a timeout to abort waiting in case the thread is stuck. The problem with this is, that the threads stuck in an endless loop will run forever in the background, which of course will at some point have a considerable impact on game performance.
// TestClass is the compiled class containing the function above and the corresponding test suite
Callable<Boolean> task = new Callable<Boolean>() {
#Override
public Boolean call() throws Exception {
// call the test suite
return new TestClass().test();
}
};
Future<Boolean> future = executorService.submit(task);
try {
Boolean result = future.get(100, TimeUnit.MILLISECONDS);
System.out.println("result: " + (result == null ? "null" : result.toString()));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
future.cancel(true);
}
My question is now: How can I gracefully end the threads that accidentally spin inside an endless loop?
*EDIT To clarify why in this case, preventing infinite loops is not possible/feasable: The functions, their test suite and the lines to fill the gaps are loaded from disk. There will be hundrets of functions with at least two lines of code that could be inserted. The player can drag any line into any gap. The effort needed to make sure no combination of function gap/code line produces something that loops infinitely or even runs longer than the timeout grows exponentially with the number of functions. This quickly gets to the point where nobody has the time to check all of these combinations manually. Also, in general, determining, whether a function will finish in time is pretty much impossible because of the halting problem.
There is no such thing as "graceful termination" of a thread inside the same process. The terminated thread can leave inconsistent shared-memory state behind it.
You can either organize things so that each task is started in its own JVM, or make do with forceful termination using the deprecated Thread.stop() method.
Another option is inserting a check into the generated code, but this would require much more effort to implement properly.
The right way is to change the design and avoids never ending loops.
For the time being, inside your loop you could check if the thread is interrupted some way by: isInterrupted() or even isAlive().
And if it is you just exit.
It is not normal to have a never ending loop if it not wanted.
To solve the problem You can add a counter in the loop and if you reach a limit you can exit.
int counter = 0;
while (n > 0) {
counter++;
if (counter > THRESHOLD) {
break;
}
i += n;
// this is the place where the player inserts one of many predefined lines of code
// the right one would be: n--;
// but the player could also insert something silly like: i++;
}

Thread, while loop and a System.out.print statement

Hi guys I have a nested Thread and using it to see when a player has taken a turn (in a draughts game) O have a game object and a public variable changedTurns.
new Thread() {
public void run(){
check();
}
private void check(){
boolean sent = false;
while(true){
System.out.println("ee"); \\line 9
if(game.changedTurns){
System.out.println("gg");
if(!sent){
System.out.println("ok");
sent =true;
}
}
}
}.start();
Everything works as expected like above I.e console shows plenty of "ee" followed by "gg" and then "ok" when the player takes their turn, however without line 9 nothing is shown when the player takes their turn ?!
You don't need to use threading to analyze whether the player has taken a turn. Just create a Player[] array and add a variable turn for the index of the player whose turn it is. Increment turn when a Player makes a turn.

java while loop is not counting correctly

Imma java newbie so I try to learn things. I created a boolean method, it's a small craps game. I need this method is called from main method and keep playing while return value is true (game is won) but stop executing if return value is false. I also need how many times player won. So I created something like this:
while(Craps.play())
{
Craps.play();
i++;
}
System.out.println("In total you won " + i + " times");
i is initialized as private static int i = 0; //Game counter
But output didn't see me correct. First of all, if the game is lost (return value is false) it wo't stop execution. And it doesn't count winning number correct. For example if 3 games are won, it calculates if he won 2 games.
Is there any logic error with this loop?
Thanks
You shouldn't call Craps.play() inside the loop, since you will miss validations. Also, in your original code, you are calling Craps.play() twice, but you are incremeting i only once. Change your code to this and it should work as expected:
while(Craps.play()) {
i++;
}

controlling threads flow

I had a task to write simple game simulating two players picking up 1-3 matches one after another until the pile is gone. I managed to do it for computer choosing random value of matches but now I'd like to go further and allow humans to play the game. Here's what I already have : http://paste.pocoo.org/show/201761/
Class Player is a computer player, and PlayerMan should be human being. Problem is, that thread of PlayerMan should wait until proper value of matches is given but I cannot make it work this way. Logic is as follows: thread runs until matches equals to zero. If player number is correct at the moment function pickMatches() is called. After decreasing number of matches on table, thread should wait and another thread should be notified. I know I must use wait() and notify() but I can't place them right.
Class Shared keeps the value of current player, and also amount of matches.
public void suspendThread() {
suspended = true;
}
public void resumeThread() {
suspended = false;
}
#Override
public void run(){
int matches=1;
int which = 0;
int tmp=0;
Shared data = this.selectData();
String name = this.returnName();
int number = this.getNumber();
while(data.getMatches() != 0){
while(!suspended){
try{
which = data.getCurrent();
if(number == which){
matches = pickMatches();
tmp = data.getMatches() - matches;
data.setMatches(tmp, number);
if(data.getMatches() == 0){
System.out.println(" "+
name+" takes "+matches+" matches.");
System.out.println("Winner is player: "+name);
stop();
}
System.out.println(" "+
name+" takes "+matches+" matches.");
if(number != 0){
data.setCurrent(0);
}
else{
data.setCurrent(1);
}
}
this.suspendThread();
notifyAll();
wait();
}catch(InterruptedException exc) {}
}
}
}
#Override
synchronized public int pickMatches(){
Scanner scanner = new Scanner(System.in);
int n = 0;
Shared data = this.selectData();
System.out.println("Choose amount of matches (from 1 to 3): ");
if(data.getMatches() == 1){
System.out.println("There's only 1 match left !");
while(n != 1){
n = scanner.nextInt();
}
}
else{
do{
n = scanner.nextInt();
}
while(n <= 1 && n >= 3);
}
return n;
}
}
Well, let me first say that I think you are making this hardier than you need to. If it were me, I would create a 'GameMaster' class whose job it is to loop and tell each player when their turn comes up. Your player classes wouldn't have loops, just a takeTurn method. This way you can remove the waiting/notifying behavior from your player classes.
If you wish to keep the design you have, I would still get rid of the wait/notify and use a Semaphore. Check the docs for proper usage, but the gist is that you would remove the suspend/resume methods and have a acquire() call at the top of your loop and release at the bottom. Just make sure fairness is set to true in the constructor, that way you won't have to worry about a player taking two turns in a row by acquiring the semaphore lock twice in a row.
Ok, so I managed to do it without wait() etc.
http://paste.pocoo.org/show/201966/
When you find yourself having to set up communication between threads to synchronize their execution, just so that a specified sequence of events takes place (such as taking turns playing a game), it's a good sign that you may have more threads than you need.
In this case, consider a single thread that executes a takeTurn() method on various extensions of a Player class might make life easier for you. You could make Player an abstract base class that mandates .takeTurn(), then have HumanPlayer and MachinePlayer classes encapsulate the code that makes sense for each type of player inside that method. This should make extension to larger numbers of players relatively trivial as compared to lots of wait() and notify().

Categories