/
Sndfile.pm
605 lines (429 loc) · 21.1 KB
/
Sndfile.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use v6;
use NativeCall;
=begin pod
=head1 NAME
Audio::Sndfile - read/write audio data via libsndfile
=head1 SYNOPSIS
=begin code
# Convert file to 32 bit float WAV
use Audio::Sndfile;
my $in = Audio::Sndfile.new(filename => "t/data/cw_glitch_noise15.wav", :r);
my $out-format = Audio::Sndfile::Info::WAV +| Audio::Sndfile::Info::FLOAT;
my $out = Audio::Sndfile.new(filename => "out.wav", :w,
channels => $in.channels,
samplerate => $in.samplerate,
format => $out-format);
loop {
my @in-frames = $in.read-float(1024);
$out.write-float(@in-frames);
last if ( @in-frames / $in.channels ) != 1024;
}
$in.close;
$out.close;
=end code
Other examples are available in the "examples" sub-directory of the
repository.
=head1 DESCRIPTION
This library provides a mechanism to read and write audio data files in
various formats by using the API provided by libsndfile.
A full list of the formats it is able to work with can be found at:
http://www.mega-nerd.com/libsndfile/#Features
if you need to work with formats that aren't listed then you will need
to find another library.
The interface presented is slightly simplified with regard to that of
libsndfile and whilst it does nearly everything I need it do, I have
opted to release the most useful functionality early and progressively
add features as it becomes clear how they should be implemented.
As of the first release, the methods provided for writing audio data do
not constrain the items to the range expected by the method. If out of
range data is supplied wrapping will occur which may lead to unexpected
results.
=head3 FRAMES vs ITEMS
In the description of the L<methods|#METHODS> below a 'frame' refers to
one or more items ( that is the number of channels in the audio data.)
The frame data is interleaved per channel consecutively and must always
have a number of items that is a multiple of the number of channels.
The read methods will always return a number of items that is the
multiple of number of channels - thus you can determine if you got the
number back that you requested with something like:
@frames.elems == ( $num-channels * $frames-requested)
The write methods will throw an exception if supplied with an array that
isn't some multiple of the number of channels and will always return
the number of 'frames' written.
There are no methods provided for interleaving or de-interleaving frame
data as Perl's list methods (e.g. C<zip> or C<rotor>) are perfect for
this task.
=head2 METHODS
=head3 new
method new(Audio::Sndfile:U: Str :$filename!, :r)
The C<:r> adverb opens the specified file for reading. If the file
can't be opened or there is some other problem with it (such as it not
being a supported format,) then an exception will be thrown.
method new(Audio::Sndfile:U: Str :$filename!, :w, Audio::Sndfile::Info :$info)
method new(Audio::Sndfile:U: Str :$filename!, :w, *%info)
The C<:w> adverb opens the specified file for writing. It requires
either an existing valid L<Audio::Sndfile::Info> supplied as the C<info>
named parameter, or attributes that will be passed to the constructor of
L<Audio::Sndfile::Info>. If the file cannot be opened for some reason
or the resulting L<Audio::Sndfile::Info> is incomplete or invalid an
exception will be thrown.
The info requires the attributes C<channels>, C<format>,
C<samplerate>. If copying or converting an existing file opened for
reading the L<clone-info|#clone-info> method can be used to obtain a
valid L<Audio::Sndfile::Info>.
=head3 info
This is an accessor to the L<Audio::Sndfile::Info> for the opened file. It is
read-only as it doesn't make sense to change the info after the file has been
opened.
There are several accessors delegated to this object:
=item format
The format of the file formed by the bitwise or of C<type>, C<sub-type>
and C<endian>
=item channels
The number of channels in the file.
=item samplerate
The sample rate of the file as an Int (e.g. 44100, 48000).
=item frames
The number of frames in the file. This only makes sense when the file
is opened for reading.
=item sections
=item seekable
A Bool indicating whether the open file is seekable, this will be True
for most regular files and False for special files such as a pipe,
however as there currently isn't any easy way to open other than a
regular file this may not be useful.
=item type
A value of the enum L<Audio::Sndfile::Info::Format> indicating the major
format of the file (e.g. WAV,) this is bitwise or-ed with the C<sub-type>
and C<endian> to create C<format> (which is what is used by the underlying
library functions.)
=item sub-type
A value of the enum L<Audio::Sndfile::Info::Subformat> indicating the
minor format or sample encoding of the file (e.g PCM_16, FLOAT, ) this
is bitwise or-ed with the C<type> and C<endian> to create C<format>
=item endian
A value of the enum L<Audio::Sndfile::Info::End> that indicate the
endian-ness of the sample data. This is bitwise or-ed with C<type>
and C<sub-type> to provide C<format>.
=item duration
A L<Duration> object that describes the duration of the file in (possibly
fractional) seconds. This probably only makes sense for files opened
for reading.
=head3 library-version
method library-version(Audio::Sndfile:D: ) returns Str
Returns a string representation of the version reported by libsndfile.
=head3 close
method close(Audio::Sndfile:D:) returns Int
This closes the file stream for the opened file. Attempting to write or
read the object after this has been called is an error.
=head3 error-number
method error-number(Audio::Sndfile:D:) returns Int
This returns non-zero if there was an error in the last read, write
or open operation. The actual error message can be obtained with
L<error|#error> below.
=head3 error
method error(Audio::Sndfile:D:) returns Str
This will return the string describing the last error if
L<error-number|#error-number> was non-zero or 'No Error' otherwise.
=head3 sync
method sync(Audio::Sndfile:D:) returns Int
If the file is opened for writing then any buffered data will be flushed
to disk. If the file was opened for reading it does nothing.
=head3 read-short
multi method read-short(Audio::Sndfile:D: Int $frames) returns Array[int16]
multi method read-short(Audio::Sndfile:D: Int $frames, :$raw!) returns [CArray[int16], Int]
This returns an array of size C<$frames> * $num-channels of 16 bit
integers from the opened file. The returned array may be empty or
shorter than expected if there is no more data to read.
With the the ':raw' adverb specified it will return a two element array containing the raw CArray
returned from the underlying library and the number of frames. This is for convenience
(and efficiency ) where the data is going to be passed directly to another native libray
function.
=head3 write-short
multi method write-short(Audio::Sndfile:D: @frames) returns Int
multi method write-short(Audio::Sndfile:D: CArray[int16] $frames-in, Int $frames) returns Int
This writes the array @frames of 16 bit integers to the file. @frames
must have a number of elements that is a multiple of the number of
channels or an exception will be thrown.
If the files isn't opened for writing or if it has been closed an error
will occur.
If the values are outside the range for an int16 then wrapping will occur.
The second multi is for the convenience of applications which may have obtained their data
from some native function.
=head3 read-int
multi method read-int(Audio::Sndfile:D: Int $frames) returns Array[Int]
multi method read-int(Audio::Sndfile:D: Int $frames, :$raw!) returns [CArray[int32], Int]
This returns an array of size C<$frames> * $num-channels of 32 bit
integers from the opened file. The returned array may be empty or
shorter than expected if there is no more data to read.
With the the ':raw' adverb specified it will return a two element array
containing the raw CArray returned from the underlying library and the
number of frames. This is for convenience (and efficiency ) where the
data is going to be passed directly to another native libray function.
=head3 write-int
multi method write-int(Audio::Sndfile:D: @frames) returns Int
multi method write-int(Audio::Sndfile:D: CArray[int32] $frames-in, Int $frames) returns Int
This writes the array @frames of 32 bit integers to the file. @frames
must have a number of elements that is a multiple of the number of
channels or an exception will be thrown.
If the files isn't opened for writing or if it has been closed an error
will occur.
If the values are outside the range for an int32 then wrapping will occur.
The second multi is for the convenience of applications which may have
obtained their data from some native function.
=head3 read-float
multi method read-float(Audio::Sndfile:D: Int $frames) returns Array[num32]
multi method read-float(Audio::Sndfile:D: Int $frames, :$raw!) returns [CArray[num32], Int]
This returns an array of size C<$frames> * $num-channels of 32 bit
floating point numbers from the opened file. The returned array may be
empty or shorter than expected if there is no more data to read.
With the the ':raw' adverb specified it will return a two element array
containing the raw CArray returned from the underlying library and the
number of frames. This is for convenience (and efficiency ) where the
data is going to be passed directly to another native libray function.
=head3 write-float
multi method write-float(Audio::Sndfile:D: @frames) returns Int
multi method write-float(Audio::Sndfile:D: CArray[num32] $frames-in, Int $frames) returns Int
This writes the array @frames of 32 bit floating point numbers to the
file. @frames must have a number of elements that is a multiple of the
number of channels or an exception will be thrown.
If the files isn't opened for writing or if it has been closed an error
will occur.
If the values are outside the range for a num32 then wrapping will occur.
The second multi is for the convenience of applications which may have
obtained their data from some native function.
=head3 read-double
multi method read-double(Audio::Sndfile:D: Int $frames) returns Array[num64]
multi method read-double(Audio::Sndfile:D: Int $frames, :$raw!) returns [CArray[num64], Int]
This returns an array of size C<$frames> * $num-channels of 64 bit
floating point numbers from the opened file. The returned array may be
empty or shorter than expected if there is no more data to read.
With the the ':raw' adverb specified it will return a two element array
containing the raw CArray returned from the underlying library and the
number of frames. This is for convenience (and efficiency ) where the
data is going to be passed directly to another native libray function.
=head3 write-double
multi method write-double(Audio::Sndfile:D: @frames) returns Int
multi method write-double(Audio::Sndfile:D: CArray[num64] $frames-in, Int $frames) returns Int
This writes the array @frames of 64 bit floating point numbers to the
file. @frames must have a number of elements that is a multiple of the
number of channels or an exception will be thrown.
If the files isn't opened for writing or if it has been closed an error
will occur.
If the values are outside the range for a num64 then wrapping will occur.
As of the time of the initial release, this may fail if the frame data was
directly retrieved via L<read-double|#read-double> due to an infelicity in
the runtime, this should be fixed at some point but can be worked around by
copying the values.
The second multi is for the convenience of applications which may have obtained their data
from some native function.
=head3 clone-info
method clone-info(Audio::Sndfile:D: ) returns Audio::Sndfile::Info
This returns a new L<Audio::Sndfile::Info> based on the details of the
current file suitable for being passed to L<new|#new> for instance when
copying or converting the file.
=end pod
class Audio::Sndfile:ver<0.0.13>:auth<github:jonathanstowe>:api<1.0> {
my constant LIB = [ 'sndfile', v1 ];
subset RawEncode of Array where ($_.elems == 2 ) && ($_[0] ~~ CArray) && ($_[1] ~~ Int);
use Audio::Sndfile::Info;
# The opaque type returned from open
my class File is repr('CPointer') {
sub sf_close(File $file --> int32 ) is native(LIB) { * }
method close( --> Int ) {
sf_close(self);
}
sub sf_error(File $file --> int32 ) is native(LIB) { * }
method error-number( --> Int ) {
sf_error(self);
}
sub sf_strerror(File $file --> Str ) is native(LIB) { * }
method error( --> Str ) {
sf_strerror(self);
}
multi method read-read(Int $frames, Audio::Sndfile::Info $info, &read-sub, Mu:U $type, :$raw! --> RawEncode ) {
my $buff = CArray[$type].new;
$buff[$frames * $info.channels] = $type ~~ Num ?? Num(0) !! Int(0);
my $rc = &read-sub(self, $buff, $frames);
[ $buff, $rc ];
}
multi method read-read(Int $frames, Audio::Sndfile::Info $info, &read-sub, Mu:U $type --> Array ) {
my ($buff, $rc ) = self.read-read($frames, $info, &read-sub, $type, :raw).list;
my @tmp_arr = (^($rc * $info.channels)).map({ $buff[$_] });
@tmp_arr;
}
multi method write-write(Audio::Sndfile::Info $info, &write-sub, Mu:U $type, @items --> Int ) {
my $buff = CArray[$type].new;
for ^@items.elems -> $i {
$buff[$i] = @items[$i];
}
my Int $frames = (@items.elems / $info.channels).Int;
self.write-write(&write-sub, $buff, $frames);
}
multi method write-write(&write-sub, CArray $frames-in, Int $frames --> Int ) {
&write-sub(self, $frames-in, $frames);
}
sub sf_readf_short(File , CArray[int16] is rw, int64 --> int64 ) is native(LIB) { * }
multi method read-short(Int $frames, Audio::Sndfile::Info $info --> Array ) {
self.read-read($frames, $info, &sf_readf_short, int16);
}
multi method read-short(Int $frames, Audio::Sndfile::Info $info, :$raw! --> RawEncode ) {
self.read-read($frames, $info, &sf_readf_short, int16, :raw);
}
sub sf_writef_short(File , CArray[int16], int64 --> int64 ) is native(LIB) { * }
multi method write-short(Audio::Sndfile::Info $info, @items --> Int ) {
self.write-write($info, &sf_writef_short, int16, @items);
}
multi method write-short(CArray[int16] $frames-in, Int $frames --> Int ) {
self.write-write(&sf_writef_short, $frames-in, $frames);
}
sub sf_readf_int(File , CArray[int32], int64 --> int64 ) is native(LIB) { * }
multi method read-int(Int $frames, Audio::Sndfile::Info $info --> Array ) {
self.read-read($frames, $info, &sf_readf_int, int32);
}
multi method read-int(Int $frames, Audio::Sndfile::Info $info, :$raw! --> RawEncode ) {
self.read-read($frames, $info, &sf_readf_int, int32, :raw);
}
sub sf_writef_int(File , CArray[int32], int64 --> int64 ) is native(LIB) { * }
multi method write-int(Audio::Sndfile::Info $info, @items --> Int ) {
self.write-write($info, &sf_writef_int, int32, @items);
}
multi method write-int(CArray[int32] $frames-in, Int $frames --> Int ) {
self.write-write(&sf_writef_int, $frames-in, $frames);
}
sub sf_readf_double(File , CArray[num64] is rw, int64 --> int64 ) is native(LIB) { * }
multi method read-double(Int $frames, Audio::Sndfile::Info $info --> Array ) {
self.read-read($frames, $info, &sf_readf_double, num64);
}
multi method read-double(Int $frames, Audio::Sndfile::Info $info, :$raw! --> RawEncode ) {
self.read-read($frames, $info, &sf_readf_double, num64, :raw);
}
sub sf_writef_double(File , CArray[num64], int64 --> int64 ) is native(LIB) { * }
multi method write-double(Audio::Sndfile::Info $info, @items --> Int ) {
self.write-write($info, &sf_writef_double, num64, @items);
}
multi method write-double(CArray[num64] $frames-in, Int $frames --> Int ) {
self.write-write(&sf_writef_double, $frames-in, $frames);
}
sub sf_readf_float(File , CArray[num32] is rw, int64 --> int64 ) is native(LIB) { * }
multi method read-float(Int $frames, Audio::Sndfile::Info $info --> Array ) {
self.read-read($frames, $info, &sf_readf_float, num32);
}
multi method read-float(Int $frames, Audio::Sndfile::Info $info, :$raw! --> RawEncode ) {
self.read-read($frames, $info, &sf_readf_float, num32, :raw);
}
sub sf_writef_float(File , CArray[num32], int64 --> int64 ) is native(LIB) { * }
multi method write-float(Audio::Sndfile::Info $info, @items --> Int ) {
self.write-write($info, &sf_writef_float, num32, @items);
}
multi method write-float(CArray[num32] $frames-in, Int $frames --> Int ) {
self.write-write(&sf_writef_float, $frames-in, $frames);
}
sub sf_write_sync(File) is native(LIB) { * }
method sync() {
sf_write_sync(self);
}
}
enum OpenMode (:Read(0x10), :Write(0x20), :ReadWrite(0x30));
has Str $.filename;
has File $!file handles <close error-number error sync>;
has Audio::Sndfile::Info $.info handles <format channels samplerate frames sections seekable type sub-type endian duration>;
has OpenMode $.mode;
sub sf_version_string( --> Str ) is native(LIB) { * }
method library-version(--> Str ) {
sf_version_string();
}
sub sf_open(Str $filename, int32 $mode, Audio::Sndfile::Info $info --> File ) is native(LIB) { * }
submethod BUILD(Str() :$!filename!, Bool :$r, Bool :$w, Bool :$rw, Audio::Sndfile::Info :$!info?, *%info) {
if one($r, $w, $rw ) {
$!mode = do if $r {
Read;
}
elsif $w {
$!info //= Audio::Sndfile::Info.new(|%info);
die "invalid format supplied to :w" if not $!info.format-check;
Write;
}
else {
ReadWrite;
}
$!info //= Audio::Sndfile::Info.new();
$!file = sf_open($!filename, $!mode.Int, $!info);
if $!file.error-number > 0 {
die $!file.error;
}
}
else {
die "exactly one of ':r', ':w', ':rw' must be provided";
}
}
multi method read-short(Int $frames --> Array ) {
$!file.read-short($frames, $!info);
}
multi method read-short(Int $frames, :$raw! --> RawEncode ) {
$!file.read-short($frames, $!info, :raw);
}
multi method write-short(@frames --> Int ) {
self!assert-frame-length(@frames);
$!file.write-short($!info, @frames);
}
multi method write-short(CArray[int16] $frames-in, Int $frames --> Int ) {
$!file.write-short($frames-in, $frames);
}
multi method read-int(Int $frames --> Array ) {
$!file.read-int($frames, $!info);
}
multi method read-int(Int $frames, :$raw! --> RawEncode ) {
$!file.read-int($frames, $!info, :raw);
}
multi method write-int(@frames --> Int ) {
self!assert-frame-length(@frames);
$!file.write-int($!info, @frames);
}
multi method write-int(CArray[int32] $frames-in, Int $frames --> Int ) {
$!file.write-int($frames-in, $frames);
}
multi method read-float(Int $frames --> Array ) {
$!file.read-float($frames, $!info);
}
multi method read-float(Int $frames, :$raw! --> RawEncode ) {
$!file.read-float($frames, $!info, :raw);
}
multi method write-float(@frames --> Int ) {
self!assert-frame-length(@frames);
$!file.write-float($!info, @frames);
}
multi method write-float(CArray[num32] $frames-in, Int $frames --> Int ) {
$!file.write-float($frames-in, $frames);
}
multi method read-double(Int $frames --> Array ) {
$!file.read-double($frames, $!info);
}
multi method read-double(Int $frames, :$raw! --> RawEncode ) {
$!file.read-double($frames, $!info, :raw);
}
multi method write-double(@frames --> Int ) {
self!assert-frame-length(@frames);
$!file.write-double($!info, @frames);
}
multi method write-double(CArray[num64] $frames-in, Int $frames --> Int ) {
$!file.write-double($frames-in, $frames);
}
method !assert-frame-length(@frames) {
if (@frames.elems % $!info.channels) != 0 {
die "items not a multiple of channels";
}
}
# minimum detail required
method clone-info( --> Audio::Sndfile::Info ) {
Audio::Sndfile::Info.new(
samplerate => $!info.samplerate,
channels => $!info.channels,
format => $!info.format
);
}
method Numeric( --> Numeric ) {
$!info.type;
}
}
# vim: expandtab shiftwidth=4 ft=perl6