cz.cuni.jagrlib.piece
Class WAVFileFormatMapped

java.lang.Object
  extended by cz.cuni.jagrlib.DefaultProperty
      extended by cz.cuni.jagrlib.Piece
          extended by cz.cuni.jagrlib.DefaultFileFormat
              extended by cz.cuni.jagrlib.piece.WAVFileFormatMapped
All Implemented Interfaces:
Breakable, DataFileFormat, Property, SampleData, Template

public class WAVFileFormatMapped
extends DefaultFileFormat
implements SampleData

WAV sound file filter. Keeps data in disk file, accessed via memory-mapped mode in BitStream.

Since:
0.25
See Also:
WAVFileFormatMapped.java

Field Summary
protected  int bitsPerSample
          Actual sample accuracy in bits.
protected  int blockAlign
          Number of bytes in one time-slot.
protected  int bytesPerSample
          Physical sample size in bytes.
protected static java.lang.String CATEGORY
          Object category.
protected  long dataOffset
          Data offset within the binary file (in bytes).
protected  double duration
          Sound duration in seconds.
static java.lang.String DURATION
           
protected  long frequency
          Sampling frequency in Hz.
protected  boolean headerChanged
          Was the binary header changed?
protected  int channels
          Actual number of channels.
static java.lang.String CHANNELS
           
protected  boolean propChanged
          File properties were changed (for commit()).
static RegPiece reg
          Static registration instance for this class.
protected  long samples
          Actual number of samples (time axis).
protected  int sampleType
          Actual sample type.
protected  BitStream stream
          Opened bit-stream or null.
protected static long TAG_DATA
           
protected static long TAG_FMT
           
protected static long TAG_RIFF
           
protected static long TAG_WAVE
           
protected static java.lang.String TEMPLATE_NAME
          Object template identifier.
protected  boolean writable
          Is the WAV file writable?
 
Fields inherited from class cz.cuni.jagrlib.Piece
info, pl, plugs, TEMPLATE_TYPE, userBreak
 
Fields inherited from interface cz.cuni.jagrlib.iface.SampleData
FREQUENCY, SAMPLE_BYTE, SAMPLE_COMPLEX, SAMPLE_DOUBLE, SAMPLE_FLOAT, SAMPLE_INT, SAMPLE_LONG, SAMPLE_RGBA, SAMPLE_SHORT, SAMPLE_TYPE
 
Fields inherited from interface cz.cuni.jagrlib.iface.Property
LOGGING, STATISTICS, TEXT_DESCRIPTION
 
Fields inherited from interface cz.cuni.jagrlib.iface.DataFileFormat
DOUBLE_STREAM
 
Fields inherited from interface cz.cuni.jagrlib.Template
ALL_PLUGS, C_1D, C_2D, C_3D, C_ALPHA, C_BINARY, C_BREP, C_CAMERA, C_CIRCLE, C_CLIP, C_COMPRESSION, C_CURVE, C_DATA, C_DRAW, C_EDITOR, C_ELLIPSE, C_FILL, C_FILTER, C_FLOAT, C_FUNCTION, C_IMAGE, C_INTEGER, C_IO, C_LIGHT, C_LINE, C_METRIC, C_POLYGON, C_PROJECTION, C_RADIOSITY, C_RASTER, C_RENDER, C_SAMPLE, C_SCENE, C_SET, C_SOLID, C_TEXT, C_TRANSFORM, C_VECTOR, C_VIEWER, C_WORKER, CAT_EMPTY, EMPTY, IFACE, JAGRLIB, JAGRLIB2, MANIPULATOR_COMBO, MANIPULATOR_CUSTOM, MANIPULATOR_DEFAULT, MANIPULATOR_MULTILINE, PL_ALPHAMASK, PL_BITMASK, PL_CAUSTIC, PL_CODEC, PL_COLORMAP, PL_COMPARE, PL_DATA, PL_DIRECT, PL_EDITOR, PL_FILTER, PL_FUNCTION, PL_IMAGE, PL_IMPORT, PL_INPUT, PL_INTERSECTION, PL_LIGHTSOURCE, PL_ORDER, PL_OUTPUT, PL_PALETTE, PL_PHOTON, PL_PROPERTY, PL_QUANTIZER, PL_RASTER, PL_RENDER, PL_SHADOW, PL_STREAM, PL_TRANSFORM, PL_TRIGGER, PL_VOLUME, PL_WINDOW, TYPE_BOOLEAN, TYPE_DOUBLE, TYPE_FLOAT, TYPE_INTEGER, TYPE_LONG, TYPE_OBJECT, TYPE_STRING
 
