예제 #1
0
  /**
   * Transforms the Voices of the Piece into Pitches which are then subscribed according to timing
   * to an internal SequencePlayer, then plays that SequencePlayer to play the Piece out loud
   *
   * <p>Catches SequencePlayer Errors internally to avoid them being part of the ABCMusic's
   * responsibility
   *
   * <p>Note: because tempo must be converted to quarter notes, tempos that are not divisible by 4
   * will have rounding errors that propagate into the music itself, its fairly negligible, but it
   * exists.
   *
   * <p>Note: Assumes that 1/16 of the default note length is the shortest note that would be
   * desired to play, any note shorter will play for 1/16 of a default note duration
   */
  public void PlayMusic() {
    try {
      // Right now it converts bpm to be based on the number of 1/4 given how many L*Q notes exist
      // with the quantized time being
      int quartersPerMin = (getTempo() * getNoteLength()[0] * 4) / (getNoteLength()[1]);
      int ticksPerQuarter = (getNoteLength()[1] * 16) / (getNoteLength()[0] * 4);
      player = new SequencePlayer(quartersPerMin, ticksPerQuarter);
      // Figure out what the Key means, and keep it as a map to edit notes as they are added
      keySignature = getKeySignature();
      // Initialize the accidentals as none, then use that as what gets edited through a meter
      resetKeyAccidental();

      for (Iterator<Voice> i = VoicesList.iterator(); i.hasNext(); ) {
        int startTick = 0;
        for (Iterator<Bars> j = i.next().BarsList.iterator(); j.hasNext(); ) {
          for (Iterator<Meters> k = j.next().MetersList.iterator(); k.hasNext(); ) {
            for (Iterator<NoteToken> l = k.next().getElts().iterator(); l.hasNext(); ) {
              NoteToken workingNote = l.next();
              if (workingNote.getType() == Tokens.Type.CHORD) {
                int noteTicks = 0;
                for (int m = 0; m < workingNote.getElts().length; m++) {
                  Pitch newNote = constructNote(workingNote.getElts()[m]);
                  // Length of a note in terms of quarters
                  double noteLength =
                      ((double) workingNote.getElts()[m].getLength()[0]
                              / (double) workingNote.getElts()[m].getLength()[1])
                          * ((double) (getNoteLength()[0] * 4) / ((double) getNoteLength()[1]));
                  // Convert the quarters to valid ticks
                  noteTicks = (int) (noteLength * ticksPerQuarter);
                  player.addNote(newNote.toMidiNote(), startTick, noteTicks);
                }
                startTick += noteTicks;
              } else if (workingNote.getType() == Tokens.Type.REST) {
                // Don't need to make rests
                // Length of a note in terms of quarters
                double noteLength =
                    ((double) workingNote.getLength()[0] / (double) workingNote.getLength()[1])
                        * ((double) (getNoteLength()[0] * 4) / ((double) getNoteLength()[1]));
                // Convert the quarters to valid ticks
                // Add ticks for the rests skipping time
                int noteTicks = (int) (noteLength * ticksPerQuarter);
                startTick += noteTicks;
              } else {
                Pitch newNote = constructNote(workingNote);
                // Length of a note in terms of quarters
                double noteLength =
                    ((double) workingNote.getLength()[0] / (double) workingNote.getLength()[1])
                        * ((double) (getNoteLength()[0] * 4) / ((double) getNoteLength()[1]));
                // Convert the quarters to valid ticks
                int noteTicks = (int) (noteLength * ticksPerQuarter);
                player.addNote(newNote.toMidiNote(), startTick, noteTicks);
                startTick += noteTicks;
              }
            }
            // Reset Accidentals at the end of the Meter
            resetKeyAccidental();
          }
        }
      }
      // Play the finished SequencePlayer with all Voice there-in
      player.play();
      // Catch undesirable errors
    } catch (MidiUnavailableException e) {
      e.printStackTrace();
    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    }
  }
