Exemplo n.º 1
0
  public void go() {
    setUpGui();

    try {

      Sequencer sequencer = MidiSystem.getSequencer();
      sequencer.open();
      // make a sequencer and open
      sequencer.addControllerEventListener(m1, new int[] {127});
      Sequence seq = new Sequence(Sequence.PPQ, 4);
      Track track = seq.createTrack();

      int r = 0;
      for (int i = 0; i < 300; i += 4) {

        r = (int) ((Math.random() * 50) + 1);
        track.add(makeEvent(144, 1, r, 100, i));
        track.add(makeEvent(176, 1, 127, 0, i));
        track.add(makeEvent(128, 1, r, 100, i + 2));
      } // end loop

      sequencer.setSequence(seq);
      sequencer.start();
      sequencer.setTempoInBPM(120);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  } // close method
Exemplo n.º 2
0
  public void buildTrackAndStart() {
    int[] trackList = null;

    sequence.deleteTrack(track);
    track = sequence.createTrack();

    for (int i = 0; i < 16; i++) {
      trackList = new int[16];

      int key = instruments[i];

      for (int j = 0; j < 16; j++) {
        JCheckBox jc = (JCheckBox) checkboxList.get(j + (16 * i));
        if (jc.isSelected()) {
          trackList[j] = key;
        } else {
          trackList[j] = 0;
        }
      } // close inner loop

      makeTracks(trackList);
      track.add(makeEvent(176, 1, 127, 0, 16));
    } // close outer

    track.add(makeEvent(192, 9, 1, 0, 15));
    try {
      sequencer.setSequence(sequence);
      sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY);
      sequencer.start();
      sequencer.setTempoInBPM(120);
    } catch (Exception e) {
      e.printStackTrace();
    }
  } // close buildTrackAndStart method
Exemplo n.º 3
0
  public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
    MidiEvent event = null;
    try {
      ShortMessage a = new ShortMessage();
      a.setMessage(comd, chan, one, two);
      event = new MidiEvent(a, tick);

    } catch (Exception e) {
      e.printStackTrace();
    }
    return event;
  }
Exemplo n.º 4
0
  public void setUpMidi() {
    try {
      sequencer = MidiSystem.getSequencer();
      sequencer.open();
      sequence = new Sequence(Sequence.PPQ, 4);
      track = sequence.createTrack();
      sequencer.setTempoInBPM(120);

    } catch (Exception e) {
      e.printStackTrace();
    }
  } // close method
Exemplo n.º 5
0
 public boolean createEvent(int type, int num, long tick) {
   ShortMessage message = new ShortMessage();
   try {
     message.setMessage(type + cc.num, num, cc.velocity);
     MidiEvent event = new MidiEvent(message, tick);
     track.add(event);
     return true;
   } catch (Exception ex) {
     ex.printStackTrace();
     return false;
   }
 }