Constructor Summary
WAVFileFormatMapped()
           
 
Method Summary
 void closeFile()
          Closes the file (used in file-mapping mode).
 void commit()
          Commits all previous changes (set(java.lang.String, java.lang.Object)) to the internal object state.
 java.lang.String[] fileNameMasks()
          Returns file-name masks associated with the file-format type.
 java.lang.Object get(java.lang.String key)
          Gets the given property.
 java.lang.Object getDataArray(int[] dim)
          Returns the original data array.
 int getDataDim(int[] dim)
          Returns actual array dimensions.
 void getSampleGeometry(double[] geom)
          Gets the sample geometry (in regular sample matrix).
 java.lang.Object getSampleMapping(java.lang.Object map, int srcFrom, int srcTo, int[] bounds)
          Retrieves the whole sample mapping (for mapped arrays only).
 int getSamples(int[] from, int[] to, double[] data)
          Reads the given sample segment (float formats: float, double).
 int getSamples(int[] from, int[] to, int[] data)
          Reads the given sample segment (integer formats: byte, short, int).
 int getSamples(int[] from, int[] to, long[] data)
          Reads the given sample segment (long format).
 int getSamplesGeneral(int[] from, int[] to, java.lang.Object data)
          Reads the given sample segment (general data format).
 int getSampleType()
          Returns actual sample type (SAMPLE_* constant).
 int headerLength()
          Length of file-format header (number of bytes which is necessary for file-format matching).
protected  int checkComponentClass(java.lang.Object array, boolean allowDouble)
          Checks array's component type.
 boolean loadFile(BitStream stream, java.lang.Object support)
          Retrieves data from the given BitStream.
 boolean loadFile(java.lang.String fileName, java.lang.String detail)
          Retrieves data from the given file (stream).
 double match(byte[] header, java.lang.String fileName)
          File-format match test.
protected  boolean readHeader()
          Reads header of existing WAV file.
 void resetData(int type, boolean mapped, int[] dim)
          (Re-)initialize the data array.
 boolean saveFile(BitStream stream, java.lang.Object support)
          Saves data to the given BitStream.
 boolean saveFile(java.lang.String fileName, java.lang.String detail)
          Saves data to the given file (stream).
 void set(java.lang.String key, java.lang.Object value)
          Sets the given property.
 void setDataArray(java.lang.Object data, int srcFrom, int[] dstFrom, int[] dstTo)
          Sets data array in the raw (internal) format.
 void setSampleGeometry(double[] geom)
          Sets the sample geometry (in regular sample matrix).
 void setSampleMapping(java.lang.Object map, int srcFrom, int srcTo, int dstFrom)
          Sets (partially) the new sample mapping (for mapped arrays only).
 void setSamples(int[] from, int[] to, double[] data)
          Writes the given sample segment (float formats: float, double).
 void setSamples(int[] from, int[] to, int[] data)
          Writes the given sample segment (integer formats: byte, short, int).
 void setSamples(int[] from, int[] to, long[] data)
          Writes the given sample segment (long format).
 void setSamplesGeneral(int[] from, int[] to, java.lang.Object data)
          Writes the given sample segment (general data format).
static int setTemplate(Template t, int ord)
          General-purpose registration routine.
protected  boolean writeHeader()
          Synchronize disk file header with the actual object state.
 
Methods inherited from class cz.cuni.jagrlib.DefaultFileFormat
init, readFloat, readLine, readParameters, readToken, readUnsigned16, readUnsigned16, readUnsigned32, writeFloat, writeLine, writeParameters, writeUnsigned16, writeUnsigned16, writeUnsigned32
 
Methods inherited from class cz.cuni.jagrlib.Piece
className, connect, findPlug, findPlug, getInfo, getInterface, getInterface, getPlug, getReg, isCompatible, isCompatible, isConnected, logError, logWarning, myInterface, newInputPlug, newOptOutputPlug, newOutputPlug, newPlug, noTemplates, propBegin, propBounds, propDefault, propEnd, propEnum, propManipulator, propManipulator, setProgressListener, setRegStrings, setTemplateDynamic, stop
 
Methods inherited from class cz.cuni.jagrlib.DefaultProperty
booleanProperty, booleanProperty, colorDoubleProperty, colorProperty, doubleProperty, doubleProperty, doubleProperty, enumProperty, floatProperty, floatProperty, floatProperty, intProperty, intProperty, intProperty, longProperty, longProperty, longProperty, set, stringProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface cz.cuni.jagrlib.Breakable
setProgressListener, stop
 

Field Detail

stream

