"Audero Wav Extractor" is a PHP library that allows to extract a chunk from a wav file. The extracted chunk of the wav file can be saved on the hard disk, can be forced to be prompted as download by the user's browser, returned as a string for a later processing or a combination of the first and second possibilities. It is very easy to use "Audero Wav Extractor" to extract a piece of audio from a wav file. All you have to do is give the name of the file, the start and the end time to extract (optionally you can provide a name for the extracted chunk).

The follow is an example of how to extract a chunk from a wav file and force the download using this class:

   require_once('AuderoWavExtractor/AuderoWavExtractor.php');

   $InputFile = 'audio.wav'; // path to input wav file
   $OutputFile = 'chunk.wav'; // path to the output chunk
   $Start = 0 * 1000; // Start time of the chunk
   $End = 20 * 1000; // End time of the chunk

   $Extractor = new AuderoWavExtractor($InputFile);
   $Extractor->extractChunk($Start, $End);

The follow is an example of how to extract a chunk from a wav file and save it into the local disk:

   require_once('AuderoWavExtractor.php');

   $InputFile = 'audio.wav'; // path to input wav file
   $OutputFile = 'chunk.wav'; // path to the output chunk
   $Start = 12 * 1000; // Start time of the chunk
   $End = 20 * 1000; // End time of the chunk

   $Extractor = new AuderoWavExtractor($InputFile);
   $Extractor->extractChunk($Start, $End, 2, $OutputFile);

LICENSE: "Audero Wav Extractor" (from now on "The software") is released under the CC BY-NC 3.0 ("Creative Commons Attribution NonCommercial 3.0") license. More details can be found here: http://creativecommons.org/licenses/by-nc/3.0/

WARRANTY: The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

author Aurelio De Rosa
version 1.0
license CC BY-NC 3.0
link https://bitbucket.org/AurelioDeRosa/audero-wav-extractor
package Audero\AuderoWavExtractor

 Methods

The constructor of the object. It loads all the required class and create the Wav object that represents the choosen one.

__construct(string $FilePath) 

Parameters

$FilePath

string

The filename, including the path, of the managed file wav

Extract a portion of the current, from the $Start to $End. The chunk will be saved, with name $Filename, in the local server, the browser or the user and so on based on the value of $Destination.

extractChunk(int $Start, int $End, int $Destination, string $Filename) : string

The possible value of $Destination are:

  • 1: send the file to the browser and force the download with the name given by $Filename.
  • 2: save into the local disk the file with the name given by $Filename.
  • 3: return the wav as a string. In this case $Filename is ignored.
  • 4: both 2 and 3

Parameters

$Start

int

The start time, in milliseconds, of the portion to extract

$End

int

The end time, in milliseconds, of the portion to extract

$Destination

int

Where to save the extracted chunk

$Filename

string

&$Filename The name to assing to the chunk. If no name is provided the method generate one using this rule: "InputFilename-Start-End.wav"

Exceptions

\InvalidArgumentException If the give boundary is invalid (for example if the end is lower than start) or if the destination value isn't valid (for example greater than 4).
\RuntimeException If there's not enough memory to save the given range

Returns

stringThe chunk to extract, including the headers

Get the instance of the current wav file

getWav() : \Wav

Returns

\WavThe current wav file

Try to test if the available memory is enough to extract the chunk

isEnoughMemory(int $Start, int $End) : bool

Parameters

$Start

int

The start time, in milliseconds, of the chunk that has to be extracted

$End

int

The end time, in milliseconds, of the chunk that has to be extracted

Returns

boolTrue if the memory is enough. False otherwise.

This method sets the headers to force the download and send the data of the chunk.

downloadChuck(string $Chunk, string $Filename) 

Parameters

$Chunk

string

The string containing the bytes of the chunk

$Filename

string

The filename that will be shown by the browser

Get the filename (include the path) of the current wav file.

getFilename(boolean $IncludeExtention) : string

If false is passed, the extention of the file is stripped.

Parameters

$IncludeExtention

boolean

Include or not the extention (.wav) of the file. Default is true.

Returns

stringThe filename of the managed wav file

This method sets the include paths in order to allow the library to work

init() 

Save the extracted chunk on the harddisk

saveChunk(string $Chunk, string $Filename) 

Parameters

$Chunk

string

The string containing the bytes of the chunk

$Filename

string

The filename to use to save the file on the harddisk

Exceptions

\InvalidArgumentException If the chunk is empty
\Exception If the library is unable to create the file on the harddisk

 Properties

 

The current wav file

$Wav : \Wav