Codebase list libaudio-wav-perl / HEAD
HEAD

Tree @HEAD (Download .tar.gz)

  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
---------------------------------------------------------------------
                  README file for Audio::Wav (0.11).
---------------------------------------------------------------------

Modules for reading & writing Microsoft WAV files.

---------------------------------------------------------------------
                             INSTALLATION
---------------------------------------------------------------------

tar zxvf Audio-Wav-0.06.tar.gz
cd Audio-Wav-0.06
perl Makefile.PL
make test
make install

---------------------------------------------------------------------
                        LICENSE AND COPYRIGHT
---------------------------------------------------------------------

his program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

Copyright (c) 2010 Brian Szymanski <ski-cpan@allafrica.com>
Copyright (c) 1999-2001,2004-2006 Nick Peskett (http://www.peskett.co.uk/)
Copyright (c) 2004 Kurt George Gjerde <KJERDE@cpan.org>

---------------------------------------------------------------------
                            DOCUMENTATION
---------------------------------------------------------------------

                              Audio::Wav
          Modules for reading & writing Microsoft WAV files.
---------------------------------------------------------------------

NAME
    Audio::Wav - Modules for reading & writing Microsoft WAV files.

SYNOPSIS
    # copying a file and adding some cue points to the output file
    use Audio::Wav;
    my $wav = new Audio::Wav;
    my $read = $wav -> read( 'input.wav' );
    my $write = $wav -> write( 'output.wav', $read -> details() );
    print "input is ", $read -> length_seconds(), " seconds long\n";

    $write -> set_info( 'software' => 'Audio::Wav' );
    my $data;
    #read 512 bytes
    while ( defined( $data = $read -> read_raw( 512 ) ) ) {
        $write -> write_raw( $data );
    }
    my $length = $read -> length_samples();
    my( $third, $half, $twothirds ) = map int( $length / $_ ), ( 3, 2, 1.5 );
    my %samp_loop = (
        'start' => $third,
        'end'   => $twothirds,
    );
    $write -> add_sampler_loop( %samp_loop );
    $write -> add_cue( $half, "cue label 1", "cue note 1" );
    $write -> finish();

    # splitting a multi-channel file to seperate mono files (slowly!);
    use Audio::Wav;
    my $read = $wav -> read( '4ch.wav' );
    my $details = $read -> details();
    my %out_details = map { $_ => $details -> {$_} } 'bits_sample', 'sample_rate';
    $out_details{'channels'} = 1;
    my @out_files;
    my $in_channels = $details -> {'channels'};
    foreach my $channel ( 1 .. $in_channels ) {
        push @out_files, $wav -> write( 'multi_' . $channel . '.wav', \%out_details );
    }

    while ( 1 ) {
        my @channels = $read -> read();
        last unless @channels;
        foreach my $channel_id ( 0 .. $#channels ) {
            $out_files[$channel_id] -> write( $channels[$channel_id] );
        }
    }

    # not entirely neccessary as finish is done in DESTROY now (if the file hasn't been finished already).
    foreach my $write ( @out_files ) {
        $write -> finish();
    }

NOTES
    All sample positions are now in sample offsets (unless option
    '.01compatible' is true).

    There is now *very* basic support for WAVEFORMATEXTENSIBLE (in fact it
    only recognises that the file is in this format). The key 'wave-ex' is
    used in the detail hash to denote this format when reading or writing.
    I'd like to do more with this, but don't have any hardware or software
    to test these files, also don't really have any spare time to do the
    implementation at present.

    One day I plan to learn enough C to do the sample reading/ writing in
    XS, but for the time being it's done using pack/ unpack in Perl and is
    slow. Working with the raw format doesn't suffer in this way.

    It's likely that reading/ writing files with bit-depth greater than 16
    won't work properly, I need to look at this at some point.

DESCRIPTION
    These modules provide a method of reading & writing uncompressed
    Microsoft WAV files.

METHODS
  new
    Returns a blessed Audio::Wav object. All the parameters are optional and
    default to 0

        my %options = (
            '.01compatible'         => 0,
            'oldcooledithack'       => 0,
            'debug'                 => 0,
        );
        my $wav = Audio::Wav -> new( %options );

  write
    Returns a blessed Audio::Wav::Write object.

        my $details = {
            'bits_sample'   => 16,
            'sample_rate'   => 44100,
            'channels'      => 2,
        };

        my $write = $wav -> write( 'testout.wav', $details );

    See Audio::Wav::Write for methods.

  read
    Returns a blessed Audio::Wav::Read object.

        my $read = $wav -> read( 'testout.wav' );

    See Audio::Wav::Read for methods.

  set_error_handler
    Specifies a subroutine for catching errors. The subroutine should take a
    hash as input. The keys in the hash are 'filename', 'message' (error
    message), and 'warning'. If no error handler is set, die and warn will
    be used.

        sub myErrorHandler {
            my( %parameters ) = @_;
            if ( $parameters{'warning'} ) {
                # This is a non-critical warning
                warn "Warning: $parameters{'filename'}: $parameters{'message'}\n";
            } else {
                # Critical error!
                die "ERROR: $parameters{'filename'}: $parameters{'message'}\n";
            }
        }
        $wav -> set_error_handler( \&myErrorHandler );

---------------------------------------------------------------------
                           Audio::Wav::Read
               Module for reading Microsoft WAV files.
---------------------------------------------------------------------

NAME
    Audio::Wav::Read - Module for reading Microsoft WAV files.

SYNOPSIS
    use Audio::Wav;

    my $wav = new Audio::Wav;
    my $read = $wav -> read( 'filename.wav' );
OR
    my $read = Audio::Wav -> read( 'filename.wav' );

    my $details = $read -> details();

DESCRIPTION
    Reads Microsoft Wav files.

SEE ALSO
    Audio::Wav

    Audio::Wav::Write

NOTES
    This module shouldn't be used directly, a blessed object can be returned
    from Audio::Wav.

METHODS
  file_name
    Returns the file name.

        my $file = $read -> file_name();

  get_info
    Returns information contained within the wav file.

        my $info = $read -> get_info();

    Returns a reference to a hash containing; (for example, a file marked up
    for use in Audio::Mix)

        {
            'keywords'      => 'bpm:126 key:a',
            'name'          => 'Mission Venice',
            'artist'        => 'Nightmares on Wax'
        };

  get_cues
    Returns the cuepoints marked within the wav file.

        my $cues = $read -> get_cues();

    Returns a reference to a hash containing; (for example, a file marked up
    for use in Audio::Mix) (position is sample position)

        {
            1 => {
                label       => 'sig',
                position    => 764343,
                note        => 'first'
            },
            2 => {
                label       => 'fade_in',
                position    => 1661774,
                note        => 'trig'
            },
            3 => {
                label       => 'sig',
                position    => 18033735,
                note        => 'last'
            },
            4 => {
                label       => 'fade_out',
                position    => 17145150,
                note        => 'trig'
            },
            5 => {
                label       => 'end',
                position    => 18271676
            }
        }

  read_raw
    Reads raw packed bytes from the current audio data position in the file.

        my $data = $self -> read_raw( $byte_length );

  read_raw_samples
    Reads raw packed samples from the current audio data position in the
    file.

        my $data = $self -> read_raw_samples( $samples );

  read
    Returns the current audio data position sample across all channels.

        my @channels = $self -> read();

    Returns an array of unpacked samples. Each element is a channel i.e (
    left, right ). The numbers will be in the range;

        where $samp_max = ( 2 ** bits_per_sample ) / 2
        -$samp_max to +$samp_max

  position
    Returns the current audio data position (as byte offset).

        my $byte_offset = $read -> position();

  position_samples
    Returns the current audio data position (in samples).

        my $samples = $read -> position_samples();

  move_to
    Moves the current audio data position to byte offset.

        $read -> move_to( $byte_offset );

  move_to_sample
    Moves the current audio data position to sample offset.

        $read -> move_to_sample( $sample_offset );

  length
    Returns the number of bytes of audio data in the file.

        my $audio_bytes = $read -> length();

  length_samples
    Returns the number of samples of audio data in the file.

        my $audio_samples = $read -> length_samples();

  length_seconds
    Returns the number of seconds of audio data in the file.

        my $audio_seconds = $read -> length_seconds();

  details
    Returns a reference to a hash of lots of details about the file. Too
    many to list here, try it with Data::Dumper.....

        use Data::Dumper;
        my $details = $read -> details();
        print Data::Dumper->Dump([ $details ]);

  reread_length
    Rereads the length of the file in case it is being written to as we are
    reading it.

        my $new_data_length = $read -> reread_length();

---------------------------------------------------------------------
                          Audio::Wav::Write
               Module for writing Microsoft WAV files.
---------------------------------------------------------------------

NAME
    Audio::Wav::Write - Module for writing Microsoft WAV files.

SYNOPSIS
        use Audio::Wav;

        my $wav = new Audio::Wav;

        my $sample_rate = 44100;
        my $bits_sample = 16;

        my $details = {
            'bits_sample'   => $bits_sample,
            'sample_rate'   => $sample_rate,
            'channels'      => 1,
            # if you'd like this module not to use a write cache, uncomment the next line
            #'no_cache'     => 1,

        };

        my $write = $wav -> write( 'testout.wav', $details );

        &add_sine( 200, 1 );

        sub add_sine {
            my $hz = shift;
            my $length = shift;
            my $pi = ( 22 / 7 ) * 2;
            $length *= $sample_rate;
            my $max_no =  ( 2 ** $bits_sample ) / 2;
            for my $pos ( 0 .. $length ) {
                $time = $pos / $sample_rate;
                $time *= $hz;
                my $val = sin $pi * $time;
                my $samp = $val * $max_no;
                $write -> write( $samp );
            }
        }

        $write -> finish();

DESCRIPTION
    Currently only writes to a file.

SEE ALSO
    Audio::Wav

    Audio::Wav::Read

NOTES
    This module shouldn't be used directly, a blessed object can be returned
    from Audio::Wav.

METHODS
  finish
    Finishes off & closes the current wav file.

        $write -> finish();

  add_cue
    Adds a cue point to the wav file. If $sample is undefined then the
    position will be the current position (end of all data written so far).

        # $byte_offset for 01 compatibility mode
        $write -> add_cue( $sample, "label", "note"  );

  set_sampler_info
    All parameters are optional.

        my %info = (
            'midi_pitch_fraction'   => 0,
            'smpte_format'          => 0,
            'smpte_offset'          => 0,
            'product'               => 0,
            'sample_period'         => 0,
            'manufacturer'          => 0,
            'sample_data'           => 0,
            'midi_unity_note'       => 65,
        );
        $write -> set_sampler_info( %info );

  add_sampler_loop
    All parameters are optional except start & end.

        my $length = $read -> length_samples();
        my( $third, $twothirds ) = map int( $length / $_ ), ( 3, 1.5 );
        my %loop = (
            'start'                 => $third,
            'end'                   => $twothirds,
            'fraction'              => 0,
            'type'                  => 0,
        );
        $write -> add_sampler_loop( %loop );

  add_display
  set_info
    Sets information to be contained in the wav file.

        $write -> set_info( 'artist' => 'Nightmares on Wax', 'name' => 'Mission Venice' );

  file_name
    Returns the current filename.

        my $file = $write -> file_name();

  write
    Adds a sample to the current file.

        $write -> write( @sample_channels );

    Each element in @sample_channels should be in the range of;

        where $samp_max = ( 2 ** bits_per_sample ) / 2
        -$samp_max to +$samp_max

  write_raw
    Adds some pre-packed data to the current file.

        $write -> write_raw( $data, $data_length );

    Where;

        $data is the packed data
        $data_length (optional) is the length in bytes of the data

  write_raw_samples
    Adds some pre-packed data to the current file, returns number of samples
    written.

        $write -> write_raw_samples( $data, $data_length );

    Where;

        $data is the packed data
        $data_length (optional) is the length in bytes of the data

---------------------------------------------------------------------
                              AUTHORS
---------------------------------------------------------------------

    Nick Peskett (see http://www.peskett.co.uk/ for contact details).
    Brian Szymanski <ski-cpan@allafrica.com> (0.07-0.11)
    Wolfram humann (pureperl 24 and 32 bit read support in 0.09)
    Kurt George Gjerde <kurt.gjerde@media.uib.no>. (0.02-0.03)

---------------------------------------------------------------------
                                END
---------------------------------------------------------------------