protected BitStream stream
Opened bit-stream or null.


writable

protected boolean writable
Is the WAV file writable?


samples

protected long samples
Actual number of samples (time axis).


channels

protected int channels
Actual number of channels.


blockAlign

protected int blockAlign
Number of bytes in one time-slot.


bitsPerSample

protected int bitsPerSample
Actual sample accuracy in bits.


bytesPerSample

protected int bytesPerSample
Physical sample size in bytes.


frequency

protected long frequency
Sampling frequency in Hz.


dataOffset

protected long dataOffset
Data offset within the binary file (in bytes).


headerChanged

protected boolean headerChanged
Was the binary header changed?


sampleType

protected int sampleType
Actual sample type.


duration

protected double duration
Sound duration in seconds.


propChanged

protected boolean propChanged
File properties were changed (for commit()).


TAG_RIFF

protected static final long TAG_RIFF
See Also:
Constant Field Values

TAG_WAVE

protected static final long TAG_WAVE
See Also:
Constant Field Values

TAG_FMT

protected static final long TAG_FMT
See Also:
Constant Field Values

TAG_DATA

protected static final long TAG_DATA
See Also:
Constant Field Values

CHANNELS

public static final java.lang.String CHANNELS
See Also:
Constant Field Values

DURATION

public static final java.lang.String DURATION
See Also:
Constant Field Values

TEMPLATE_NAME

protected static final java.lang.String TEMPLATE_NAME
Object template identifier.

See Also:
Constant Field Values

CATEGORY

protected static final java.lang.String CATEGORY
Object category.

See Also:
Constant Field Values

reg

public static final RegPiece reg
Static registration instance for this class. Automatically initialized in class-loading time.

Constructor Detail

WAVFileFormatMapped

public WAVFileFormatMapped()
Method Detail

readHeader

protected boolean readHeader()
Reads header of existing WAV file.

Returns:
true if the header was OK.

writeHeader

protected boolean writeHeader()
Synchronize disk file header with the actual object state.

Returns:
true if the operation was successfull.

checkComponentClass

protected int checkComponentClass(java.lang.Object array,
                                  boolean allowDouble)
Checks array's component type.

Returns:
Array size or -1 if array type is not compatible.

headerLength

public int headerLength()
Length of file-format header (number of bytes which is necessary for file-format matching).

Specified by:
headerLength in interface DataFileFormat
Specified by:
headerLength in class DefaultFileFormat
Returns:
Number of bytes from beginning of a file which are enough for file-format match test.
See Also:
match(byte[], java.lang.String)

match

public double match(byte[] header,
                    java.lang.String fileName)
File-format match test. Guesses whether the binary file-header (and file-name) can belong to this file-format type.

Specified by:
match in interface DataFileFormat
Specified by:
match in class DefaultFileFormat
Parameters:
header - Bytes from beginning of the file.
fileName - Optional file-name string.
Returns:
Probability of being my file-format type (0.0 .. no way, 0.5 .. maybe, 0.9 .. almost sure, 1.0 .. absolutely - no need to check another formats).
See Also:
headerLength(), fileNameMasks()

fileNameMasks

public java.lang.String[] fileNameMasks()
Returns file-name masks associated with the file-format type. Needs not be implemented in systems where "file-name -> file-type" mapping is irrelevant.

Specified by:
fileNameMasks in interface DataFileFormat
Specified by:
fileNameMasks in class DefaultFileFormat
Returns:
Array of wild-card file-name masks (e.g. ["*.png"]) or null.
See Also:
match(byte[], java.lang.String)

loadFile

public boolean loadFile(java.lang.String fileName,
                        java.lang.String detail)
                 throws java.io.IOException
Retrieves data from the given file (stream). Can be used for "file opening" in virtual-mapped mode.

Specified by:
loadFile in interface DataFileFormat
Specified by:
loadFile in class DefaultFileFormat
Parameters:
fileName - File-name (stream-name) to read from (can be null).
detail - Further implementation-specific information ("r" or "rw" for mapped mode).
Returns:
true if the operation was successful.
Throws:
java.io.IOException
See Also:
loadFile(BitStream,Object), saveFile(String,String), closeFile()

loadFile

public boolean loadFile(BitStream stream,
                        java.lang.Object support)
                 throws java.io.IOException
Retrieves data from the given BitStream. This metod is optional.

The stream should be opened before - only the BitStream.reOpen(boolean, java.lang.String) call can be used.

