Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Inline Encoding Audio::Wav Objects

by NateTut (Deacon)
on Jun 28, 2005 at 15:32 UTC ( [id://470670]=perlquestion: print w/replies, xml ) Need Help??

NateTut has asked for the wisdom of the Perl Monks concerning the following question:

I am currently using: bintoscalar to encode images I use in my Tk applications so they can be stored in-line in the code. Like this:
# # right_bmp # # Label BMP Stored as a Scalar # # FILENAME: C:/My Documents/Clients/Dem/pl/MMG/Right.bmp # THIS FUNCTION RETURNS A BASE64 ENCODED # REPRESENTATION OF THE ABOVE FILE. # SUITABLE FOR USE BY THE -data PROPERTY. # OF A Perl/Tk PHOTO. # # Arguments: # # Returns: $binary_data # sub right_bmp { my $binary_data = <<EOD; Qk1W5ggAAAAAADYAAAAoAAAAsAEAAMIBAAABABgAAAAAACDmCAAAAAAAAAAAAAAAAAAAAA +AAAAAA . . . #Long sequence of encoded data deleted for brevity . . . CQAACwAADgIAFQwACwQABQMAAAIBAgwTAAAOAAAR EOD return($binary_data); } # END right_bmp... # # Later on in the Code... # my $Right_Picture = $root->Photo ( -data => MMG_Pictures::right_bmp(), -format => 'bmp', ); . . . # # Give Feedback to User # if($Answer == $My_Answer) { $text_Messages->Insert("YOU ARE CORRECT!\n$Problem $Answer\n"); $label_Picture->configure ( -image => $Right_Picture, ); } else { $text_Messages->Insert("WRONG!\n$Problem $Answer\nYou said:$My_A +nswer\n"); $label_Picture->configure ( -image => $Wrong_Picture, ); }
The above works great, but I would like to do the same with Audio::Wav objects so I don't have to have the .wav files all over the place, but they don't seem to have a data property. I have to admit I'm not an OO kind of person...

Any Ideas?

readmore tags added by holli

Replies are listed 'Best First'.
Re: Inline Encoding Audio::Wav Objects
by thundergnat (Deacon) on Jun 28, 2005 at 17:08 UTC

    I am assuming you are running under Windows since you reference "C:/My Documents" in your script.

    To save the files in your script, you are going to need to encode them into a text encoding. MIME::Base64 seems to be a good choice; it is widely used and supported, and there are modules readily available to work with them. Just be aware that wav files tend to be fairly large, and will take up a lot of space (relatively speaking).

    Use a script something like this to encode the .wav files:

    ########################################################### use warnings; use strict; use MIME::Base64; my $filename = shift; open my $file, '<', $filename or die $!; my $data; while (sysread($file, $data, 57 * 17)) { print encode_base64($data); } ###########################################################

    Feed it a wav filename and redirect the output to a file.

    Once you have that, you can use it in something like this.

    You may be better off storing the wavs in a separate module. Just call/load them as necessary.

      Thanks! I'll give that a shot.
Re: Inline Encoding Audio::Wav Objects
by waswas-fng (Curate) on Jun 28, 2005 at 16:53 UTC
    Looks like you may have some issues with this, Audio::Wav::read is written to use FileHandle when opening the file argument. You would have to rewrite the Audio::Wav::Read file open steps or maybe export the inline encoded wav files to a temp file on the filesystem before handing the path for that temp file off to Audio::Wav.


    -Waswas
Re: Inline Encoding Audio::Wav Objects
by zentara (Archbishop) on Jun 29, 2005 at 11:46 UTC
    Additionally, if it's just voice(or simple tones) you are storing, you could encode them as speex or low quality mp3's before storing them with Base64. It would save alot of text space. Of course, your end users would need the decoding software on their end, but most people have mp3 decoding ability. You also could use something like Sox to convert your high quality stereo wavs, down to low-bitrate single channel wavs, to save space. For instance, this audio link is only 9.6k of binary audio, and is 13k after being base64 encoded.

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found