Пример #1
0
  public static void main(String[] args) throws Exception {

    synth = MidiSystem.getSynthesizer();
    synth.open();
    synth.loadAllInstruments(synth.getDefaultSoundbank());
    //        int s = 0;
    //        for(Instrument i : synth.getAvailableInstruments()){
    //            System.out.println(s+" "+i.getName());
    //            s++;
    //        }
    chan = synth.getChannels()[0];
    for (Instrument i : synth.getLoadedInstruments())
      if (i.getName().toLowerCase().trim().contains("sine"))
        chan.programChange(i.getPatch().getProgram());

    if (chan.getProgram() == 0)
      JOptionPane.showMessageDialog(null, "Could not find a valid instrument. Sound is disabled");
    // chan.programChange(synth.getLoadedInstruments()[197].getPatch().getProgram());

    for (int i = 0; i < array.length; i++) marked.add(-5);
    rianr(array);
    window.setSize(new Dimension(640, 480));
    window.setLocation(0, 0);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setTitle("Array Visualizer");

    uf = new UtilFrame(window);

    // AUDIO THREAD
    new Thread() {
      @Override
      public void run() {
        while (true) {
          chan.allNotesOff();
          if (SOUND == false) {
            continue;
          }

          int tmp = 1;
          for (int i : marked) if (i != -5) tmp++;
          for (int i : marked)
            if (i != -5) {
              int pitch =
                  (int)
                      Math.round(
                          (double) array[Math.min(Math.max(i, 0), array.length - 1)]
                                  / array.length
                                  * 96
                              + 16);
              // int vel = (int)(((128-pitch)/320.0+0.4)   *   (128.0/Math.pow(tmp,0.33)));
              int vel = (int) (64.0 / Math.pow(tmp, 0.5));
              chan.noteOn(pitch, vel);
            }
          // ((int)((127-(array[Math.min(Math.max(i,
          // 0),array.length-1)]/16.0))/Math.sqrt(calcVel())))*5
          // double tmp = (array[Math.min(Math.max(i, 0),array.length-1)]/32.0+47);
          // chan.setPitchBend(8192*2-(int)((tmp-Math.floor(tmp))*8192*2));
          /*
          ArrayList<Integer> tmp = new ArrayList<Integer>();
          for(int i : marked)
              if(i != -5)
                  tmp.add(i);

          if(tmp.size() > 0){
              do{
                  int i = tmp.get(snd%tmp.size());
                  snd++;
              chan.noteOn(array[Math.min(Math.max(i, 0),array.length-1)]/32+47, 127);
              double tmpd = (array[Math.min(Math.max(i, 0),array.length-1)]/32.0+47);
              chan.setPitchBend(8192*2-(int)((tmpd-Math.floor(tmpd))*8192*2));
              }while(false);}*/
          try {
            sleep(1);
          } catch (Exception e) {
          }
        }
      }
    }.start();

    // DRAW THREAD
    new Thread() {
      @Override
      public void run() {
        int cw = window.getWidth();
        int ch = window.getHeight();
        Image img = window.createVolatileImage(cw, ch);
        // Graphics g = window.getGraphics();
        Graphics g = img.getGraphics();
        double xscl, yscl;
        while (true) {

          if (window.getWidth() != cw
              || window.getHeight() != ch
              || window.getX() != cx
              || window.getY() != cy) {
            uf.reposition();
            if (v != null && v.isVisible()) v.reposition();
            cx = window.getX();
            cy = window.getY();
          }

          if (window.getWidth() != cw || window.getHeight() != ch) {
            cw = window.getWidth();
            ch = window.getHeight();
            img = window.createVolatileImage(cw, ch);
            fon = new Font("TimesRoman", Font.PLAIN, (int) (cw / 1280.0 * 25));
            g = img.getGraphics();
          }
          int gamt = 32; // (int)(frames/1000.0%64);
          g.setColor(new Color(gamt, gamt, gamt));
          g.fillRect(0, 0, img.getWidth(null), img.getHeight(null));
          xscl = (double) window.getWidth() / array.length;
          yscl = (double) (window.getHeight() - 30) / array.length;
          int amt = 0;
          int circamt = array.length / 2;
          frames++;
          if (CIRCLEDRAW)
            for (int i = 0; i < array.length; i++) {
              if (marked.contains(i)
                  || marked.contains(i - 1)
                  || marked.contains(i - 2)
                  || marked.contains(i - 3)) g.setColor(Color.BLACK);
              else g.setColor(getIntColor(array[i]));
              // COLOR ONLY NO LENGTH
              if (COLORONLY)
                g.drawLine(
                    window.getWidth() / 2,
                    window.getHeight() / 2,
                    window.getWidth() / 2
                        + (int) (Math.sin(i * Math.PI / circamt) * (window.getWidth() - 64) / 2.0),
                    window.getHeight() / 2
                        - (int)
                            (Math.cos(i * Math.PI / circamt) * (window.getHeight() - 96) / 2.0));
              else if (PIXELDRAW)
                g.fillRect(
                    window.getWidth() / 2
                        + (int)
                            (Math.sin(i * Math.PI / circamt)
                                * ((window.getWidth() - 64)
                                    / 2.0
                                    * (array[i] / (double) array.length))),
                    window.getHeight() / 2
                        - (int)
                            (Math.cos(i * Math.PI / circamt)
                                * ((window.getHeight() - 96)
                                    / 2.0
                                    * (array[i] / (double) array.length))),
                    (int) (2 * (window.getWidth() / 640.0)),
                    (int) (2 * (window.getHeight() / 480.0)));
              // LENGTH AND COLOR
              else
                g.drawLine(
                    window.getWidth() / 2,
                    window.getHeight() / 2,
                    window.getWidth() / 2
                        + (int)
                            (Math.sin(i * Math.PI / circamt)
                                * ((window.getWidth() - 64)
                                    / 2.0
                                    * (array[i] / (double) array.length))),
                    window.getHeight() / 2
                        - (int)
                            (Math.cos(i * Math.PI / circamt)
                                * ((window.getHeight() - 96)
                                    / 2.0
                                    * (array[i] / (double) array.length))));
            }
          else
            for (int i = 0; i < array.length; i++) {
              if (marked.contains(i)
                  || marked.contains(i - 1)
                  || marked.contains(i - 2)
                  || marked.contains(i - 3)) g.setColor(Color.BLACK);
              else g.setColor(getIntColor(array[i]));

              int y = 0;
              int width = (int) (xscl * i) - amt;

              if (width > 0) {
                if (COLORONLY) {
                  y = (int) (window.getHeight() - 750 * yscl);
                  g.fillRect(amt, y, width, Math.max((int) (750 * yscl), 1));
                  g.setColor(getRevColor());
                  g.fillRect((int) (i * xscl), y, width, 6);
                } else if (PIXELDRAW) {
                  y = (int) (window.getHeight() - array[i] * yscl);
                  g.fillRect(amt, y, width, 3);
                } else {
                  y = (int) (window.getHeight() - array[i] * yscl);
                  g.fillRect(amt, y, width, Math.max((int) (array[i] * yscl), 1));
                  g.setColor(getRevColor());
                  g.fillRect(amt, y, width, 6);
                }
              }
              amt += width;
            }

          int coltmp = 255; // (int)Math.abs(Math.sin(frames*0.01)*255);
          g.setColor(new Color(coltmp, coltmp, coltmp));
          Font f = g.getFont();
          g.setFont(fon);
          g.drawString(heading, 10, (int) (cw / 1280.0 * 20) + 30);
          g.drawString(
              "Comparisons: " + comps + " Array Accesses: " + aa,
              10,
              (int) (cw / 1280.0 * 40) + 30);
          g.setFont(f);
          Graphics g2 = window.getGraphics();
          g2.setColor(Color.BLACK);
          g2.drawImage(img, 0, 0, null);
        }
      }

      public Color getIntColor(int i) {
        return Color.getHSBColor(((float) i / array.length), 1.0F, 0.8F);
      }

      public Color getRevColor() {
        return getIntColor((int) (Math.sin(frames / 66.67) * array.length));
      }
    }.start();

    uf.setVisible(false);
    v = new ViewPrompt(window);
    while (v.isVisible()) Thread.sleep(1);
    uf.setVisible(true);

    // bogoSort();
    //        while(true){
    //            clearmarked();
    //
    //            //heading = "Linear Search";
    //
    //            //Arrays.sort(array);
    //            //marked.set(1,730);
    //            //linearSearch(730);
    //            //refresharray();
    //
    //            //heading = "Binary Search";
    //
    //            //Arrays.sort(array);
    //            //marked.set(1, 730);
    //            //binarySearch(730);
    //
    //            //heading = "Shatter-Time Sort";
    //
    //            //timeSort();
    //
    //            //chan.allNotesOff();
    //            //refresharray();
    //            weaveMergeSort(0, array.length-1);
    //            refresharray();
    //            cocktailShakerSort();
    //            refresharray();
    //            radixLSDsort(4);
    //            refresharray()
    //  ;RadixLSDInPlace.inPlaceRadixLSDSort(10);refresharray();
    //
    //            heading = "Insertion Sort";
    //
    //            insertionSort();
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Bubble Sort";
    //
    //            bubbleSort();
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Selection Sort";
    //
    //            selectionSort();
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Cocktail Shaker Sort";
    //
    //            cocktailShakerSort();
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Double Selection Sort";
    //
    //            doubleSelectionSort(array);
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Merge Sort In-Place";
    //
    //            mergeSort(0, array.length - 1);
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Merge Sort Out-of-Place";
    //
    //            mergeSortOP();
    //
    //            chan.allNotesOff();
    //            refresharray();
    //
    //            heading = "Gravity Sort (Abacus/Bead)";
    //
    //            gravitySort();
    //
    //            chan.allNotesOff();
    //            refresharray();
    //
    //            heading = "Quick Sort";
    //
    //            quickSort(array, 0, array.length-1);
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Counting Sort";
    //
    //            countingSort();
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Radix LSD Sort";
    //
    //            radixLSDsort(4);
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Radix LSD In-Place Sort";
    //
    //            inPlaceRadixLSDSort(10);
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            heading = "Radix MSD Sort";
    //
    //            radixMSDSort(4);
    //            refresharray();
    //            //heading = "Shatter Partition";
    //
    //            //shatterPartition(1);
    //
    //            //chan.allNotesOff();
    //            //refresharray();
    //            heading = "Shatter Sort";
    //
    //            shatterSort(128);
    //
    //            chan.allNotesOff();
    //            refresharray();
    //            //heading = "Simple Shatter Sort";
    //
    //            //simpleShatterSort(128, 4);
    //
    //            //chan.allNotesOff();
    //            //refresharray();
    //
    // keep on keeping on
    while (window.isActive()) Thread.sleep(1);
  }