Specified by:
loadFile in interface DataFileFormat
Specified by:
loadFile in class DefaultFileFormat
Parameters:
stream - Input bit-stream (opened - call BitStream.reOpen(boolean, java.lang.String) first).
support - Implementation-specific data (e.g. second BitStream for file-formats with DataFileFormat.DOUBLE_STREAM property).
Returns:
true if the operation was successful.
Throws:
java.io.IOException
See Also:
loadFile(String,String), DataFileFormat.DOUBLE_STREAM

saveFile

public boolean saveFile(java.lang.String fileName,
                        java.lang.String detail)
Saves data to the given file (stream).

Specified by:
saveFile in interface DataFileFormat
Specified by:
saveFile in class DefaultFileFormat
Parameters:
fileName - File-name (stream-name) to write to (can be null).
detail - Further implementation-specific information.
Returns:
true if the operation was successful.
See Also:
saveFile(BitStream,Object), loadFile(String,String)

saveFile

public boolean saveFile(BitStream stream,
                        java.lang.Object support)
                 throws java.io.IOException
Saves data to the given BitStream. This metod is optional.

The stream should be opened and empty - the reOpen(true,null) call is not required.

Specified by:
saveFile in interface DataFileFormat
Specified by:
saveFile in class DefaultFileFormat
Parameters:
stream - Output bit-stream (opened and empty).
support - Implementation-specific data (e.g. second BitStream for file-formats with DataFileFormat.DOUBLE_STREAM property).
Returns:
true if the operation was successful.
Throws:
java.io.IOException
See Also:
saveFile(String,String), DataFileFormat.DOUBLE_STREAM

closeFile

public void closeFile()
               throws java.io.IOException
Closes the file (used in file-mapping mode).

Specified by:
closeFile in interface DataFileFormat
Overrides:
closeFile in class DefaultFileFormat
Throws:
java.io.IOException
See Also:
loadFile(String,String)

resetData

public void resetData(int type,
                      boolean mapped,
                      int[] dim)
(Re-)initialize the data array.

Specified by:
resetData in interface SampleData
Parameters:
type - Sample type (one of SAMPLE_* constants).
mapped - Should the array contain mapped values?
dim - Dimensions of an array.
See Also:
SampleData.SAMPLE_BYTE, SampleData.SAMPLE_SHORT, SampleData.SAMPLE_INT, SampleData.SAMPLE_LONG, SampleData.SAMPLE_FLOAT, SampleData.SAMPLE_DOUBLE, SampleData.SAMPLE_COMPLEX, SampleData.SAMPLE_RGBA

getSampleType

public int getSampleType()
Returns actual sample type (SAMPLE_* constant).

Specified by:
getSampleType in interface SampleData
Returns:
Sample type (SAMPLE_* constant).
See Also:
getDataDim(int[]), SampleData.SAMPLE_BYTE, SampleData.SAMPLE_SHORT, SampleData.SAMPLE_INT, SampleData.SAMPLE_LONG, SampleData.SAMPLE_FLOAT, SampleData.SAMPLE_DOUBLE, SampleData.SAMPLE_COMPLEX, SampleData.SAMPLE_RGBA

getDataDim

public int getDataDim(int[] dim)
Returns actual array dimensions.

Specified by:
getDataDim in interface SampleData
Parameters:
dim - Pre-allocated array to hold individual dimensions' sizes.
Returns:
Dimensionality (number of dimensions).
See Also:
getSampleType()

setSampleGeometry

public void setSampleGeometry(double[] geom)
Sets the sample geometry (in regular sample matrix).

Specified by:
setSampleGeometry in interface SampleData
Parameters:
geom - Sample periods for all dimensions.
See Also:
getSampleGeometry(double[])

getSampleGeometry

public void getSampleGeometry(double[] geom)
Gets the sample geometry (in regular sample matrix).

Specified by:
getSampleGeometry in interface SampleData
Parameters:
geom - Sample periods for all dimensions.
See Also:
setSampleGeometry(double[])

setSampleMapping

public void setSampleMapping(java.lang.Object map,
                             int srcFrom,
                             int srcTo,
                             int dstFrom)
Sets (partially) the new sample mapping (for mapped arrays only).

Specified by:
setSampleMapping in interface SampleData
Parameters:
map - Mapping array to be set (should be an array of the apropriate type).
srcFrom - First source index (map[from]).
srcTo - After the last source index.
dstFrom - First destination index.
See Also:
getSampleMapping(java.lang.Object, int, int, int[])

getSampleMapping

public java.lang.Object getSampleMapping(java.lang.Object map,
                                         int srcFrom,
                                         int srcTo,
                                         int[] bounds)
Retrieves the whole sample mapping (for mapped arrays only).

