Package net.mtrop.doom.sound
Class DMXSound
java.lang.Object
net.mtrop.doom.sound.DMXSound
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.mtrop.doom.object.BinaryObject
BinaryObject.InlineScanner<BO extends BinaryObject>, BinaryObject.Reflect, BinaryObject.Scanner<BO extends BinaryObject>, BinaryObject.Shared, BinaryObject.Transformer<BO extends BinaryObject>
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
11 kHz Sampling rate.static final int
22 kHz Sampling rate.static final int
44 kHz Sampling rate.static final int
8 kHz Sampling rate. -
Constructor Summary
ConstructorDescriptionDMXSound()
Creates a new, blank DMXSound.DMXSound
(int sampleRate) Creates a new DMXSound using a particular sampling rate.DMXSound
(int sampleRate, double[] samples) Creates a new DMXSound using a set of discrete samples at a particular sampling rate.DMXSound
(int sampleRate, int capacity) Creates a new DMXSound using a set of discrete samples at a particular sampling rate. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addSample
(double sample) Adds a single sample, clamped between -1.0 and 1.0.void
addSamples
(double[] samples) Adds a set of samples, clamped between -1.0 and 1.0.void
addSamples
(double[] sample, int offset, int length) Adds a set of samples, clamped between -1.0 and 1.0.void
deleteSamples
(int index, int count) Deletes a chunk of samples.double
getSample
(int index) Gets a single sample from a specific sample index.int
int
int
getSampleUnsignedByte
(int index) Gets a single sample from a specific sample index as an unsigned byte.double
getWaveFormSample
(DMXSound.InterpolationType type, double periodScalar) Gets a sampled value from along the full waveform.void
readBytes
(InputStream in) Reads from anInputStream
and sets this object's fields.resample
(DMXSound.InterpolationType type, int newSamplingRate) Gets a full copy of the contained audio resampled at a new sampling rate using an interpolation typevoid
setSample
(int index, double sample) Sets a single sample, clamped between -1.0 and 1.0.void
setSampleRate
(int sampleRate) Sets the sampling rate of this sound clip in samples per second.void
setSamples
(int index, double[] samples) Sets a set of samples from a sample index, clamped between -1.0 and 1.0.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.void
setSampleUnsignedByte
(int index, int sample) Sets a single sample, clamped between 0 and 255 (-1.0 to 1.0).toString()
void
writeBytes
(OutputStream out) Writes this object to anOutputStream
.
-
Field Details
-
SAMPLERATE_8KHZ
public static final int SAMPLERATE_8KHZ8 kHz Sampling rate.- See Also:
-
SAMPLERATE_11KHZ
public static final int SAMPLERATE_11KHZ11 kHz Sampling rate.- See Also:
-
SAMPLERATE_22KHZ
public static final int SAMPLERATE_22KHZ22 kHz Sampling rate.- See Also:
-
SAMPLERATE_44KHZ
public static final int SAMPLERATE_44KHZ44 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
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
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
Description copied from interface:BinaryObject
Reads from anInputStream
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 interfaceBinaryObject
- Parameters:
in
- theInputStream
to read from.- Throws:
IOException
- if a read error occurs.
-
writeBytes
Description copied from interface:BinaryObject
Writes this object to anOutputStream
.- Specified by:
writeBytes
in interfaceBinaryObject
- Parameters:
out
- theOutputStream
to write to.- Throws:
IOException
- if a write error occurs.
-
toString
-