/* Este es un programa que utiliza a la pedalera FC 200 para grabar y reproducir hasta 10 archivos WAV los program Change 0 - 9 reproducen los audios los program change 10 -19 graban los audios. */ import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import com.softsynth.jsyn.*; import com.softsynth.jsyn.util.*; import com.softsynth.jmsl.*; import com.softsynth.jmsl.midi.*; import java.io.*; public class Gabadora extends Applet implements ActionListener, MidiListener { int numDeGrabaciones = 10; int numDeCanales = 1; SynthMixer mezcladora; LineOut miSalida; String direccionGrabaciones = "c:/DOCUME~1/HUGOSO~1/MISDOC~1/jwork/PROYEC~1/PAPIT/AUDIOG~1/grabacion"; grabadoraWAV grabadora[] = new grabadoraWAV[numDeGrabaciones]; lectorWAV lector[] = new lectorWAV[numDeGrabaciones]; boolean primeraGrab[] = new boolean[numDeGrabaciones]; boolean estadoGrab[] = new boolean[numDeGrabaciones]; boolean archivoGrabado[] = new boolean[numDeGrabaciones]; boolean primeraGrabFin[] = new boolean[numDeGrabaciones]; Button rectangulo[] = new Button[numDeGrabaciones]; double tiempoInicio[] = new double[numDeGrabaciones]; double tiempoFinal[] = new double[numDeGrabaciones]; double tiempoTotal[] = new double[numDeGrabaciones]; /* Can be run as either an application or as an applet. */ public static void main(String args[]) { Gabadora applet = new Gabadora(); AppletFrame frame = new AppletFrame("Grabadora", applet); frame.setSize(1400,1000); frame.show(); frame.test(); frame.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { Synth.stopEngine(); JMSL.midi.closeDevices(); System.exit(0); } }); } /* * Setup synthesis. */ public Gabadora() { try { setLayout(new GridLayout(numDeGrabaciones/5 ,5)); JMSL.midi.openDevices(0,0); MidiParser midiFrases = new MidiParser(); JMSL.midi.addMidiParser( midiFrases ); midiFrases.addMidiListener(this); Synth.initialize(); Synth.start( Synth.FLAG_ENABLE_INPUT, Synth.DEFAULT_FRAME_RATE); mezcladora = new SynthMixer((numDeGrabaciones * numDeCanales),2); miSalida = new LineOut(); mezcladora.connectOutput( 0, miSalida.input, 0); mezcladora.connectOutput( 1, miSalida.input, 1); mezcladora.start(); miSalida.start(); for (int i = 0; i < numDeGrabaciones; i++) { primeraGrab[i] = true; estadoGrab[i] = false; archivoGrabado[i] = false; primeraGrabFin[i] = true; rectangulo[i]= new Button("Audio " + Integer.toString(i + 1)); add(rectangulo[i]); rectangulo[i].setBackground(Color.gray); } } catch(SynthException e) { SynthAlert.showError(this,e); } } // Metodos de recepción MIDI, necesario definir todos al implementar MidiListener public void handleNoteOn(double timeStamp, int channel, int pitch, int velocity) { } public void handleNoteOff(double timeStamp, int channel, int pitch, int velocity) { } public void handlePolyphonicAftertouch(double timeStamp, int channel, int pitch, int pressure) { } public void handleControlChange(double timeStamp, int channel, int id, int valor) { } public void handleProgramChange(double timeStamp, int channel, int program) { // CORRESPONDIENTE A LA GRABACIÓN for (int i = 0; i < numDeGrabaciones; i++) { if (program == (i + numDeGrabaciones)) { if (estadoGrab[i] == false) { if (primeraGrab[i] == true) { try { tiempoInicio[i] = JMSL.now(); grabadora[i] = new grabadoraWAV(direccionGrabaciones + Integer.toString(i + 1) + ".wav", numDeCanales); grabadora[i].grabar(); primeraGrab[i] = false; estadoGrab[i] = true; //System.out.println("grabando por primera vez el audio" + Integer.toString(i + 1)); rectangulo[i].setBackground(Color.red); } catch( IOException e ) { SynthAlert.showError(this,e); } } else { try { tiempoInicio[i] = JMSL.now(); grabadora[i].grabar(); estadoGrab[i] = true; //System.out.println("grabando el audio" + Integer.toString(i + 1) + "por segunda o tercera o cuarta... vez"); rectangulo[i].setBackground(Color.red); } catch( IOException e ) { SynthAlert.showError(this,e); } } } else { if (primeraGrabFin[i] == true) { try { tiempoFinal[i] = JMSL.now(); grabadora[i].detener(); estadoGrab[i] = false; archivoGrabado[i] = true; primeraGrabFin[i] = false; //System.out.println("apagando la grabacion de audio por primera ocacion y cargando audio por primera ocacion" + Integer.toString(i + 1)); rectangulo[i].setBackground(Color.orange); lector[i] = new lectorWAV(direccionGrabaciones + Integer.toString(i + 1) + ".wav", numDeCanales); lector[i].cargar(direccionGrabaciones + Integer.toString(i + 1) + ".wav"); if (numDeCanales == 1) { mezcladora.connectInput(i, lector[i].miLector.output, 0); mezcladora.setGain(i, 0, 1.0); mezcladora.setGain(i, 1, 1.0); } if (numDeCanales == 2) { mezcladora.connectInput((i * 2), lector[i].miLector.output, 0); mezcladora.connectInput(((i * 2) + 1), lector[i].miLector.output, 1); mezcladora.setGain(i * 2, 0, 1.0); mezcladora.setGain((i * 2) + 1, 1, 1.0); } } catch( IOException e ) { SynthAlert.showError(this,e); } } else { try { tiempoFinal[i] = JMSL.now(); grabadora[i].detener(); estadoGrab[i] = false; archivoGrabado[i] = true; rectangulo[i].setBackground(Color.orange); //System.out.println("apagando la grabacion de audio por segunda ocacion y cargando audio por segunda ocacion" + Integer.toString(i + 1)); lector[i].cargar(direccionGrabaciones + Integer.toString(i + 1) + ".wav"); } catch( IOException e ) { SynthAlert.showError(this,e); } } } } } // CORRESPONDIENTE A LA REPODUCCIÓN for (int i = 0; i < numDeGrabaciones; i++) { if (program == i && archivoGrabado[i] == true) { lector[i].reproducir(); rectangulo[i].setBackground(Color.green); tiempoTotal[i] = tiempoFinal[i] - tiempoInicio[i]; apagaBoton apBot = new apagaBoton(rectangulo[i]); apBot.launch(JMSL.now() + tiempoTotal[i]); //System.out.println("reproducioendo el audio" + Integer.toString(i + 1)); } } } public void handleChannelAftertouch(double timeStamp, int channel, int pressure) { } public void handlePitchBend(double timeStamp, int channel, int lsb, int msb) { } public void actionPerformed(ActionEvent e) { } public void stop() { try { Synth.stopEngine(); JMSL.midi.closeDevices(); } catch(SynthException e) { SynthAlert.showError(this,e); } } public class lectorWAV { SynthSampleWAV muestra = new SynthSampleWAV(); SampleReader miLector; public lectorWAV(String archivo, int numDeCanales) throws IOException { if (numDeCanales == 1) { miLector = new SampleReader_16F1(); } if (numDeCanales == 2) { miLector = new SampleReader_16F2(); } FileInputStream datos = new FileInputStream(archivo); muestra.load(datos); } public void cargar(String archivo) throws IOException { muestra.clear(); FileInputStream datos = new FileInputStream(archivo); muestra.load(datos); } public void reproducir() { miLector.start(); miLector.samplePort.queue( muestra ); } } public class grabadoraWAV { LineIn miEntrada; RandomAccessFile ranfile; WAVFileWriter wavWriter; BufferedOutputStream out; StreamRecorder recorder; int numeroDeCanales; public grabadoraWAV(String filename, int numChannels) throws IOException { miEntrada = new LineIn(); numeroDeCanales = numChannels; ranfile = new RandomAccessFile(filename, "rw"); wavWriter = new WAVFileWriter(ranfile); wavWriter.writeHeader(numChannels, 44100); out = new BufferedOutputStream(wavWriter); recorder = new StreamRecorder(out, 8*1024, 4, numChannels); } public void grabar() throws IOException { miEntrada.start(); if (numeroDeCanales == 1) { miEntrada.output.connect(0, recorder.input, 0); } if (numeroDeCanales == 2) { miEntrada.output.connect(0, recorder.input, 0); miEntrada.output.connect(1, recorder.input, 1); } recorder.start(Synth.getTickCount()); } public void detener() throws IOException { ranfile.setLength(ranfile.getFilePointer()); recorder.stop(Synth.getTickCount()); out.flush(); wavWriter.fixSizes(); out.close(); miEntrada.stop(); } } public class apagaBoton extends MusicJob { private Button bot; apagaBoton (Button boton) { super(); bot = boton; } public double start (double playTime) throws InterruptedException { bot.setBackground(Color.orange); return playTime; } } }