Ejemplo n.º 1
0
  @Test
  public void sampleTest2() {
    String input =
        "d^f^f ^cee|de^f g^fe|d^f^f ^cee|d^fe dBA|\n"
            + "d^f^f ^cee|de^f g^fe|^fa^f g^fe|[1 d^fe dBA:|[2 d^fe d^cB|]\n"
            + "A3 B3|g^fe ^fdB|A^FA B2^c|d^fe d^cB|\n"
            + "A3 B3|e^fe e^fg|^fa^f g^fe|[1 d^fe d^cB:|[2 d^fe dBA|]\n"
            + "^fAA eAA| de^f g^fe|^fAA eAA|d^fe dBA|\n"
            + "^fAA eAA| de^f g^fe|^fa^f g^fe|d^fe dBA:|\n";

    CharStream stream = new ANTLRInputStream(RepeatHandler.handleRepeats(input));
    MusicGrammarLexer lexer = new MusicGrammarLexer(stream);
    lexer.reportErrorsAsExceptions();
    TokenStream tokens = new CommonTokenStream(lexer);
    MusicGrammarParser parser = new MusicGrammarParser(tokens);
    parser.reportErrorsAsExceptions();
    ParseTree tree = parser.root();
    ParseTreeWalker walker = new ParseTreeWalker();
    ConstructMusic listener = new ConstructMusic(new HeaderInfo());
    walker.walk(listener, tree);
    Music sample = listener.getExpression();
    try {
      SequencePlayer player = new SequencePlayer(200, 12);
      sample.play(player, 0);
      player.play();
      Thread.sleep((long) sample.duration() * 1000);
    } catch (MidiUnavailableException | InvalidMidiDataException e) {
      e.printStackTrace();
    } catch (InterruptedException uie) {
      uie.printStackTrace();
    }
  }
Ejemplo n.º 2
0
  @Test
  public void sampleTest() {
    String input =
        "C,, D,, E,, F,, | G,, A,, B,, C, | "
            + "C, D, E, F, | G, A, B, C | "
            + "C D E F | G A B c "
            + "| c d e f | g a b c' | "
            + "c' d' e' f' | g' a' b' c''\n";

    CharStream stream = new ANTLRInputStream(input);
    MusicGrammarLexer lexer = new MusicGrammarLexer(stream);
    lexer.reportErrorsAsExceptions();
    TokenStream tokens = new CommonTokenStream(lexer);
    MusicGrammarParser parser = new MusicGrammarParser(tokens);
    parser.reportErrorsAsExceptions();
    ParseTree tree = parser.root();
    ParseTreeWalker walker = new ParseTreeWalker();
    ConstructMusic listener = new ConstructMusic(new HeaderInfo());
    walker.walk(listener, tree);
    Music sample = listener.getExpression();
    try {
      SequencePlayer player = new SequencePlayer(140, 12);
      sample.play(player, 0);
      player.play();
      Thread.sleep((long) sample.duration() * 1000);
    } catch (MidiUnavailableException | InvalidMidiDataException e) {
      e.printStackTrace();
    } catch (InterruptedException uie) {
      uie.printStackTrace();
    }
  }
