Blog der Heimetli Software AG

Java spielt Jingle-Bells

Was whrscheinlich viele Leute nicht wissen: Java hat auch ein Sound-Interface.

Das API ist anscheinend nur ein dünner Layer auf der MIDI-Schnittstelle. Aber keine Angst, es braucht keine MIDI-Hardware, der Sound wird durch einen Software- Synthesizer erzeugt.

Das Programm ist im Brute-Force Stil geschrieben, aber ich kann das Stück jetzt nicht mehr hören, und darum bleibt es mindestens bis zur nächsten Weihnacht wie es gerade ist...

Der Code für Jingle-Bells

/******************************************************************************/
/*                                                                            */
/*                                                          FILE: Jingle.java */
/*                                                                            */
/*     This program plays Jingle Bells (of MIDI is installed)                 */
/*     ======================================================                 */
/*                                                                            */
/*     V0.01  25-DEC-2020 Te (with the help of my daughter)                   */
/*                                                                            */
/******************************************************************************/

import javax.sound.midi.* ;
import java.io.* ;

public class Jingle
{
   public static int DELAY = 200 ;

   private static void play( MidiChannel channel, int note, int attack, int duration ) throws InterruptedException
   {
      channel.noteOn( note, attack ) ;
      Thread.sleep( duration ) ;
      channel.noteOff( note, 0 ) ;
   }
   
   private static void play( MidiChannel channel, int note, int attack, int duration, int pause ) throws InterruptedException
   {
      play( channel, note, attack, duration ) ;
      Thread.sleep( pause ) ;
   }

