WavWriter
in package
Writes Discord voice PCM audio to a standard WAV file.
Discord voice PCM is always 48000 Hz, stereo (2 channels), signed 16-bit little-endian — these parameters are fixed and not configurable.
Table of Contents
Constants
- BITS_PER_SAMPLE : mixed = 16
- NUM_CHANNELS : mixed = 2
- SAMPLE_RATE : mixed = 48000
Properties
- $dataBytes : int
- $handle : resource|null
- $path : string
Methods
- __construct() : mixed
- finalize() : void
- Seeks back to offset 0 and rewrites the complete WAV header with the correct RIFF chunk size and data sub-chunk size, then closes the file.
- getPath() : string
- Returns the path this writer writes to.
- open() : void
- Opens the output file and writes a 44-byte WAV header with placeholder sizes. Must be called before {@see write()}.
- write() : void
- Appends raw PCM bytes to the file and accumulates the byte count.
- buildHeader() : string
- Builds a 44-byte WAV header for the given data size.
Constants
BITS_PER_SAMPLE
private
mixed
BITS_PER_SAMPLE
= 16
NUM_CHANNELS
private
mixed
NUM_CHANNELS
= 2
SAMPLE_RATE
private
mixed
SAMPLE_RATE
= 48000
Properties
$dataBytes
private
int
$dataBytes
= 0
$handle
private
resource|null
$handle
= null
$path
private
string
$path
Methods
__construct()
public
__construct(string $path) : mixed
Parameters
- $path : string
-
Destination path for the WAV file.
finalize()
Seeks back to offset 0 and rewrites the complete WAV header with the correct RIFF chunk size and data sub-chunk size, then closes the file.
public
finalize() : void
Tags
getPath()
Returns the path this writer writes to.
public
getPath() : string
Return values
stringopen()
Opens the output file and writes a 44-byte WAV header with placeholder sizes. Must be called before {@see write()}.
public
open() : void
Tags
write()
Appends raw PCM bytes to the file and accumulates the byte count.
public
write(string $pcm) : void
Parameters
- $pcm : string
Tags
buildHeader()
Builds a 44-byte WAV header for the given data size.
private
buildHeader(int $dataBytes) : string
Header layout (all values little-endian): 0– 3 "RIFF" 4– 7 chunkSize = 36 + dataBytes 8–11 "WAVE" 12–15 "fmt " 16–19 subchunk1Size = 16 (PCM) 20–21 audioFormat = 1 (PCM) 22–23 numChannels = 2 24–27 sampleRate = 48000 28–31 byteRate = sampleRate * numChannels * (bitsPerSample/8) 32–33 blockAlign = numChannels * (bitsPerSample/8) 34–35 bitsPerSample = 16 36–39 "data" 40–43 subchunk2Size = dataBytes
Parameters
- $dataBytes : int