Class DMXSound

java.lang.Object
net.mtrop.doom.sound.DMXSound
All Implemented Interfaces:
BinaryObject

public class DMXSound extends Object implements BinaryObject
This class holds digital sound information. The format that this reads is the DMX PCM Format, by Digital Expressions, Inc., written by Paul Radek. Doom uses this format for storing sound data.

NOTE: Even though this stores samples as doubles, DMX Sounds are serialized as mono, 8-bit PCM waveforms. Some sound information will be lost on conversion!

Author:
Matthew Tropiano
  • Field Details

    • SAMPLERATE_8KHZ

      public static final int SAMPLERATE_8KHZ
      8 kHz Sampling rate.
      See Also:
    • SAMPLERATE_11KHZ

      public static final int SAMPLERATE_11KHZ
      11 kHz Sampling rate.
      See Also:
    • SAMPLERATE_22KHZ

      public static final int SAMPLERATE_22KHZ
      22 kHz Sampling rate.
      See Also:
    • SAMPLERATE_44KHZ

      public static final int SAMPLERATE_44KHZ
      44 kHz Sampling rate.
      See Also:
  • Constructor Details

    • DMXSound

      public DMXSound()
      Creates a new, blank DMXSound.
    • DMXSound

      public DMXSound(int sampleRate)
      Creates a new DMXSound using a particular sampling rate. Capacity is initialized to one second worth of sound.
      Parameters:
      sampleRate - the sampling rate of this sound in samples per second.
    • DMXSound

      public DMXSound(int sampleRate, int capacity)
      Creates a new DMXSound using a set of discrete samples at a particular sampling rate.
      Parameters:
      sampleRate - the sampling rate of this sound in samples per second.
      capacity - the capacity of the sample vector in this DMXSound.
    • DMXSound

      public DMXSound(int sampleRate, double[] samples)
      Creates a new DMXSound using a set of discrete samples at a particular sampling rate. The source array is copied.
      Parameters:
      sampleRate - the sampling rate of this sound in samples per second.
      samples - the discrete samples.
  • Method Details

    • getSampleRate

      public int getSampleRate()
      Returns:
      the sampling rate of this sound clip in samples per second.
    • setSampleRate

      public void setSampleRate(int sampleRate)
      Sets the sampling rate of this sound clip in samples per second. This does NOT change the underlying waveform!
      Parameters:
      sampleRate - the new sampling rate.
      Throws:
      IllegalArgumentException - if the sample rate is outside the range of 0 to 65535.
    • setSample

      public void setSample(int index, double sample)
      Sets a single sample, clamped between -1.0 and 1.0.
      Parameters:
      index - the sample index to set.
      sample - the added sample.
    • setSampleUnsignedByte

      public void setSampleUnsignedByte(int index, int sample)
      Sets a single sample, clamped between 0 and 255 (-1.0 to 1.0).
      Parameters:
      index - the sample index to set.
      sample - the added sample.
    • setSamples

      public void setSamples(int index, double[] samples)
      Sets a set of samples from a sample index, clamped between -1.0 and 1.0.
      Parameters:
      index - the starting sample index to set.
      samples - the samples to add.
    • setSamples

      public void setSamples(int index, double[] samples, int offset, int length)
      Sets a set of samples from a sample index, clamped between -1.0 and 1.0.
      Parameters:
      index - the starting sample index to set.
      samples - the samples to add.
      offset - the offset into the array.
      length - the amount of samples to copy.
    • addSample

      public void addSample(double sample)
      Adds a single sample, clamped between -1.0 and 1.0.
      Parameters:
      sample - the added sample.
    • addSamples

      public void addSamples(double[] samples)
      Adds a set of samples, clamped between -1.0 and 1.0.
      Parameters:
      samples - the array of samples.
    • addSamples

      public void addSamples(double[] sample, int offset, int length)
      Adds a set of samples, clamped between -1.0 and 1.0.
      Parameters:
      sample - the array of samples.
      offset - the offset into the array.
      length - the amount of samples to copy.
    • deleteSamples

      public void deleteSamples(int index, int count)
      Deletes a chunk of samples.
      Parameters:
      index - the starting sample index.
      count - the amount of samples to cut.
    • getSampleCount

      public int getSampleCount()
      Returns:
      the amount of samples in this sound.
    • getSample

      public double getSample(int index)
      Gets a single sample from a specific sample index.
      Parameters:
      index - the index of the sample.
      Returns:
      the corresponding sample value.
    • getSampleUnsignedByte

      public int getSampleUnsignedByte(int index)
      Gets a single sample from a specific sample index as an unsigned byte.
      Parameters:
      index - the index of the sample.
      Returns:
      the corresponding sample value.
    • getWaveFormSample

      public double getWaveFormSample(DMXSound.InterpolationType type, double periodScalar)
      Gets a sampled value from along the full waveform.
      Parameters:
      type - the interpolation type.
      periodScalar - the offset along this wave's full period (0.0 is the beginning, 1.0 is the end).
      Returns:
      a sampled value.
    • resample

      public DMXSound resample(DMXSound.InterpolationType type, int newSamplingRate)
      Gets a full copy of the contained audio resampled at a new sampling rate using an interpolation type
      Parameters:
      type - the interpolation type for the resample.
      newSamplingRate - the new sampling rate.
      Returns:
      a new DMXSound that is the result of the resample.
    • readBytes

      public void readBytes(InputStream in) throws IOException
      Description copied from interface: BinaryObject
      Reads from an InputStream and sets this object's fields. Only reads the amount of bytes that it takes to read a single instance of the object. Note that not every object may have a consistent length!
      Specified by:
      readBytes in interface BinaryObject
      Parameters:
      in - the InputStream to read from.
      Throws:
      IOException - if a read error occurs.
    • writeBytes

      public void writeBytes(OutputStream out) throws IOException
      Description copied from interface: BinaryObject
      Writes this object to an OutputStream.
      Specified by:
      writeBytes in interface BinaryObject
      Parameters:
      out - the OutputStream to write to.
      Throws:
      IOException - if a write error occurs.
    • toString

      public String toString()
      Overrides:
      toString in class Object