NAME

Peta::MP3::Encode - MP3 (MPEG-1 and MPEG-2 Layer III) encoder

VERSION

version 0.2610180

SYNOPSIS

use Peta::MP3::Encode;

my $mp3 = Peta::MP3::Encode->new(rate => 44100, channels => 2, bitrate => 192);
print $fh $mp3->encode(\@left, \@right);    # samples -1 .. 1, as many as you like
print $fh $mp3->finish;

DESCRIPTION

Encodes samples into MPEG-1 Layer III frames, or MPEG-2's at 16, 22.05 and 24 kHz: constant or variable bitrate, the bit reservoir, joint stereo (mid and side, frame by frame), short blocks where there are attacks, and an Info tag for gapless playback.

METHODS

new

Peta::MP3::Encode->new(
    rate           => 44100,     # 32000, 44100, 48000; MPEG-2: 16000, 22050, 24000
    channels       => 2,
    bitrate        => 192,       # kbit/s
    vbr            => undef,     # 0 .. 9 for a variable bitrate
    mode           => 'joint',   # joint, stereo; mono with one channel
    filterbank     => 'auto',    # perl, pdl, auto
    ffmpeg_gapless => 0,
);

bitrate is one of 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256 and 320 (MPEG-1; by default 192, mono 96), or 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144 and 160 (MPEG-2; by default 96, mono 48).

vbr gives a variable bitrate instead, as LAME's -V: 0 is the finest, and each level comes out near the bitrate LAME gives. bitrate is then not used.

filterbank auto uses PDL where it is installed: the same output, faster. For ffmpeg_gapless see "THE ENCODER'S NAME".

encode

my $bytes = $mp3->encode(\@left, \@right);

Returns the frames these samples complete. Samples are held until there are enough for a frame (1152; MPEG-2 576), and one granule more.

finish

Returns the rest: the last samples and the frames still waiting for their main data.

info_frame

Returns the frame to put before all the others: an Info tag (Xing, with a table for seeking, at a variable bitrate) with the LAME extension, which gives the encoder's delay and padding for gapless playback. It describes the frames given out so far, so write it first to keep its place, and again over it once finish has been called.

filterbank

perl or pdl: which filterbank the encoder uses.

frames, samples, bitrate, shorts, vbr

The frames and samples encoded; the bitrate (at a variable bitrate the average of the frames given out); the granules coded in short blocks; the VBR level (undefined at a constant bitrate).

THE ENCODER'S NAME

The Info frame names the encoder PetaMP3. mpg123 reads the encoder delay and padding whatever the name, and plays the file gapless. FFmpeg (and the browsers and players built on it) reads them only when the name begins with LAME, Lavf or Lavc; otherwise it plays the delay and padding along, some 1600 samples of silence.

For gapless playback there, ffmpeg_gapless => 1 writes LavfPeta instead, Lavf being FFmpeg's own prefix (its libavformat). The file is made by neither FFmpeg nor LAME; the rest of the name says whose it is, as far as nine characters allow. It is a concession to how FFmpeg recognizes the tag, and off by default.

AUTHOR

PetaMem s.r.o. <info@petamem.com>

COPYRIGHT

Copyright (c) 2026 PetaMem s.r.o.

LICENSE

This package is free software, dual-licensed under the Artistic License 2.0 and the BSD 2-Clause License. See the LICENSE file of the distribution.