예제 #2
0
 /**
  * The terminal recursion method that compiles the list of NoteTokens that make up a given meter
  * according to the ABC Grammar specifications
  *
  * <p>Note: Currently only enforces that a meter not have timing greater than specified in the
  * ABCHeader but does not care about truncated Meters, apparently those are okay in music
  *
  * <p>Note: Assumes that meters can be of any length if desired, even if those lengths violate the
  * M field this is due to the availability of truncated starts and ends, or switches to time mid
  * music
  *
  * @return An ADT Meters that contains a list of all the NoteTokens that exist in the meter it
  *     begins from
  */
 private Meters parseMeter() {
   next = lexer.GetNextTokenType();
   Meters currentMeter = new Meters();
   // Create variables to enforce meter lengths
   double noteLength;
   double meterCount = 0;
   double meterLength =
       ((double) ThePiece.getMeterSum()[0]) / ((double) ThePiece.getMeterSum()[1]);
   // Until the given meter is terminated
   while (next != Tokens.Type.BARLINE
       && next != Tokens.Type.REPEAT
       && next != Tokens.Type.END_OF_LINE
       && next != Tokens.Type.COMMENT
       && meterCount < meterLength) {
     NoteToken nextToken = lexer.getNextNoteToken();
     switch (next) {
       case NOTE:
       case REST:
         // Construct the note or rest length and add it to the meter length
         noteLength =
             (((double) (nextToken.getLength()[0] * ThePiece.getNoteLength()[0]))
                 / ((double) (nextToken.getLength()[1] * (double) ThePiece.getNoteLength()[1])));
         meterCount += noteLength;
         // As long as the meter is not overloaded, add the note or rest to the Meters List
         if (meterCount <= meterLength) {
           currentMeter.addElts(nextToken);
           next = lexer.GetNextTokenType();
         } else {
           throw new IllegalArgumentException(
               "Overloaded Meter with count greater than permitted");
         }
         next = lexer.GetNextTokenType();
         break;
       case TUPLET:
         // Calculate the Tuplet length as a product of the type of Tuplet and its inner note
         // lengths
         // this is dependent on all Tuplet notes having the same length
         noteLength =
             (((double) (nextToken.getElts()[0].getLength()[0] * ThePiece.getNoteLength()[0]))
                 / ((double)
                     (nextToken.getElts()[0].getLength()[1]
                         * (double) ThePiece.getNoteLength()[1])));
         noteLength = noteLength * nextToken.getLength()[0];
         meterCount += noteLength;
         // Add each note in the Tuplet as long as the whole tuplet would fit in the meter
         if (meterCount <= meterLength) {
           for (int i = 0; i < nextToken.getElts().length; i++) {
             currentMeter.addElts(
                 nextToken.getElts()[i].changeLength(
                     nextToken.getLength()[0], nextToken.getLength()[1]));
           }
           next = lexer.GetNextTokenType();
         } else {
           throw new IllegalArgumentException(
               "Overloaded Meter with a tuplet, is it counted wrong?");
         }
         next = lexer.GetNextTokenType();
         break;
       case CHORD:
         // A Chord is multiple notes played at once, so its length is simply the length of one of
         // the elements
         // This relies on the Chord having all notes of the same length
         noteLength =
             (((double) (nextToken.getLength()[0] * ThePiece.getNoteLength()[0]))
                 / ((double) (nextToken.getLength()[1] * (double) ThePiece.getNoteLength()[1])));
         meterCount += noteLength;
         // If the Chord fits, add its construct to the Meters List
         if (meterCount <= meterLength) {
           currentMeter.addElts(nextToken);
           next = lexer.GetNextTokenType();
         } else {
           throw new IllegalArgumentException(
               "Overloaded Meter with a tuplet, is it counted wrong?");
         }
         next = lexer.GetNextTokenType();
         break;
       default:
         throw new IllegalArgumentException(
             "Somehow made a note token, but it was not a NOTE, TUPLET, CHORD or REST");
     }
   }
   // To check measure lengths vs meter, apparently this no longer matters, kept for legacy
   /*
   System.out.println(meterCount+" vs "+meterLength);
   if(meterCount != meterLength && meterCount + .000001 < meterLength){
       throw new IllegalArgumentException("Got a meter with the wrong timing, bad stuff mang");
   }
   */
   // Return the finished meter
   return currentMeter;
 }