Perlcritic compliance + bump version and update Changes
[audio-opusfile.git] / lib / Audio / Opusfile.pm
CommitLineData
a3f1cbda
MG
1package Audio::Opusfile;
2
3use 5.014000;
4use strict;
5use warnings;
6use Carp;
7
8use parent qw/Exporter/;
9use AutoLoader;
10
11my @constants =
12 qw/OPUS_CHANNEL_COUNT_MAX
13 OP_ABSOLUTE_GAIN
14 OP_DEC_FORMAT_FLOAT
15 OP_DEC_FORMAT_SHORT
16 OP_DEC_USE_DEFAULT
17 OP_EBADHEADER
18 OP_EBADLINK
19 OP_EBADPACKET
20 OP_EBADTIMESTAMP
21 OP_EFAULT
22 OP_EIMPL
23 OP_EINVAL
24 OP_ENOSEEK
25 OP_ENOTAUDIO
26 OP_ENOTFORMAT
27 OP_EOF
28 OP_EREAD
29 OP_EVERSION
30 OP_FALSE
31 OP_GET_SERVER_INFO_REQUEST
32 OP_HEADER_GAIN
33 OP_HOLE
34 OP_HTTP_PROXY_HOST_REQUEST
35 OP_HTTP_PROXY_PASS_REQUEST
36 OP_HTTP_PROXY_PORT_REQUEST
37 OP_HTTP_PROXY_USER_REQUEST
38 OP_PIC_FORMAT_GIF
39 OP_PIC_FORMAT_JPEG
40 OP_PIC_FORMAT_PNG
41 OP_PIC_FORMAT_UNKNOWN
42 OP_PIC_FORMAT_URL
43 OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST
44 OP_TRACK_GAIN/;
45
46our @EXPORT_OK = @constants;
47our @EXPORT = @constants;
48
a3133f60
MG
49our $VERSION = '1.000';
50
51sub AUTOLOAD { ## no critic (ProhibitAutoloading)
52 my $constname;
53 our $AUTOLOAD;
54 ($constname = $AUTOLOAD) =~ s/.*:://s;
55 croak '&Audio::Opusfile::constant not defined' if $constname eq 'constant';
56 my ($error, $val) = constant($constname);
57 if ($error) { croak $error; }
58 {
59 no strict 'refs'; ## no critic (ProhibitNoStrict)
60 *$AUTOLOAD = sub { $val };
61 }
62 goto &$AUTOLOAD;
a3f1cbda
MG
63}
64
65require XSLoader;
66XSLoader::load('Audio::Opusfile', $VERSION);
b9bd6a0d 67require Audio::Opusfile::Head;
a3f1cbda 68require Audio::Opusfile::Tags;
2f4b5b1b 69require Audio::Opusfile::PictureTag;
a3f1cbda 70
a3f1cbda
MG
71sub new_from_file {
72 my ($class, $file) = @_;
73 open_file($file)
74}
75
5fbea9a2
MG
76sub new_from_memory {
77 my ($class, $buf) = @_;
78 open_memory($buf)
79}
80
a3f1cbda
MG
811;
82__END__
83
84=encoding utf-8
85
86=head1 NAME
87
196e0b27 88Audio::Opusfile - partial interface to the libopusfile Ogg Opus library
a3f1cbda
MG
89
90=head1 SYNOPSIS
91
92 use Audio::Opusfile;
93 my $of = Audio::Opusfile->new_from_file('silence.opus');
94 my $tags = $of->tags;
95 say $tags->query('TITLE'); # Cellule
96
97=head1 DESCRIPTION
98
99Opus is a totally open, royalty-free, highly versatile audio codec.
100Opus is unmatched for interactive speech and music transmission over
101the Internet, but is also intended for storage and streaming
102applications. It is standardized by the Internet Engineering Task
103Force (IETF) as RFC 6716 which incorporated technology from Skype's
104SILK codec and Xiph.Org's CELT codec.
105
106libopusfile is a library for decoding and basic manipulation of Ogg
107Opus files.
108
196e0b27
MG
109Audio::Opusfile is an interface to libopusfile. It exports nearly all
110of the functions for obtaining metadata of an Opus file or buffer in
111that library. Future versions will additionally provide functions for
112decoding Opus data into PCM.
a3f1cbda 113
196e0b27 114The API might change in future versions.
a3f1cbda
MG
115
116=head1 METHODS
117
118=over
119
120=item Audio::Opusfile->B<new_from_file>(I<$file>)
121
122Creates a new Audio::Opusfile object from an Ogg Opus file.
123
124Dies if the given file does not exist or is not a valid Ogg Opus file.
125
33fac129
MG
126=item Audio::Opusfile->B<new_from_memory>(I<$buffer>)
127
128Creates a new Audio::Opusfile object from a buffer containing Ogg Opus
129data.
130
131Dies if the given buffer does not contain valid data.
132
133=item Audio::Opusfile::test(I<$buffer>)
134
135Returns true if the given buffer looks like the beginning of a valid
136Ogg Opus file, false otherwise.
137
138Dies if the given buffer does not have sufficient data to tell if it
139is an Opus stream or if it looks like a Opus stream but parsing it
140failed.
141
10773025 142=item $of->B<head>
2f4b5b1b
MG
143
144Returns an L<Audio::Opusfile::Head> object corresponding to the file.
145
10773025 146=item $of->B<tags>
a3f1cbda
MG
147
148Returns an L<Audio::Opusfile::Tags> object corresponding to the file.
149
10773025 150=item $of->B<seekable>
33fac129
MG
151
152Returns whether or not the data source being read is seekable.
153
10773025 154=item $of->B<link_count>
33fac129
MG
155
156Returns the number of links in this chained stream. Always returns 1
157for unseekable sources.
158
10773025 159=item $of->B<serialno>([I<$link_index>])
33fac129
MG
160
161Get the serial number of the given link in a (possibly-chained) Ogg
162Opus stream. If the given index is greater than the total number of
163links, this returns the serial number of the last link.
164
165If the source is not seekable, I<$link_index> is negative, or
166I<$link_index> is not given, then this function returns the serial
167number of the current link.
168
10773025 169=item $of->B<raw_total>([I<$link_index>])
dc986a40
MG
170
171Get the total (compressed) size of the stream (with no arguments), or
172of an individual link in a (possibly-chained) Ogg Opus stream (with
173one nonnegative argument), including all headers and Ogg muxing
174overhead.
175
176The stream must be seekable to get the total. A negative value is
177returned if the stream is not seekable.
178
179B<Warning:> If the Opus stream (or link) is concurrently multiplexed
180with other logical streams (e.g., video), this returns the size of the
181entire stream (or link), not just the number of bytes in the first
182logical Opus stream. Returning the latter would require scanning the
183entire file.
184
10773025 185=item $of->B<pcm_total>([I<$link_index>])
dc986a40
MG
186
187Get the total PCM length (number of samples at 48 kHz) of the stream
188(with no arguments), or of an individual link in a (possibly-chained)
189Ogg Opus stream (with one nonnegative argument).
190
191Users looking for op_time_total() should use this function instead.
192Because timestamps in Opus are fixed at 48 kHz, there is no need for a
193separate function to convert this to seconds.
194
195The stream must be seekable to get the total. A negative value is
196returned if the stream is not seekable.
197
9fc88b29
MG
198=item $of->B<current_link>
199
200Retrieve the index of the current link.
201
202This is the link that produced the data most recently read by
203op_read_float() or its associated functions, or, after a seek, the
204link that the seek target landed in. Reading more data may advance the
205link index (even on the first read after a seek).
206
10773025 207=item $of->B<bitrate>([I<$link_index>])
dc986a40
MG
208
209Computes the bitrate of the stream (with no arguments), or of an
210individual link in a (possibly-chained) Ogg Opus stream (with one
211nonnegative argument).
212
213The stream must be seekable to compute the bitrate. A negative value
214is returned if the stream is not seekable.
215
216B<Warning:> If the Opus stream (or link) is concurrently multiplexed with
217other logical streams (e.g., video), this uses the size of the entire
218stream (or link) to compute the bitrate, not just the number of bytes
219in the first logical Opus stream.
220
10773025
MG
221=item $of->B<bitrate_instant>
222
223Compute the instantaneous bitrate, measured as the ratio of bits to
224playable samples decoded since a) the last call to B<bitrate_instant>,
225b) the last seek, or c) the start of playback, whichever was most
226recent.
227
228This will spike somewhat after a seek or at the start/end of a chain
229boundary, as pre-skip, pre-roll, and end-trimming causes samples to be
230decoded but not played.
231
232=item $of->B<raw_tell>
233
234Obtain the current value of the position indicator of I<$of>. This is
235the byte position that is currently being read from.
236
237=item $of->B<pcm_tell>
238
239Obtain the PCM offset of the next sample to be read.
240
241If the stream is not properly timestamped, this might not increment by
242the proper amount between reads, or even return monotonically
243increasing values.
244
245=item $of->B<raw_seek>(I<$offset>)
246
247Seek to a byte offset relative to the compressed data.
248
249This also scans packets to update the PCM cursor. It will cross a
250logical bitstream boundary, but only if it can't get any packets out
251of the tail of the link to which it seeks.
252
253=item $of->B<pcm_seek>(I<$offset>)
254
255Seek to the specified PCM offset, such that decoding will begin at
256exactly the requested position. The PCM offset is in samples at 48 kHz
257relative to the start of the stream.
258
259=item $of->B<set_gain_offset>(I<$gain_type>, I<$gain_offset>)
260
261Sets the gain to be used for decoded output.
262
263By default, the gain in the header is applied with no additional
264offset. The total gain (including header gain and/or track gain, if
265applicable, and this offset), will be clamped to [-32768,32767]/256
266dB. This is more than enough to saturate or underflow 16-bit PCM.
267
268B<Note:> The new gain will not be applied to any already buffered,
269decoded output. This means you cannot change it sample-by-sample, as
270at best it will be updated packet-by-packet. It is meant for setting a
271target volume level, rather than applying smooth fades, etc.
272
273I<$gain_type> is one of OP_HEADER_GAIN, OP_TRACK_GAIN, or
274OP_ABSOLUTE_GAIN. I<$gain_offset> is in 1/256ths of a dB.
275
276=item $of->B<set_dither_enabled>(I<$enabled>)
277
278Sets whether or not dithering is enabled for 16-bit decoding.
279
280By default, when libopusfile is compiled to use floating-point
281internally, calling read() or read_stereo() will first decode to
282float, and then convert to fixed-point using noise-shaping dithering.
283This flag can be used to disable that dithering. When the application
284uses read_float() or read_float_stereo(), or when the library has been
285compiled to decode directly to fixed point, this flag has no effect.
286
287=item $of->B<read>([I<$bufsize>])
288
289It is recommended to use B<read_float> instead of this method if the
290rest of your audio processing chain can handle floating point.
291
292Reads more samples from the stream. I<$bufsize> is the maximum number
293of samples read, and it defaults to 1048576. Returns a list whose
294first element is the link index this data was decoded from, and the
295rest of the elements are PCM samples, as signed 16-bit values at 48
296kHz with a nominal range of [-32768,32767). Multiple channels are
297interleaved using the L<Vorbis channel ordering|https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-810004.3.9>.
298
299You can use C<< $of->head($li)->channel_count >> to find out the
300channel count of a given link index.
301
302=item $of->B<read_float>([I<$bufsize>])
303
304Like B<read>, but samples are signed floats with a nominal range of
305[-1.0, 1.0].
306
307=item $of->B<read_stereo>([I<$bufsize>])
308
309Like B<read>, but downmixes the stream to stereo (therefore you will
310always get two channels) and does NOT return the link index (the first
311return value is the first sample).
312
313=item $of->B<read_float_stereo>([I<$bufsize>])
314
315Like B<read_float>, but downmixes the stream to stereo (therefore you
316will always get two channels) and does NOT return the link index (the
317first return value is the first sample).
318
a3f1cbda
MG
319=back
320
321=head1 EXPORT
322
323All constants are exported by default:
324
325 OPUS_CHANNEL_COUNT_MAX
326 OP_ABSOLUTE_GAIN
327 OP_DEC_FORMAT_FLOAT
328 OP_DEC_FORMAT_SHORT
329 OP_DEC_USE_DEFAULT
330 OP_EBADHEADER
331 OP_EBADLINK
332 OP_EBADPACKET
333 OP_EBADTIMESTAMP
334 OP_EFAULT
335 OP_EIMPL
336 OP_EINVAL
337 OP_ENOSEEK
338 OP_ENOTAUDIO
339 OP_ENOTFORMAT
340 OP_EOF
341 OP_EREAD
342 OP_EVERSION
343 OP_FALSE
344 OP_GET_SERVER_INFO_REQUEST
345 OP_HEADER_GAIN
346 OP_HOLE
347 OP_HTTP_PROXY_HOST_REQUEST
348 OP_HTTP_PROXY_PASS_REQUEST
349 OP_HTTP_PROXY_PORT_REQUEST
350 OP_HTTP_PROXY_USER_REQUEST
351 OP_PIC_FORMAT_GIF
352 OP_PIC_FORMAT_JPEG
353 OP_PIC_FORMAT_PNG
354 OP_PIC_FORMAT_UNKNOWN
355 OP_PIC_FORMAT_URL
356 OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST
357 OP_TRACK_GAIN
358
359
360=head1 SEE ALSO
361
b9bd6a0d 362L<Audio::Opusfile::Head>,
a3f1cbda
MG
363L<Audio::Opusfile::Tags>,
364L<http://opus-codec.org/>,
365L<http://opus-codec.org/docs/opusfile_api-0.7/index.html>
366
367=head1 AUTHOR
368
369Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
370
371=head1 COPYRIGHT AND LICENSE
372
36bdc941 373Copyright (C) 2016-2017 by Marius Gavrilescu
a3f1cbda
MG
374
375This library is free software; you can redistribute it and/or modify
376it under the same terms as Perl itself, either Perl version 5.24.0 or,
377at your option, any later version of Perl 5 you may have available.
378
379
380=cut
This page took 0.033817 seconds and 4 git commands to generate.