The Artima Developer Community
Sponsored Link

Java Answers Forum
how to listen to various effects of this program on midi files ??

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Kartik

Posts: 2
Nickname: kartik1
Registered: Feb, 2005

how to listen to various effects of this program on midi files ?? Posted: Feb 26, 2005 5:32 AM
Reply to this message Reply
Advertisement
My Questions :

1.how to run the client program and server program on same machine ( plz give detailed info ) ??

2. how to listen to various sound effects of this program on midi files ?? ( Detailed procedure )

Client Code :

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 = {
"bass1", "bass2", "drum1", "drum2", "hihat"
};

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 < 5; i++) {
nameBox.add(new Label(instrumentNames));
}

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

theFrame.getContentPane().add(background);

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

for (int i = 0; i < 50; 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 < 5; i++) {
trackList = new ArrayList();
for(int j = 0; j < 5; j++) {
JCheckBox jc = (JCheckBox) checkboxList.get(j + (5 * 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[50];
for(int i = 0; i < 50; 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 < 50; 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 < 5; 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();
}
}
}
}

Topic: Graphics Previous Topic   Next Topic Topic: PDF document in Servlets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use