Exemplo n.º 1
0
 /**
  * Saves the MIDI data from a MusicString into a file.
  *
  * @param musicString the MusicString to save
  * @param file the File to save the MusicString to. Should include file extension, such as .mid
  */
 public void saveMidi(String musicString, File file) throws IOException {
   Pattern pattern = new Pattern(musicString);
   saveMidi(pattern, file);
 }
  public static void main(String[] args) {

    int number_of_voices = voice_array.length;
    Random my_roll = new Random();

    Pattern[] rhythm_patterns = james.generate(piece_length, number_of_voices);
    for (int i = 0; i < number_of_voices; i++) {
      MelodicVoice this_voice = new MelodicVoice();
      this_voice.setRange(voice_array[i]);
      AccentListener my_accent_listener = new AccentListener();
      this_voice.setNoteArray(my_accent_listener.listen(rhythm_patterns[i]));
      System.out.println("adding voice " + i + " to unbuiltvoices");
      unbuilt_voices.add(this_voice);
    }
    int ub_size = unbuilt_voices.size();
    for (int i = 0; i < number_of_voices; i++) {
      // int voice_index = my_roll.nextInt(unbuilt_voices.size());
      int voice_index = i;
      System.out.println(" about to build voice pitches for " + voice_index);
      MelodicVoice nextVoice =
          buildVoicePitches(unbuilt_voices.get(voice_index), number_of_voices, my_mode_module);
      ArrayList<MelodicNote> verify_array = nextVoice.getNoteArray();
      System.out.println("Return Me: ");
      for (MelodicNote verify : verify_array) {
        if (verify.getRest()) System.out.println("rest " + verify.getDuration() + "  ");
        else System.out.println(verify.getPitch() + " " + verify.getDuration() + "   ");
      }
      if (i == 0) {
        harmonic_prog = nextVoice;
        harmonic_prog_built = true;
      } else built_voices.add(nextVoice);
      // unbuilt_voices.remove(voice_index);
    }
    unbuilt_voices.clear();
    Pattern music_output = new Pattern();
    music_output.addElement(new Tempo(tempo_bpm));

    for (byte i = 0; i < built_voices.size(); i++) {
      // create a jfugue musicstring from the built voice
      MelodicVoice final_voice = built_voices.get(i);
      ArrayList<MelodicNote> final_note_array = final_voice.getNoteArray();
      Voice jf_voice = new Voice(i);
      music_output.addElement(jf_voice);
      for (MelodicNote final_note : final_note_array) {
        int jf_int = 0;
        Note jf_note = new Note();
        jf_note.setDecimalDuration(final_note.getDuration());
        if (final_note.getPitch() != null && final_note.getRest() == false) {
          jf_int = final_note.getPitch();
          byte jf_note_byte = (byte) jf_int;
          jf_note.setValue(jf_note_byte);
          if (!final_note.getAccent()) jf_note.setAttackVelocity((byte) 40);
        } else {
          // System.out.println("setting jf note to rest");
          jf_note.setRest(true);
          jf_note.setAttackVelocity((byte) 0);
          jf_note.setDecayVelocity((byte) 0);
        }

        music_output.addElement(jf_note);
      } // add the musicstring to a pattern
    }

    // save and play the pattern
    System.out.println(music_output.getMusicString());
    Player my_player = new Player();
    my_player.play(music_output);
    Date today = new Date(System.currentTimeMillis());
    // Date format: "yyyy-MM-dd_HH:mm:ss"
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd-yyyy-HH-mm");
    String dateString = DATE_FORMAT.format(today);
    try {
      my_player.saveMidi(
          music_output,
          new File(
              "C:\\Documents and Settings\\alyssa\\Desktop\\Species Midi\\species-"
                  + tempo_bpm
                  + "-"
                  + dateString
                  + ".mid"));
    } catch (IOException ex) {
    }
  }