Example #1
0
  public void _testGPWithPlayer(MidiSongDefinition sd, int usq) throws Exception {
    MidiSongDefinition testFile = SongArchive.testFileSongDefinition();
    try {
      GPInputStream gpis = new GPInputStream(sd.getGpFileName());
      GPSong gpsong = (GPSong) gpis.readObject();
      gpis.close();

      int tempoGPSong = gpsong.getTempo();
      // OLD assertEquals((int)(60*1000*1000/usq),tempoGPSong);
      assertEquals((60 * 1000 * 1000 / usq), tempoGPSong);

      Song song = GPAdaptor.makeSong(gpsong);

      Tempo tempoSong = song.getTempo();
      assertEquals(usq, (int) tempoSong.getUSQ());

      MasterPlayer player = new MasterPlayer();
      player.setSoundPlayer(new MidiFiler(testFile.getMidiFileName()));
      Performance performance = player.arrange(song, null);

      Tempo tempoPerformance = performance.getTempo();
      assertEquals(usq, (int) tempoPerformance.getUSQ());

      // a performance is really a sequence. So make sure there
      // is a tempo event (meta 0x51) on track 0.

      // make sure as well there is exactly ONE tempo event at timestamp 0
      Sequence sequence = (Sequence) performance;
      Track[] midiTracks = sequence.getTracks();
      Track tempoMap = midiTracks[0];

      Tempo tempoInMIDI = new Tempo();
      for (int i = 0; i < tempoMap.size(); i++) {
        MidiEvent me = tempoMap.get(i);
        long tick = me.getTick();
        if (tick > 0) break;
        MidiMessage mm = me.getMessage();
        if (mm.getStatus() == MetaMessage.META) {
          MetaMessage meta = (MetaMessage) mm;
          if (meta.getType() == 0x51) {
            byte[] data = meta.getData();
            tempoInMIDI.setUSQ(
                ((data[0] & 0x00FF) << 16) | ((data[1] & 0x00FF) << 8) | ((data[2] & 0x00FF)));
            break;
          }
        }
      }
      assertEquals(usq, (int) tempoInMIDI.getUSQ());

      MidiOutputStream mos = new MidiOutputStream(new FileOutputStream(testFile.getMidiFileName()));
      mos.write(performance);
      mos.close();

      compareMIDIFiles(
          sd.getMidiFileName(), testFile.getMidiFileName(), sd.getChannels(), sd.getEventRemap());
    } catch (FileNotFoundException e) {
      fail("file not found exception");
    } catch (GPFormatException e) {
      fail("gp format exception");
    } catch (IOException e) {
      fail("ioexception");
    } catch (CodecFormatException e) {
      fail("codec format exception");
    } catch (InvalidMidiDataException e) {
      fail("invalid midi data exception");
    }
  }
  /**
   * @param piece a loaded GP4 file
   * @return a representation of the piece that supports the Song interface
   * @throws GP4AdaptorException
   */
  public static Song makeSong(GPSong piece) throws GPFormatException {
    String methodName = "makeSong";
    logger.entering(className, methodName);

    int bpm = piece.getTempo();
    logger.finer("Tempo: " + bpm + "BPM");

    Tempo tempo = new Tempo();
    tempo.setBPM(bpm);

    Song song = new SongImpl(PPQ_HIGH_RESOLUTION / PPQ_SCALE_FACTOR, tempo);

    List<GPMeasure> measures = piece.getMeasures();
    logger.finer("Number of measures: " + measures.size());

    List<GPTrack> tracks = piece.getTracks();
    logger.finer("Number of tracks: " + tracks.size());

    // List fretStates = new LinkedList();

    int index = 1;
    for (Iterator<GPTrack> it = tracks.iterator(); it.hasNext(); index++) {
      GPTrack track = it.next();
      int port = track.getPort();
      int channel = track.getChannel();
      int channelEffects = track.getChannelEffects();

      logger.fine(
          "Track " + index + " port " + port + " channels " + channel + "," + channelEffects);

      int channelIndex = (port - 1) * 16 + (channel - 1);
      GPMIDIChannel mc = piece.getChannels(channelIndex);

      int strings = track.getNumberOfStrings();

      SongTrack st = new SongTrackImpl(index, strings); // channel is
      // 1-based
      st.setPrimaryDevice(new SongDeviceImpl(port, channel));
      if (channel != channelEffects) {
        st.setSecondaryDevice(new SongDeviceImpl(port, channelEffects));
      }

      st.setBendSensitivity(2);

      int instrument = mc.getInstrument();
      logger.fine("Instrument " + instrument);

      // Only set valid instruments. The drum track usually doesn't have
      // one.
      st.setProgram(instrument);

      // TODO Verify the volume setting is correct
      st.setVolume(8 * mc.getVolume());
      // TODO Verify the pan setting is correct
      st.setPan(8 * mc.getBalance());
      // TODO Verify the chorus setting is correct
      st.setChorus(8 * mc.getChorus());
      // TODO verify the reverb setting is correct
      st.setReverb(8 * mc.getReverb());
      // TODO Verify the tremolo setting is correct
      st.setTremolo(8 * mc.getTremolo());
      // TODO Verify the phaser setting is correct
      st.setPhaser(8 * mc.getPhaser());

      song.addTrack(st);
    }

    List<GPMeasureTrackPair> measureTrackPairs = piece.getMeasuresTracksPairs();
    logger.finer("Measure/Track pairs: " + measureTrackPairs.size());

    ListIterator<GPMeasureTrackPair> itMTP = measureTrackPairs.listIterator();
    ListIterator<GPMeasure> itM = measures.listIterator();

    while (itM.hasNext()) {
      SongPhrase sp = phraseFactory(song, itM, itMTP, tracks);
      song.addPhrase(sp);
    }

    logger.exiting(className, methodName, song);
    return song;
  }