Package ru.mail.teodorgig.mididermi.base

Examples of ru.mail.teodorgig.mididermi.base.Melody


    try {
      statement = connection.prepareCall("{ call get_all_melodies() }", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      result = statement.executeQuery();

      while (result.next() != false) {
        Melody melody = new Melody();
        melody.setId(result.getLong("system_id"));
        String sequence = result.getString("sequence");
        StringTokenizer tokenizer = new StringTokenizer(sequence, " ", false);
        while (tokenizer.hasMoreElements()) {
          Note note = new Note();
          try {
            note.setNote((new Integer((String) tokenizer.nextElement())).intValue());
            note.setOffset((new Integer((String) tokenizer.nextElement())).intValue());
            note.setDuration((new Integer((String) tokenizer.nextElement())).intValue());
            note.setVelocity((new Integer((String) tokenizer.nextElement())).intValue());
          } catch (Exception ex) {
            ex.printStackTrace();
          }
          melody.addNote(note);
        }
        melody.setTimber(result.getInt("timber"));
        melody.setScore(result.getInt("score"));
        melody.setGenre(result.getInt("genre"));
      }
    } catch (SQLException ex) {
      ex.printStackTrace();
    }
View Full Code Here


   *
   * @date 25 Jun 2008
   */
  public static void storeMelodies(Vector<Melody> melodies) {
    CallableStatement statement = null;
    Melody melody = null;

    if (connection == null) {
      return;
    }

    for (int i = 0; i < melodies.size(); i++) {
      melody = melodies.elementAt(i);

      try {
        statement = connection.prepareCall("{ call add_melody(?, ?, ?, ?, ?) }");

        statement.setLong(1, melody.getId());
        statement.setString(2, melody.getNotesInNumbers());
        statement.setInt(3, melody.getTimber());
        statement.setInt(4, melody.getScore());
        statement.setInt(5, melody.getGenre());

        statement.execute();
      } catch (SQLException ex) {
        ex.printStackTrace();
      }
View Full Code Here

   * @date 30 May 2008
   */
  public static Vector<Melody> provide(int size) {
    Vector<Melody> melodies = new Vector<Melody>();

    Melody melody = null;
    for (int i = 0; i < size; i++) {
      melody = RandomMelodyProvider.provide();
      melody.setId(Melody.getUniqueId());

      melodies.add(melody);
    }

    return (melodies);
View Full Code Here

   * @email teodorgig@mail.ru
   *
   * @date 29 May 2008
   */
  public static Melody provide(int length) {
    Melody melody = new Melody();

    for (int i = 0; i < length; i++)
      melody.addNote(Note.provideRandom());

    melody.setTimber(1 + (int) (Math.random() * 128));
    melody.setId(Melody.getUniqueId());
    melody.setScore(0);

    melody.sort();

    return (melody);
  }
View Full Code Here

  public static Vector<Melody> provide() {
    Vector<Melody> melodies = new Vector<Melody>();

    String filesList[] = (new File(TEXT_MELODIES_FOLDER)).list();

    Melody melody = null;
    for (int i = 0; filesList != null && i < filesList.length; i++) {
      if ((new File(TEXT_MELODIES_FOLDER + filesList[i])).isFile() == true) {
        try {
          melody = FileMelodyProvider.provide(TEXT_MELODIES_FOLDER + filesList[i]);
          melody.setId(Melody.getUniqueId());
          melodies.add(melody);
        } catch (NotValidDescriptorFileException ex) {
          ex.printStackTrace();
        } catch (IOException ex) {
          ex.printStackTrace();
View Full Code Here

   * @email teodorgig@mail.ru
   *
   * @date 29 May 2008
   */
  public static Melody provide(int length) {
    Melody melody = new Melody();

    // TODO Better fractal formula can be implemented.
    Note next = null;
    Note previous = null;
    for (int i = 0; i < length; i++) {
      next = Note.provideRandom();

      if (i > 0 && previous.getNote() > next.getNote()) {
        next.setNote(next.getNote() + 1);
      } else if (i > 0 && previous.getNote() < next.getNote()) {
        next.setNote(next.getNote() - 1);
      }

      melody.addNote(next);
      previous = next;
    }

    melody.setTimber(1 + (int) (Math.random() * 128));
    melody.setId(Melody.getUniqueId());
    melody.setScore(0);

    melody.sort();

    return (melody);
  }
View Full Code Here

   * @date 30 May 2008
   */
  public static Vector<Melody> provide(int size) {
    Vector<Melody> melodies = new Vector<Melody>();

    Melody melody = null;
    for (int i = 0; i < size; i++) {
      melody = FractalMelodyProvider.provide();
      melody.setId(Melody.getUniqueId());

      melodies.add(melody);
    }

    return (melodies);
View Full Code Here

   *
   * @date 29 May 2008
   */
  public static Melody provide(String fileName) throws NotValidDescriptorFileException, IOException {
    // TODO Better file format description as documentation is needed.
    Melody melody = new Melody();

    String melodyText = "";

    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    while ((line = in.readLine()) != null) {
      melodyText += line;
    }
    in.close();

    StringTokenizer tokenizer = new StringTokenizer(melodyText, " ", false);

    /*
     * First number into the file format is timber number.
     */
    if (tokenizer.hasMoreTokens() == false) {
      throw (new NotValidDescriptorFileException("Timber is not available!"));
    }

    /*
     * Timber should be positive integer number.
     */
    int timber = 0;
    try {
      timber = Integer.parseInt(tokenizer.nextToken());
    } catch (NumberFormatException ex) {
      throw (new NotValidDescriptorFileException("Timber is not correct number!"));
    }

    /*
     * Timber should be between 1 and 128.
     */
    if (timber < 1 || timber > 128) {
      throw (new NotValidDescriptorFileException("Timber is not between 1 and 128!"));
    } else {
      melody.setTimber(timber);
    }
    melody.setId(Melody.getUniqueId());
    melody.setScore(0);

    int timeCounter = 0;

    /*
     * All notes are fowling after timber.
     */
    while (tokenizer.hasMoreTokens() == true) {
      Note note = parseNote(tokenizer.nextToken());

      // TODO It is not clear how much time is between two notes.
      note.setOffset(timeCounter);

      melody.addNote(note);

      timeCounter += note.getDuration();
    }

    return (melody);
View Full Code Here

   * @date 04 Jun 2008
   */
  public static void produce(Vector<Melody> melodies) {
    for (int i = 0; i < melodies.size(); i++) {
      String fileName = MIDI_SAVE_FOLDER;
      Melody melody = melodies.elementAt(i);

      fileName += melody.getId();
      fileName += "_";
      fileName += melody.getGenre();
      fileName += "_";
      fileName += melody.getTimber();
      fileName += "_";
      fileName += System.currentTimeMillis();
      fileName += ".mid";

      try {
        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
        // TODO Something is wrong with saving binary information.
        out.write(melody.toMidiBytes());
        out.close();
      } catch (Exception ex) {
        System.err.println(ex);
      }
    }
View Full Code Here

TOP

Related Classes of ru.mail.teodorgig.mididermi.base.Melody

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.