// Path: app/src/main/java/com/google/audio/asr/TimeUtil.java
// public final class TimeUtil {
//   public static Instant toInstant(Timestamp t) {
//     return new Instant(Timestamps.toMillis(t));
//   }
// 
//   public static Timestamp toTimestamp(Instant t) {
//     return Timestamps.fromMillis(t.getMillis());
//   }
// 
//   public static Duration convert(com.google.protobuf.Duration d) {
//     return Duration.millis(Durations.toMillis(d));
//   }
// 
//   public static com.google.protobuf.Duration convert(Duration d) {
//     return Durations.fromMillis(d.getMillis());
//   }
//   private TimeUtil() {}
// }


import com.google.audio.asr.TimeUtil;
import com.google.audio.asr.TranscriptionResult;
import com.google.cloud.speech.v1p1beta1.WordInfo;
import com.google.common.base.Splitter;
import com.google.protobuf.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.joda.time.Duration;
import org.joda.time.Instant;
/*
 * Copyright 2019 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.audio.asr.cloud;


/**
 * Calculates unfinalized and finalized timestamps and adds them to the word-level details of
 * utterances. Finalized timestamps are simply copied from the corresponding timestamps returned by
 * the cloud. Unfinalized timestamps do not exist in the response returned by the cloud, so they are
 * computed from the returned transcript instead.
 */
public class TimestampCalculator {

  /**
   * Keeps track of the session start time because the finalized word times are relative to the time
   * of the beginning of the session.
   */
  private final Instant sessionStartTime;

  /**
   * Stores the time instants of each word in the un-finalized utterance. As the utterance is
   * updated with more words, this array marks the time of the new words.
   */
  private final ArrayList<Instant> unfinalizedWordInstants = new ArrayList<>();

  private static final int NANOS_PER_MILLIS = 1_000_000;

  public TimestampCalculator(Instant newSessionStartTime) {
    this.sessionStartTime = newSessionStartTime;
  }

  public Timestamp getFinalizedStartTimestamp(WordInfo wordInfo) {
    Duration startOffset =
        Duration.standardSeconds(wordInfo.getStartTime().getSeconds())
            .plus(Duration.millis(wordInfo.getStartTime().getNanos() / NANOS_PER_MILLIS));
// Path: app/src/main/java/com/google/audio/asr/TimeUtil.java
// public final class TimeUtil {
//   public static Instant toInstant(Timestamp t) {
//     return new Instant(Timestamps.toMillis(t));
//   }
// 
//   public static Timestamp toTimestamp(Instant t) {
//     return Timestamps.fromMillis(t.getMillis());
//   }
// 
//   public static Duration convert(com.google.protobuf.Duration d) {
//     return Duration.millis(Durations.toMillis(d));
//   }
// 
//   public static com.google.protobuf.Duration convert(Duration d) {
//     return Durations.fromMillis(d.getMillis());
//   }
//   private TimeUtil() {}
// }


// Path: app/src/main/java/com/google/audio/asr/cloud/TimestampCalculator.java
import com.google.audio.asr.TimeUtil;
import com.google.audio.asr.TranscriptionResult;
import com.google.cloud.speech.v1p1beta1.WordInfo;
import com.google.common.base.Splitter;
import com.google.protobuf.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.joda.time.Duration;
import org.joda.time.Instant;

/*
 * Copyright 2019 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.audio.asr.cloud;


/**
 * Calculates unfinalized and finalized timestamps and adds them to the word-level details of
 * utterances. Finalized timestamps are simply copied from the corresponding timestamps returned by
 * the cloud. Unfinalized timestamps do not exist in the response returned by the cloud, so they are
 * computed from the returned transcript instead.
 */
public class TimestampCalculator {

  /**
   * Keeps track of the session start time because the finalized word times are relative to the time
   * of the beginning of the session.
   */
  private final Instant sessionStartTime;

  /**
   * Stores the time instants of each word in the un-finalized utterance. As the utterance is
   * updated with more words, this array marks the time of the new words.
   */
  private final ArrayList<Instant> unfinalizedWordInstants = new ArrayList<>();

  private static final int NANOS_PER_MILLIS = 1_000_000;

  public TimestampCalculator(Instant newSessionStartTime) {
    this.sessionStartTime = newSessionStartTime;
  }

  public Timestamp getFinalizedStartTimestamp(WordInfo wordInfo) {
    Duration startOffset =
        Duration.standardSeconds(wordInfo.getStartTime().getSeconds())
            .plus(Duration.millis(wordInfo.getStartTime().getNanos() / NANOS_PER_MILLIS));
    return TimeUtil.toTimestamp(sessionStartTime.plus(startOffset));