aspose file tools*
The moose likes Java in General and the fly likes MIDI Network Program Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "MIDI Network Program Error" Watch "MIDI Network Program Error" New topic
Author

MIDI Network Program Error

Kartik Nibjiya
Greenhorn

Joined: Feb 26, 2005
Posts: 1
My Questions :

1. How To Make The Program Run So That On A Single Machine As Its Not Running When I Am Running Server First And Then Client ?? Whats Wrong In Code ??

2. How To Listen To Various Sound Effects Created By Checkboxes ???

3. I Cant Hear Any Effects Of These 256 Checkboxes... Why ?



import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.sound.midi.*;
import java.util.*;
import java.awt.event.*;
import java.net.*; // Class Socket is in java.net
import javax.swing.event.*;

public class BeatBoxFinal implements MetaEventListener {
public BeatBoxFinal() {
buildGUI();
}

JFrame theFrame;
JPanel mainPanel;
JButton submitMessage;
//JTextField message;
JTextArea chatArea;
JList incomingList;
JTextField userMessage;
ArrayList checkboxList;
int bpm = 120;
int nextNum;
Vector listVector = new Vector();
String userName;
ObjectOutputStream out;
ObjectInputStream in;

/*
A HashMap is like an ArrayList except you get to store a key/velue pair.
This is used to store the incoming messages from the server. The key is
the String message from the server. The value is the pattern associated
with that message.
*/

HashMap otherSeqsMap = new HashMap();
Sequencer sequencer;
Sequence sequence;
Sequence mySequence = null;

Track track;

// Instrument names come from this array

String[] instrumentNames = {
"Bass Drum", "Closed Hi-Hat", "Open Hi-Hat", "Acoustic Snare", "Crash Cymbal", "Hand Clap", "High Tom", "Hi Bongo", "Maracas", "Whistle", "Low Conga", "Cowbell", "Vibraslap", "Low-mid Tom", "High Agogo", "Open Hi Conga"
};

int[] instruments = {
35, 42, 46, 38, 49, 39, 50, 60, 70, 72, 64, 56, 58, 47, 67, 63
};

public static void main(String[] args) {

new BeatBoxFinal().startUp((args.length == 0)? "standard" : args[0]);
}

public void startUp(String name) {
userName = name;
// Open connection to the server
try {
/*
Make a Socket connection to the server. 4242
is the port number. 127.0.0.1 is the IP address
for "localhost". This can be changed in order to
run the programs from different machines.
*/

Socket sock = new Socket("127.0.0.1", 4242);
out = new ObjectOutputStream(sock.getOutputStream());
in = new ObjectInputStream(sock.getInputStream());

Thread remote = new Thread(new RemoteReader());
remote.start();
}

catch(Exception ex) {
System.out.println("Couldn't connect - you'll have to play alone");
}

setUpMidi();
buildGUI();
}

public void buildGUI() {
theFrame = new JFrame("Cyber BeatBox");
BorderLayout layout = new BorderLayout();
JPanel background = new JPanel(layout);
background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

checkboxList = new ArrayList();

Box buttonBox = new Box(BoxLayout.Y_AXIS);

submitMessage = new JButton("Submit message");
submitMessage.addActionListener(new MySubmitMessageListener());
buttonBox.add(submitMessage);

// message = new JTextField();
// buttonBox.add(message);

chatArea = new JTextArea();
buttonBox.add(chatArea);

/*
Create buttons and checkboxes. Add the buttons to the
GUI and add ActionListeners to them. Instead of passing
(this) to the buttons ActionListener registration method,
pass a new instance of the appropiate listener class.

The BoxLayout constructor needs to know the component its
laying out and the axis - the buttonBox and y axis.
*/

JButton start = new JButton("Start");
start.addActionListener(new MyStartListener());
buttonBox.add(start);

JButton stop = new JButton("Stop");
stop.addActionListener(new MyStopListener());
buttonBox.add(stop);

JButton upTempo = new JButton("Tempo up");
upTempo.addActionListener(new MyUpTempoListener());
buttonBox.add(upTempo);

JButton downTempo = new JButton("Tempo down");
downTempo.addActionListener(new MyDownTempoListener());
buttonBox.add(downTempo);

JButton sendIt = new JButton("Send it");
sendIt.addActionListener(new MySendListener());
buttonBox.add(sendIt);

userMessage = new JTextField();
buttonBox.add(userMessage);

incomingList = new JList();
incomingList.addListSelectionListener(new MyListSelectionListener());
incomingList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane theList = new JScrollPane(incomingList);
buttonBox.add(theList);
incomingList.setListData(listVector);

Box nameBox = new Box(BoxLayout.Y_AXIS);

for(int i = 0; i < 16; i++) {
nameBox.add(new Label(instrumentNames));
}

background.add(BorderLayout.EAST, buttonBox);
background.add(BorderLayout.WEST, nameBox);

theFrame.getContentPane().add(background);

GridLayout grid = new GridLayout(16, 16);
grid.setVgap(1);
grid.setHgap(2);
mainPanel = new JPanel(grid);
background.add(BorderLayout.CENTER, mainPanel);

for (int i = 0; i < 256; i++) {
JCheckBox c = new JCheckBox();
c.setSelected(false);
checkboxList.add(c);
mainPanel.add(c);
}

theFrame.setBounds(50, 50, 300, 300);
theFrame.pack();
theFrame.setVisible(true);
}

public void setUpMidi() {
try {
// Get the sequencer, make a sequence and make a track

/*
The sequencer object is the main part of the MIDI
device/instrument being used. It's the thing that
"sequences" all the MIDI information into a song.
*/

sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.addMetaEventListener(this);

// Make a sequence and a track
sequence = new Sequence(Sequence.PPQ, 4);
track = sequence.createTrack();

sequencer.setTempoInBPM(bpm);
}
catch(Exception e) {
e.printStackTrace();
}
}

public void buildTrackAndStart() {
/*
Build a track by walking through the checkboxes to
get their state, and mapping that to an instrument
(and making the MidiEvent for it).
*/

ArrayList trackList = null;
sequence.deleteTrack(track);
track = sequence.createTrack();

for(int i = 0; i < 16; i++) {
trackList = new ArrayList();
for(int j = 0; j < 16; j++) {
JCheckBox jc = (JCheckBox) checkboxList.get(j + (16 * i));
if(jc.isSelected()) {
int key = instruments;
trackList.add(new Integer(key));
}
else {
trackList.add(null);
}
}
makeTracks(trackList);
}
track.add(makeEvent(192, 9, 1, 0, 15));
try {
// Start it running
sequencer.setSequence(sequence);
sequencer.start();
sequencer.setTempoInBPM(bpm);
}

catch(Exception e) {
e.printStackTrace();
}
}

// The GUI listeners

/*
Implement the ActionListener interfaces ActionPerformed()
method. This is the event-handling method.
*/

public class MyStartListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
buildTrackAndStart();
}
}

