Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Manipulating Audio Data in Perl

by Zaxo (Archbishop)
on Jun 13, 2002 at 20:52 UTC ( [id://174340]=note: print w/replies, xml ) Need Help??


in reply to Manipulating Audio Data in Perl

Consider pack/unpack in a pure perl implementation. It may be helpful to set the record delimiter $/ to a reference to a constant integer. That triggers magic which will let you read a file in fixed length chunks.

There is a PDL::Audio module, but I had difficulty with it, seemed to be version problems. PDL in general is a good way to improve the performance of numerical array operations. I'm currently using it to do FFT of data.

Update: Have you looked at Mmap for working on these files? Here's a PDL FFT example with a small enough data set to show results explicitly:

#!/usr/bin/perl -w use strict; use PDL; use PDL::IO::Misc; use PDL::FFT qw(:Func); use constant TWOPI=>8*atan2 1,1; my $data = PDL->new([ map { exp(-$_/10) * cos(TWOPI*3*$_/16) } 0..15 ] +); # this is a print routine print 'Original Data:',$/; PDL::IO::Misc::wcols($data); # works in place, modifies $data realfft($data); print 'Transformed Data:',$/; PDL::IO::Misc::wcols($data); realifft($data); print 'Restored Data:',$/; PDL::IO::Misc::wcols($data);
Which produces:
$ perl  fft.pl
Original Data:
1 
0.346266288866367 
-0.57893006746741 
-0.684426791399269 
-1.23131728120463e-16 
0.56036126234907 
0.38806842947617 
-0.190034968516957 
-0.449328964117222 
-0.155587472885039 
0.260130047511444 
0.307532781193507 
1.65979955538993e-16 
-0.251786545542726 
-0.174370385423124 
0.0853882155497732 
Transformed Data:
0.463281829594583 
0.486027823589927 
0.659813926688149 
4.40456725406594 
0.655773011785699 
0.476109468453563 
0.441528145077408 
0.43061131035946 
0.427856290365133 
-0.275073975558085 
-0.808370936709314 
0.0824047624460787 
0.980794295960617 
0.473606356659868 
0.256625191308709 
0.116127618655707 
Restored Data:
1 
0.346266288866367 
-0.57893006746741 
-0.684426791399269 
-2.02962646689286e-16 
0.56036126234907 
0.38806842947617 
-0.190034968516957 
-0.449328964117222 
-0.155587472885039 
0.260130047511444 
0.307532781193507 
2.02962646689286e-16 
-0.251786545542726 
-0.174370385423124 
0.0853882155497732 

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Manipulating Audio Data in Perl
by lofichurch (Beadle) on Jun 13, 2002 at 23:49 UTC

    Oops, guess I should've specified the ability to look backwards / forwards in the data, hence the substr(). I use the pack/unpack when I don't need to know anything other than a current sample value.

    For example, without using arrays or hashes, be able to look forward in the data while processing an waveform:

    assuming 16bit/mono:
    my $cur_smp = 0; my $len = length($input_data) / 2; foreach (unpack("s*",$input_data)) { # take the value and add to it the sample value # five samples ahead if($len < $cur_smp + 5) { # if we don't have enough samples left to look ahead, # just go with what we've got $output .= pack("s",$_); $cur_smp++; next; } my $five_ahead = unpack("s",substr($input_data,($cur_smp + 5) * 2,2) +); $output .= pack("s",$_ + $five_ahead); $cur_smp++; next; }

    That substr() is a big hit on large amounts of data, but some sort of look-ahead/back is necessary on things like multi-tap delays and variable delays, and a perl array is out of the question. I'm balking at the math knowledge necessary for PDL, since DSP is a pretty new subject for me, on the low-level aspects of the algorithms.

    Would you mind sharing an example of doing the FFT with PDL?

    Thanks!
    !c

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://174340]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2025-07-20 03:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.