Ejemplo n.º 3
0
 // musicPlayEmptyTest(): empty music
 @Test
 public void musicPlayEmptyTest() {
   SequencePlayer player;
   try {
     player = new SequencePlayer(140, 12);
     Music empty = new Parallel(new Rest(0), new Merge(new Rest(0), new Rest(0)));
     empty.play(player, 0);
     assertEquals("Meta event: END_OF_TRACK Tick: 0\n", player.toString());
   } catch (MidiUnavailableException | InvalidMidiDataException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 4
0
 // mergePlayEmptyTest(): m1 and/or m2 empty
 @Test
 public void mergePlayEmptyTest() {
   Merge a = new Merge(new Rest(0), new Rest(0));
   try {
     SequencePlayer player = new SequencePlayer(140, 12);
     a.play(player, 0);
     assertEquals("Meta event: END_OF_TRACK Tick: 0\n", player.toString());
   } catch (MidiUnavailableException mue) {
     mue.printStackTrace();
   } catch (InvalidMidiDataException imde) {
     imde.printStackTrace();
   }
 }
Ejemplo n.º 5
0
 // musicPlayLargeCaseTest(): row row row your boat
 @Test
 public void musicPlayLargeCaseTest() {
   try {
     SequencePlayer player = new SequencePlayer(140, 12);
     rowRowRowYourBoat.play(player, .125);
     player.play();
     Thread.sleep(10000);
   } catch (MidiUnavailableException mue) {
     mue.printStackTrace();
   } catch (InvalidMidiDataException imde) {
     imde.printStackTrace();
   } catch (InterruptedException ie) {
     ie.printStackTrace();
   }
 }
Ejemplo n.º 6
0
 // musicPlaySingleNoteTest(): single note
 @Test
 public void musicPlaySingleNoteTest() {
   SequencePlayer player;
   try {
     player = new SequencePlayer(140, 12);
     Music a = new Note(1, new Pitch('A'));
     a.play(player, 0);
     assertEquals(
         "Event: NOTE_ON  Pitch: 69  Tick: 0\n"
             + "Event: NOTE_OFF Pitch: 69  Tick: 12\n"
             + "Meta event: END_OF_TRACK Tick: 12\n",
         player.toString());
   } catch (MidiUnavailableException | InvalidMidiDataException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 7
0
 // parallelPlayEqualsTest(): m1 = m2
 @Test
 public void parallelPlayEqualsTest() {
   Parallel a = new Parallel(new Note(1, new Pitch('C')), new Note(1, new Pitch('C')));
   try {
     SequencePlayer player = new SequencePlayer(140, 12);
     a.play(player, 0);
     assertEquals(
         "Event: NOTE_ON  Pitch: 60  Tick: 0\n"
             + "Event: NOTE_ON  Pitch: 60  Tick: 0\n"
             + "Event: NOTE_OFF Pitch: 60  Tick: 12\n"
             + "Event: NOTE_OFF Pitch: 60  Tick: 12\n"
             + "Meta event: END_OF_TRACK Tick: 12\n",
         player.toString());
   } catch (MidiUnavailableException mue) {
     mue.printStackTrace();
   } catch (InvalidMidiDataException imde) {
     imde.printStackTrace();
   }
 }
Ejemplo n.º 8
0
 // chordPlayDifferentDurationsTest(): m1.duration != m2.duration
 @Test
 public void chordPlayDifferentDurationsTest() {
   Chord a = new Chord(new Note(1, new Pitch('D')), new Note(2, new Pitch('C')));
   try {
     SequencePlayer player = new SequencePlayer(140, 12);
     a.play(player, 0);
     assertEquals(
         "Event: NOTE_ON  Pitch: 62  Tick: 0\n"
             + "Event: NOTE_ON  Pitch: 60  Tick: 0\n"
             + "Event: NOTE_OFF Pitch: 62  Tick: 12\n"
             + "Event: NOTE_OFF Pitch: 60  Tick: 24\n"
             + "Meta event: END_OF_TRACK Tick: 24\n",
         player.toString());
   } catch (MidiUnavailableException mue) {
     mue.printStackTrace();
   } catch (InvalidMidiDataException imde) {
     imde.printStackTrace();
   }
 }
Ejemplo n.º 9
0
 // notePlayTest(): covers all partitions
 @Test
 public void notePlayTest() {
   Note shortNote = new Note(.5, new Pitch('A'));
   Note highNote = new Note(20, new Pitch('A')).transpose(20);
   try {
     SequencePlayer player = new SequencePlayer(140, 12);
     shortNote.play(player, 0);
     highNote.play(player, .5);
     assertEquals(
         "Event: NOTE_ON  Pitch: 69  Tick: 0\n"
             + "Event: NOTE_OFF Pitch: 69  Tick: 6\n"
             + "Event: NOTE_ON  Pitch: 89  Tick: 6\n"
             + "Event: NOTE_OFF Pitch: 89  Tick: 246\n"
             + "Meta event: END_OF_TRACK Tick: 246\n",
         player.toString());
   } catch (MidiUnavailableException mue) {
     mue.printStackTrace();
   } catch (InvalidMidiDataException imde) {
     imde.printStackTrace();
   }
 }