For Programmers: Free Programming Magazines  


Home > Archive > AWK > June 2007 > Re: Generic awk script to write 1/2/4/8/16 bytes at a particular









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Generic awk script to write 1/2/4/8/16 bytes at a particular
loki harfagr

2007-06-07, 9:57 pm

On Tue, 05 Jun 2007 21:12:36 +0200, Jürgen Kahrs wrote:

> Vassilis wrote:
>
>
> Years ago, I wrote the following functions for writing binary data
> (sound data in .wav format).
>
> function PrintBin(Value, Count) {
> for (; Count > 0; Count--) {
> printf("%c", Value%256);
> Value /= 256
> }
> }
>
> The header of the .wav file is written like this:
>
> printf("RIFF");
> PrintBin(nr*Channels*BytesPerSample+36, 4) printf("WAVEfmt ");
> PrintBin(16, 4)
> PrintBin(1, 2)
> PrintBin(Channels, 2)
> PrintBin(SampleRate, 4)
>


That's nice ! Some time ago I also had some fun with
sound data in awk, the idea was to easily test different
flavours of compression algos, so I naturally chose awk to
have a try :D)

Going slightly OT now...

Well, really, it was funny to do, but proved to be very
time consuming while dealing with real audio files, that is,
depending on the piece and the samplesize and freq it
grows between 20 and 300 MB per file :D)

Anyway, related with your function I used simple functions like
these, though it's been quite while and as it was an unfinished work
some bugs may lay in the dark as I used to savagely modify the
scripts, launch it on a huge file then go to bed then some day
I stopped going back to check the results and not necessarily I
would have corrected a freshly introduced bug ;D)

-------------------------
########################################
##########
###
### Input is a string of 8 [01]
### Output is the char
### ex: 00000000 --> null (\000)
###
function _bool2char(_bin){
_char=0
for (_i=1; _i<9; _i++){
_char+=(_bin[_i]*(2^(8-_i)));
}
return _char
}
###
### Intput is a char
### Output is a string of 8 [01]
### ex: null --> 00000000
###
function _char2bool(char,data, mask)
{
if (char == 0) return "00000000"
mask = 1
for (; char != 0; char=rshift(char, 1))
data=(and(char,mask) ?"1":"0") data
while ((length(data) % 8) != 0) data = "0" data
return data
}
###
-------------------------


Which were called by functions that horrific :D)

-------------------------
###
###
function _letmeout(){
_maxout=length(_smurf)
if(debug) print "\nsmurf=["_smurf"] maxout=("_maxout")\n"
split(_smurf,_gargamel,"")
print ""
for(i=1;i<=_maxout;i++){
### _dex=i%8
_dex=1+(i-1)%8
_byte[_dex]=_gargamel[i]

if(_dex%8) continue
if(debug) print "\nb2c()="_bool2char(_byte)"\n"
printf("%c",_bool2char(_byte))
}
_smurf=""
}
###
###
function Riceit(){
### scatter chars in array
_len=split($0,_line,"")
### fee foo fie fum
for(i=1;i<=_len;i++){
_smurf=_smurf""_tab[-1 + index(_aleph,_line[i])]
if(debug) print "\nindex("_line[i]")="index(_aleph,_line[i])"\n"
}
_smurf=_smurf""_tab[_CR]
}
###
###
function UnRiceit(i){
### scatter array in chars
_len=split($0,_line,"")
### fee foo fie fum
for(i=1;i<=_len;i++){
_boolstr=_char2bool(index(_bat,_line[i])
)
printf(_boolstr)
if(debug) print "\nread "i" char["_line[i]"] idx="index(_bat,_line[i])" --> bool="_boolstr"\n"
}
}
###
-------------------------
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com