Computer Science 252
Problem Solving with Java
Spring 2016, The College of Saint Rose
SoundPlayerDemo BlueJ Project
Click here to download a BlueJ project for SoundPlayerDemo.
SoundPlayerDemo Source Code
The Java source code for SoundPlayerDemo is below. Click on a file name to download it.
import objectdraw.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/*
* Example SoundPlayerDemo: example of how to use the sound player
* from jfugue.org.
*
* This program uses a library called ``JFugue'' from David Koelle to
* make music. Much like objectdraw, this library is not part of a standard
* Java or BlueJ installation, so you will need to add it.
*
* First, visit http://www.jfugue.org/download.html and download the first
* file in the list (jfugue-4.0.3.jar). Save this to your account and then
* add it to BlueJ's list of libraries. You can accomplish this by going
* into BlueJ's preferences, choosing ``Libraries'', then adding the path to
* the JFugue library to the list of User Libraries. After doing this, quit
* and restart BlueJso it can load the library correctly.
*
* Jim Teresco, The College of Saint Rose, CSC 252, Fall 2014
*
* $Id: SoundPlayerDemo.java 2478 2014-11-10 03:15:16Z terescoj $
*/
// To use this library, we need an additional import:
import org.jfugue.*;
public class SoundPlayerDemo extends objectdraw.Controller implements ActionListener {
private JButton playC, playF, playScale, playFrere;
// The JFugue synthesizer player.
private Player player = new Player();
public void begin() {
// we have no canvas in a Controller (as opposed to a WindowController)
// so we'll use a different layout manager to get a bunch of buttons
// we can press
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(4,1));
// and some buttons
playC = new JButton("Play short C on Contrabass");
playC.addActionListener(this);
contentPane.add(playC);
playF = new JButton("Play long F on Flute");
playF.addActionListener(this);
contentPane.add(playF);
playScale = new JButton("Play Scale on Piano");
playScale.addActionListener(this);
contentPane.add(playScale);
playFrere = new JButton("Play Frere Jacques");
playFrere.addActionListener(this);
contentPane.add(playFrere);
}
public void actionPerformed(ActionEvent e) {
// see http://jfugue.org/examples.html for more about what can go in the
// "MusicString"s that get passed to these to play music.
if (e.getSource() == playC) {
player.play(new Pattern("I[CONTRABASS] C3i"));
}
else if (e.getSource() == playF) {
player.play(new Pattern("I[Flute] F4w"));
}
else if(e.getSource() == playScale) {
player.play(new Pattern("I[Piano] C5w D5w E5w F5w G5w A6w B6w C6w"));
}
else {
// example from http://jfugue.org/examples.html
// "Frere Jacques"
Pattern pattern1 = new Pattern("C5q D5q E5q C5q");
// "Dormez-vous?"
Pattern pattern2 = new Pattern("E5q F5q G5h");
// "Sonnez les matines"
Pattern pattern3 = new Pattern("G5i A5i G5i F5i E5q C5q");
// "Ding ding dong"
Pattern pattern4 = new Pattern("C5q G4q C5h");
// Put all of the patters together to form the song
Pattern song = new Pattern();
song.add(pattern1, 2); // Adds 'pattern1' to 'song' twice
song.add(pattern2, 2); // Adds 'pattern2' to 'song' twice
song.add(pattern3, 2); // Adds 'pattern3' to 'song' twice
song.add(pattern4, 2); // Adds 'pattern4' to 'song' twice
// Play the song!
player.play(song);
}
}
}