public class MyStopListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
sequencer.stop();
}
}

public class MyUpTempoListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
bpm += 3;
}
}

public class MyDownTempoListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
bpm -= 3;
}
}

public class MySubmitMessageListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
if(a.getSource() == submitMessage) {
System.out.println("Message submitted");
chatArea.setText(userMessage.getText());
}
}
}

public class MySendListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
// Make an arrayList of just the STATE of the checkboxes
boolean[] checkboxState = new boolean[256];
for(int i = 0; i < 256; i++) {
JCheckBox check = (JCheckBox) checkboxList.get(i);
if(check.isSelected()) {
checkboxState = true;
}
}

/*
Serialize two objects (the String message and the beat pattern)
and write those two objects to the socket output stream to the server
*/

String messageToSend = null;

try {
out.writeObject(userName + nextNum++ + ": " + userMessage.getText());
out.writeObject(checkboxState);
}
catch(Exception ex) {
System.out.println("Sorry! Could not sent it to the server!");
}

userMessage.setText("");
}
}

/*
ListSelectionListener tells is when the user made a selection on the
list of messages. When the user selects a message, we immediately
load the associated beat pattern and start playing it.
*/

public class MyListSelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent le) {
if(!le.getValueIsAdjusting()) {
String selected = (String) incomingList.getSelectedValue();
if(selected != null) {
boolean[] selectedState = (boolean[]) otherSeqsMap.get(selected);
changeSequence(selectedState);
sequencer.stop();
buildTrackAndStart();
}
}
}
}