Specified by:
getSampleMapping in interface SampleData
Parameters:
map - Optional pre-allocated mapping array to be filled.
srcFrom - First index to be retrieved.
srcTo - After the last index to be retrieved.
bounds - Retrieved index bounds (bounds[0] = the 1st mapped value written in map[0], bounds[1] = index after the last value).
See Also:
setSampleMapping(java.lang.Object, int, int, int)

getDataArray

public java.lang.Object getDataArray(int[] dim)
Returns the original data array.

Specified by:
getDataArray in interface SampleData
Parameters:
dim - Returned dimensionality (array of individual dimension sizes). Can be null.
Returns:
Packed 1D sample array (read / write access) or null if the service is not available.
See Also:
setDataArray(java.lang.Object, int, int[], int[])

setDataArray

public void setDataArray(java.lang.Object data,
                         int srcFrom,
                         int[] dstFrom,
                         int[] dstTo)
Sets data array in the raw (internal) format. Can be used for data fragment or for the whole data array.

Specified by:
setDataArray in interface SampleData
Parameters:
data - Packed 1D source data array (of apropriate type).
srcFrom - Source index to begin from.
dstFrom - Start of the region to be set. Can be null (instead of [0,0,0,..]).
dstTo - End of the region to be set. Can be null (instead of dim).

getSamplesGeneral

public int getSamplesGeneral(int[] from,
                             int[] to,
                             java.lang.Object data)
Reads the given sample segment (general data format).

Specified by:
getSamplesGeneral in interface SampleData
Parameters:
from - Starting index.
to - Index after retrieved data.
data - Pre-allocated array to hold the result (packed 1D array).
Returns:
Number of returned samples.

setSamplesGeneral

public void setSamplesGeneral(int[] from,
                              int[] to,
                              java.lang.Object data)
Writes the given sample segment (general data format).

Specified by:
setSamplesGeneral in interface SampleData
Parameters:
from - Starting index.
to - Index after the written data.
data - Data to be written - in 1D array.

getSamples

public int getSamples(int[] from,
                      int[] to,
                      int[] data)
Reads the given sample segment (integer formats: byte, short, int).

Specified by:
getSamples in interface SampleData
Parameters:
from - Starting index.
to - Index after retrieved data.
data - Pre-allocated array to hold the result.
Returns:
Number of returned samples.

getSamples

public int getSamples(int[] from,
                      int[] to,
                      long[] data)
Reads the given sample segment (long format).

Specified by:
getSamples in interface SampleData
Parameters:
from - Starting index.
to - Index after retrieved data.
data - Pre-allocated array to hold the result.
Returns:
Number of returned samples.

getSamples

public int getSamples(int[] from,
                      int[] to,
                      double[] data)
Reads the given sample segment (float formats: float, double).

Specified by:
getSamples in interface SampleData
Parameters:
from - Starting index.
to - Index after retrieved data.
data - Pre-allocated array to hold the result (complex uses double-sized array).
Returns:
Number of returned samples.

setSamples

public void setSamples(int[] from,
                       int[] to,
                       int[] data)
Writes the given sample segment (integer formats: byte, short, int).

Specified by:
setSamples in interface SampleData
Parameters:
from - Starting index.
to - Index after written data.
data - Data to be written (1D array).

setSamples

public void setSamples(int[] from,
                       int[] to,
                       long[] data)
Writes the given sample segment (long format).

Specified by:
setSamples in interface SampleData
Parameters:
from - Starting index.
to - Index after written data.
data - Data to be written (1D array).

setSamples

public void setSamples(int[] from,
                       int[] to,
                       double[] data)
Writes the given sample segment (float formats: float, double).

Specified by:
setSamples in interface SampleData
Parameters:
from - Starting index.
to - Index after written data.
data - Data to be written (1D array). Complex uses double-sized array.

set

public void set(java.lang.String key,
                java.lang.Object value)
Sets the given property.

Specified by:
set in interface Property
Overrides:
set in class DefaultProperty
Parameters:
key - Key string.
value - The new value.
See Also:
Property.commit()

get

public java.lang.Object get(java.lang.String key)
Gets the given property.

Specified by:
get in interface Property
Overrides:
get in class DefaultProperty
Parameters:
key - Key string.
Returns:
The actual value or null.

commit

public void commit()
Commits all previous changes (set(java.lang.String, java.lang.Object)) to the internal object state. Non-mandatory.

Specified by:
commit in interface Property
Overrides:
commit in class DefaultProperty

setTemplate

public static int setTemplate(Template t,
                              int ord)
General-purpose registration routine. Sets all plugs, strings, etc. to the given Template.