/**
   * Sets the InputStream from which this StreamDataSource reads.
   *
   * @param inputStream the InputStream from which audio data comes
   * @param streamName the name of the InputStream
   */
  public void setInputStream(AudioInputStream inputStream, String streamName) {
    dataStream = inputStream;
    streamEndReached = false;
    utteranceEndSent = false;
    utteranceStarted = false;

    AudioFormat format = inputStream.getFormat();
    sampleRate = (int) format.getSampleRate();
    bigEndian = format.isBigEndian();

    String s = format.toString();
    logger.finer("input format is " + s);

    if (format.getSampleSizeInBits() % 8 != 0)
      throw new Error("StreamDataSource: bits per sample must be a multiple of 8.");
    bytesPerValue = format.getSampleSizeInBits() / 8;

    // test whether all files in the stream have the same format

    AudioFormat.Encoding encoding = format.getEncoding();
    if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED)) signedData = true;
    else if (encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED)) signedData = false;
    else throw new RuntimeException("used file encoding is not supported");

    totalValuesRead = 0;
  }
  WaveFileFormat(
      AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) {

    super(type, lengthInBytes, format, lengthInFrames);

    AudioFormat.Encoding encoding = format.getEncoding();

    if (encoding.equals(AudioFormat.Encoding.ALAW)) {
      waveType = WAVE_FORMAT_ALAW;
    } else if (encoding.equals(AudioFormat.Encoding.ULAW)) {
      waveType = WAVE_FORMAT_MULAW;
    } else if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED)
        || encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
      waveType = WAVE_FORMAT_PCM;
    } else {
      waveType = WAVE_FORMAT_UNKNOWN;
    }
  }
Beispiel #3
0
  public AudioInputStream getAudioInputStream(
      AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {
    AudioFormat sourceFormat = sourceStream.getFormat();
    AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();

    if (sourceEncoding.equals(targetEncoding)) {
      return sourceStream;
    } else {
      AudioFormat targetFormat = null;
      if (!isConversionSupported(targetEncoding, sourceStream.getFormat())) {
        throw new IllegalArgumentException(
            "Unsupported conversion: "
                + sourceStream.getFormat().toString()
                + " to "
                + targetEncoding.toString());
      }
      if (AudioFormat.Encoding.ULAW.equals(sourceEncoding)
          && AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding)) {
        targetFormat =
            new AudioFormat(
                targetEncoding,
                sourceFormat.getSampleRate(),
                16,
                sourceFormat.getChannels(),
                2 * sourceFormat.getChannels(),
                sourceFormat.getSampleRate(),
                sourceFormat.isBigEndian());
      } else if (AudioFormat.Encoding.PCM_SIGNED.equals(sourceEncoding)
          && AudioFormat.Encoding.ULAW.equals(targetEncoding)) {
        targetFormat =
            new AudioFormat(
                targetEncoding,
                sourceFormat.getSampleRate(),
                8,
                sourceFormat.getChannels(),
                sourceFormat.getChannels(),
                sourceFormat.getSampleRate(),
                false);
      } else {
        throw new IllegalArgumentException(
            "Unsupported conversion: "
                + sourceStream.getFormat().toString()
                + " to "
                + targetEncoding.toString());
      }

      return getAudioInputStream(targetFormat, sourceStream);
    }
  }
Beispiel #4
0
  /**
   * Returns the AudioFileFormat describing the file that will be written from this
   * AudioInputStream. Throws IllegalArgumentException if not supported.
   */
  private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!isFileTypeSupported(type, stream)) {
      throw new IllegalArgumentException("File type " + type + " not supported.");
    }
    AudioFormat format = null;
    WaveFileFormat fileFormat = null;
    AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;

    AudioFormat streamFormat = stream.getFormat();
    AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();

    float sampleRate;
    int sampleSizeInBits;
    int channels;
    int frameSize;
    float frameRate;
    int fileSize;

    int waveType = WaveFileFormat.WAVE_FORMAT_PCM;

    if (AudioFormat.Encoding.ALAW.equals(streamEncoding)
        || AudioFormat.Encoding.ULAW.equals(streamEncoding)) {

      encoding = streamEncoding;
      sampleSizeInBits = streamFormat.getSampleSizeInBits();
      if (streamEncoding.equals(AudioFormat.Encoding.ALAW)) {
        waveType = WaveFileFormat.WAVE_FORMAT_ALAW;
      } else {
        waveType = WaveFileFormat.WAVE_FORMAT_MULAW;
      }
    } else if (streamFormat.getSampleSizeInBits() == 8) {
      encoding = AudioFormat.Encoding.PCM_UNSIGNED;
      sampleSizeInBits = 8;
    } else {
      encoding = AudioFormat.Encoding.PCM_SIGNED;
      sampleSizeInBits = streamFormat.getSampleSizeInBits();
    }

    format =
        new AudioFormat(
            encoding,
            streamFormat.getSampleRate(),
            sampleSizeInBits,
            streamFormat.getChannels(),
            streamFormat.getFrameSize(),
            streamFormat.getFrameRate(),
            false); // WAVE is little endian

    if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
      fileSize =
          (int) stream.getFrameLength() * streamFormat.getFrameSize()
              + WaveFileFormat.getHeaderSize(waveType);
    } else {
      fileSize = AudioSystem.NOT_SPECIFIED;
    }

    fileFormat =
        new WaveFileFormat(
            AudioFileFormat.Type.WAVE, fileSize, format, (int) stream.getFrameLength());

    return fileFormat;
  }