 |
|
 |

Como ya he dicho, es posible sintetizar los sonidos. Esto es lo que hace el próximo programa que hay que editar en el archivo sintetizador.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.yo.*;
import java.util.*;
import javax.sound.sampled.*;
import javax.sound.midi.*;
public class sintetizador extends JFrame
{
ent TIEMPO=50;
ent CANAL=0;
ent UNS=-1;
ent INSTRUMENTO=0;
Sintetizador SINTETIZADOR=new Sintetizador();
// GUI
JLabel teclado1=new JLabel(new ImageIcon("teclado1.gif"));
JLabel teclado2=new JLabel(new ImageIcon("teclado2.gif"));
JButton OkB=new JButton("Ok");
JComboBox Instrumentos=new JComboBox();
JComboBox Canales=new JComboBox();
JLabel uns=new JLabel("Última nota tocada");
JTextField msg=new JTextField(
"Ninguna nota"
);
JSlider SL=new JSlider();
public sintetizador()
{
setTitle("Sintetizador sonoro de Pietro Castellucci");
setupAspecto();
setupValores();
setupSucesos();
SINTETIZADOR.setInstrumento(
Instrumentos.getSelectedIndex(),CANAL);
setResizable(false);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
pack();
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d=t.getScreenSize();
Dimension win=getSize();
win=getSize();
setLocation(d.width/2-(win.width/2)-1,d.height/2-(win.height/2)-1);
show();
}
void setupAspecto()
{
teclado1.setCursor(new Cursor(Cursor.HAND_CURSOR));
teclado2.setCursor(new Cursor(Cursor.HAND_CURSOR));
OkB.setCursor(new Cursor(Cursor.HAND_CURSOR));
tastiera1.setToolTipText(
"Clícame para tocar"
);
tastiera2.setToolTipText(
"Clícame para tocar"
);
OkB.setToolTipText(
"Salir del programa"
);
JPanel P=new JPanel(new BorderLayout());
JPanel Panel=new JPanel(new GridLayout(2,1));
Panel.add(teclado1);
Panel.add(teclado2);
P.add(Panel,BorderLayout.CENTER);
JPanel SP=new JPanel(new FlowLayout());
Instrumentos.setBorder(
BorderFactory.createTitledBorder(
"Instrumentos"
)
);
Canales.setBorder(
BorderFactory.createTitledBorder(
"Canales"
)
);
SP.add(Instrumentos);
// SP.add(Canales);
SP.add(uns);
msg.setEditable(false);
msg.setBackground(P.getBackground());
SP.add(msg);
SP.add(OkB);
P.add(SP,BorderLayout.SOUTH);
SL.setBorder(BorderFactory.createTitledBorder(
"Presión "
));
SL.setOrientation(JSlider.VERTICAL);
SL.setMajorTickSpacing(25);
SL.setMaximum(100);
SL.setMinimum(0);
SL.setMinorTickSpacing(1);
SL.setPaintLabels(true);
SL.setPaintTicks(true);
SL.setPaintTrack(true);
SL.setSnapToTicks(true);
SL.setValue(TIEMPO);
P.add(SL,BorderLayout.EAST);
setContentPane(P);
}
void setupValores()
{
try
{
ent e=0;
while (true)
{
String nombre= SINTETIZADOR.Instrumentos[i++].getName();
Instrumentos.addItem(nombre);
}
}
catch (ArrayIndexOutOfBoundsException e)
{};
try
{
int i=0;
while (true)
{
String nombre="Canales"+" "+(i+1);
SINTETIZADOR.CANALES[i++].allNotesOff();
Canales.addItem(nombre);
}
}
catch (ArrayIndexOutOfBoundsException e)
{};
if (Instrumentos.getItemCount()>0)
Instrumentos.setSelectedIndex(INSTRUMENTO);
}
void setupSucesos()
{
teclado1.addMouseListener(new Teclado1Listener());
teclado2.addMouseListener(new Teclado2Listener());
Instrumentos.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
SINTETIZADOR.setInstrumento(
Instrumentos.getSelectedIndex(),CANALES);
}
}
);
Canales.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
CANALES=Canales.getSelectedIndex();
SINTETIZADOR.cambiaCanal(CANAL);
}
});
OkB.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("¡Gracias por haber tocado conmigo!");
System.exit(0);
}
}
);
}
// Sucesos
public class Teclado1Listener implements MouseListener
{
public void mouseClicked(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
// System.out.println("Teclado 1 Punto: (x="+e.getX()+",y="+e.getY()+")");
UNS=getNota(e.getX());
// System.out.println("Nota="+nota);
TIEMPO=SL.getValue();
SINTETIZADOR.tocar(UNS,TIEMPO,CANAL);
msg.setText(""+(UNS+1));
}
public void mouseReleased(MouseEvent e)
{}
}
public class Teclado2Listener implements MouseListener
{
public void mouseClicked(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
// System.out.println("Teclado 2 Punto: (x="+e.getX()+",y="+e.getY()+")");
UNS=64+getNota(e.getX());
// System.out.println("Nota="+nota);
TIEMPO=SL.getValue();
SINTETIZADOR.suona(UNS,TIEMPO,CANAL);
msg.setText(""+(UNS+1));
}
public void mouseReleased(MouseEvent e)
{}
}
// Utility fun
int getNota (int pos)
{
int nota;
nota=(pos/12);
return nota;
}
public static void main(String[] arg)
{
new sintetizador();
}
//*****************************************
// SOUND MANAGER
//*****************************************
//***********************************************************
// Sintetizador:
//***********************************************************
public class Sintetizador
{
private Synthesizer SYNT;
private Sequencer sequencer;
private Sequence seq;
private Soundbank BANK;
public Instrument[] Instrumentos;
public MidiChannel[] CANALES;
public Sintetizador()
{
setupSintetizador();
}
void setupSintetizador()
{
try
{
SYNT=MidiSystem.getSynthesizer();
sequencer=MidiSystem.getSequencer();
seq= new Sequence(Sequence.PPQ, 10);
SYNT.open();
BANK = SYNT.getDefaultSoundbank();
if (BANK != null)
Instrumentos = SYNT.getDefaultSoundbank().getInstruments();
else
Instrumentos = SYNT.getAvailableInstruments();
CANALES=SYNT.getChannels();
}
catch(MidiUnavailableException ecc){todonull();}
catch(InvalidMidiDataException ecc2){todonull();}
;
}
void todonull()
{
SYNT=null;
sequencer=null;
seq=null;
BANK=null;
Instrumentos=null;
}
public void setInstrumento(int str,int can)
{
SA=str;
int prog=Instrumentos[str].getPatch().getProgram();
CANALES[can].programChange(prog);
}
private int SA=0;
public void cambiaCanal(int can)
{
int prog=Instrumentos[SA].getPatch().getProgram();
CANALES[can].programChange(prog);
}
public void tocar(ent nota,ent tiempo, ent canal)
{
CANALES[canal].allNotesOff();
CANALES[canal].noteOn(nota,tiempo);
}
public void tocar(ent nota,ent tiempo)
{
tocar(nota,tiempo,0);
}
public void callar()
{
CANALES[0].allNotesOff();
}
} // End of Sintetizador
}
Os habéis dado cuenta de que es un programa consistente, sin embargo no tenéis que espantaros porque la mayor parte del código sirve para la gráfica y para escuchar los sucesos. La parte interesante para sintetizar los sonidos la he puesto en la subclase Sintetizador, es decir, en sintetizador.Sintetizador
es decir, la siguiente:
//***********************************************************
// Sintetizador:
//***********************************************************
public class Sintetizador
{
private Synthesizer SYNT;
private Sequencer sequencer;
private Sequence seq;
private Soundbank BANK;
public Instrument[] Instrumentos;
public MidiChannel[] CANALES;
public Sintetizador()
{
setupSintetizador();
}
void setupSintetizador()
{
try
{
SYNT=MidiSystem.getSynthesizer();
sequencer=MidiSystem.getSequencer();
seq= new Sequence(Sequence.PPQ, 10);
SYNT.open();
BANK = SYNT.getDefaultSoundbank();
if (BANK != null)
Instrumentos = SYNT.getDefaultSoundbank().getInstruments();
else
Instrumentos = SYNT.getAvailableInstruments();
CANALES=SYNT.getChannels();
}
catch(MidiUnavailableException ecc){todonull();}
catch(InvalidMidiDataException ecc2){todonull();}
;
}
void todonull()
{
SYNT=null;
sequencer=null;
seq=null;
BANK=null;
Instrumentos=null;
}
public void set nstrumento(ent str,ent can)
{
SA=str;
ent prog=Instrumentos[str].getPatch().getProgram();
CANALES[can].programChange(prog);
}
private ent SA=0;
public void cambiaCanal(ent can)
{
ent prog=Instrumentos[SA].getPatch().getProgram();
CANALES[can].programChange(prog);
}
public void tocar(ent nota,ent tiempo, ent canal)
{
CANALES[canal].allNotesOff();
CANALES[canal].noteOn(nota,tiempo);
}
public void tocar(ent nota,ent tiempo)
{
tocar(nota,tiempo,0);
}
public void callar()
{
CANALES[0].allNotesOff();
}
} // End of Sintetizador
El aspecto del programa es:

|
|
 |
|
 |