Exemplo n.º 6
0
 public static void stopSound(String name) {
   try {
     if (name.endsWith(".mid")) {
       stopMidi(name);
     }
     if (name.endsWith(".mp3")) {
       stopMp3(name);
     }
     if (name.endsWith(".wav")) {
       stopWav(name);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 7
0
 public static void stopAll() {
   try {
     for (String name : midiMap.keySet()) {
       stopMidi(name);
     }
     for (String name : mp3Map.keySet()) {
       stopMp3(name);
     }
     for (String name : wavMap.keySet()) {
       stopWav(name);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 8
0
 public void go() {
   try {
     sq.open();
     Sequence seq = new Sequence(Sequence.PPQ, 4);
     Track track = seq.createTrack();
     track.add(addNote(144, 9, 56, 100, 1));
     track.add(addNote(128, 9, 56, 100, 4));
     sq.setSequence(seq);
     sq.setTempoInBPM(tempo);
     sq.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
     sq.start();
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
Exemplo n.º 9
0
 public MIDIReader(String filename) {
   this.filename = filename;
   try {
     this.recebedor = MidiSystem.getReceiver();
     this.sequencia = MidiSystem.getSequence(new File(filename));
     this.tempoProcessor = new MidiUtils.TempoCache(sequencia);
     this.player = MidiSystem.getSequencer(true);
     this.player.setSequence(sequencia);
     this.player.open();
     this.interval = 0.5f;
     this.loadNotes();
     this.duration = this.getRealDuration();
   } catch (Exception ex) {
     Utilidades.alertar(ex.getMessage());
   }
 }
Exemplo n.º 10
0
 public static void playSound(String name, boolean loop, double volume) {
   volume *= GLOBAL_VOLUME;
   try {
     if (name.endsWith(".mid")) {
       playMidi(name, loop, volume);
     }
     if (name.endsWith(".mp3")) {
       playMp3(name, loop, volume);
     }
     if (name.endsWith(".wav")) {
       playWav(name, loop, volume);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 11
0
  // Permite a execucao de uma nota do video
  public void tocar(float seconds, int corda) {
    if (!this.canPlay) {
      return;
    }
    corda = 6 - corda;
    try {
      for (MIDINote nota : this.notas) {

        if (nota.getSecond() == seconds && nota.getChord() == corda) {
          this.recebedor.send(nota.getShortMessage(), -1);
          return;
        }
      }
    } catch (Exception ex) {
      Utilidades.alertar("Erro na execucao do MIDI: " + ex.getMessage());
    }
  }
Exemplo n.º 12
0
 public boolean saveMidiFile(File file) {
   try {
     int[] fileTypes = MidiSystem.getMidiFileTypes(sequence);
     if (fileTypes.length == 0) {
       System.out.println("Can't save sequence");
       return false;
     } else {
       if (MidiSystem.write(sequence, fileTypes[0], file) == -1) {
         throw new IOException("Problems writing to file");
       }
       return true;
     }
   } catch (SecurityException ex) {
     ex.printStackTrace();
     return false;
   } catch (Exception ex) {
     ex.printStackTrace();
     return false;
   }
 }
Exemplo n.º 13
0
  /** Gets the default transmitter and receiver, and then links them. */
  private void linkTransmitterToReceiver() {
    try {
      // Set up the sequencer (including its tempo)
      sequencer.open();
      sequencer.setSequence(sequence);
      sequencer.setTempoInBPM(song.getBPM());

      // Get the system's default synthesizer and set that up, too.
      Synthesizer synth = MidiSystem.getSynthesizer();
      synth.open();

      // Get the receiver and transmitter to use and set those up.
      Receiver receiver = synth.getReceiver();
      Transmitter transmitter = sequencer.getTransmitter();
      transmitter.setReceiver(receiver);
    } catch (Exception ex) {
      // Something went wrong.
      ex.printStackTrace();
      System.exit(1);
    }
  }
Exemplo n.º 14
0
    public void actionPerformed(ActionEvent ev) {

      try {

        // make (and open) a sequencer, make a sequence and track

        Sequencer sequencer = MidiSystem.getSequencer();
        sequencer.open();

        sequencer.addControllerEventListener(myPanel, new int[] {127});
        Sequence seq = new Sequence(Sequence.PPQ, 4);
        Track track = seq.createTrack();

        // now make two midi events (containing a midi message)

        for (int i = 0; i < 100; i += 4) {

          int rNum = (int) ((Math.random() * 50) + 1);
          if (rNum < 38) { // so now only do it if num <38 (75% of the time)

            track.add(makeEvent(144, 1, rNum, 100, i));

            track.add(makeEvent(176, 1, 127, 0, i));

            track.add(makeEvent(128, 1, rNum, 100, i + 2));
          }
        } // end loop

        // add the events to the track
        // add the sequence to the sequencer, set timing, and start

        sequencer.setSequence(seq);

        sequencer.start();
        sequencer.setTempoInBPM(220);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    } // close actionperformed
Exemplo n.º 15
0
  public void play(int instrument, int note) {

    try {

      Sequencer player = MidiSystem.getSequencer();
      player.open();

      Sequence seq = new Sequence(Sequence.PPQ, 4);
      Track track = seq.createTrack();

      // MidiEvent event = null;

      ShortMessage first = new ShortMessage();
      first.setMessage(192, 1, instrument, 0);
      MidiEvent changeInstrument = new MidiEvent(first, 1);
      track.add(changeInstrument);

      ShortMessage a = new ShortMessage();
      a.setMessage(144, 1, note, 100);
      MidiEvent noteOn = new MidiEvent(a, 1);
      track.add(noteOn);

      ShortMessage b = new ShortMessage();
      b.setMessage(128, 1, note, 100);
      MidiEvent noteOff = new MidiEvent(b, 16);
      track.add(noteOff);
      player.setSequence(seq);
      player.start();

      Thread.sleep(2000);
      player.close();
      System.exit(0);

    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    }
  } // close play
Exemplo n.º 16
0
  public boolean open() {

    try {
      if (synthesizer == null) {
        if ((synthesizer = MidiSystem.getSynthesizer()) == null) {
          System.out.println("getSynthesizer() failed!");
          return false;
        }
      }
      synthesizer.open();
      sequencer = MidiSystem.getSequencer();
      sequencer.addMetaEventListener(new ProcessMeta());

      sequence = new Sequence(Sequence.PPQ, 10);
    } catch (Exception ex) {
      System.out.println("midi exception 1 ");
      ex.printStackTrace();
      return false;
    }

    System.out.println("midi opening");

    Soundbank sb = synthesizer.getDefaultSoundbank();
    if (sb != null) {
      instruments = synthesizer.getDefaultSoundbank().getInstruments();
      synthesizer.loadInstrument(instruments[0]);
    }
    MidiChannel midiChannels[] = synthesizer.getChannels();
    numChannels = midiChannels.length;
    channels = new ChannelData[midiChannels.length];
    if (channels.length == 0) return false;
    for (int i = 0; i < channels.length; i++) {
      channels[i] = new ChannelData(midiChannels[i], i);
    }
    cc = channels[0];
    return true;
  }
Exemplo n.º 17
0
  public void los() {
    guiErstellen();

    try {

      // einen Sequencer erzeugen (und öffnen),
      // eine Sequence und einen Track erzeugen

      Sequencer sequencer = MidiSystem.getSequencer();
      sequencer.open();
      sequencer.addControllerEventListener(ml, new int[] {127});
      Sequence seq = new Sequence(Sequence.PPQ, 4);
      Track track = seq.createTrack();

      // jetzt werden MidiEvents (die eine
      // MidiMessage enthalten) erzeugt

      int r = 0;
      for (int i = 0; i < 60; i += 4) {
        r = (int) ((Math.random() * 50) + 1);
        track.add(eventErzeugen(144, 1, r, 100, i));
        track.add(eventErzeugen(176, 1, 127, 0, i));
        track.add(eventErzeugen(128, 1, r, 100, i + 2));
      } // Ende der for-Schleife

      // Hinzufügen der Events zum Track und der Sequence
      // zum Sequencer, Setzen der Zeiten und Starten

      sequencer.setSequence(seq);
      sequencer.start();
      sequencer.setTempoInBPM(120);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  } // Methode los schließen
Exemplo n.º 18
0
  public boolean writeSequence(NoteList noteList) {

    this.noteList = noteList;

    toneMap = toneMapFrame.getToneMap();
    timeSet = toneMap.getTimeSet();
    pitchSet = toneMap.getPitchSet();
    timeRange = timeSet.getRange();
    pitchRange = pitchSet.getRange();

    if (!buildNoteSequence()) return false;

    try {
      sequence = new Sequence(Sequence.PPQ, 10);
    } catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }

    track = sequence.createTrack();
    startTime = System.currentTimeMillis();

    // add a program change right at the beginning of
    // the track for the current instrument

    createEvent(PROGRAM, cc.program + 1, 1);

    for (int i = 0; i < noteSequence.size(); i++) {
      noteSequenceElement = noteSequence.get(i);
      if (noteSequenceElement.state == ON)
        if (!createEvent(NOTEON, noteSequenceElement.note, noteSequenceElement.tick)) return false;
      if (noteSequenceElement.state == OFF)
        if (!createEvent(NOTEOFF, noteSequenceElement.note, noteSequenceElement.tick)) return false;
    }
    return true;
  }
Exemplo n.º 19
0
  public boolean play() {

    try {
      if (playState != STOPPED) playStop();

      if (sequence == null) return false;
      if (sequencer == null) return false;

      sequencer.open();
      sequencer.setSequence(sequence);

      double playStartTime = (player.getSeekTime() / 100) * (playGetLength());
      sequencer.setMicrosecondPosition((long) playStartTime);

      sequencer.start();
      playState = PLAYING;
      return true;

    } catch (Exception ex) {
      ex.printStackTrace();
      playState = STOPPED;
      return false;
    }
  }
Exemplo n.º 20
0
 public void saveUnsaved() throws SaveAbortedException {
   if (!saved) {
     int option = 0;
     if (loadedFile == null)
       option =
           JOptionPane.showConfirmDialog(
               this,
               new JLabel("Save changes to UNTITLED?"),
               "Warning",
               JOptionPane.YES_NO_CANCEL_OPTION,
               JOptionPane.WARNING_MESSAGE);
     else
       option =
           JOptionPane.showConfirmDialog(
               this,
               new JLabel("Save changes to " + loadedFile.getName() + "?"),
               "Warning",
               JOptionPane.YES_NO_CANCEL_OPTION,
               JOptionPane.WARNING_MESSAGE);
     if (option == JOptionPane.YES_OPTION) {
       if (loadedFile == null) // SAVE NEW FILE
       {
         if (nameTF.getText().equals("")) {
           JOptionPane.showMessageDialog(
               this,
               new JLabel("Please type in the Scale Name"),
               "Warning",
               JOptionPane.WARNING_MESSAGE);
           throw new SaveAbortedException();
         }
         fileChooser.setFileFilter(filter);
         int option2 = fileChooser.showSaveDialog(this);
         if (option2 == JFileChooser.APPROVE_OPTION) {
           File target = fileChooser.getSelectedFile();
           try {
             PrintStream stream = new PrintStream(new FileOutputStream(target), true);
             save(stream);
             stream.close();
           } catch (Exception ex) {
             JOptionPane.showMessageDialog(
                 this,
                 new JLabel("Error: " + ex.getMessage()),
                 "Error",
                 JOptionPane.ERROR_MESSAGE);
           }
         } else throw new SaveAbortedException();
         ;
       } else // save LOADED FILE
       {
         try {
           PrintStream stream = new PrintStream(new FileOutputStream(loadedFile), true);
           save(stream);
           stream.close();
         } catch (Exception ex) {
           JOptionPane.showMessageDialog(
               this, new JLabel("Error: " + ex.getMessage()), "Error", JOptionPane.ERROR_MESSAGE);
         }
       }
     } else if (option == JOptionPane.CANCEL_OPTION) throw new SaveAbortedException();
     ;
   }
 }
Exemplo n.º 21
0
  public void actionPerformed(ActionEvent e) {
    JButton b = (JButton) e.getSource();
    if (b.getText() == "PLAY") {
      if (scoreP != null) scoreP.playScale(sp.getScale(), tempoP.getValue());
    } else if (b.getText() == "New") {
      try {
        saveUnsaved();
      } catch (SaveAbortedException ex) {
        return;
      }

      sp.notes.removeAllElements();
      sp.repaint();
      nameTF.setText("");
      loadedFile = null;
    } else if (b.getText() == "Save") {
      if (nameTF.getText().equals("")) {
        JOptionPane.showMessageDialog(
            this,
            new JLabel("Please type in the Scale Name"),
            "Warning",
            JOptionPane.WARNING_MESSAGE);
        return;
      }
      fileChooser.setFileFilter(filter);
      int option = fileChooser.showSaveDialog(this);
      if (option == JFileChooser.APPROVE_OPTION) {
        File target = fileChooser.getSelectedFile();
        if (target.getName().indexOf(".scl") == -1) target = new File(target.getPath() + ".scl");
        try {
          PrintStream stream = new PrintStream(new FileOutputStream(target), true);
          save(stream);
          stream.close();
        } catch (Exception ex) {
          JOptionPane.showMessageDialog(
              this, new JLabel("Error: " + ex.getMessage()), "Error", JOptionPane.ERROR_MESSAGE);
        }
      }
    } else if (b.getText() == "Load") {
      try {
        saveUnsaved();
      } catch (SaveAbortedException ex) {
        return;
      }

      fileChooser.setFileFilter(filter);
      int option = fileChooser.showOpenDialog(this);
      if (option == JFileChooser.APPROVE_OPTION) {
        loadedFile = fileChooser.getSelectedFile();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        ScaleParser handler = new ScaleParser(false);
        try {
          SAXParser parser = factory.newSAXParser();
          parser.parse(loadedFile, handler);
          // System.out.println("success");
        } catch (Exception ex) {
          // System.out.println("no!!!!!! exception: "+e);
          // System.out.println(ex.getMessage());
          ex.printStackTrace();
        }
        // -----now :P:P---------------
        System.out.println("name: " + handler.getName());
        nameTF.setText(handler.getName());
        sp.notes.removeAllElements();
        int[] scale = handler.getScale();
        for (int i = 0; i < scale.length; i++) {
          sp.addNote(scale[i]);
        }
        sp.repaint();
      } else loadedFile = null;
    }
  }