/*
This is the thread work - reading in data from the server.
The data in this program will always be the two serialized
objects: the String message and the beat pattern - an
ArrayList of cjeckbox state values.

When a message comes in, we read or deserialize the two objects
(the message and the ArrayList of Boolean checkbox state values)
and add it to the JList component.

Adding to a JList is two-step process: you keep a Vector of the
lists data and then tell the JList to use that Vector as it's
source for what to display in the list.
*/

public class RemoteReader implements Runnable {
boolean[] checkboxState = null;
String nameToShow = null;
Object obj = null;
public void run() {
try {
while((obj = in.readObject()) != null) {
String nameToShow = (String) obj;
checkboxState = (boolean[]) in.readObject();
otherSeqsMap.put(nameToShow, checkboxState);
listVector.add(nameToShow);
incomingList.setListData(listVector);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}

/*
This method is called when the user selects something
from the list. The pattern is immediately changed to
the one the user selected.
*/

public void changeSequence(boolean[] checkboxState) {
for(int i = 0; i < 256; i++) {
JCheckBox check = (JCheckBox) checkboxList.get(i);
check.setSelected(checkboxState);
}
}

public void makeTracks(ArrayList list) {
Iterator it = list.iterator();
// Detect which key was pressed
for(int i = 0; i < 16; i++) {
Integer num = (Integer) it.next();
if(num != null) {
int numKey = num.intValue();
// This is where the beat is picked up from.
track.add(makeEvent(144, 9, numKey, 100, i));
track.add(makeEvent(128, 9, numKey, 100, i + 1));
}
}
}

// Add the four arguments for the message

public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
MidiEvent event = null;
try {
// Make the message and the event, using the parameters
// Make a message instance
ShortMessage a = new ShortMessage();
// Call setMessage() with the instructions
a.setMessage(comd, chan, one, two);
// Make a MidiEvent instance for the message
event = new MidiEvent(a, tick);
} catch(Exception e) {
}

// Return the event

return event;
}

public void meta(MetaMessage message) {
if(message.getType() == 47) {
sequencer.start();
sequencer.setTempoInBPM(bpm);
}
}
}



Server Code :



import java.io.*;
import java.net.*;
import java.util.*;

public class MusicServer
{
ArrayList clientOutputStreams;

public static void main(String[] args)
{
new MusicServer().go();
}

public class ClientHandler implements Runnable
{
ObjectInputStream in;
Socket clientSocket;

public ClientHandler(Socket socket)
{
try
{
clientSocket = socket;

in = new ObjectInputStream(clientSocket.getInputStream());
}

catch(Exception ex)
{
ex.printStackTrace();
}
}

public void run()
{
Object o2 = null;
Object o1 = null;

try
{
while((o1 = in.readObject()) != null)
{
o2 = in.readObject();

System.out.println("Read two objects");
tellEveryone(o1, o2);
}
}

catch(Exception ex)
{
ex.printStackTrace();
}
}
}

public void go()
{
clientOutputStreams = new ArrayList();

try
{
ServerSocket serverSock = new ServerSocket(4242);

while(true)
{
Socket clientSocket = serverSock.accept();
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
clientOutputStreams.add(out);

Thread t = new Thread(new ClientHandler(clientSocket));
t.start();

System.out.println("Got a connection");
}
}

catch(Exception ex)
{
ex.printStackTrace();
}
}

public void tellEveryone(Object one, Object two)
{
Iterator it = clientOutputStreams.iterator();

while(it.hasNext())
{
try
{
ObjectOutputStream out = (ObjectOutputStream) it.next();
out.writeObject(one);
out.writeObject(two);
}

catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi! Yet another first post! Welcome to the ranch!

Your best bet around here is to isolate a smallish bit of code where you think things are going wrong. Can you verify some of this works and try to figure out exactly where it doesn't? Use the "Code" button below the text entry box to preserve your code format - your indenting will make it easier for us to read!

I did some MIDI programming in Pascal many years ago but none in Java; I'll be interested to see how you get along with this. Try to post some little bits and we'll surely read them with you.

Cheers!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
Consider Paul's rocket mass heater.
 
subject: MIDI Network Program Error