   public static void main( String args[] )
   {
      int DELAY = 200 ;
 
      try
      {
         Synthesizer synth = MidiSystem.getSynthesizer() ;
         synth.open() ;

         if( synth.getDefaultSoundbank() != null )
         {
            Instrument  inst[] = synth.getDefaultSoundbank().getInstruments() ;
            synth.loadInstrument( inst[0] ) ;
         }

         MidiChannel channels[] = synth.getChannels() ;

         if( channels[4] != null )
         {
            MidiChannel channel = channels[4] ;
            
            // Try 123 instead of 14
            channel.programChange( 14 ) ;

            // 1
            play( channel, 55,  96, DELAY ) ;
            play( channel, 64,  89, DELAY ) ;
            play( channel, 62,  90, DELAY ) ;
            play( channel, 60,  93, DELAY ) ;

            // 2
            play( channel, 55,  96, 2 * DELAY, DELAY ) ;
            play( channel, 55, 102, DELAY / 2 ) ;
            play( channel, 55,  99, DELAY / 2 ) ;

            // 3
            play( channel, 55,  90, DELAY ) ;
            play( channel, 64,  90, DELAY ) ;
            play( channel, 62,  88, DELAY ) ;
            play( channel, 60,  94, DELAY ) ;
            
            // 4
            play( channel, 57, 102, 3 * DELAY, DELAY ) ;

            // 5
            play( channel, 57,  96, DELAY ) ;
            play( channel, 65,  94, DELAY ) ;
            play( channel, 64,  95, DELAY ) ;
            play( channel, 62,  91, DELAY ) ;

            // 6
            play( channel, 59,  99, 3 * DELAY, DELAY ) ;

            // 7
            play( channel, 67, 110, DELAY ) ;
            play( channel, 67, 110, DELAY ) ;
            play( channel, 65,  91, DELAY ) ;
            play( channel, 62,  90, DELAY ) ;

            // 8
            play( channel, 64, 103, 3 * DELAY, DELAY ) ;

            // 9
            play( channel, 55,  91, DELAY ) ;
            play( channel, 64,  89, DELAY ) ;
            play( channel, 62,  90, DELAY ) ;
            play( channel, 60,  87, DELAY ) ;

            // 10
            play( channel, 55,  91, 3 * DELAY, DELAY ) ;

            // 11
            play( channel, 55,  90, DELAY ) ;
            play( channel, 64,  90, DELAY ) ;
            play( channel, 62,  88, DELAY ) ;
            play( channel, 60,  94, DELAY ) ;

            // 12
            play( channel, 57, 102, 2 * DELAY, DELAY ) ;
            play( channel, 57, 102, DELAY ) ;

            // 13
            play( channel, 57,  96, DELAY ) ;
            play( channel, 65,  94, DELAY ) ;
            play( channel, 64,  95, DELAY ) ;
            play( channel, 62,  91, DELAY ) ;

            // 14
            play( channel, 67, 110, DELAY ) ;
            play( channel, 67, 110, DELAY ) ;
            play( channel, 67, 110, DELAY ) ;
            play( channel, 67, 110, DELAY ) ;

            // 15
            play( channel, 69, 110, DELAY ) ;
            play( channel, 67, 110, DELAY ) ;
            play( channel, 65,  91, DELAY ) ;
            play( channel, 62,  90, DELAY ) ;

            // 16
            play( channel, 60,  90, DELAY, DELAY ) ;
            play( channel, 67,  90, 2 * DELAY ) ;

            // Refrain
            play( channel, 64,  89, DELAY ) ;
            play( channel, 64,  89, DELAY ) ;
            play( channel, 64,  89, 2 * DELAY ) ;

            play( channel, 64,  89, DELAY ) ;
            play( channel, 64,  89, DELAY ) ;
            play( channel, 64,  89, 2 * DELAY ) ;

            play( channel, 64,  89, DELAY ) ;
            play( channel, 67, 110, DELAY ) ;
            play( channel, 60,  90, 3 * DELAY / 2 ) ;
            play( channel, 62,  90, DELAY / 2 ) ;

            play( channel, 64,  105, 3 * DELAY, DELAY ) ;

            play( channel, 65,  89, DELAY ) ;
            play( channel, 65, 110, DELAY ) ;
            play( channel, 65,  90, 3 * DELAY / 2 ) ;
            play( channel, 65,  90, DELAY / 2 ) ;

            play( channel, 65,  95, DELAY ) ;
            play( channel, 64,  95, DELAY ) ;
            play( channel, 64,  95, DELAY ) ;
            play( channel, 64,  95, DELAY / 2 ) ;
            play( channel, 64,  95, DELAY / 2 ) ;

            play( channel, 64,  95, DELAY ) ;
            play( channel, 62,  90, DELAY ) ;
            play( channel, 62,  90, DELAY ) ;
            play( channel, 64,  95, DELAY ) ;

            play( channel, 62,  95, 2 * DELAY ) ;
            play( channel, 67,  90, 2 * DELAY ) ;
            
            play( channel, 64,  89, DELAY ) ;
            play( channel, 64,  89, DELAY ) ;
            play( channel, 64,  89, 2 * DELAY ) ;

            play( channel, 64,  89, DELAY ) ;
            play( channel, 64,  89, DELAY ) ;
            play( channel, 64,  89, 2 * DELAY ) ;

            play( channel, 64,  89, DELAY ) ;
            play( channel, 67, 110, DELAY ) ;
            play( channel, 60,  90, 3 * DELAY / 2 ) ;
            play( channel, 62,  90, DELAY / 2 ) ;

            play( channel, 64,  105, 3 * DELAY, DELAY ) ;

            play( channel, 65,  89, DELAY ) ;
            play( channel, 65, 110, DELAY ) ;
            play( channel, 65,  90, 3 * DELAY / 2 ) ;
            play( channel, 65,  90, DELAY / 2 ) ;

            play( channel, 65,  95, DELAY ) ;
            play( channel, 64,  95, DELAY ) ;
            play( channel, 64,  95, DELAY ) ;
            play( channel, 64,  95, DELAY / 2 ) ;
            play( channel, 64,  95, DELAY / 2 ) ;

            play( channel, 67,  95, DELAY ) ;
            play( channel, 67,  95, DELAY ) ;
            play( channel, 65,  95, DELAY ) ;
            play( channel, 62,  90, DELAY ) ;

            play( channel, 60,  90, 3 * DELAY, DELAY ) ;
         }

         synth.close() ;
         System.exit( 0 ) ;
      }
      catch( Exception e )
      {
         System.out.println( "Exception: " + e ) ;
      }
   }
}

Selber spielen?

Mit diesem Link können Sie die Source herunterladen