2

私はこのコードを持っています。

  private static Map<Long, List<TimePitchValue>> alternativeMethod(AudioFormat audioformat, 
      List<ChunkDTO> listChunkDTO, long index, int sizeChunk) {

    int numBytesPerSample = audioformat.getSampleSizeInBits() / 8;
    int quantitySamples = sizeChunk / numBytesPerSample;
    long baseTime = quantitySamples * index;

    Map<Long, List<TimePitchValue>> mapListTimePitchValue = new LinkedHashMap<>();

    for (int i = 0; i < quantitySamples; i++) {
      int time = i;
      List<TimePitchValue> listTimePitchValueWithTime = listChunkDTO.stream().map(chunkDTO -> {
        int value = extractValue(chunkDTO.getChunk(), numBytesPerSample, time);
        TimePitchValue timePitchValue = new TimePitchValue(chunkDTO.getPitch(), baseTime + time, value);
        return timePitchValue;
      }).collect(Collectors.toList());
      listTimePitchValueWithTime.sort(Comparator.comparingInt(TimePitchValue::getValue).reversed());
      mapListTimePitchValue.put(baseTime + time, listTimePitchValueWithTime);
    }


    return mapListTimePitchValue;
  }

time値ごとList<TimePitchValue>に が名前listTimePitchValueで生成さlistTimePitchValueれ、指定された を で指定されたものに関連付けbaseTime + timeますmapListTimePitchValue

サポートのための

  private static int extractValue(byte[] bytesSamples, int numBytesPerSample, int time) {
    byte[] bytesSingleNumber = Arrays.copyOfRange(bytesSamples, time * numBytesPerSample, (time + 1) * numBytesPerSample);
    int value = numBytesPerSample == 2
        ? (Byte2IntLit(bytesSingleNumber[0], bytesSingleNumber[1]))
        : (byte2intSmpl(bytesSingleNumber[0]));
    return value;
  }

  public static int Byte2IntLit(byte Byte00, byte Byte08) {
    return (((Byte08) << 8)
        | ((Byte00 & 0xFF)));
  }

  public static int byte2intSmpl(byte theByte) {
    return (short) (((theByte - 128) & 0xFF)
        << 8);
  }

ChunkDTOクラス

public class ChunkDTO {

  private final byte[] chunk;
  private final long index;
  private final Integer pitch;

  public ChunkDTO(byte[] chunk, long index, Integer pitch) {
    this.chunk = chunk;
    this.index = index;
    this.pitch = pitch;
  }

  public byte[] getChunk() {
    return chunk;
  }

  public long getIndex() {
    return index;
  }

  public Integer getPitch() {
    return pitch;
  }

  @Override
  public String toString() {
    return "ChunkDTO{" + "index=" + index + ", pitch=" + pitch 
        + ", chunk.length=" + (chunk!=null?chunk.length:"null") + 
        '}';
  }

  @Override
  public int hashCode() {
    int hash = 5;
    hash = 29 * hash + (int) (this.index ^ (this.index >>> 32));
    hash = 29 * hash + Objects.hashCode(this.pitch);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final ChunkDTO other = (ChunkDTO) obj;
    if (this.index != other.index) {
      return false;
    }
    if (!Objects.equals(this.pitch, other.pitch)) {
      return false;
    }
    return true;
  }

}

TimePitchValueクラス

public class TimePitchValue {

  private int pitch;
  private long time;
  private int value;

  public TimePitchValue() {
  }

  public TimePitchValue(int pitch, long time, int value) {
    this.time = time;
    this.pitch = pitch;
    this.value = value;
  }

  public int getPitch() {
    return pitch;
  }

  public void setPitch(int pitch) {
    this.pitch = pitch;
  }

  public long getTime() {
    return time;
  }

  public void setTime(long time) {
    this.time = time;
  }

  public int getValue() {
    return value;
  }

  public void setValue(int value) {
    this.value = value;
  }

  @Override
  public String toString() {
    int length = String.valueOf(value).length();
    String stringValue = new String(new char[12 - length]).replace('\0', ' ') + value;
    return "TimeNoteValue{" 
        + ", value='" + stringValue + "'}";
  }

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 13 * hash + this.pitch;
    hash = 13 * hash + (int) (this.time ^ (this.time >>> 32));
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final TimePitchValue other = (TimePitchValue) obj;
    if (this.pitch != other.pitch) {
      return false;
    }
    if (this.time != other.time) {
      return false;
    }
    return true;
  }

}

質問は、ソートされたものを直接返すことは可能Map<Long, List<TimePitchValue>>ですIntStreamか? (事前に作成Map<Long, List<TimePitchValue>> mapListTimePitchValue = new LinkedHashMap<>();して後で配置せずにmapListTimePitchValue.put(baseTime + time, listTimePitchValue);

4

1 に答える 1