Skip to content

Instantly share code, notes, and snippets.

@pbloem
Last active March 18, 2024 20:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbloem/d29bf80e69d333415622 to your computer and use it in GitHub Desktop.
Save pbloem/d29bf80e69d333415622 to your computer and use it in GitHub Desktop.
A simple java program, showing how to play a tune.
import java.util.Arrays;
import java.util.List;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Synthesizer;
import javax.sound.midi.MidiChannel;
/**
* A little example showing how to play a tune in Java.
*
* Inputs are not sanitized or checked, this is just to show how simple it is.
*
* @author Peter
*/
public class Synth {
private static List<String> notes = Arrays.asList("C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B");
private static MidiChannel[] channels;
private static int INSTRUMENT = 0; // 0 is a piano, 9 is percussion, other channels are for other instruments
private static int VOLUME = 80; // between 0 et 127
public static void main( String[] args ) {
try {
// * Open a synthesizer
Synthesizer synth = MidiSystem.getSynthesizer();
synth.open();
channels = synth.getChannels();
// * Play some notes
play("6D", 1000);
rest(500);
play("6D", 300);
play("6C#", 300);
play("6D", 1000);
rest(500);
play("6D", 300);
play("6C#", 300);
play("6D", 1000);
play("6E", 300);
play("6E", 600);
play("6G", 300);
play("6G", 600);
rest(500);
// * finish up
synth.close();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Plays the given note for the given duration
*/
private static void play(String note, int duration) throws InterruptedException
{
// * start playing a note
channels[INSTRUMENT].noteOn(id(note), VOLUME );
// * wait
Thread.sleep( duration );
// * stop playing a note
channels[INSTRUMENT].noteOff(id(note));
}
/**
* Plays nothing for the given duration
*/
private static void rest(int duration) throws InterruptedException
{
Thread.sleep(duration);
}
/**
* Returns the MIDI id for a given note: eg. 4C -> 60
* @return
*/
private static int id(String note)
{
int octave = Integer.parseInt(note.substring(0, 1));
return notes.indexOf(note.substring(1)) + 12 * octave + 12;
}
}
@farmazon3000
Copy link

cool, it even works :P

@IndyRishi
Copy link

Do I have permission to use some of this code to help someone in a Bounty on Replit? I will credit you of course.

@pbloem
Copy link
Author

pbloem commented Apr 13, 2023

Sure, go for it. The above is hereby released under an MIT license.

@IndyRishi
Copy link

Ok, thank you so much!

@nihalpoosa
Copy link

Playing is cool. But can we write this to a file like wav instead of playing it?

@pbloem
Copy link
Author

pbloem commented May 1, 2023

Capturing audio is described in the docs here https://docs.oracle.com/javase/tutorial/sound/capturing.html It seems a bit more involved. maybe stackexchange has some ready-to-use code.

@nihalpoosa
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment