Class WavFile
Project: PHPWavUtils: Classes for creating, reading, and manipulating WAV files in PHP
File: WavFile.php
Copyright (c) 2014, Drew Phillips All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any modifications to the library should be indicated clearly in the source code
to inform users that the changes are not a part of the original software.
Copyright: 2014 Drew Phillips
License: License Changelog: 1.1.1 (09/08/2015) - Fix degrade() method to call filter correctly (Rasmus Lerdorf) 1.1 (02/8/2014) - Add method setIgnoreChunkSizes() to allow reading of wav data with bogus chunk sizes set. This allows streamed wav data to be processed where the chunk sizes were not known when writing the header. Instead calculates the chunk sizes automatically. - Add simple volume filter to attenuate or amplify the audio signal. 1.0 (10/2/2012) - Fix insertSilence() creating invalid block size 1.0 RC1 (4/20/2012) - Initial release candidate - Supports 8, 16, 24, 32 bit PCM, 32-bit IEEE FLOAT, Extensible Format - Support for 18 channels of audio - Ability to read an offset from a file to reduce memory footprint with large files - Single-pass audio filter processing - Highly accurate and efficient mix and normalization filters (http://www.voegler.eu/pub/audio/) - Utility filters for degrading audio, and inserting silence 0.6 (4/12/2012) - Support 8, 16, 24, 32 bit and PCM float (Paul Voegler) - Add normalize filter, misc improvements and fixes (Paul Voegler) - Normalize parameters to filter() to use filter constants as array indices - Add option to mix filter to loop the target file if the source is longer 0.5 (4/3/2012) - Fix binary pack routine (Paul Voegler) - Add improved mixing function (Paul Voegler)
Author: Drew Phillips drew@drew-phillips.com
Author: Paul Voegler http://www.voegler.eu/
Version: 1.1.1 (Sep 2015)
Located at WavFile.php
Methods summary
public
|
#
__construct( string|integer $numChannelsOrFileName = null, integer|boolean $sampleRateOrReadData = null, integer $bitsPerSample = null )
WavFile Constructor. |
public
|
|
public
|
|
public
string
|
|
public static
integer|float|null
|
#
unpackSample( string $sampleBinary, integer $bitDepth = null )
Unpacks a single binary sample to numeric value. |
public static
string|null
|
|
public static
array
|
#
unpackSampleBlock( string $sampleBlock, integer $bitDepth, integer $numChannels = null )
Unpacks a binary sample block to numeric values. |
public static
string
|
#
packSampleBlock( array $samples, integer $bitDepth )
Packs an array of numeric channel samples to a binary sample block. |
public static
float
|
#
normalizeSample( float $sampleFloat, float $threshold )
Normalizes a float audio sample. Maximum input range assumed for compression is [-2, 2]. See http://www.voegler.eu/pub/audio/ for more information. |
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
protected
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
string
|
#
makeHeader( )
Construct a wav header from this object. Includes "fact" chunk if necessary. http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/wave.html |
public
string
|
|
public
|
|
public
|
|
public
|
|
public
|
#
setWavData( string & $data, boolean $free = true )
Set the wav file data and properties from a wav file in a string. |
protected
|
|
protected
|
#
readWavHeader( )
Parse a wav header. http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/wave.html |
public
|
#
readWavData( integer $dataOffset = 0, integer $dataSize = null )
Read the wav data from the file into the buffer. |
public
string|null
|
|
public
|
#
setSampleBlock( string $sampleBlock, integer $blockNum )
Set a single sample block. |
public
float|null
|
#
getSampleValue( integer $blockNum, integer $channelNum )
Get a float sample value for a specific sample block and channel number. |
public
|
#
setSampleValue( float $sampleFloat, integer $blockNum, integer $channelNum )
Sets a float sample value for a specific sample block number and channel. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
#
generateNoise( float $duration = 1.0, float $percent = 100 )
Generate noise at the end of the wav for the specified duration and volume. |
public
|
|
public
|
Constants summary
integer |
FILTER_MIX
|
#
0x01
|
integer |
FILTER_NORMALIZE
|
#
0x02
|
integer |
FILTER_DEGRADE
|
#
0x04
|
integer |
FILTER_VOLUME
|
#
0x08
|
integer |
MAX_CHANNEL
|
#
18
|
integer |
MAX_SAMPLERATE
|
#
192000
|
integer |
SPEAKER_DEFAULT
Channel Locations for ChannelMask |
#
0x000000
|
integer |
SPEAKER_FRONT_LEFT
|
#
0x000001
|
integer |
SPEAKER_FRONT_RIGHT
|
#
0x000002
|
integer |
SPEAKER_FRONT_CENTER
|
#
0x000004
|
integer |
SPEAKER_LOW_FREQUENCY
|
#
0x000008
|
integer |
SPEAKER_BACK_LEFT
|
#
0x000010
|
integer |
SPEAKER_BACK_RIGHT
|
#
0x000020
|
integer |
SPEAKER_FRONT_LEFT_OF_CENTER
|
#
0x000040
|
integer |
SPEAKER_FRONT_RIGHT_OF_CENTER
|
#
0x000080
|
integer |
SPEAKER_BACK_CENTER
|
#
0x000100
|
integer |
SPEAKER_SIDE_LEFT
|
#
0x000200
|
integer |
SPEAKER_SIDE_RIGHT
|
#
0x000400
|
integer |
SPEAKER_TOP_CENTER
|
#
0x000800
|
integer |
SPEAKER_TOP_FRONT_LEFT
|
#
0x001000
|
integer |
SPEAKER_TOP_FRONT_CENTER
|
#
0x002000
|
integer |
SPEAKER_TOP_FRONT_RIGHT
|
#
0x004000
|
integer |
SPEAKER_TOP_BACK_LEFT
|
#
0x008000
|
integer |
SPEAKER_TOP_BACK_CENTER
|
#
0x010000
|
integer |
SPEAKER_TOP_BACK_RIGHT
|
#
0x020000
|
integer |
SPEAKER_ALL
|
#
0x03FFFF
|
integer |
WAVE_FORMAT_PCM
|
#
0x0001
|
integer |
WAVE_FORMAT_IEEE_FLOAT
|
#
0x0003
|
integer |
WAVE_FORMAT_EXTENSIBLE
|
#
0xFFFE
|
string |
WAVE_SUBFORMAT_PCM
|
#
"0100000000001000800000aa00389b71"
|
string |
WAVE_SUBFORMAT_IEEE_FLOAT
|
#
"0300000000001000800000aa00389b71"
|
Properties summary
protected static
array
|
$LOOKUP_LOGBASE
Log base modifier lookup table for a given threshold (in 0.05 steps) used by normalizeSample. Adjusts the slope (1st derivative) of the log function at the threshold to 1 for a smooth transition from linear to logarithmic amplitude output. |
#
array(
2.513, 2.667, 2.841, 3.038, 3.262,
3.520, 3.819, 4.171, 4.589, 5.093,
5.711, 6.487, 7.483, 8.806, 10.634,
13.302, 17.510, 24.970, 41.155, 96.088
)
|
protected
integer
|
$_actualSize
The actual physical file size |
|
protected
integer
|
$_chunkSize
The size of the file in RIFF header |
|
protected
integer
|
$_fmtChunkSize
The size of the "fmt " chunk |
|
protected
integer
|
$_fmtExtendedSize
The size of the extended "fmt " data |
|
protected
integer
|
$_factChunkSize
The size of the "fact" chunk |
|
protected
integer
|
$_dataSize
Size of the data chunk |
|
protected
integer
|
$_dataSize_fp
Size of the data chunk in the opened wav file |
|
protected
boolean
|
$_dataSize_valid
Does _dataSize really reflect strlen($_samples)? Case when a wav file is read with readData = false |
|
protected
integer
|
$_dataOffset
Starting offset of data chunk |
|
protected
integer
|
$_audioFormat
The audio format - WavFile::WAVE_FORMAT_* |
|
protected
integer|string|null
|
$_audioSubFormat
The audio subformat - WavFile::WAVE_SUBFORMAT_* |
|
protected
integer
|
$_numChannels
Number of channels in the audio file |
|
protected
integer
|
$_channelMask
The channel mask |
|
protected
integer
|
$_sampleRate
Samples per second |
|
protected
integer
|
$_bitsPerSample
Number of bits per sample |
|
protected
integer
|
$_validBitsPerSample
Number of valid bits per sample |
|
protected
integer
|
$_blockAlign
NumChannels * BitsPerSample/8 |
|
protected
integer
|
$_numBlocks
Number of sample blocks |
|
protected
integer
|
$_byteRate
Bytes per second |
|
protected
boolean
|
$_ignoreChunkSizes
Ignore chunk sizes when reading wav data (useful when reading data from a stream where chunk sizes contain dummy values) |
|
protected
string
|
$_samples
Binary string of samples |
|
protected
resource|null
|
$_fp
The file pointer used for reading wavs from file or memory |