Birthday wish in Java style – a java sound example

The Java Sound API can be used for controlling audio playback, audio capture, MIDI synthesis, and basic MIDI sequencing.
The following example shows how to use  Java MIDI synthesizer in its simplest form.

[java]
import javax.sound.midi.*;

public class Doremi {
public static void main(String[] args) {
int[] notes = new int[]{60, 62, 64, 65, 67, 69, 71, 72, 72, 71, 69, 67, 65, 64, 62, 60};
//plays Do Re Mi Fa (western) // Sa Re Ga Ma (indian)
int delay=50;
try {
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
MidiChannel channel = synthesizer.getChannels()[0];

for (int note : notes) {
channel.noteOn(note, delay);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
break;
} finally {
channel.noteOff(note);
}
}
} catch (MidiUnavailableException e) {
e.printStackTrace();
}
}
}
[/java]

Though its gives full control but it is complex to use this API.
JFugue is an open-source Java API for programming music without the complexities of MIDI.
The following is a example of playing notes of “Happy Birth Day” with JFugue

[java]
package january.tushar.birthday;

import org.jfugue.Pattern;
import org.jfugue.Player;

public class Main {
public static void main(String[] args) {
Player player = new Player();
Pattern pattern = new Pattern("Ci D C F Ew Ci D C G Fw Ci C6 A F E Dw Bb Bb A F G Fw");
System.out.println("Happy Birthday to you Tushar.\nPlaying piano…");
player.play(pattern);
System.out.println(":-)");
System.exit(0);
}
}
[/java]

The sample eclipse project can be downloaded from here https://github.com/tushroy/HappyBirthDaySound. Also the compiled JAR file can be downloaded and run with “java -jar Happy-Birthday-Tush.jar” command. It is the birthday wish for someone geeky in java programming style 😉 .

Incoming search terms:

  • happy birthday java program
  • java program for birthday wishes
  • birthday wishes using java code
  • happy birthday program in java
  • birthday wishes in programming style
  • Java program to wish happy Birthday
  • how to write a programs for bday wish

Tushar Roy

Software Engineer (Backend engineer), Android Developer,

Leave a Reply

Your email address will not be published. Required fields are marked *


*