Пример #2
0
  // possibly .......,.
  @Deprecated
  protected void setUpKeys() {

    getPlayOptions();

    if (!isDrumLane()) {
      getPlayOptions().drumMapped = false;

    } else {
      if (playOptions.noteMap != null) {
        playOptions.drumMapped = true;
      }
    }
    notifyFocusListeners();

    if (true) {
      return;
    }

    keyNames = null;
    if (!isDrumLane()) {
      playOptions.drumMapped = false;

    } else {
      if (playOptions.noteMap != null) {
        playOptions.drumMapped = true;
      }
      Instrument inst = null;
      MidiDevice dev = getMidiDevice();

      if (dev instanceof SynthRack) {
        Synth syn = ((SynthRack) dev).getSynth(midiChannel);
        if (syn instanceof MySampler) {
          keyNames = new String[128];
          MySampler mys = (MySampler) syn;
          SampledSoundSettings[][] ssss = mys.sampledSounds;

          for (int i = 0; i < 128; i++) {
            if (ssss[i][0] != null) {
              keyNames[i] = ssss[i][0].toString();
            }
          }
        }
      } else if (dev instanceof SynthWrapper) {
        dev = ((SynthWrapper) dev).getRealDevice();
        if (dev instanceof Synthesizer) {
          Synthesizer synth = (Synthesizer) dev;
          //      System.out.println(synth);
          MyPatch patch = getProgram();

          Method getChannels = null;

          //         System.out.println(" LANE PATCH " + patch);
          Instrument insts[] = synth.getLoadedInstruments();
          for (Instrument ins : insts) {
            //        System.out.println(" INST :" + ins);
            Instrument li = (Instrument) ins;

            boolean[] channels = null;
            try {
              if (getChannels != null) {
                if (getChannels.getDeclaringClass() != li.getClass()) {
                  getChannels = null;
                }
              }
              if (getChannels == null) {
                getChannels = li.getClass().getMethod("getChannels");
              }
              if (getChannels != null) {
                channels = (boolean[]) getChannels.invoke(li, (Object[]) null);
              }
            } catch (Exception e) {
            }

            //                         System. out.print(ins.getName() + " "
            //                         + ins.getPatch().getBank() + " "
            //                         + ins.getPatch().getProgram() + " ");
            //                         for (int i = 0; i < li.getChannels().length; i++) {
            //                         if (li.getChannels()[i])
            //                         System. out.print(i + "|");
            //                         }
            //
            //                         System. out.println(li.getChannels());
            if (channels != null) {
              if ((ins.getPatch().getProgram() == patch.prog) && channels[midiChannel]) {
                inst = ins;
                break;
              }
            }
          }

          if (inst == null) {
            insts = synth.getAvailableInstruments();
            for (Instrument ins : insts) {
              Instrument li = (Instrument) ins;

              boolean[] channels = null;
              try {
                if (getChannels != null) {
                  if (getChannels.getDeclaringClass() != li.getClass()) {
                    getChannels = null;
                  }
                }
                if (getChannels == null) {
                  getChannels = li.getClass().getMethod("getChannels");
                }
                if (getChannels != null) {
                  channels = (boolean[]) getChannels.invoke(li, (Object[]) null);
                }
              } catch (Exception e) {
              }
              // System. out.print(ins.getName() + " "
              // + ins.getPatch().getBank() + " "
              // + ins.getPatch().getProgram() + " ");
              // for (int i = 0; i < li.getChannels().length; i++) {
              // if (li.getChannels()[i])
              // System. out.print(i + "|");
              // }
              //
              // System. out.println(li.getChannels());
              if (channels != null) {
                if ((ins.getPatch().getProgram() == patch.prog) && channels[midiChannel]) {
                  inst = ins;
                  break;
                }
              }
            }
          }

          if (inst != null) {
            try {
              Method getKeys = inst.getClass().getMethod("getKeys");
              if (getKeys != null) {
                keyNames = (String[]) getKeys.invoke(inst, (Object[]) null);
              }
            } catch (Exception e) {
            }
          }
        }
      }
    }

    // System. out.println(" Setup keynames " + keyNames);
    notifyFocusListeners();
  }